How to create chart in Java?

JFreeChart Java chart library that makes it easy for developers to display professional quality charts in their applications. In this example we will see how to create a simple vertical bar chart using JFreeChart.

Example

package com.pretech;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class JavaVerticalBarExample {
	public static void main(String[] args) {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.setValue(40, "Phone", "Apple");
		dataset.setValue(45, "Phone", "Samsung");
		dataset.setValue(38, "Phone", "Sony");
		dataset.setValue(17, "Phone", "Nokia");
		dataset.setValue(12, "Phone", "LG");
		JFreeChart chart = ChartFactory.createBarChart("Phone Sales chart",
				"Phone", "Sales", dataset, PlotOrientation.VERTICAL, false,
				true, false);
		ChartFrame chartFrame = new ChartFrame(
				"Pretech shop Phone Sales chart", chart);
		chartFrame.setVisible(true);
		chartFrame.setSize(500, 400);
	}
}

Output



image


No comments:

Post a Comment

Model Context Protocol (MCP) — Complete Guide for Backend Engineers

  Model Context Protocol (MCP) — Complete Guide for Backend Engineers Build Tools, Resources, and AI-Driven Services Using LangChain Moder...

Featured Posts