JSci.swing.JBarGraph Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsci Show documentation
Show all versions of jsci Show documentation
JSci is a set of open source Java packages. The aim is to encapsulate scientific methods/principles in the most natural way possible. As such they should greatly aid the development of scientific based software.
It offers: abstract math interfaces, linear algebra (support for various matrix and vector types), statistics (including probability distributions), wavelets, newtonian mechanics, chart/graph components (AWT and Swing), MathML DOM implementation, ...
Note: some packages, like javax.comm, for the astro and instruments package aren't listed as dependencies (not available).
The newest version!
package JSci.swing;
import java.awt.*;
import java.awt.geom.Point2D;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import JSci.awt.*;import JSci.maths.ExtraMath;
/**
* A bar graph Swing component.
* Multiple series are side-by-side.
* @version 1.2
* @author Ismael Orenstein
*/
public class JBarGraph extends JCategoryGraph2D {
/**
* Bar colors.
*/
protected Color barColor[]={Color.blue,Color.green,Color.red,Color.yellow,Color.cyan,Color.lightGray,Color.magenta,Color.orange,Color.pink};
/**
* Min and max data points.
*/
protected float minY,maxY;
/**
* Axis scaling.
*/
private final float yIncPixels = 40.0f;
private float yInc;
private boolean autoYInc = true;
private float xScale,yScale;
protected int barWidth;
/**
* Padding.
*/
protected final int barPad=0;
/**
* Axis numbering.
*/
protected boolean numbering=true;
protected NumberFormat yNumberFormat = new DecimalFormat("##0.0");
/**
* Constructs a bar graph.
*/
public JBarGraph(CategoryGraph2DModel cgm) {
super(cgm);
dataChanged(new GraphDataEvent(model));
}
/**
* Implementation of GraphDataListener.
* Application code will not use this method explicitly, it is used internally.
*/
public void dataChanged(GraphDataEvent e) {
minY=0.0f;
maxY=Float.NEGATIVE_INFINITY;
model.firstSeries();
do {
for(int i=0;i 0.0f) {
g.drawLine(leftAxisPad-scalePad,getHeight()-axisPad,getWidth()-(axisPad-scalePad),getHeight()-axisPad);
} else {
g.drawLine(leftAxisPad-scalePad,origin.y,getWidth()-(axisPad-scalePad),origin.y);
}
g.drawLine(origin.x,axisPad-scalePad,origin.x,getHeight()-(axisPad-scalePad));
// x-axis labels
for(int x=0; x=minY; y-=yInc) {
drawYLabel(g, (float) y);
}
}
}
protected void drawXLabel(Graphics g, int x) {
Point p = dataToScreen(x+1.0f, 0.0f);
String str = model.getCategory(x);
FontMetrics metrics = g.getFontMetrics();
int strWidth = metrics.stringWidth(str);
int strHeight = metrics.getHeight();
boolean numberingAbove = (maxY <= 0.0f);
if(numberingAbove) {
g.drawLine(p.x,p.y,p.x,p.y-5);
g.drawString(str, dataToScreen(x+0.5f-0.5f*strWidth/xScale, 0.0f).x, origin.y-5);
} else {
g.drawLine(p.x,p.y,p.x,p.y+5);
g.drawString(str, dataToScreen(x+0.5f-0.5f*strWidth/xScale, 0.0f).x, origin.y+strHeight);
}
}
protected void drawYLabel(Graphics g, float y) {
Point p = dataToScreen(0.0f, y);
String str = yNumberFormat.format(y);
FontMetrics metrics = g.getFontMetrics();
int strWidth = metrics.stringWidth(str);
int strHeight = metrics.getHeight();
g.drawLine(p.x, p.y, p.x-5, p.y);
g.drawString(str, p.x-8-strWidth, p.y+strHeight/3);
}
/**
* Draws the graph bars.
*/
protected void drawBars(Graphics g) {
// bars
int numSeries=1;
model.firstSeries();
while(model.nextSeries())
numSeries++;
if(numSeries==1) {
for(int i=0;i 0.0f)
deltaY = maxY;
else if(maxY < 0.0)
deltaY = -minY;
else
deltaY = maxY-minY;
yScale = (float) ((double)(thisHeight-2*axisPad) / (double)(deltaY));
if(autoYInc) {
yInc = (float) ExtraMath.round((double)yIncPixels/(double)yScale, 1);
if(yInc == 0.0f)
yInc = Float.MIN_VALUE;
}
barWidth=Math.round(xScale-2*barPad);
origin.x=leftAxisPad;
origin.y=thisHeight-axisPad+Math.round(minY*yScale);
redraw();
}
/**
* Converts a data point to screen coordinates.
*/
protected final Point dataToScreen(float x,float y) {
return new Point(origin.x+Math.round(xScale*x),origin.y-Math.round(yScale*y));
}
/**
* Converts a screen point to data coordinates.
*/
protected final Point2D.Float screenToData(Point p) {
double x = (double)(p.x-origin.x) / (double)xScale;
double y = (double)(origin.y-p.y) / (double)yScale;
return new Point2D.Float((float)x, (float)y);
}
}