📄 panscrollzoomdemo.java
字号:
/* =========================================================== * JFreeChart : a free chart library for the Java(tm) platform * =========================================================== * * (C) Copyright 2000-2004, 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., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. * * [Java is a trademark or registered trademark of Sun Microsystems, Inc. * in the United States and other countries.] * * ---------------------- * PanScrollZoomDemo.java * ---------------------- * (C) Copyright 2004, by Object Refinery Limited and Contributors. * * Original Author: Matthias Rose (Ablay & Fodi GmbH, Germany); * Contributor(s): Eduardo Ramalho; * David Gilbert (for Object Refinery Limited); * * $Id: PanScrollZoomDemo.java,v 1.10 2004/05/11 14:56:17 mungady Exp $ * * Changes * ------- * 18-Feb-2004 : Version 1 added to JFreeChart distribution, contributed by Matthias Rose (DG); * */package org.jfree.chart.demo;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Cursor;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.geom.Point2D;import java.awt.geom.Rectangle2D;import javax.swing.AbstractButton;import javax.swing.BoundedRangeModel;import javax.swing.ButtonGroup;import javax.swing.DefaultBoundedRangeModel;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JScrollBar;import javax.swing.JToggleButton;import javax.swing.JToolBar;import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import org.jfree.chart.ChartPanel;import org.jfree.chart.ChartRenderingInfo;import org.jfree.chart.JFreeChart;import org.jfree.chart.axis.NumberAxis;import org.jfree.chart.axis.ValueAxis;import org.jfree.chart.event.ChartChangeEvent;import org.jfree.chart.event.ChartChangeListener;import org.jfree.chart.labels.StandardXYToolTipGenerator;import org.jfree.chart.plot.Plot;import org.jfree.chart.plot.ValueAxisPlot;import org.jfree.chart.plot.XYPlot;import org.jfree.chart.renderer.StandardXYItemRenderer;import org.jfree.chart.renderer.XYItemRenderer;import org.jfree.chart.title.TextTitle;import org.jfree.data.Range;import org.jfree.data.XYSeries;import org.jfree.data.XYSeriesCollection;import org.jfree.ui.RefineryUtilities;import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;/** * A demo for panning, scrolling and zooming. */public class PanScrollZoomDemo extends JFrame implements ActionListener, ChangeListener, ChartChangeListener, MouseListener, MouseMotionListener { /** The panel that displays the chart. */ private ChartPanel chartPanel; /** The scroll factor. */ private double scrollFactor = 1000; /** The scroll bar. */ private JScrollBar scrollBar; /** The starting point for panning. */ private Point2D panStartPoint; /** The min/max values for the primary axis. */ private double[] primYMinMax = new double[2]; /** The min/max values for the secondary axis. */ private double[] secondYMinMax = new double[2]; /** Action command for the 'Pan' button. */ private static final String ACTION_CMD_PAN = "pan"; /** Action command for the zoom box button. */ private static final String ACTION_CMD_ZOOM_BOX = "zoomBox"; /** Action command for the zoom fit button. */ private static final String ACTION_CMD_ZOOM_TO_FIT = "zoomFit"; /** Action command for the '+' button. */ private static final String ACTION_CMD_ZOOM_IN = "zoomIn"; /** Action command for the '-' button. */ private static final String ACTION_CMD_ZOOM_OUT = "zoomOut"; /** The zoom factor. */ private static final double ZOOM_FACTOR = 0.8; /** The toolbar. */ private JToolBar toolBar; /** The zoom button. */ private AbstractButton zoomButton; /** The pan button. */ private AbstractButton panButton; /** The zoom in button. */ private AbstractButton zoomInButton; /** The zoom out button. */ private AbstractButton zoomOutButton; /** The fit button. */ private AbstractButton fitButton; /** * Creates a new demo instance. * * @param frameTitle the frame title. */ public PanScrollZoomDemo(final String frameTitle) { super(frameTitle); this.toolBar = createToolbar(); getContentPane().setLayout(new BorderLayout()); getContentPane().add(this.toolBar, BorderLayout.SOUTH); final JFreeChart chart = createChart(); this.scrollBar.setModel(new DefaultBoundedRangeModel()); recalcScrollBar(chart.getPlot()); this.chartPanel = new ChartPanel(chart) { public void autoRangeBoth() { System.out.println("Use 'Fit all' button"); } }; chart.addChangeListener(this); // enable zoom actionPerformed(new ActionEvent(this, 0, ACTION_CMD_ZOOM_BOX)); // MouseListeners for pan function this.chartPanel.addMouseListener(this); this.chartPanel.addMouseMotionListener(this); // remove popup menu to allow panning // with right mouse pressed this.chartPanel.setPopupMenu(null); getContentPane().add(this.chartPanel); } /** * Creates a sample chart. * * @return a sample chart. */ private JFreeChart createChart() { final XYSeriesCollection primaryJFreeColl = new XYSeriesCollection(); final XYSeries left1 = new XYSeries("Left 1"); left1.add(1, 2); left1.add(2.8, 5.9); left1.add(3, null); left1.add(3.4, 2); left1.add(5, -1); left1.add(7, 1); primaryJFreeColl.addSeries(left1); final XYSeriesCollection secondaryJFreeColl = new XYSeriesCollection(); final XYSeries right1 = new XYSeries("Right 1"); right1.add(3.5, 2.2); right1.add(1.2, 1.3); right1.add(5.7, 4.1); right1.add(7.5, 7.4); secondaryJFreeColl.addSeries(right1); final NumberAxis xAxis = new NumberAxis("X"); xAxis.setAutoRangeIncludesZero(false); xAxis.setAutoRangeStickyZero(false); final NumberAxis primaryYAxis = new NumberAxis("Y1"); primaryYAxis.setAutoRangeIncludesZero(false); primaryYAxis.setAutoRangeStickyZero(false); // create plot final XYItemRenderer y1Renderer = new StandardXYItemRenderer(StandardXYItemRenderer.LINES); y1Renderer.setSeriesPaint(0, Color.blue); y1Renderer.setToolTipGenerator(new StandardXYToolTipGenerator()); final XYPlot xyPlot = new XYPlot(primaryJFreeColl, xAxis, primaryYAxis, y1Renderer); // 2nd y-axis final NumberAxis secondaryYAxis = new NumberAxis("Y2"); secondaryYAxis.setAutoRangeIncludesZero(false); secondaryYAxis.setAutoRangeStickyZero(false); xyPlot.setRangeAxis(1, secondaryYAxis); xyPlot.setDataset(1, secondaryJFreeColl); xyPlot.mapDatasetToRangeAxis(1, 1); xyPlot.mapDatasetToDomainAxis(1, 1); final XYItemRenderer y2Renderer = new StandardXYItemRenderer( StandardXYItemRenderer.SHAPES_AND_LINES ); y2Renderer.setToolTipGenerator(new StandardXYToolTipGenerator()); xyPlot.setRenderer(1, y2Renderer); // set some fixed y-dataranges and remember them // because default chartPanel.autoRangeBoth() // would destroy them ValueAxis axis = xyPlot.getRangeAxis(); this.primYMinMax[0] = -5; this.primYMinMax[1] = 15; axis.setLowerBound(this.primYMinMax[0]); axis.setUpperBound(this.primYMinMax[1]); axis = xyPlot.getRangeAxis(1); this.secondYMinMax[0] = -1; this.secondYMinMax[1] = 10; axis.setLowerBound(this.secondYMinMax[0]); axis.setUpperBound(this.secondYMinMax[1]); // Title + legend final String title = "To pan in zoom mode hold right mouse pressed"; final JFreeChart ret = new JFreeChart(title, null, xyPlot, true); final TextTitle textTitle = new TextTitle( "(but you can only pan if the chart was zoomed before)" ); ret.addSubtitle(textTitle); return ret; } /** * Creates the toolbar. * * @return the toolbar. */ private JToolBar createToolbar() { final JToolBar toolbar = new JToolBar(); final ButtonGroup groupedButtons = new ButtonGroup(); // ACTION_CMD_PAN this.panButton = new JToggleButton(); prepareButton(this.panButton, ACTION_CMD_PAN, " Pan ", "Pan mode"); groupedButtons.add(this.panButton); toolbar.add(this.panButton); // ACTION_CMD_ZOOM_BOX this.zoomButton = new JToggleButton(); prepareButton(this.zoomButton, ACTION_CMD_ZOOM_BOX, " Zoom ", "Zoom mode"); groupedButtons.add(this.zoomButton); this.zoomButton.setSelected(true); // no other makes sense after startup toolbar.add(this.zoomButton); // end of toggle-button group for select/pan/zoom-box toolbar.addSeparator(); // ACTION_CMD_ZOOM_IN this.zoomInButton = new JButton(); prepareButton(this.zoomInButton, ACTION_CMD_ZOOM_IN, " + ", "Zoom in"); toolbar.add(this.zoomInButton); // ACTION_CMD_ZOOM_OUT this.zoomOutButton = new JButton(); prepareButton(this.zoomOutButton, ACTION_CMD_ZOOM_OUT, " - ", "Zoom out"); toolbar.add(this.zoomOutButton); // ACTION_CMD_ZOOM_TO_FIT this.fitButton = new JButton(); prepareButton(this.fitButton, ACTION_CMD_ZOOM_TO_FIT, " Fit ", "Fit all"); toolbar.add(this.fitButton); toolbar.addSeparator(); this.scrollBar = new JScrollBar(JScrollBar.HORIZONTAL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -