badperson badperson - 4 days ago 5
Java Question

set lower/upper bound in jfreechart bar charts

I'm a noob with jfreechart, and I have an app that creates a simple bar chart that is working well. The issue is, I want all the charts to show a range of 1 to 10. When the highest val in the chart is lower than that, that lower value will be the upper bound of the chart, and it will display in different increments. It wasn't obvious to me from the online notes where to change that....

DefaultCategoryDataset barDataSet = new DefaultCategoryDataset();
System.out.println("Setting values:");

barDataSet.setValue(rating.getFlavor(), "category", "Flavor");
barDataSet.setValue(rating.getBody(), "category", "Body");
barDataSet.setValue(rating.getAftertaste(), "category", "Aftertaste");
barDataSet.setValue(rating.getSweetness(), "category", "Sweetness");
barDataSet.setValue(rating.getFloral(), "category", "Floral");
barDataSet.setValue(rating.getSpice(), "category", "Spice");


JFreeChart chart = ChartFactory.createBarChart(null, // title
"category", // left heading
"score", // top heading
barDataSet, // dataset
PlotOrientation.HORIZONTAL,
false, // no idea what this is
true, // or this
false); // or this

System.out.println("setting bg color");
Color bgcolor = new Color(237, 232, 228);
chart.setBackgroundPaint(bgcolor);

CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(new Color(112,112,112) );


thanks,
bp

Answer

You can change the displayed range via the axes. If i recall right, you can ask the JFreeChart instance for the XYPlot, with has a domain (x) and a range (y) axis. Both (when instances of NumberAxis) will have a setLowerBound(double) and setUpperBound(double) method.

Comments