I wanted to draw a dangerzone in the background of a scatter plot, so I thought I would try something like this:
StandardXYItemRenderer sxyir = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES) {
public void initialise(java.awt.Graphics2D g2,
java.awt.geom.Rectangle2D dataArea,
XYPlot plot,
com.jrefinery.data.XYDataset data) {
System.out.println("Initialise was called");
ValueAxis hAxis = (ValueAxis)plot.getHorizontalAxis();
double transX = hAxis.translateValueToJava2D(45 , dataArea);
g2.drawLine((int)transX,0,(int)transX,(int)dataArea.getHeight());
}
};
plot.setXYItemRenderer(sxyir);
But it didnt seem to work so I popped open XYPlot.java and did a search for renderer.initialise and didnt find it........ I'm not very familiar with the code yet, but shouldnt initialise be called from XYPlot?
Am I doing something wrong or is there a better way to achieve my danger zone?
An oversight?
Re: An oversight?
Hi Eric,
The idea of initialise(...) is to precalculate any items the renderer requires once at the start of drawing the chart, rather than calculating the same thing on every call to the drawItem(...) method. It's not really intended for actually drawing anything.
The call to initialise(...) is missing in 0.8.1, but my latest source file has a call in the right place, so someone must have fixed it. Perhaps even me!
For your danger zone, you might be able to use the addHorizontalLine(...) method in XYPlot...although I recall someone sending me a bugfix for the actual drawing of these lines...browse the CVS repository on SourceForge to see what has changed.
Regards,
Dave Gilbert
The idea of initialise(...) is to precalculate any items the renderer requires once at the start of drawing the chart, rather than calculating the same thing on every call to the drawItem(...) method. It's not really intended for actually drawing anything.
The call to initialise(...) is missing in 0.8.1, but my latest source file has a call in the right place, so someone must have fixed it. Perhaps even me!
For your danger zone, you might be able to use the addHorizontalLine(...) method in XYPlot...although I recall someone sending me a bugfix for the actual drawing of these lines...browse the CVS repository on SourceForge to see what has changed.
Regards,
Dave Gilbert
Re: An oversight?
Thanks for the info Dave,
I tried dropping initialise into the 0.8.1 source where i thought it should go... and though it may not be the intended use of initialise it worked like a charm..... I managed to make a glowing aura effect using the aplha channel and a nice gradient with yellow... and i'm very pleased... I hope it works the same when I update my libraries.
regards,
Eric
I tried dropping initialise into the 0.8.1 source where i thought it should go... and though it may not be the intended use of initialise it worked like a charm..... I managed to make a glowing aura effect using the aplha channel and a nice gradient with yellow... and i'm very pleased... I hope it works the same when I update my libraries.
regards,
Eric
Re: An oversight?
Well, perhaps initialise() is a good place to draw an initial background...I never thought of it that way. If you want to, post the code you used...a lot of people are probably interested...
Regards,
DG.
Regards,
DG.
Re: An oversight?
Sure, here it is... not a lot different from the originally posted code...
to get this to work with 0.8.1 add a call to initialise(...) in the XYPlot.java source code...
hAxis = new HorizontalNumberAxis("Days");
vAxis = new VerticalNumberAxis("Price");
XYPlot plot = new XYPlot(hAxis, vAxis);
StandardXYItemRenderer sxyir = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES) {
//Overload initialise to draw a zone in the background between 30 and 45 on the hAxis
public void initialise(java.awt.Graphics2D g2,
java.awt.geom.Rectangle2D dataArea,
XYPlot plot,
com.jrefinery.data.XYDataset data) {
//System.out.println("Initialise was called");
ValueAxis hAxis = (ValueAxis)plot.getHorizontalAxis();
ValueAxis vAxis = (ValueAxis)plot.getVerticalAxis();
double transY = vAxis.translateValueToJava2D(vAxis.getMaximumAxisValue(),dataArea);
double transY1 = vAxis.translateValueToJava2D(vAxis.getMinimumAxisValue(),dataArea);
double transX = hAxis.translateValueToJava2D(30 , dataArea);
double transX1 = hAxis.translateValueToJava2D(45 , dataArea);
//g2.setColor(new Color(255,255,204));
GradientPaint paint = new GradientPaint(new Point2D.Double(transX,0d)
, new Color(1f,1f,.8f,.3f)
, new Point2D.Double(transX1,0d)
, Color.yellow);
g2.setPaint(paint);
//g2.drawLine((int)transX,0,(int)transX,(int)dataArea.getHeight()-3);
g2.fillRect((int)transX,(int)transY+1,(int)(transX1-transX),(int)(transY1-transY));
}
};
plot.setXYItemRenderer(sxyir);
plot.setToolTipGenerator(new BMXYDatasetToolTipsGenerator());
chart = new JFreeChart(null, plot, "Inventory Aging", JFreeChart.DEFAULT_TITLE_FONT, true);
to get this to work with 0.8.1 add a call to initialise(...) in the XYPlot.java source code...
hAxis = new HorizontalNumberAxis("Days");
vAxis = new VerticalNumberAxis("Price");
XYPlot plot = new XYPlot(hAxis, vAxis);
StandardXYItemRenderer sxyir = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES) {
//Overload initialise to draw a zone in the background between 30 and 45 on the hAxis
public void initialise(java.awt.Graphics2D g2,
java.awt.geom.Rectangle2D dataArea,
XYPlot plot,
com.jrefinery.data.XYDataset data) {
//System.out.println("Initialise was called");
ValueAxis hAxis = (ValueAxis)plot.getHorizontalAxis();
ValueAxis vAxis = (ValueAxis)plot.getVerticalAxis();
double transY = vAxis.translateValueToJava2D(vAxis.getMaximumAxisValue(),dataArea);
double transY1 = vAxis.translateValueToJava2D(vAxis.getMinimumAxisValue(),dataArea);
double transX = hAxis.translateValueToJava2D(30 , dataArea);
double transX1 = hAxis.translateValueToJava2D(45 , dataArea);
//g2.setColor(new Color(255,255,204));
GradientPaint paint = new GradientPaint(new Point2D.Double(transX,0d)
, new Color(1f,1f,.8f,.3f)
, new Point2D.Double(transX1,0d)
, Color.yellow);
g2.setPaint(paint);
//g2.drawLine((int)transX,0,(int)transX,(int)dataArea.getHeight()-3);
g2.fillRect((int)transX,(int)transY+1,(int)(transX1-transX),(int)(transY1-transY));
}
};
plot.setXYItemRenderer(sxyir);
plot.setToolTipGenerator(new BMXYDatasetToolTipsGenerator());
chart = new JFreeChart(null, plot, "Inventory Aging", JFreeChart.DEFAULT_TITLE_FONT, true);