More on printing labels on Scatter plots

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Ben Kim

More on printing labels on Scatter plots

Post by Ben Kim » Tue Aug 06, 2002 9:19 pm

DG,

I previous asked whether or not we could have string labels on the x-axis on a scatter plot. You mentioned to use a VerticalSymbolicAxis. I tried and it didn't work. I have the sample code here and maybe you can tell me if I'm doing something wrong.

Thanks,
Ben

couple things to note
1. I used both VerticalSymbolic and HorizontalSymbolicAxis and both don't work
2. I tried setting the domain axis for the scatter plot, line plot and overlaid plot and all three in this case and all of those didn't work.
3. I know that the xaxis is being set somewhat because the title "foobar" shows up on the xaxis but only numbers show up and not abc1, abc2 ... as you excpet.

public void runScatterPlot(Double[][] x, Double[][] y, int sample, boolean allNull) {

//ValueAxis xAxis = new HorizontalNumberAxis("Build");
String labels[] = new String[_points];
for (int i = 0; i < _points; i++) {
labels = "abc=" + i;
}

HorizontalSymbolicAxis xAxis =
new HorizontalSymbolicAxis("foobar", labels);
ValueAxis yAxis = new VerticalNumberAxis("Scores");

XYDataset line = new LineXYDataset(x, y, _points, 1, sample);
XYPlot lplot = new XYPlot(line, xAxis, yAxis);

XYDataset data = new ScatterXYDataset(x, y, _points, sample);
XYPlot plot = new XYPlot(data, xAxis, yAxis);
plot.setXYItemRenderer(new StandardXYItemRenderer
(StandardXYItemRenderer.SHAPES,
new StandardXYToolTipGenerator()));
NumberAxis axis = (NumberAxis)plot.getRangeAxis();
axis.setAutoRangeIncludesZero(false);

OverlaidXYPlot oplot = new OverlaidXYPlot("Build", "Score");
oplot.setDomainAxis(xAxis);
oplot.setRangeAxis(yAxis);
// in the case that the graph is entirely empty, don't create
if (!allNull) {
oplot.add(plot);
oplot.add(lplot);
}

JFreeChart chart =
new JFreeChart(_title, JFreeChart.DEFAULT_TITLE_FONT, oplot, true);
//then customise it a little...
chart.setBackgroundPaint(new GradientPaint
(0, 0, Color.white, 1000, 0, Color.green));

XYPlot xyp = (XYPlot) chart.getPlot();
NumberAxis domain = (NumberAxis)xyp.getDomainAxis();
// Set the graph range to +/-2%
domain.setRange(new Range(-2.0, _points + 1));

NumberAxis range = (NumberAxis)xyp.getRangeAxis();
range.setAutoRangeIncludesZero(false);

writeToJPG(chart);
}

Ben Kim

Re: More on printing labels on Scatter plots

Post by Ben Kim » Thu Aug 08, 2002 1:19 am

Since no one is replying to this message, I went ahead and hacked the HorizontalNumberAxis class. There I added a List called xLabels and in the refreshTicks method, get the text from the xLabels list instead of from the ticks List avaliable in Axis.java. Again, if people know of a way to do this without hacking the code, I would like to go that route.

Thanks
Ben

Locked