/*
* @(#)Chart.java 1.6f 95/03/27 Sami Shaio
*
* Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
* without fee is hereby granted.
* Please refer to the file http://java.sun.com/copy_trademarks.html
* for further important copyright and trademark information and to
* http://java.sun.com/licensing.html for further important licensing
* information for the Java (tm) Technology.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*
* THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
* CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
* PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
* NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
* SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
* SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
* PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SUN
* SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
* HIGH RISK ACTIVITIES.
*/
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.awt.Event;
import java.awt.FontMetrics;
import java.io.*;
import java.lang.*;
import java.net.URL;
import java.util.Vector;
import SimParam;
public class MGraph extends java.awt.Frame implements
Runnable {
static final int VERTICAL = 0;
static final int HORIZONTAL = 1;
static final int SOLID = 0;
static final int STRIPED = 1;
Thread myThread=null;
int orientation=HORIZONTAL;
String title=null;
Font titleFont;
FontMetrics titleFontMetrics;
int titleHeight = 15;
int columns;
int values[];
double dvals[];
int pvalues[];
double chartScale[];
Color chartColour[];
Object colors[];
Object labels[];
Vector oPvalues;
Vector oValues;
Vector oTicks;
SimParam chartParams[]=null;
int nParams=0;
int nTicks; // the N for the data
int styles[];
int scale = 1;
int maxLabelWidth = 0;
int barWidth=40;
int barSpacing = 10;
int max = 0;
MGraph(String t) {
title=t;
this.resize(100,100);
}
public void register(SimParam s) {
chartParams[nParams++] = s;
barWidth = (int) 400/(nParams+1);
//initParams();
}
public void sInit() { // initialise static variables
chartColour = new Color[15];
chartColour[0] = Color.red;
chartColour[1] = Color.blue;
chartColour[2] = Color.green;
chartColour[3] = Color.pink;
chartColour[4] = Color.orange;
chartColour[5] = Color.magenta;
chartColour[6] = Color.cyan;
chartColour[7] = Color.yellow;
chartColour[8] = Color.lightGray;
chartColour[9] = Color.gray;
chartColour[10] = Color.darkGray;
chartColour[11] = Color.black;
chartColour[12] = Color.white;
chartParams = new SimParam[15];
oPvalues = new Vector(500,200);
oValues = new Vector(500,200);
oTicks = new Vector(500,200);
}
public void sReset() { // initialise static
variables
oPvalues = new Vector(500,200);
oValues = new Vector(500,200);
oTicks = new Vector(500,200);
repaint();
}
public void init() {
if (nParams == 0) sInit();
}
public synchronized void initParams() {
titleFont = new java.awt.Font("Courier",
Font.BOLD, 12);
titleFontMetrics = getFontMetrics(titleFont);
if (title == null) {
title = "Chart";
}
columns = nParams;
values = new int[nParams];
pvalues = new int[nParams];
dvals = new double[nParams];
colors = new Color[nParams];
labels = new String[nParams];
styles = new int[nParams];
chartScale = new double[nParams];
// double tmpx;
for (int i=0; i < columns; i++) {
// parse the value for this column
// tmpx = (dvals[i] = chartParams[i].getStaticValue());
nTicks = chartParams[0].getN();
chartScale[i] = chartParams[i].getChartMax() - chartParams[i].getChartMin();
if (chartScale[i] == 0) chartScale[i] = 2*chartParams[i].getStaticValue();
values[i] = (int) (dvals[i] = chartParams[i].getStaticValue()); // /chartScale[i]) * 100);
pvalues[i] = (int) (((dvals[i] - chartParams[i].getChartMin())/chartScale[i])
* barWidth); // I Hope
// pvalues[i] = (int) (((values[i] - chartParams[i].getChartMin())/chartScale[i])
* barWidth); // I Hope
if (pvalues[i] < 0) pvalues[i] = 1;
max = nTicks+10;
labels[i] = chartParams[i].getLabel();
maxLabelWidth = Math.max(titleFontMetrics.stringWidth((String)(labels[i])),
maxLabelWidth);
colors[i] = chartColour[i];
// parse the color attribute for this column
styles[i] = chartParams[i].getStyle();
}
oPvalues.addElement(pvalues);
oValues.addElement(dvals);
oTicks.addElement(new Integer(nTicks));/**/
//repaint();
}
public void setBarWidth(int n) {
barWidth=n;
}
public int getBarWidth() {
return(barWidth);
}
public synchronized void paint(Graphics g) {
int i, j;
int cx, cy;
char l[] = new char[1];
// draw the title centered at the bottom of the bar graph
if (nParams == 0) return;
switch (orientation) {
case VERTICAL:
default:
// barWidth = 100; // maxLabelWidth;
resize(Math.max(columns * (barWidth + barSpacing),
titleFontMetrics.stringWidth(title)) +
titleFont.getSize() + 5 + 12,
(max * scale) + (2 * titleFont.getSize()) + 5 + titleFont.getSize());
break;
case HORIZONTAL:
// barWidth = 100; // titleFont.getSize();
resize(Math.max((max * scale) + titleFontMetrics.stringWidth(""
+ max),
titleFontMetrics.stringWidth(title)) + maxLabelWidth + 5 + 12,
(columns * (barWidth + barSpacing)) + titleFont.getSize() + 10);
break;
}
g.setColor(Color.black);
i = titleFontMetrics.stringWidth(title);
g.setFont(titleFont);
g.drawString(title+" "+nTicks+"
Trials", Math.max((size().width - i)/2, 0),
size().height - titleFontMetrics.getDescent());
for (i=0; i < columns; i++) {
switch (orientation) {
case VERTICAL:
default:
// set the next X coordinate to account for the label
// being wider than the bar size().width.
cx = (Math.max((barWidth + barSpacing),maxLabelWidth) * i) +
barSpacing;
// center the bar chart
cx += Math.max((size().width - (columns *
(barWidth + (2 * barSpacing))))/2,0);
// set the next Y coordinate to account for the size().height
// of the bar as well as the title and labels painted
// at the bottom of the chart.
cy = size().height - (values[i] * scale) - 1 - (2 * titleFont.getSize());
// draw the label
g.setColor(Color.black);
g.drawString((String)labels[i], cx,
size().height - titleFont.getSize() - titleFontMetrics.getDescent());
// draw the shadow bar
if (colors[i] == Color.black) {
g.setColor(Color.gray);
}
g.setColor((Color)(colors[i]));
switch (styles[i]) {
case SOLID:
default:
g.drawRect(cx, cy, barWidth, (nTicks * scale));
break;
case STRIPED:
{
int steps = (values[i] * scale) / 2;
int ys;
for (j=0; j < steps; j++) {
ys = cy + (2 * j);
g.drawLine(cx, ys, cx + barWidth, ys);
}
}
break;
}
g.drawString("" + values[i], cx, cy -
titleFontMetrics.getDescent());
break;
case HORIZONTAL:
// set the Y coordinate
cy = ((barWidth + barSpacing) * i) + barSpacing;
// set the X coordinate to be the size().width of the widest
// label
cx = maxLabelWidth + 1;
cx += Math.max((size().width - (maxLabelWidth + 1 +
titleFontMetrics.stringWidth("" +
max) +
(max * scale))) / 2, 0);
// draw the labels and the shadow
g.setColor(Color.black);
g.drawString((String)labels[i], cx - maxLabelWidth - 1,
cy + titleFontMetrics.getAscent());
if (colors[i] == Color.black) {
g.setColor(Color.gray);
}
g.setColor((Color)(colors[i]));
switch (styles[i]) {
case SOLID:
default:
g.drawRect(cx, cy, (nTicks * scale+1), barWidth);
break;
case STRIPED:
{
int steps = (values[i] * scale) / 2;
int ys;
for (j=0; j < steps; j++) {
ys = cx + (2 * j);
g.drawLine(ys, cy, ys, cy + barWidth);
}
}
break;
}
int vs = oPvalues.size();
if (vs > 0) {
int ltick = cx;
int tick=ltick;
int val=cy;
int lval = cy+barWidth;
int lmean=0;
double dmean=0;
for(j=0;j<vs;j++) { // draw the data points
int[] valx = ((int []) oPvalues.elementAt(j));
double[] dvalx = ((double []) oValues.elementAt(j));
try {
val = valx[i];
if (j>= 0) lmean += val;
if (j>= 0) dmean += dvalx[i];
tick = ((Integer) oTicks.elementAt(j)).intValue();
if (j > 0) g.drawLine(ltick,lval,cx+tick*scale,cy+(barWidth - val));
} catch (Exception e) {}
ltick = cx+tick*scale;
lval = cy+(barWidth - val);
}
// dmean = lmean;
lmean /= (vs);
dmean /= (vs);
dmean = ((double)((int)(100*dmean+.5)))/100.0;
g.drawLine(cx,cy+barWidth-lmean, cx + (nTicks * scale),cy+barWidth-lmean);
g.drawString("" + dmean, cx + (nTicks
* scale) + 5, cy + barWidth - lmean + titleFontMetrics.getAscent()/2);
}
g.drawString("" + (chartParams[i].getChartMax()),
cx + (nTicks * scale) + 5, cy + titleFontMetrics.getAscent()/2);
// g.drawString("" + ((chartParams[i].getChartMax()+chartParams[i].getChartMin()+1)/2),
cx + (nTicks * scale) + 5, cy + barWidth/2 + titleFontMetrics.getAscent()/2);
g.drawString("" + chartParams[i].getChartMin(),
cx + (nTicks * scale) + 5, cy +barWidth + titleFontMetrics.getAscent()/2);
break;
}
}
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setColumns(int columns) {
this.columns = columns;
}
public int getColumns() {
return columns;
}
public void setScale(int scale) {
this.scale = scale;
}
public int getScale() {
return scale;
}
public void setOrientation(int o) {
this.orientation = o;
}
public int getOrientation() {
return orientation;
}
public boolean handleEvent(Event event) {
try {
myThread.sleep(100);
} catch (Exception e){};
if (event.target == this && event.id == Event.WINDOW_DESTROY)
{
MGraph_WindowDestroy(event);
return true;
}
return super.handleEvent(event);
}
public void stop() {
myThread.stop();
}
public void start() {
myThread = new Thread(this);
myThread.start();
}
public void run() {
//for(;;) {
try {
myThread.sleep(100);
} catch (Exception e){};
//}
}
void MGraph_WindowDestroy(Event event) {
//{{CONNECTION
hide();
//}}
}
}