com.mxgraph.examples.swing.chart.TimeSeriesChartDemo1.java Source code

Java tutorial

Introduction

Here is the source code for com.mxgraph.examples.swing.chart.TimeSeriesChartDemo1.java

Source

/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2009, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * -------------------------
 * TimeSeriesChartDemo1.java
 * -------------------------
 * (C) Copyright 2003-2009, by Object Refinery Limited and Contributors.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   ;
 *
 * Changes
 * -------
 * 09-Mar-2005 : Version 1, copied from the demo collection that ships with
 *               the JFreeChart Developer Guide (DG);
 *
 */

package com.mxgraph.examples.swing.chart;

import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;

import javax.swing.JPanel;
import org.jfree.data.time.Day;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.labels.XYItemLabelGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Hour;
import org.jfree.data.time.Minute;
import org.jfree.data.time.Month;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.date.MonthConstants;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;

import com.sun.xml.internal.ws.encoding.soap.SerializerConstants;

/**
 * An example of a time series chart.  For the most part, default settings are
 * used, except that the renderer is modified to show filled shapes (as well as
 * lines) at each data point.
 */
public class TimeSeriesChartDemo1 extends ApplicationFrame {

    /**
     * A demonstration application showing how to create a simple time series
     * chart.  This example uses monthly data.
     *
     * @param title  the frame title.
     */
    public TimeSeriesChartDemo1(String title) {
        super(title);
        ChartPanel chartPanel = (ChartPanel) createDemoPanel();
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
    }

    /**
     * Creates a chart.
     *
     * @param dataset  a dataset.
     *
     * @return A chart.
     */
    public static JFreeChart createChart(XYDataset dataset, String name) {

        JFreeChart chart = ChartFactory.createTimeSeriesChart(name, // title
                "Date", // x-axis label
                "Value", // y-axis label
                dataset, // data
                true, // create legend?
                true, // generate tooltips?
                false // generate URLs?
        );

        chart.setBackgroundPaint(Color.white);

        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairVisible(true);

        XYItemRenderer r = plot.getRenderer();
        if (r instanceof XYLineAndShapeRenderer) {
            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
            renderer.setBaseShapesVisible(true);
            renderer.setBaseShapesFilled(true);
            renderer.setDrawSeriesLineAsPath(true);
            renderer.setItemLabelGenerator(new StandardXYItemLabelGenerator());
            renderer.setBaseItemLabelsVisible(true);
            // 
            renderer.setItemLabelsVisible(true);
            // 
            renderer.setBaseShapesVisible(true);
            // 
            renderer.setBaseShapesFilled(true);

            //  renderer.setLegendItemLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));

        }

        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("HH:mm:ss"/*"yyyy-MM-dd HH:mm:ss"*/));

        chart.getTitle().setFont(new Font("", Font.BOLD, 15));
        chart.getLegend().setItemFont(new Font("", Font.BOLD, 15));
        //
        plot.getRangeAxis().setLabelFont(new Font("", Font.BOLD, 15));
        //
        chart.getLegend().setItemFont(new Font("", Font.ITALIC, 15));
        //
        plot.getDomainAxis().setTickLabelFont(new Font("", 1, 15));
        //
        plot.getDomainAxis().setLabelFont(new Font("", 1, 12));

        return chart;

    }

    /**
     * Creates a dataset, consisting of two series of monthly data.
     *
     * @return The dataset.
     */
    public static XYDataset createDataset() {

        TimeSeries s1 = new TimeSeries("L&G European Index Trust", Second.class);
        s1.add(new Second(30, new Minute()), 181.8);
        s1.add(new Second(10, new Minute()), 151.8);
        s1.add(new Second(20, new Minute()), 167.8);
        s1.add(new Second(37, new Minute()), 155.8);
        s1.add(new Second(45, new Minute()), 166.8);
        s1.add(new Second(24, new Minute()), 144.8);
        s1.add(new Second(6, new Minute()), 181.8);

        /* s1.add(new Second(1,1,1,1,3, 2001), 167.3);
         s1.add(new Second(3,3,3,3,4, 2001), 153.8);
         s1.add(new Second(5,5,5,5,5, 2001), 167.6);
         s1.add(new Second(6,6,6,6,6, 2001), 158.8);
         s1.add(new Second(7,7,7,7,7, 2001), 148.3);
         s1.add(new Second(8,8,8,8,8, 2001), 153.9);
         s1.add(new Second(9,9,9,9,9, 2001), 142.7);
         s1.add(new Second(10,10,8,8,8, 2001), 123.2);
         s1.add(new Second(1,1,1,1,11, 2001), 131.8);
         s1.add(new Second(1,2,3,4,12, 2001), 139.6);
         s1.add(new Second(5,2,4,2,1, 2002), 142.9);
         s1.add(new Second(5,6,5,6,2, 2002), 138.7);*/

        // ******************************************************************
        //  More than 150 demo applications are included with the JFreeChart
        //  Developer Guide...for more information, see:
        //
        //  >   http://www.object-refinery.com/jfreechart/guide.html
        //
        // ******************************************************************

        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);

        return dataset;

    }

    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     *
     * @return A panel.
     */
    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset(), "");
        ChartPanel panel = new ChartPanel(chart);
        panel.setFillZoomRectangle(true);
        panel.setMouseWheelEnabled(true);
        return panel;
    }

    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {

        TimeSeriesChartDemo1 demo = new TimeSeriesChartDemo1("Time Series Chart Demo 1");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}