1

I'm new to the JFreeChart library. I'm currently playing around with the 3D Bar Chart, and I wonder if it is possible to have each generated 3D bar in different colours? All of them are currently yellow as shown below.

Is there something I can override to change the colours?

Thanks.

Code:

import java.awt.Dimension;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class jfree extends ApplicationFrame
{
    {
        // set a theme using the new shadow generator feature available in
        // 1.0.14 - for backwards compatibility it is not enabled by default
        ChartFactory.setChartTheme(StandardChartTheme.createDarknessTheme());
    }

    public jfree(String s)
    {
        super(s);
        JPanel jpanel = createDemoPanel();
        jpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(jpanel);
    }

    private static CategoryDataset createDataset()
    {
        DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
                    defaultcategorydataset.addValue(25D, "Series 1", "Pipe Stricking");
                    defaultcategorydataset.addValue(17D, "Series 1", "Lost Circulation");
                    defaultcategorydataset.addValue(34D, "Series 1", "Hole Deviation");
                    defaultcategorydataset.addValue(12D, "Series 1", "Kick Blowout");
                    defaultcategorydataset.addValue(54D, "Series 1", "Formation Change");
                    defaultcategorydataset.addValue(10D, "Series 1", "Borehole Instability");
                    defaultcategorydataset.addValue(19D, "Series 1", "Cavings/Pack-offs");
        return defaultcategorydataset;
    }

    private static JFreeChart createChart(CategoryDataset categorydataset)
    {
        JFreeChart jfreechart = ChartFactory.createBarChart3D("IPS", "", "Value", categorydataset, PlotOrientation.VERTICAL, false, false, false);
                    CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
                    CategoryAxis categoryaxis = categoryplot.getDomainAxis();
                    categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.2D));
                    CategoryItemRenderer categoryitemrenderer = categoryplot.getRenderer();
                    categoryitemrenderer.setBaseItemLabelsVisible(true);
                    BarRenderer barrenderer = (BarRenderer)categoryitemrenderer;
                    barrenderer.setItemMargin(200D);
        return jfreechart;
    }

    public static JPanel createDemoPanel()
    {
        JFreeChart jfreechart = createChart(createDataset());
        return new ChartPanel(jfreechart);
    }

    public static void main(String args[])
    {
        jfree jfree = new jfree("3D Bar Chart Demo 3");
        jfree.pack();
        RefineryUtilities.centerFrameOnScreen(jfree);
        jfree.setVisible(true);
    }
}

JFree3DBarChart

Mob
  • 10,958
  • 6
  • 41
  • 58

1 Answers1

1

All the bars are the same color because they all belong to the same series, identified by the rowKey parameter in addValue(). Two common approaches to changing the color include these:

  • Invoke setSeriesPaint(), as shown in the BarChartDemo1 source.

  • Override getItemPaint() to define a custom color scheme, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thank you for your response. Despite the fact that they belong to the same series and all have the same colours, can I have the bars in different colours even if they are in a single series? `setSeriesPaint()` just changes the colour for the series and not the bars. – Mob Oct 27 '12 at 07:42
  • 1
    Yes, override `getItemPaint()`; here's a more elaborate [example](http://stackoverflow.com/a/8949913/230513). – trashgod Oct 27 '12 at 11:44
  • Thank you, are you on the dev team of the library? – Mob Oct 27 '12 at 11:45
  • No, just a fan of the project; usual disclaimer [here](http://stackoverflow.com/a/12272267/230513). :-) – trashgod Oct 27 '12 at 11:54