1 38 39 package org.jfree.chart.demo; 40 41 import java.awt.BasicStroke ; 42 import java.awt.BorderLayout ; 43 import java.awt.Color ; 44 import java.awt.event.ActionEvent ; 45 import java.awt.event.ActionListener ; 46 47 import javax.swing.JCheckBox ; 48 import javax.swing.JComboBox ; 49 import javax.swing.JLabel ; 50 import javax.swing.JPanel ; 51 import javax.swing.JTextField ; 52 53 import org.jfree.chart.ChartFactory; 54 import org.jfree.chart.ChartPanel; 55 import org.jfree.chart.JFreeChart; 56 import org.jfree.chart.plot.Plot; 57 import org.jfree.chart.plot.PlotOrientation; 58 import org.jfree.chart.plot.XYPlot; 59 import org.jfree.chart.renderer.XYStepAreaRenderer; 60 import org.jfree.data.XYDataset; 61 import org.jfree.data.XYSeries; 62 import org.jfree.data.XYSeriesCollection; 63 import org.jfree.ui.ApplicationFrame; 64 import org.jfree.ui.RefineryUtilities; 65 66 71 public class XYStepAreaChartDemo extends ApplicationFrame implements ActionListener { 72 73 74 private static final String ORIENT_VERT = "Plot vertical"; 75 76 77 private static final String ORIENT_HORIZ = "Plot horizontal"; 78 79 80 private static final Object [][] TEST_DATA = { 81 {new Integer (1), new Integer (500), Boolean.TRUE}, 83 {new Integer (2), new Integer (694)}, 84 {new Integer (3), new Integer (-734)}, 85 {new Integer (4), new Integer (453)}, 86 {new Integer (5), new Integer (500), Boolean.TRUE}, 87 {new Integer (6), new Integer (200)}, 88 {new Integer (7), new Integer (550), Boolean.TRUE}, 89 {new Integer (8), new Integer (-150), Boolean.TRUE}, 90 {new Integer (9), new Integer (232)}, 91 {new Integer (10), new Integer (734)}, 92 {new Integer (11), new Integer (400), Boolean.TRUE}, 93 }; 94 95 96 private ChartPanel chartPanel; 97 98 99 private XYSeries xySeries; 100 101 102 private JCheckBox nullValuesCheckBox; 103 104 105 private JCheckBox outlineCheckBox; 106 107 108 private JTextField rangeBaseTextField; 109 110 111 private JComboBox orientationComboBox; 112 113 118 public XYStepAreaChartDemo(String title) { 119 120 super(title); 121 122 xySeries = new XYSeries("Some data"); 123 for (int i = 0; i < TEST_DATA.length; i++) { 124 xySeries.add((Integer ) TEST_DATA[i][0], (Integer ) TEST_DATA[i][1]); 125 } 126 127 XYSeriesCollection dataset = new XYSeriesCollection(xySeries); 128 129 JFreeChart chart = createChart(dataset); 130 131 chartPanel = new ChartPanel(chart); 132 133 chartPanel.setHorizontalZoom(true); 135 chartPanel.setVerticalZoom(true); 136 137 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 139 140 Plot plot = chartPanel.getChart().getPlot(); 142 plot.setOutlineStroke(new BasicStroke (2)); 143 plot.setOutlinePaint(Color.magenta); 144 145 JPanel main = new JPanel (new BorderLayout ()); 147 JPanel optionsPanel = new JPanel (); 148 149 String [] options = {ORIENT_VERT, ORIENT_HORIZ}; 150 orientationComboBox = new JComboBox (options); 151 orientationComboBox.addActionListener(this); 152 optionsPanel.add(orientationComboBox); 153 154 outlineCheckBox = new JCheckBox ("Outline"); 155 outlineCheckBox.addActionListener(this); 156 optionsPanel.add(outlineCheckBox); 157 158 optionsPanel.add(new JLabel ("Base")); 159 rangeBaseTextField = new JTextField ("0", 5); 160 rangeBaseTextField.addActionListener(this); 161 optionsPanel.add(rangeBaseTextField); 162 163 nullValuesCheckBox = new JCheckBox ("NULL values"); 164 nullValuesCheckBox.addActionListener(this); 165 optionsPanel.add(nullValuesCheckBox); 166 167 main.add(optionsPanel, BorderLayout.SOUTH); 168 main.add(chartPanel); 169 setContentPane(main); 170 } 171 172 183 190 private JFreeChart createChart(XYDataset dataset) { 191 192 JFreeChart chart = ChartFactory.createXYStepAreaChart( 193 "XY Step Area Chart Demo", 194 "Domain (X)", "Range (Y)", 195 dataset, 196 PlotOrientation.VERTICAL, 197 true, true, false ); 201 202 XYPlot plot = chart.getXYPlot(); 204 plot.getRenderer().setSeriesPaint(0, Color.green); 205 206 XYStepAreaRenderer rend = (XYStepAreaRenderer) plot.getRenderer(); 208 rend.setShapesFilled(true); 209 210 return chart; 211 } 212 213 218 public void actionPerformed(ActionEvent evt) { 219 220 Object source = evt.getSource(); 221 222 if (source == nullValuesCheckBox) { 223 224 boolean withNulls = nullValuesCheckBox.isSelected(); 225 for (int i = 0; i < TEST_DATA.length; i++) { 226 Integer yVal = (Integer ) TEST_DATA[i][1]; 227 if (withNulls && TEST_DATA[i].length > 2) { 228 yVal = null; 229 } 230 xySeries.getDataItem(i).setY(yVal); 231 } 232 233 } 234 else if (source == outlineCheckBox) { 235 236 XYPlot plot = (XYPlot) chartPanel.getChart().getPlot(); 237 ((XYStepAreaRenderer) plot.getRenderer()).setOutline(outlineCheckBox.isSelected()); 238 239 } 240 else if (source == rangeBaseTextField) { 241 242 double val = Double.parseDouble(rangeBaseTextField.getText()); 243 XYPlot plot = (XYPlot) chartPanel.getChart().getPlot(); 244 XYStepAreaRenderer rend = (XYStepAreaRenderer) plot.getRenderer(); 245 rend.setRangeBase(val); 246 247 } 248 else if (source == orientationComboBox) { 249 250 XYPlot plot = (XYPlot) chartPanel.getChart().getPlot(); 251 if (orientationComboBox.getSelectedItem() == ORIENT_HORIZ) { 252 plot.setOrientation(PlotOrientation.HORIZONTAL); 253 } 254 else if (orientationComboBox.getSelectedItem() == ORIENT_VERT) { 255 plot.setOrientation(PlotOrientation.VERTICAL); 256 } 257 } 258 259 chartPanel.repaint(); 260 } 261 262 267 public static void main(String [] args) { 268 XYStepAreaChartDemo demo = new XYStepAreaChartDemo("Step Area XY Chart Demo"); 269 demo.pack(); 270 RefineryUtilities.centerFrameOnScreen(demo); 271 demo.setVisible(true); 272 } 273 } 274 | Popular Tags |