edu.ucla.stat.SOCR.chart.demo.StatisticalLineChartDemo3.java Source code

Java tutorial

Introduction

Here is the source code for edu.ucla.stat.SOCR.chart.demo.StatisticalLineChartDemo3.java

Source

/****************************************************
Statistics Online Computational Resource (SOCR)
http://www.StatisticsResource.org
     
All SOCR programs, materials, tools and resources are developed by and freely disseminated to the entire community.
Users may revise, extend, redistribute, modify under the terms of the Lesser GNU General Public License
as published by the Open Source Initiative http://opensource.org/licenses/. All efforts should be made to develop and distribute
factually correct, useful, portable and extensible resource all available in all digital formats for free over the Internet.
     
SOCR resources are distributed in the hope that they will be useful, but without
any warranty; without any explicit, implicit or implied warranty for merchantability or
fitness for a particular purpose. See the GNU Lesser General Public License for
more details see http://opensource.org/licenses/lgpl-license.php.
     
http://www.SOCR.ucla.edu
http://wiki.stat.ucla.edu/socr
 It s Online, Therefore, It Exists! 
****************************************************/
package edu.ucla.stat.SOCR.chart.demo;

import java.awt.Color;
import java.beans.PropertyChangeListener;

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.StatisticalBarRenderer;
import org.jfree.chart.renderer.category.StatisticalLineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;
import org.jfree.data.statistics.Statistics;

import edu.ucla.stat.SOCR.chart.ChartGenerator_JTable;
import edu.ucla.stat.SOCR.chart.SuperCategoryChart_Stat_Raw_Vertical;
import edu.ucla.stat.SOCR.chart.gui.SOCRCategoryCellLabelGenerator;

/**
 * A simple demonstration application showing how to create a "statistical" 
 * line chart using data from a {@link CategoryDataset}.
 */
public class StatisticalLineChartDemo3 extends SuperCategoryChart_Stat_Raw_Vertical
        implements PropertyChangeListener {
    public void doTest() {
        JFreeChart chart;
        ChartGenerator_JTable chartMaker = new ChartGenerator_JTable();

        resetChart();
        showMessageDialog("StatisticalLineChartDemo3 doTest get called!");

        int no_series = dataTable.getRowCount() - 1;
        int no_category = dataTable.getColumnCount() - 2;
        int[][] pairs = new int[no_category][2];
        for (int i = 0; i < no_category; i++) {
            pairs[i][0] = i + 1; //column i store category i
            pairs[i][1] = 0; //column 0 stores series name
        }
        chart = chartMaker.getCategoryChart("LineStatRaw", "LineChart", "Type", "value", dataTable, no_category,
                pairs, "");
        chartPanel = new ChartPanel(chart, false);

        setChart();
    }

    /**
     * Creates a sample dataset.
     * 
     * @return The dataset.
     */
    protected CategoryDataset createDataset(boolean isDemo) {
        double mean, stdDev;
        if (isDemo) {
            SERIES_COUNT = 2;
            //   CATEGORY_COUNT = 4;
            VALUE_COUNT = 10;
            values_storage = new String[SERIES_COUNT][CATEGORY_COUNT];

            DefaultStatisticalCategoryDataset dataset = new DefaultStatisticalCategoryDataset();

            for (int s = 0; s < SERIES_COUNT; s++) {
                for (int c = 0; c < CATEGORY_COUNT; c++) {
                    Double[] values = createValueList(0, 20.0, VALUE_COUNT);
                    values_storage[s][c] = vs;
                    mean = Statistics.calculateMean(values);
                    stdDev = Statistics.getStdDev(values);
                    dataset.add(mean, stdDev, "Series " + s, "Category " + c);
                }
            }

            return dataset;
        } else
            return super.createDataset(false);

    }

    /**
     * Creates a sample chart.
     * 
     * @param dataset  a dataset.
     * 
     * @return The chart.
     */
    protected JFreeChart createChart(CategoryDataset dataset) {

        // create the chart...
        JFreeChart chart = ChartFactory.createLineChart(chartTitle, // chart title
                domainLabel, // domain axis label
                rangeLabel, // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                !legendPanelOn, // include legend
                true, // tooltips
                false // urls
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
        chart.setBackgroundPaint(Color.white);

        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.white);

        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setUpperMargin(0.0);
        domainAxis.setLowerMargin(0.0);

        // customise the range axis...
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setAutoRangeIncludesZero(true);

        // customise the renderer...
        StatisticalLineAndShapeRenderer renderer = new StatisticalLineAndShapeRenderer(true, false);
        renderer.setLegendItemLabelGenerator(
                new SOCRCategoryCellLabelGenerator(dataset, values_storage, SERIES_COUNT, CATEGORY_COUNT));
        plot.setRenderer(renderer);

        // OPTIONAL CUSTOMISATION COMPLETED.
        return chart;
    }

    protected JFreeChart createLegend(CategoryDataset dataset) {

        //  JFreeChart chart = ChartFactory.createAreaChart(
        JFreeChart chart = ChartFactory.createLineChart(chartTitle, // chart title
                domainLabel, // domain axis label
                rangeLabel, // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                true, // include legend
                true, // tooltips
                false // urls
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
        chart.setBackgroundPaint(Color.white);
        CategoryPlot plot = chart.getCategoryPlot();

        StatisticalLineAndShapeRenderer renderer = new StatisticalLineAndShapeRenderer(true, false);
        renderer.setLegendItemLabelGenerator(
                new SOCRCategoryCellLabelGenerator(dataset, values_storage, SERIES_COUNT, CATEGORY_COUNT));
        plot.setRenderer(renderer);
        return chart;

    }
}