JavaRanch Home
 
Front Page FAQs Ranchers Categories Recent Changes To Do Upload

J Chart2 D Demo   



This example shows how a simple static chart can be created using the JChart2D package. It is based on the MinimalStaticChart example of the JChart2D documentation, but shows off a few more possibilities of the package, like working with colors and grids, and writing a chart to a file.


import java.awt.image.BufferedImage;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

import info.monitorenter.gui.chart.Chart2D;
import info.monitorenter.gui.chart.IAxis;
import info.monitorenter.gui.chart.ITrace2D;
import info.monitorenter.gui.chart.traces.Trace2DSimple;
 
public class JChart2DDemo
{

    Chart2D chart;

    public static void main (String[] args)
    {
        JChart2DDemo myDemo = new JChart2DDemo();
        myDemo.staticDemo();
    }

    public void staticDemo()
    {
        // Create a chart:  
        chart = new Chart2D();

        // make it colorful
        chart.setBackground(Color.WHITE);
        chart.setForeground(Color.BLUE);
        chart.setGridColor(Color.GREEN);

        // Create an ITrace: 
        ITrace2D trace = new Trace2DSimple(); 
        // color it red
        trace.setColor(Color.RED);
        // give it a name to display
        trace.setName("Static Demo");

        // turn on grids along both axes
        IAxis axisX = chart.getAxisX();
        axisX.setPaintGrid(true);
        IAxis axisY = chart.getAxisY();
        axisY.setPaintGrid(true);

        // Add all points, as it is static: 
        Random random = new Random();
        for (int i=100; i>=0; i--)
        {
            trace.addPoint(i, random.nextDouble()*10.0+i);
        }
        // Add the trace to the chart: 
        chart.addTrace(trace);

        // Make it visible:
        // Create a frame.
        JFrame frame = new JFrame("MinimalStaticChart");
        // add the chart to the frame: 
        frame.getContentPane().add(chart);
        frame.setSize(400300);
        // Enable the termination button [cross on the upper right edge]: 
        frame.addWindowListener(
            new WindowAdapter()
            {
                public void windowClosing (WindowEvent e)
                {
                    // save the chart to a file
                    try
                    {
                        BufferedImage bi = chart.snapShot();
                        ImageIO.write(bi, "JPEG"new File("chart.jpg"));
                        // other possible file formats are PNG and BMP
                    } catch (Exception ex)
                    {
                        System.err.println("couldn't write chart to file: "+ex.getMessage());
                    }

                    System.exit(0);
                }
            }
        );
        frame.show();
    }
}


OtherOpenSourceProjectsFaq CategoryCodeSamples CodeBarn
Front Page FAQs Ranchers Categories Recent Changes To Do Upload
Last Edited: 29 September 2007 What's Changed?
 
Copyright © 1998-2008 Paul Wheaton | Home | Contact Us | Privacy | Register