I have a VerticalBarChart that displays just one set of data as bars. These bars need to be labelled on the x-axis and need to be different colours.
Firstly I tried adding just one category and adding my data values as series. This meant the bars were different colours, but the labels appeared as a legend and not on the x-axis.
Secondly I tried adding the values as different categories with only one series and disabling the legend. This meant the labels appeared correctly on the x-axis (I also displayed them vertically using setVerticalCategoryLabels(true)), but the problem then was that all the bars were all the same colour.
Basically I wanted a way to set the colour of the categories when there is only one series.
In the end I used the following code modifications, which achieved the desired result...
JFreeChart modified as follows:
new property added -
/** Paint objects used to color each category in the chart.
* Only used if only one series */
protected Paint[] categoryPaint;
new methods added -
/**
* Sets the paint used to color any shapes representing
* categories, and notifies registered
* listeners that the chart has been modified.
* Only used if only one series.
* @param paint An array of Paint objects used to color series;
*/
public void setCategorysPaint(Paint[] paint) {
this.categoryPaint = paint;
fireChartChanged();
}
/**
* Returns the Paint used to color any shapes.
* Only used if only one series
*/
public Paint[] getCategoryPaint() {
return categoryPaint;
}
/**
* Returns the Paint used to color any shapes for the
* specified category. Only used if only one series
* @param index The index of the category of interest (zero-based);
*/
public Paint getCategoryPaint(int index) {
return categoryPaint[index % categoryPaint.length];
}
VerticalBarPlot modified as follows:
method getBars(Rectangle2D plotArea) modified as follows -
int currentCategoryIndex = 0;
Iterator iterator = data.getCategories().iterator();
while (iterator.hasNext()) {
Object category = iterator.next();
for (int seriesIndex=0; seriesIndex<seriesCount; seriesIndex++) {
/* Code Altered to allow use of category colors
* if there is only one series
*/
Paint paint = null;
if (seriesCount <= 1 && chart.getCategoryPaint() != null) {
paint = chart.getCategoryPaint(currentCategoryIndex);
}
else {
paint = chart.getSeriesPaint(seriesIndex);
}
Now when I create my chart I do the following:
JFreeChart chart = new JFreeChart(graphTitle, new Font("SansSerif", Font.ITALIC, 16), categoryData, barPlot);
chart.setCategorysPaint(cols); // instead of setSeriesPaint(cols)
chart.setLegend(null);
Cheers,
Jan.
Allow Category Colours if only one Series
RE: Allow Category Colours if only one Series
Jan,
Thanks for your post - the code is excellent! The latest JFreeChart sources have changed around a bit, but I'll try to integrate something similar to what you have done into the next version of JFreeChart.
Regards,
DG.
Thanks for your post - the code is excellent! The latest JFreeChart sources have changed around a bit, but I'll try to integrate something similar to what you have done into the next version of JFreeChart.
Regards,
DG.