JFreeChart-Line Chart
JFreeChart Line Chart with Tutorial, Basic Terminology, What is Jfree Chart, Jfreechart Installation, Jfreechart class, Jfreechart's key classes, ChartUtilities class, ChartFrame class, PiePlot3D class, NumberAxis Class, XYSeriesCollection Class, TimeSeries Class, TimeSeriesCollection, PiePlot class etc.
JFreeChart-Line Chart
A line chart is a series of points connected by a straight line segment. The information is shown through these connected lines. The line chart represents how data alters at equal time-frequency.
Line Chart example
Let us consider a sample data which shows frequent traffic on our website www.hpnmaratt.com.
Date | Total number of daily visitors |
---|---|
2016-12-19 | 200 |
2016-12-20 | 150 |
2016-12-21 | 100 |
2016-12-22 | 210 |
2016-12-23 | 240 |
2016-12-24 | 195 |
2016-12-25 | 245 |
Date | Total number of unique visitors |
---|---|
2016-12-19 | 150 |
2016-12-20 | 130 |
2016-12-21 | 95 |
2016-12-22 | 195 |
2016-12-23 | 200 |
2016-12-24 | 180 |
2016-12-25 | 230 |
The following codes create a line chart from the above sample data:
LineChartExample.java
- import javax.swing.JFrame;
- import javax.swing.SwingUtilities;
- import org.jfree.chart.ChartFactory;
- import org.jfree.chart.ChartPanel;
- import org.jfree.chart.JFreeChart;
- import org.jfree.data.category.DefaultCategoryDataset;
- public class LineChartExample extends JFrame {
- private static final long serialVersionUID = 1L;
- public LineChartExample(String title) {
- super(title);
- // Create dataset
- DefaultCategoryDataset dataset = createDataset();
- // Create chart
- JFreeChart chart = ChartFactory.createLineChart(
- "Site Traffic", // Chart title
- "Date", // X-Axis Label
- "Number of Visitor", // Y-Axis Label
- dataset
- );
- ChartPanel panel = new ChartPanel(chart);
- setContentPane(panel);
- }
- private DefaultCategoryDataset createDataset() {
- String series1 = "Visitor";
- String series2 = "Unique Visitor";
- DefaultCategoryDataset dataset = new DefaultCategoryDataset();
- dataset.addValue(200, series1, "2016-12-19");
- dataset.addValue(150, series1, "2016-12-20");
- dataset.addValue(100, series1, "2016-12-21");
- dataset.addValue(210, series1, "2016-12-22");
- dataset.addValue(240, series1, "2016-12-23");
- dataset.addValue(195, series1, "2016-12-24");
- dataset.addValue(245, series1, "2016-12-25");
- dataset.addValue(150, series2, "2016-12-19");
- dataset.addValue(130, series2, "2016-12-20");
- dataset.addValue(95, series2, "2016-12-21");
- dataset.addValue(195, series2, "2016-12-22");
- dataset.addValue(200, series2, "2016-12-23");
- dataset.addValue(180, series2, "2016-12-24");
- dataset.addValue(230, series2, "2016-12-25");
- return dataset;
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(() -> {
- LineChartExample example = new LineChartExample("Line Chart Example");
- example.setAlwaysOnTop(true);
- example.pack();
- example.setSize(600, 400);
- example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- example.setVisible(true);
- });
- }
- }