搜尋此網誌

2012年10月31日

JChart2D Zoomable char

package com.qci.jchart2ddemo;

import info.monitorenter.gui.chart.Chart2D;
import info.monitorenter.gui.chart.ITrace2D;
import info.monitorenter.gui.chart.ZoomableChart;
import info.monitorenter.gui.chart.pointpainters.PointPainterDisc;
import info.monitorenter.gui.chart.traces.Trace2DSimple;
import info.monitorenter.gui.chart.views.ChartPanel;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;

public class ZoomTest extends JFrame {

     private static final long serialVersionUID = -2249660781499017221L;

     class ZoomAllAdapter implements ActionListener {

         private ZoomableChart m_zoomableChart;

         public ZoomAllAdapter(final ZoomableChart chart) {
              this.m_zoomableChart = chart;
         }

         public void actionPerformed(final ActionEvent event) {
              this.m_zoomableChart.zoomAll();
         }
     }

     public static void main(final String[] args) {

         ZoomTest zoomTest = new ZoomTest();
         // Show the frame
         zoomTest.setSize(640, 480);
         zoomTest.setVisible(true);

     }

     public ZoomTest() {

         super("ZoomTest");

         Container c = this.getContentPane();
         c.setLayout(new BorderLayout());

         // Create a chart
         ZoomableChart chart = new ZoomableChart();

         ITrace2D trace = new Trace2DSimple();
         // Add the trace to the chart. This has to be done before adding points (deadlock prevention):
         chart.addTrace(trace);   
         // 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);
         }

         chart.setToolTipType(Chart2D.ToolTipType.VALUE_SNAP_TO_TRACEPOINTS);
         // chart.setPointHighlighter(new PointPainterDisc(8));

         // Add chart to the pane
         c.add(new ChartPanel(chart));

         // Create the zoomAll button
         JButton zoomAllButton = new JButton("Zoom All");
         zoomAllButton.addActionListener(new ZoomAllAdapter(chart));

         // Add zoomAll button to the pane
         c.add(zoomAllButton, BorderLayout.NORTH);

         // Enable the termination button:
         this.addWindowListener(new WindowAdapter() {

              @Override
              public void windowClosing(final WindowEvent e) {
                  System.exit(0);
              }
         });

     }
}