<pre>
<%
String name = request.getParameter("name");
String type = request.getParameter("type");
String orient = request.getParameter("orient");
String scale = request.getParameter("scale");
String gap = request.getParameter("gap");
String border = request.getParameter("border");
String data = request.getParameter("data");
String label = request.getParameter("label");
String datasource = request.getParameter("datasource");
String labelsource = request.getParameter("labelsource");
String color1 = request.getParameter("color1");
String color2 = request.getParameter("color2");
String color3 = request.getParameter("color3");
String color4 = request.getParameter("color4");
%>
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class <%=name%> extends Applet {
String[] labels = {};
int[] data = {};
int maxValue = 0;
<% if (orient.equals("3")) { %>boolean vertical = true;
<% } %>
public void init() {
<% if (orient.equals("3")) { %>
// set up mouse action to switch graph orientation
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
vertical = !vertical;
repaint();
}
});
<% } %>
<% if (data.equals("4")) { %>
// fake data
int[] fakeData = { 14, 20, 34, 30 };
data = fakeData;
maxValue = 35;
<% } else { %>
error("Other data sources not yet implemented");
<% } %>
<% if (label.equals("2")) { %>
// read labels from applet parameters
labels = new String[data.length];
for (int j=0; j<labels.length; ++j) {
labels[j] = getParameter("label"+j);
if (labels[j]==null || labels[j].equals("")) {
labels[j] = "?";
}
}
<% } else { %>
error("Other label sources not yet implemented.");
<% } %>
}
public void paint(Graphics g) {
int w = getSize().width;
int h = getSize().height;
g.setColor(Color.black);
g.drawRect(0, 0, w-1, h-1);
<% if (type.equals("1")) { %>
// Create Bar Chart
int gap = <%=gap%>;
int border = <%=border%>;
int ndata = data.length;
if (ndata==0) {
g.setColor(Color.black);
g.drawString("No Data Available for Charting", border, h/2);
return;
}
<% if (orient.equals("3")) { %>
if (vertical) {
<% }
if (orient.equals("3") || orient.equals("2")) {
%>
// Vertical Bar chart
int ctop = 0;
int cbot = h-20;
int ch = cbot - ctop;
int cwid = (w-2*border-(ndata-1)*gap)/ndata;
for (int j=0; j<ndata; ++j) {
if (labels.length>j) {
g.setColor(Color.<%=color3%>);
g.drawString(labels[j],
border+cwid/3+j*(gap+cwid), h-5);
}
g.setColor(Color.<%=color1%>);
int colh = ch*data[j]/maxValue;
g.fillRect(border+j*(gap+cwid), cbot-colh, cwid, colh);
}
<% }
if (orient.equals("3")) {
%>
} else {
<% }
if (orient.equals("3") || orient.equals("1")) {
%>
// Horizontal Bar Chart
int rleft = border;
int rright = w-border;
int rwid = rright - rleft;
int rht = (h-2*border-(ndata-1)*gap)/ndata;
for (int j=0; j<ndata; ++j) {
g.setColor(Color.<%=color2%>);
int rowwid = rwid*data[j]/maxValue;
g.fillRect(border, border+j*(gap+rht), rowwid, rht);
if (labels.length>j) {
g.setColor(Color.<%=color4%>);
g.drawString(labels[j], border*2,
border+j*(gap+rht)+rht*2/3);
}
}
<% }
if (orient.equals("3")) {
%>
}
<% } %>
<% } else { %>
g.setColor(Color.black);
g.drawString("Chart Style not yet implemented.", border, h/2);
<% } %>
}
public void error(String m) {
errmess = m;
System.out.println(m);
status(m);
}
}
|