Customize colors in XY BarChart


Hi all,

I need to customize the colors in a XYBarChart depending on the content of the series. This content in additional needs to be translated into the destination language.
I created a customized  Java class with the following code:

public void customize(JFreeChart chart, JRChart jasperChart) {
	final XYPlot plot = (XYPlot) chart.getPlot();
	XYBarRenderer r = (XYBarRenderer) plot.getRenderer();
	final XYDataset ds = plot.getDataset();
	CategoryTableXYDataset[] dss = new CategoryTableXYDataset[plot
	Color[] colors = new Color[plot.getSeriesCount()];
	ResourceBundle rb = (ResourceBundle) getParameterValue("REPORT_RESOURCE_BUNDLE");
	for (int i = 0; i < plot.getSeriesCount(); i++) {
		CategoryTableXYDataset dsNew = new CategoryTableXYDataset();
		// key determines the color
		String key = ds.getSeriesKey(i).toString().replaceAll("#", ""); 
		String value = rb.getString(key);
		colors[i] = colorMap.get(key);
		for (int j = 0; j < ds.getItemCount(i); j++) {
			Number x = ds.getX(i, j);
			Number y = ds.getY(i, j);
			// translated value will be added in dataset
			dsNew.add(x, y, value, false);
		dss[i] = dsNew;
	for (int s = 0; s < dss.length; s++) {
		plot.setDataset(s, dss[s]); //overriding dataset
		r.setSeriesFillPaint(s, colors[s]);
		r.setSeriesItemLabelPaint(s, colors[s]);
		r.setSeriesOutlinePaint(s, colors[s]);
		r.setSeriesPaint(s, colors[s]);

From the result I see the content is translated correctly in the output chart, but all the series have the same colors. Any idea what steps in the code I missed.

thanks for your help


XY Barchart

markusin77's picture
Joined: Mar 13 2015 - 12:50pm
Last seen: 1 year 3 weeks ago

0 Answers:

No answers yet