org.jfree.chart.demo.StatisticalLineChartDemo1.java Source code

Java tutorial

Introduction

Here is the source code for org.jfree.chart.demo.StatisticalLineChartDemo1.java

Source

package org.jfree.chart.demo;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JPanel;

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.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StatisticalLineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class StatisticalLineChartDemo1 extends ApplicationFrame {

    private static final long serialVersionUID = 1L;

    public StatisticalLineChartDemo1(String s) {
        super(s);
        CategoryDataset categorydataset = createDataset();
        JFreeChart jfreechart = createChart(categorydataset);
        ChartPanel chartpanel = new ChartPanel(jfreechart);
        chartpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartpanel);
    }

    private static CategoryDataset createDataset() {
        DefaultStatisticalCategoryDataset defaultstatisticalcategorydataset = new DefaultStatisticalCategoryDataset();
        defaultstatisticalcategorydataset.add(10D, 2.3999999999999999D, "Row 1", "Column 1");
        defaultstatisticalcategorydataset.add(15D, 4.4000000000000004D, "Row 1", "Column 2");
        defaultstatisticalcategorydataset.add(13D, 2.1000000000000001D, "Row 1", "Column 3");
        defaultstatisticalcategorydataset.add(7D, 1.3D, "Row 1", "Column 4");
        defaultstatisticalcategorydataset.add(22D, 2.3999999999999999D, "Row 2", "Column 1");
        defaultstatisticalcategorydataset.add(18D, 4.4000000000000004D, "Row 2", "Column 2");
        defaultstatisticalcategorydataset.add(28D, 2.1000000000000001D, "Row 2", "Column 3");
        defaultstatisticalcategorydataset.add(17D, 1.3D, "Row 2", "Column 4");
        return defaultstatisticalcategorydataset;
    }

    private static JFreeChart createChart(CategoryDataset categorydataset) {
        JFreeChart jfreechart = ChartFactory.createLineChart("Statistical Line Chart Demo 1", "Type", "Value",
                categorydataset, PlotOrientation.VERTICAL, true, true, false);
        jfreechart.setBackgroundPaint(Color.white);
        CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
        categoryplot.setBackgroundPaint(Color.lightGray);
        categoryplot.setRangeGridlinePaint(Color.white);
        CategoryAxis categoryaxis = categoryplot.getDomainAxis();
        categoryaxis.setUpperMargin(0.0D);
        categoryaxis.setLowerMargin(0.0D);
        NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        numberaxis.setAutoRangeIncludesZero(true);
        StatisticalLineAndShapeRenderer statisticallineandshaperenderer = new StatisticalLineAndShapeRenderer(true,
                false);
        categoryplot.setRenderer(statisticallineandshaperenderer);
        return jfreechart;
    }

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

    public static void main(String args[]) {
        StatisticalLineChartDemo1 statisticallinechartdemo1 = new StatisticalLineChartDemo1(
                "JFreeChart: StatisticalLineChartDemo1.java");
        statisticallinechartdemo1.pack();
        RefineryUtilities.centerFrameOnScreen(statisticallinechartdemo1);
        statisticallinechartdemo1.setVisible(true);
    }
}