1

I would like to modify stackedBarChart's colors using key values. I know how to do this for piecharts, but am unable to accomplish the same for stackedBarCharts.

For piecharts, essentially my approach is similar to the answer stated here

The lines of code to note are:

PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionPaint("J+1", Color.black);
plot.setSectionPaint("J-1", new Color(120, 0, 120));

However for StackedBarChart, I am unsure of how to do it, essentially I have to modify the existing jfreechart code below:

 public static JFreeChart createStackedBarChart(final String title,
                        final CategoryDataset dataset) {

                JFreeChart sectorChart = ChartFactory.createStackedBarChart(title, "",
                                "", dataset, PlotOrientation.VERTICAL, true, false, false);

                CategoryPlot plot = (CategoryPlot) sectorChart.getPlot();
                formatCategoryPlot(plot);
                sectorChart.getLegend().setBorder(0, 0, 0, 0);
                sectorChart.setBorderVisible(false);
                sectorChart.setBorderPaint(Color.white);
                plot.setOutlineVisible(false);
                StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer();


                return Chart;

        }

So my question really is, is there a equivalent of

PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionPaint("J+1", Color.black);

for stackedBarCharts? If yes, how can I use it?

I can see from web resources that there is something about setSeriesPaint, but that seems to be changing colors based on index. I would like to change colors based on the labels, such as "J+1".

Community
  • 1
  • 1
stretchr
  • 615
  • 2
  • 9
  • 24

1 Answers1

1

You can override the renderer's getItemPaint() method, as shown here.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • thanks. The code doesn't show how to define colors based on section labels...can you help elaborate on that? – stretchr Apr 16 '14 at 02:48
  • You might also look at a custom `DrawingSupplier`, mentioned [here](http://stackoverflow.com/a/16754801/230513). – trashgod Apr 16 '14 at 08:04