1 /* =========================================================== 2 * JFreeChart : a free chart library for the Java(tm) platform 3 * =========================================================== 4 * 5 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors. 6 * 7 * Project Info: http://www.jfree.org/jfreechart/index.html 8 * 9 * This library is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU Lesser General Public License as published by 11 * the Free Software Foundation; either version 2.1 of the License, or 12 * (at your option) any later version. 13 * 14 * This library is distributed in the hope that it will be useful, but 15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17 * License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this library; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22 * USA. 23 * 24 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 25 * Other names may be trademarks of their respective owners.] 26 * 27 * ------------------------- 28 * SWTMultipleAxisDemo1.java 29 * ------------------------- 30 * (C) Copyright 2006-2009, by Object Refinery Limited and Contributors. 31 * 32 * Original Author: David Gilbert (for Object Refinery Limited); 33 * Contributor(s): Henry Proudhon; 34 * 35 * Changes 36 * ------- 37 * 23-Aug-2006 : New class (HP); 38 * 39 */ 40 41 package org.jfree.experimental.chart.swt.demo; 42 43 import java.awt.Color; 44 45 import javax.swing.JPanel; 46 47 import org.eclipse.swt.SWT; 48 import org.eclipse.swt.layout.FillLayout; 49 import org.eclipse.swt.widgets.Display; 50 import org.eclipse.swt.widgets.Shell; 51 import org.jfree.chart.ChartFactory; 52 import org.jfree.chart.ChartPanel; 53 import org.jfree.chart.JFreeChart; 54 import org.jfree.chart.axis.AxisLocation; 55 import org.jfree.chart.axis.NumberAxis; 56 import org.jfree.chart.plot.PlotOrientation; 57 import org.jfree.chart.plot.XYPlot; 58 import org.jfree.chart.renderer.xy.StandardXYItemRenderer; 59 import org.jfree.chart.renderer.xy.XYItemRenderer; 60 import org.jfree.chart.title.TextTitle; 61 import org.jfree.data.time.Minute; 62 import org.jfree.data.time.RegularTimePeriod; 63 import org.jfree.data.time.TimeSeries; 64 import org.jfree.data.time.TimeSeriesCollection; 65 import org.jfree.data.xy.XYDataset; 66 import org.jfree.experimental.chart.swt.ChartComposite; 67 import org.jfree.ui.RectangleInsets; 68 69 /** 70 * This demo shows a time series chart that has multiple range axes. 71 */ 72 public class SWTMultipleAxisDemo1 73 { 74 /** 75 * Creates the demo chart. 76 * 77 * @return The chart. 78 */ createChart()79 private static JFreeChart createChart() { 80 81 XYDataset dataset1 = createDataset("Series 1", 100.0, new Minute(), 82 200); 83 84 JFreeChart chart = ChartFactory.createTimeSeriesChart( 85 "Multiple Axis Demo 3", 86 "Time of Day", 87 "Primary Range Axis", 88 dataset1, 89 true, 90 true, 91 false 92 ); 93 94 chart.setBackgroundPaint(Color.white); 95 chart.setBorderVisible(true); 96 chart.setBorderPaint(Color.BLACK); 97 TextTitle subtitle = new TextTitle("Four datasets and four range axes."); 98 chart.addSubtitle(subtitle); 99 XYPlot plot = (XYPlot) chart.getPlot(); 100 plot.setOrientation(PlotOrientation.VERTICAL); 101 plot.setBackgroundPaint(Color.lightGray); 102 plot.setDomainGridlinePaint(Color.white); 103 plot.setRangeGridlinePaint(Color.white); 104 105 plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); 106 XYItemRenderer renderer = plot.getRenderer(); 107 renderer.setSeriesPaint(0, Color.black); 108 109 // AXIS 2 110 NumberAxis axis2 = new NumberAxis("Range Axis 2"); 111 axis2.setAutoRangeIncludesZero(false); 112 axis2.setLabelPaint(Color.red); 113 axis2.setTickLabelPaint(Color.red); 114 plot.setRangeAxis(1, axis2); 115 plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT); 116 117 XYDataset dataset2 = createDataset("Series 2", 1000.0, new Minute(), 118 170); 119 plot.setDataset(1, dataset2); 120 plot.mapDatasetToRangeAxis(1, 1); 121 XYItemRenderer renderer2 = new StandardXYItemRenderer(); 122 renderer2.setSeriesPaint(0, Color.red); 123 plot.setRenderer(1, renderer2); 124 125 // AXIS 3 126 NumberAxis axis3 = new NumberAxis("Range Axis 3"); 127 axis3.setLabelPaint(Color.blue); 128 axis3.setTickLabelPaint(Color.blue); 129 //axis3.setPositiveArrowVisible(true); 130 plot.setRangeAxis(2, axis3); 131 132 XYDataset dataset3 = createDataset("Series 3", 10000.0, new Minute(), 133 170); 134 plot.setDataset(2, dataset3); 135 plot.mapDatasetToRangeAxis(2, 2); 136 XYItemRenderer renderer3 = new StandardXYItemRenderer(); 137 renderer3.setSeriesPaint(0, Color.blue); 138 plot.setRenderer(2, renderer3); 139 140 // AXIS 4 141 NumberAxis axis4 = new NumberAxis("Range Axis 4"); 142 axis4.setLabelPaint(Color.green); 143 axis4.setTickLabelPaint(Color.green); 144 plot.setRangeAxis(3, axis4); 145 146 XYDataset dataset4 = createDataset("Series 4", 25.0, new Minute(), 200); 147 plot.setDataset(3, dataset4); 148 plot.mapDatasetToRangeAxis(3, 3); 149 150 XYItemRenderer renderer4 = new StandardXYItemRenderer(); 151 renderer4.setSeriesPaint(0, Color.green); 152 plot.setRenderer(3, renderer4); 153 154 return chart; 155 } 156 157 /** 158 * Creates a sample dataset. 159 * 160 * @param name the dataset name. 161 * @param base the starting value. 162 * @param start the starting period. 163 * @param count the number of values to generate. 164 * 165 * @return The dataset. 166 */ createDataset(String name, double base, RegularTimePeriod start, int count)167 private static XYDataset createDataset(String name, double base, 168 RegularTimePeriod start, int count) { 169 170 TimeSeries series = new TimeSeries(name); 171 RegularTimePeriod period = start; 172 double value = base; 173 for (int i = 0; i < count; i++) { 174 series.add(period, value); 175 period = period.next(); 176 value = value * (1 + (Math.random() - 0.495) / 10.0); 177 } 178 179 TimeSeriesCollection dataset = new TimeSeriesCollection(); 180 dataset.addSeries(series); 181 182 return dataset; 183 184 } 185 186 /** 187 * Creates a panel for the demo (used by SuperDemo.java). 188 * 189 * @return A panel. 190 */ createDemoPanel()191 public static JPanel createDemoPanel() { 192 JFreeChart chart = createChart(); 193 return new ChartPanel(chart); 194 } 195 196 /** 197 * Starting point for the demonstration application. 198 * 199 * @param args ignored. 200 */ main(String[] args)201 public static void main(String[] args) 202 { 203 final JFreeChart chart = createChart(); 204 final Display display = new Display(); 205 Shell shell = new Shell(display); 206 shell.setSize(600, 300); 207 shell.setLayout(new FillLayout()); 208 shell.setText("Test for jfreechart running with SWT"); 209 ChartComposite frame = new ChartComposite(shell, SWT.NONE, chart, true); 210 frame.setDisplayToolTips(false); 211 frame.setHorizontalAxisTrace(true); 212 frame.setVerticalAxisTrace(true); 213 shell.open(); 214 while (!shell.isDisposed()) { 215 if (!display.readAndDispatch()) 216 display.sleep(); 217 } 218 } 219 220 } 221 222