Reputation: 191
I'm using JFreeChart to graph some data, and I've set it up so that the graph shows my standard error for each point as such:
The label shows the Y value for each point, but I'd like to be able to show the Y value for the standard errors as well. Furthermore, is there a way to make it so this data only shows up if hovered over with the mouse?
This is the code I use to add both the error and the labels:
XYErrorRenderer renderer = new XYErrorRenderer();
renderer.setBaseLinesVisible(true);
renderer.setBaseShapesVisible(true);
renderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator("{2}",
NumberFormat.getNumberInstance(),NumberFormat.getNumberInstance()));
renderer.setBaseItemLabelsVisible(true);
chart.getXYPlot().setRenderer(renderer);
Thanks in advance.
Upvotes: 2
Views: 387
Reputation: 205875
XYErrorRenderer
inherits its implementation of drawItemLabel()
from the abstract parent, which knows nothing about the error bars. You'll need to override drawItem()
in a custom renderer subclass to draw the extra labels. The source for drawItemLabel()
may serve as a guide.
Addendum: A less ambitious alternative would be to display the error range in a tooltip. The custom StandardXYToolTipGenerator
below specifies a custom format string and overrides createItemArray()
to supply the relevant y values from the dataset. As your XYDataset
is a YIntervalSeriesCollection
, you can cast it as shown below.
renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator(
"{0}: {1}…{2}", NumberFormat.getInstance(), NumberFormat.getInstance()) {
@Override
protected Object[] createItemArray(XYDataset data, int series, int item) {
YIntervalSeriesCollection d = (YIntervalSeriesCollection) data;
Object[] result = new Object[3];
double y = d.getYValue(series, item);
result[0] = getYFormat().format(y);
double min = d.getStartYValue(series, item);
result[1] = getYFormat().format(min);
double max = d.getEndYValue(series, item);
result[2] = getYFormat().format(max);
return result;
}
});
Upvotes: 2