JFreeChart Bar Chart Example

Chapter: JFreeChart Last Updated: 22-09-2018 05:44:14 UTC

Program:

            /* ............... START ............... */
                
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class JavaJFreeChartBarChartExample extends ApplicationFrame {

	public JavaJFreeChartBarChartExample(final String title) {

		super(title);
		final CategoryDataset dataset = createDataset();
		final JFreeChart chart = createChart(dataset);
		final ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new Dimension(500, 270));
		setContentPane(chartPanel);

	}

	private CategoryDataset createDataset() {

		final String series1 = "First";
		final String series2 = "Second";
		final String series3 = "Third";

		final String category1 = "Category 1";
		final String category2 = "Category 2";
		final String category3 = "Category 3";
		final String category4 = "Category 4";
		final String category5 = "Category 5";

		final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		dataset.addValue(1.0, series1, category1);
		dataset.addValue(4.0, series1, category2);
		dataset.addValue(3.0, series1, category3);
		dataset.addValue(5.0, series1, category4);
		dataset.addValue(5.0, series1, category5);

		dataset.addValue(5.0, series2, category1);
		dataset.addValue(7.0, series2, category2);
		dataset.addValue(6.0, series2, category3);
		dataset.addValue(8.0, series2, category4);
		dataset.addValue(4.0, series2, category5);

		dataset.addValue(4.0, series3, category1);
		dataset.addValue(3.0, series3, category2);
		dataset.addValue(2.0, series3, category3);
		dataset.addValue(3.0, series3, category4);
		dataset.addValue(6.0, series3, category5);

		return dataset;

	}

	private JFreeChart createChart(final CategoryDataset dataset) {

		final JFreeChart chart = ChartFactory.createBarChart("Bar Chart Demo", // chart
																				// title
				"Category", // domain axis label
				"Value", // range axis label
				dataset, // data
				PlotOrientation.VERTICAL, // orientation
				true, // include legend
				true, // tooltips?
				false // URLs?
		);

		chart.setBackgroundPaint(Color.white);

		final CategoryPlot plot = chart.getCategoryPlot();
		plot.setBackgroundPaint(Color.lightGray);
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);

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

		final BarRenderer renderer = (BarRenderer) plot.getRenderer();
		renderer.setDrawBarOutline(false);

		final GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f, 0.0f, Color.lightGray);
		final GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f, 0.0f, Color.lightGray);
		final GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f, 0.0f, Color.lightGray);
		renderer.setSeriesPaint(0, gp0);
		renderer.setSeriesPaint(1, gp1);
		renderer.setSeriesPaint(2, gp2);

		final CategoryAxis domainAxis = plot.getDomainAxis();
		domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));

		return chart;
	}

	public static void main(final String[] args) {

		final JavaJFreeChartBarChartExample barChart = new JavaJFreeChartBarChartExample("Bar Chart Example");
		barChart.pack();
		RefineryUtilities.centerFrameOnScreen(barChart);
		barChart.setVisible(true);
	}
}
                /* ............... END ............... */
        

Output


JFreeChart Bar Chart Example

Notes:

  • Java bar charts that can be created with JFreeChart. Most bar charts are created using data obtained via the CategoryDataset interface.
  • A bar chart is created using data from a CategoryDataset, and represents each data item as a bar where the length of the bar is equal to the data value.
  • The first step in generating the chart is to create a dataset. You can use any class that implements the CategoryDataset interface for the example, we have used the DefaultCategoryDataset class (included in the JFreeChart distribution).
  • The createBarChart() method in the ChartFactory class provides a convenient way to create the bar chart in java.
  • The chart will be initialised using default settings for most attributes. You are, of course, free to modify any of the settings to change the appearance of your chart. Below showing the several attributes that can be modified.
  • The chart background color.
  • The “auto tick units” on the range axis (so that the tick labels always display integer values).
  • Gradient paint is used for the series colors.
  • Changing the chart’s background color is simple, because this is an attribute maintained by the JFreeChart class.
  • chart.setBackgroundPaint(new Color(0xBBBBDD)); // set the background color for the chart.

Tags

JFreeChart Bar Chart Example, Java, JFreeChart, jfreechart barchart demo, bar chart java code, jfreechart customize bar chart

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 BarChart Color Customise JFreeChart 14-10-2016
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 Pie Chart Example JFreeChart 04-06-2016

1