3

I have already searched enough on web but with no luck.

I have created a stacked 3d bar chart wherein I am unable to change the default colors. I tried all the advices provided .

Below a small snippet of my code. This is my input.

My Data from Db is:

A   0   2
B   15  53
C   0   2
D   0   2
E   0   1
F   1   0
G   0   1

Somehow I converted this to dataset requirement and also added items and models.

CategoryDataset dataset = DatasetUtilities.createCategoryDataset(item,models, data);
chart = ChartFactory.createStackedBarChart3D(chartDescription, X-axis, Y-axis, dataset,PlotOrientation.VERTICAL, true, true, true);
CategoryPlot plot = chart.getCategoryPlot();
CategoryItemRenderer r = plot.getRenderer();
r.setSeriesPaint(0, Color.GREEN);
r.setSeriesPaint(1, Color.GRAY);
 plot.setRenderer(new StackedBarRenderer3D() {

@Override
public Paint getItemPaint(int row, int col) {
    System.out.println("row:"+row);
    System.out.println("Col:"+col);
    return Color.getHSBColor(row / 42f, 1, 1);
}
});
CategoryAxis domainAxis = plot.getDomainAxis(); 
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

StackedBarRenderer3D renderer = (StackedBarRenderer3D)plot.getRenderer();
//renderer.setBarPainter(new StandardBarPainter());
renderer.setSeriesFillPaint(0,Color.BLACK);
renderer.setSeriesFillPaint(1,Color.GREEN);
renderer.setDrawBarOutline(false);
renderer.setShadowVisible(false);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER,TextAnchor.CENTER));
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setIgnoreZeroValues(true);
renderer.setMaximumBarWidth(.05);
renderer.setBaseItemLabelsVisible(true);
renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
renderer.setPositiveItemLabelPositionFallback(new ItemLabelPosition(
    ItemLabelAnchor.CENTER, TextAnchor.CENTER_RIGHT));
renderer.setNegativeItemLabelPositionFallback(new ItemLabelPosition(
ItemLabelAnchor.CENTER, TextAnchor.CENTER_RIGHT));
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
    ItemLabelAnchor.CENTER, TextAnchor.TOP_CENTER));
renderer.setBaseNegativeItemLabelPosition(new ItemLabelPosition(
    ItemLabelAnchor.CENTER, TextAnchor.TOP_CENTER));
LegendTitle legend = chart.getLegend(0); 
legend.setBackgroundPaint(Color.white);
legend.setFrame(new BlockBorder(Color.green));
StandardChartTheme theme = (StandardChartTheme)org.jfree.chart.StandardChartTheme.createJFreeTheme();
theme.setTitlePaint(Color.decode("#4572a7"));
theme.setExtraLargeFont(new Font("Arial",Font.BOLD, 16) ); 
theme.setLargeFont(new Font("Arial",Font.BOLD, 15)); 
theme.setRegularFont( new Font("Arial",Font.PLAIN, 11));
theme.setRangeGridlinePaint(Color.RED);
theme.setPlotBackgroundPaint( Color.white );
theme.setChartBackgroundPaint( Color.white );
theme.setItemLabelPaint(Color.YELLOW);
theme.setShadowVisible(true);
theme.setAxisLabelPaint( Color.decode("#666666")    );
theme.apply( chart );

EvenI have used setSeriespaint, but it is not working. Is there anything to do with the order of creation in chart,plot and renderer?

When I print the row and column I get this:

row:1
Col:0
row:0
Col:1
row:1
Col:1
row:1
Col:2
row:1
Col:3
row:1
Col:4
row:0
Col:5
row:1
Col:6
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
user2228697
  • 51
  • 2
  • 5

1 Answers1

4

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

plot.setRenderer(new StackedBarRenderer3D() {

    @Override
    public Paint getItemPaint(int row, int col) {
        return Color.getHSBColor(row / 42f, 1, 1);
    }
});

plot colors

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • trashgod: thanks and perfect! now i am able to change the color from your code, but the entire bar color changes to red.Instead i prefer to change each stack different color. Like 1st stack should have GREEN and second stack should have yellow .Any suggestions please.. – user2228697 Sep 19 '13 at 05:28
  • 1
    @user2228697: Use the `row` and `col` to decide what color to use. – Catalina Island Sep 19 '13 at 12:26
  • 1
    @CatalinaIsland has the right idea: make four palettes each having a series of related colors, e.g. greens, yellows, etc.; select the palette based on `col`; here's a related [example](http://stackoverflow.com/a/8949913/230513); see also the `DrawingSupplier` approach cited [here](http://stackoverflow.com/a/14820173/230513). – trashgod Sep 19 '13 at 15:17
  • @trashgod above link shown by you really confuses me..and i am still getting only one color instead stacked3dbar type.STUCK here.. – user2228697 Sep 20 '13 at 13:28
  • Please edit your question to include a [complete example](http://sscce.org/) that shows a small sample of your data. – trashgod Sep 20 '13 at 16:06
  • @trashgod : Any insights after my edit please..this is the entire code i have except the data part. – user2228697 Sep 26 '13 at 09:40
  • Nothing jumps out at me; most of the code is irrelevant; the important part is commented out. Please edit your question to include a short, compete example with a small sample of data, otherwise I'm just guessing. What happens when you print out the row and column in `getItemPaint()`? – trashgod Sep 26 '13 at 10:25
  • I don't understand your data model; for the _third_ time, please edit your question to include a [*Short, Self Contained, Correct (Compilable), Example*](http://sscce.org/) with your chart & data. – trashgod Sep 26 '13 at 15:08