1 | public class TimeSeriesChartDemo1 extends ApplicationFrame {↵ | | 1 | public class ↵
|
|
2 | /**↵ | | |
|
3 | * A demonstration application showing how to create a simple time series ↵ | | |
|
4 | * chart. This example uses monthly data.↵ | | |
|
5 | *↵ | | |
|
6 | * @param title the frame title.↵ | | |
|
7 | */↵ | | |
|
8 | public TimeSeriesChartDemo1(String title) {↵ | | |
|
9 | super(title);↵ | | |
|
10 | ChartPanel chartPanel = (ChartPanel) createDemoPanel();↵ | | |
|
11 | chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));↵ | | |
|
12 | chartPanel.setMouseZoomable(true, false);↵ | | |
|
13 | setContentPane(chartPanel);↵ | | |
|
14 | }↵ | | |
|
15 |
↵ | | 2 | SWTTimeSeriesDemo↵
|
| | | 3 | {↵
|
|
16 | /**
↵ | | 4 | /**↵
|
17 | * Creates a chart.
↵ | | 5 | * Creates a chart.↵
|
18 | *
↵ | | 6 | * ↵
|
19 | * @param dataset a dataset.
↵ | | 7 | * @param dataset a dataset.↵
|
20 | *
↵ | | 8 | * ↵
|
21 | * @return A chart.
↵ | | 9 | * @return A chart.↵
|
22 | */
↵ | | 10 | */↵
|
23 | private static JFreeChart createChart(XYDataset dataset) {↵ | | 11 | private static JFreeChart createChart(XYDataset dataset) {↵
|
24 |
↵ | | |
|
|
25 | JFreeChart chart = ChartFactory.createTimeSeriesChart(
↵ | | 12 | JFreeChart chart = ChartFactory.createTimeSeriesChart(↵
|
26 | "Legal & General Unit Trust Prices", // title
↵ | | 13 | "Legal & General Unit Trust Prices", // title↵
|
27 | "Date", // x-axis label
↵ | | 14 | "Date", // x-axis label↵
|
28 | "Price Per Unit", // y-axis label
↵ | | 15 | "Price Per Unit", // y-axis label↵
|
29 | dataset, // data
↵ | | 16 | dataset, // data↵
|
30 | true, // create legend?
↵ | | 17 | true, // create legend?↵
|
31 | true, // generate tooltips?
↵ | | 18 | true, // generate tooltips?↵
|
32 | false // generate URLs?
↵ | | 19 | false // generate URLs?↵
|
33 | );↵ | | 20 | );↵
|
34 |
↵ | | |
|
|
35 | chart.setBackgroundPaint(Color.white);↵ | | 21 | chart.setBackgroundPaint(Color.white);↵
|
36 |
↵ | | |
|
|
37 | XYPlot plot = (XYPlot) chart.getPlot();
↵ | | 22 | XYPlot plot = (XYPlot) chart.getPlot();↵
|
38 | plot.setBackgroundPaint(Color.lightGray);
↵ | | 23 | plot.setBackgroundPaint(Color.lightGray);↵
|
39 | plot.setDomainGridlinePaint(Color.white);
↵ | | 24 | plot.setDomainGridlinePaint(Color.white);↵
|
40 | plot.setRangeGridlinePaint(Color.white);
↵ | | 25 | plot.setRangeGridlinePaint(Color.white);↵
|
41 | plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
↵ | | 26 | plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));↵
|
42 | plot.setDomainCrosshairVisible(true);
↵ | | 27 | plot.setDomainCrosshairVisible(true);↵
|
43 | plot.setRangeCrosshairVisible(true);
↵ | | 28 | plot.setRangeCrosshairVisible(true);↵
|
44 |
↵ | | 29 | ↵
|
45 | XYItemRenderer r = plot.getRenderer();
↵ | | 30 | XYItemRenderer r = plot.getRenderer();↵
|
46 | if (r instanceof XYLineAndShapeRenderer) {
↵ | | 31 | if (r instanceof XYLineAndShapeRenderer) {↵
|
47 | XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
↵ | | 32 | XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;↵
|
48 | renderer.setBaseShapesVisible(true);
↵ | | 33 | renderer.setBaseShapesVisible(true);↵
|
49 | renderer.setBaseShapesFilled(true);
↵ | | 34 | renderer.setBaseShapesFilled(true);↵
|
50 | }
↵ | | 35 | }↵
|
51 |
↵ | | 36 | ↵
|
52 | DateAxis axis = (DateAxis) plot.getDomainAxis();
↵ | | 37 | DateAxis axis = (DateAxis) plot.getDomainAxis();↵
|
53 | axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
↵ | | 38 | axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));↵
|
54 |
↵ | | 39 | ↵
|
55 | return chart;↵ | | 40 | return chart;↵
|
56 |
↵ | | |
|
|
57 | }
↵ | | 41 | }↵
|
58 |
↵ | | 42 | ↵
|
59 | /**
↵ | | 43 | /**↵
|
60 | * Creates a dataset, consisting of two series of monthly data.
↵ | | 44 | * Creates a dataset, consisting of two series of monthly data.↵
|
61 | *
↵ | | 45 | *↵
|
62 | * @return The dataset.
↵ | | 46 | * @return The dataset.↵
|
63 | */
↵ | | 47 | */↵
|
64 | private static XYDataset createDataset() {↵ | | 48 | private static XYDataset createDataset() {↵
|
65 |
↵ | | |
|
|
66 | TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class);
↵ | | 49 | TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class);↵
|
67 | s1.add(new Month(2, 2001), 181.8);
↵ | | 50 | s1.add(new Month(2, 2001), 181.8);↵
|
68 | s1.add(new Month(3, 2001), 167.3);
↵ | | 51 | s1.add(new Month(3, 2001), 167.3);↵
|
69 | s1.add(new Month(4, 2001), 153.8);
↵ | | 52 | s1.add(new Month(4, 2001), 153.8);↵
|
70 | s1.add(new Month(5, 2001), 167.6);
↵ | | 53 | s1.add(new Month(5, 2001), 167.6);↵
|
71 | s1.add(new Month(6, 2001), 158.8);
↵ | | 54 | s1.add(new Month(6, 2001), 158.8);↵
|
72 | s1.add(new Month(7, 2001), 148.3);
↵ | | 55 | s1.add(new Month(7, 2001), 148.3);↵
|
73 | s1.add(new Month(8, 2001), 153.9);
↵ | | 56 | s1.add(new Month(8, 2001), 153.9);↵
|
74 | s1.add(new Month(9, 2001), 142.7);
↵ | | 57 | s1.add(new Month(9, 2001), 142.7);↵
|
75 | s1.add(new Month(10, 2001), 123.2);
↵ | | 58 | s1.add(new Month(10, 2001), 123.2);↵
|
76 | s1.add(new Month(11, 2001), 131.8);
↵ | | 59 | s1.add(new Month(11, 2001), 131.8);↵
|
77 | s1.add(new Month(12, 2001), 139.6);
↵ | | 60 | s1.add(new Month(12, 2001), 139.6);↵
|
78 | s1.add(new Month(1, 2002), 142.9);
↵ | | 61 | s1.add(new Month(1, 2002), 142.9);↵
|
79 | s1.add(new Month(2, 2002), 138.7);
↵ | | 62 | s1.add(new Month(2, 2002), 138.7);↵
|
80 | s1.add(new Month(3, 2002), 137.3);
↵ | | 63 | s1.add(new Month(3, 2002), 137.3);↵
|
81 | s1.add(new Month(4, 2002), 143.9);
↵ | | 64 | s1.add(new Month(4, 2002), 143.9);↵
|
82 | s1.add(new Month(5, 2002), 139.8);
↵ | | 65 | s1.add(new Month(5, 2002), 139.8);↵
|
83 | s1.add(new Month(6, 2002), 137.0);
↵ | | 66 | s1.add(new Month(6, 2002), 137.0);↵
|
84 | s1.add(new Month(7, 2002), 132.8);↵ | | 67 | s1.add(new Month(7, 2002), 132.8);↵
|
85 |
↵ | | |
|
|
86 | TimeSeries s2 = new TimeSeries("L&G UK Index Trust", Month.class);
↵ | | 68 | TimeSeries s2 = new TimeSeries("L&G UK Index Trust", Month.class);↵
|
87 | s2.add(new Month(2, 2001), 129.6);
↵ | | 69 | s2.add(new Month(2, 2001), 129.6);↵
|
88 | s2.add(new Month(3, 2001), 123.2);
↵ | | 70 | s2.add(new Month(3, 2001), 123.2);↵
|
89 | s2.add(new Month(4, 2001), 117.2);
↵ | | 71 | s2.add(new Month(4, 2001), 117.2);↵
|
90 | s2.add(new Month(5, 2001), 124.1);
↵ | | 72 | s2.add(new Month(5, 2001), 124.1);↵
|
91 | s2.add(new Month(6, 2001), 122.6);
↵ | | 73 | s2.add(new Month(6, 2001), 122.6);↵
|
92 | s2.add(new Month(7, 2001), 119.2);
↵ | | 74 | s2.add(new Month(7, 2001), 119.2);↵
|
93 | s2.add(new Month(8, 2001), 116.5);
↵ | | 75 | s2.add(new Month(8, 2001), 116.5);↵
|
94 | s2.add(new Month(9, 2001), 112.7);
↵ | | 76 | s2.add(new Month(9, 2001), 112.7);↵
|
95 | s2.add(new Month(10, 2001), 101.5);
↵ | | 77 | s2.add(new Month(10, 2001), 101.5);↵
|
96 | s2.add(new Month(11, 2001), 106.1);
↵ | | 78 | s2.add(new Month(11, 2001), 106.1);↵
|
97 | s2.add(new Month(12, 2001), 110.3);
↵ | | 79 | s2.add(new Month(12, 2001), 110.3);↵
|
98 | s2.add(new Month(1, 2002), 111.7);
↵ | | 80 | s2.add(new Month(1, 2002), 111.7);↵
|
99 | s2.add(new Month(2, 2002), 111.0);
↵ | | 81 | s2.add(new Month(2, 2002), 111.0);↵
|
100 | s2.add(new Month(3, 2002), 109.6);
↵ | | 82 | s2.add(new Month(3, 2002), 109.6);↵
|
101 | s2.add(new Month(4, 2002), 113.2);
↵ | | 83 | s2.add(new Month(4, 2002), 113.2);↵
|
102 | s2.add(new Month(5, 2002), 111.6);
↵ | | 84 | s2.add(new Month(5, 2002), 111.6);↵
|
103 | s2.add(new Month(6, 2002), 108.8);
↵ | | 85 | s2.add(new Month(6, 2002), 108.8);↵
|
104 | s2.add(new Month(7, 2002), 101.6);↵ | | 86 | s2.add(new Month(7, 2002), 101.6);↵
|
105 |
↵ | | |
|
106 | // ******************************************************************↵ | | 87 | ↵
|
107 | // More than 150 demo applications are included with the JFreeChart↵ | | |
|
108 | // Developer Guide...for more information, see:↵ | | |
|
109 | //↵ | | |
|
110 | // > http://www.object-refinery.com/jfreechart/guide.html↵ | | |
|
111 | //↵ | | |
|
112 | // ******************************************************************↵ | | |
|
113 |
↵ | | |
|
114 | TimeSeriesCollection dataset = new TimeSeriesCollection();
↵ | | 88 | TimeSeriesCollection dataset = new TimeSeriesCollection();↵
|
115 | dataset.addSeries(s1);
↵ | | 89 | dataset.addSeries(s1);↵
|
116 | dataset.addSeries(s2);
↵ | | 90 | dataset.addSeries(s2);↵
|
117 |
↵ | | 91 | ↵
|
118 | return dataset;↵ | | 92 | return dataset;↵
|
119 |
↵ | | |
|
120 | }↵ | | 93 | }↵
|
121 |
↵ | | |
|
| | | 94 | ↵
|
122 | /**
↵ | | 95 | /**↵
|
123 | * Creates a panel for the demo (used by SuperDemo.java).
↵ | | 96 | * Starting point for the demonstration application.↵
|
124 | *
↵ | | 97 | *↵
|
125 | * @return A panel.
↵ | | 98 | * @param args ignored.↵
|
126 | */
↵ | | 99 | */↵
|
127 | public static JPanel createDemoPanel() {
↵ | | 100 | public static void main(String[] args) {↵
|
128 | JFreeChart chart = createChart(createDataset());
↵ | | 101 | final JFreeChart chart = createChart(createDataset());↵
|
129 | return new ChartPanel(chart);
↵ | | 102 | final Display display = new Display();↵
|
130 | }↵ | | 103 | ↵
|
131 | ↵ | | 104 | ↵
|
132 | /**↵ | | |
|
133 | * Starting point for the demonstration application.↵ | | |
|
134 | *↵ | | |
|
135 | * @param args ignored.↵ | | |
|
136 | */↵ | | |
|
137 | public static void main(String[] args) {↵ | | |
|
|
138 | TimeSeriesChartDemo1 demo = new TimeSeriesChartDemo1(↵ | | |
|
139 | "Time Series Chart Demo 1"↵ | | 105 | Shell shell = new Shell(display);↵
|
| | | 106 | shell.setSize(600, 300);↵
|
| | | 107 | shell.setLayout(new FillLayout());↵
|
| | | 108 | shell.setText("Time series demo for jfreechart running with SWT");↵
|
| | | 109 | ChartComposite frame = new ChartComposite(shell, SWT.NONE, chart, true);↵
|
| | | 110 | frame.setDisplayToolTips(true);↵
|
| | | 111 | frame.setHorizontalAxisTrace(false);↵
|
140 | );
↵ | | 112 | frame.setVerticalAxisTrace(false);↵
|
141 | demo.pack();
↵ | | 113 | shell.open();↵
|
142 | RefineryUtilities.centerFrameOnScreen(demo);↵ | | 114 | ↵
|
143 | demo.setVisible(true↵ | | 115 | while (!shell.isDisposed()) {↵
|
| | | 116 | if (!display.readAndDispatch())↵
|
144 | ) | | 117 | display.sleep()
|