1 38 39 package org.jfree.chart.demo; 40 41 import java.awt.BorderLayout ; 42 import java.awt.event.ItemEvent ; 43 import java.awt.event.ItemListener ; 44 45 import javax.swing.JCheckBox ; 46 import javax.swing.JPanel ; 47 48 import org.jfree.chart.ChartFactory; 49 import org.jfree.chart.ChartPanel; 50 import org.jfree.chart.JFreeChart; 51 import org.jfree.chart.plot.PlotOrientation; 52 import org.jfree.ui.ApplicationFrame; 53 import org.jfree.ui.RefineryUtilities; 54 55 60 public class MouseZoomDemo extends ApplicationFrame { 61 62 63 private ChartPanel chartPanel; 64 65 66 private JCheckBox xzoom; 67 68 69 private JCheckBox yzoom; 70 71 76 public MouseZoomDemo(String title) { 77 78 super(title); 79 SampleXYDataset data = new SampleXYDataset(); 80 JFreeChart chart = ChartFactory.createXYLineChart( 81 "Mouse Zoom Demo", 82 "X", 83 "Y", 84 data, 85 PlotOrientation.VERTICAL, 86 true, 87 true, 88 false 89 ); 90 91 chartPanel = new ChartPanel(chart); 92 chartPanel.setHorizontalZoom(false); 93 chartPanel.setVerticalZoom(false); 94 chartPanel.setHorizontalAxisTrace(false); 95 chartPanel.setVerticalAxisTrace(false); 96 chartPanel.setFillZoomRectangle(true); 97 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 98 99 JPanel main = new JPanel (new BorderLayout ()); 100 JPanel checkpanel = new JPanel (); 101 xzoom = new JCheckBox ("Horizontal Mouse Zooming"); 102 xzoom.setSelected(false); 103 yzoom = new JCheckBox ("Vertical Mouse Zooming"); 104 yzoom.setSelected(false); 105 CheckListener clisten = new CheckListener(); 106 xzoom.addItemListener(clisten); 107 yzoom.addItemListener(clisten); 108 checkpanel.add(xzoom); 109 checkpanel.add(yzoom); 110 main.add(checkpanel, BorderLayout.SOUTH); 111 main.add(chartPanel); 112 setContentPane(main); 113 114 } 115 116 127 132 public static void main(String [] args) { 133 134 MouseZoomDemo demo = new MouseZoomDemo("Mouse Zoom Demo"); 135 demo.pack(); 136 RefineryUtilities.centerFrameOnScreen(demo); 137 demo.setVisible(true); 138 139 } 140 141 146 class CheckListener implements ItemListener { 147 148 153 public void itemStateChanged(ItemEvent e) { 154 Object source = e.getItemSelectable(); 155 if (source == xzoom) { 156 if (e.getStateChange() == ItemEvent.DESELECTED) { 157 chartPanel.setHorizontalZoom(false); 158 chartPanel.setHorizontalAxisTrace(false); 159 chartPanel.repaint(); 160 } 161 else { 162 chartPanel.setHorizontalZoom(true); 163 chartPanel.setHorizontalAxisTrace(true); 164 } 165 } 166 else if (source == yzoom) { 167 if (e.getStateChange() == ItemEvent.DESELECTED) { 168 chartPanel.setVerticalZoom(false); 169 chartPanel.setVerticalAxisTrace(false); 170 chartPanel.repaint(); 171 } 172 else { 173 chartPanel.setVerticalZoom(true); 174 chartPanel.setVerticalAxisTrace(true); 175 } 176 } 177 } 178 } 179 180 } 181 | Popular Tags |