JFreeChart BarChart Color Customise

Chapter: JFreeChart Last Updated: 14-10-2016 13:37:40 UTC

Program:

            /* ............... START ............... */
                
import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

public class JFreeChartBarColors {

	public static void main(String args[]) {

		String title = "Bar chart Color Example";

		DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		dataset.addValue(1, "Student heights", "Less than 130");
		dataset.addValue(20, "Student heights", "130 to 134");
		dataset.addValue(30, "Student heights", "135 to 139");
		dataset.addValue(16, "Student heights", "140 to 144");
		dataset.addValue(9, "Student heights", "144 to 148");

		dataset.addValue(4, "Teacher heights", "Less than 130");
		dataset.addValue(3, "Teacher heights", "130 to 134");
		dataset.addValue(10, "Teacher heights", "135 to 139");
		dataset.addValue(5, "Teacher heights", "140 to 144");
		dataset.addValue(1, "Teacher heights", "144 to 148");

		/* create chart */
		JFreeChart chart = ChartFactory.createBarChart(title, "heights", "Number Of Persons", dataset);

		/* Get instance of CategoryPlot */
		CategoryPlot plot = chart.getCategoryPlot();

		/* Change Bar colors */
		BarRenderer renderer = (BarRenderer) plot.getRenderer();

		renderer.setSeriesPaint(0, Color.red);
		renderer.setSeriesPaint(1, Color.green);
		renderer.setSeriesPaint(2, Color.blue);

		renderer.setDrawBarOutline(false);
		renderer.setItemMargin(0);

		/* create and display chart on frame */
		ChartFrame frame = new ChartFrame("JFreeChart Demo", chart);
		frame.setVisible(true);
		frame.pack();

	}
}
                /* ............... END ............... */
        

Output


JFreeChart BarChart Color Customise

Notes:

  • You can customise the colors used in a bar chart in the same way that you would for most other chart types. You need to obtain a reference to the renderer (the object responsible for drawing the bars in the chart) and set the series colors there:
  • CategoryPlot plot = chart.getCategoryPlot();
  • BarRenderer renderer = (BarRenderer) plot.getRenderer();
  • renderer.setSeriesPaint(0, Color.red);
  • renderer.setSeriesPaint(1, Color.green);
  • renderer.setSeriesPaint(2, Color.blue);
  • The setSeriesPaint() method is defined in the AbstractRenderer class.

Tags

BarChart Colors Customise, Java, JFreeChart

Similar Programs Chapter Last Updated
JFreeChart Scatter Chart JFreeChart 20-10-2017
JFreeChart XY Step Chart JFreeChart 10-06-2017
JFreeChart Wafer Map Chart JFreeChart 10-06-2017
JFreeChart XY Step Area Chart JFreeChart 10-06-2017
JFreeChart Bar Chart 3D Example JFreeChart 05-08-2016
JFreeChart Bar Chart 3D Horizontal JFreeChart 05-08-2016
JFreeChart Area Chart Example JFreeChart 05-08-2016
JFreeChart Bubble Chart Example JFreeChart 03-08-2016
JFreeChart XY Chart Example JFreeChart 03-08-2016
JFreeChart Dynamic Data Demo Chart Example JFreeChart 08-07-2016
JFreeChart Background Paint Example JFreeChart 07-07-2016
JFreeChart Get Plot Example JFreeChart 07-07-2016
JFreeChart Background Color Example JFreeChart 06-07-2016
JFreeChart Subtitles Example JFreeChart 06-07-2016
JFreeChart Chart Title Example JFreeChart 06-07-2016
JFreeChart Chart Border Example JFreeChart 05-07-2016
JFreeChart Time Series Chart Example JFreeChart 23-06-2016
JFreeChart Pie Chart 3D Example JFreeChart 08-06-2016
JFreeChart Line Chart Example JFreeChart 04-06-2016
JFreeChart Bar Chart Example JFreeChart 22-09-2018
JFreeChart Pie Chart Example JFreeChart 04-06-2016

1