stretchr
stretchr

Reputation: 615

Change StackedBarChart Colors based on key values

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".

Upvotes: 1

Views: 338

Answers (1)

trashgod
trashgod

Reputation: 205785

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

image

Upvotes: 1

Related Questions