001/* =========================== 002 * TimeSeriesChartFXDemo1.java 003 * =========================== 004 * 005 * Copyright (c) 2014, Object Refinery Limited. 006 * All rights reserved. 007 * 008 * http://www.jfree.org/jfreechart/index.html 009 * 010 * Redistribution and use in source and binary forms, with or without 011 * modification, are permitted provided that the following conditions are met: 012 * - Redistributions of source code must retain the above copyright 013 * notice, this list of conditions and the following disclaimer. 014 * - Redistributions in binary form must reproduce the above copyright 015 * notice, this list of conditions and the following disclaimer in the 016 * documentation and/or other materials provided with the distribution. 017 * - Neither the name of the Object Refinery Limited nor the 018 * names of its contributors may be used to endorse or promote products 019 * derived from this software without specific prior written permission. 020 * 021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 022 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 024 * ARE DISCLAIMED. IN NO EVENT SHALL OBJECT REFINERY LIMITED BE LIABLE FOR ANY 025 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 026 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 027 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 028 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 029 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 031 * 032 */ 033 034package org.jfree.chart.fx.demo; 035 036import static javafx.application.Application.launch; 037 038import java.awt.BasicStroke; 039import java.awt.Color; 040import java.awt.Font; 041import javafx.application.Application; 042import javafx.scene.Scene; 043import javafx.stage.Stage; 044import org.jfree.chart.ChartFactory; 045import org.jfree.chart.JFreeChart; 046import org.jfree.chart.block.BlockBorder; 047import org.jfree.chart.fx.ChartViewer; 048import org.jfree.chart.plot.XYPlot; 049import org.jfree.chart.renderer.xy.XYItemRenderer; 050import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; 051import org.jfree.chart.title.TextTitle; 052import org.jfree.data.time.Month; 053import org.jfree.data.time.TimeSeries; 054import org.jfree.data.time.TimeSeriesCollection; 055import org.jfree.data.xy.XYDataset; 056import org.jfree.ui.HorizontalAlignment; 057 058/** 059 * A demo showing the display of JFreeChart within a JavaFX application. 060 * 061 * The ChartCanvas code is based on: 062 * http://dlemmermann.wordpress.com/2014/04/10/javafx-tip-1-resizable-canvas/ 063 * 064 */ 065public class TimeSeriesChartFXDemo1 extends Application { 066 067 /** 068 * Creates a chart. 069 * 070 * @param dataset a dataset. 071 * 072 * @return A chart. 073 */ 074 private static JFreeChart createChart(XYDataset dataset) { 075 076 JFreeChart chart = ChartFactory.createTimeSeriesChart( 077 "International Coffee Organisation : Coffee Prices", // title 078 null, // x-axis label 079 "US cents/lb", // y-axis label 080 dataset); 081 082 String fontName = "Palatino"; 083 chart.getTitle().setFont(new Font(fontName, Font.BOLD, 18)); 084 chart.addSubtitle(new TextTitle("Source: http://www.ico.org/historical/2010-19/PDF/HIST-PRICES.pdf", 085 new Font(fontName, Font.PLAIN, 14))); 086 087 XYPlot plot = (XYPlot) chart.getPlot(); 088 plot.setDomainPannable(true); 089 plot.setRangePannable(true); 090 plot.setDomainCrosshairVisible(true); 091 plot.setRangeCrosshairVisible(true); 092 plot.getDomainAxis().setLowerMargin(0.0); 093 plot.getDomainAxis().setLabelFont(new Font(fontName, Font.BOLD, 14)); 094 plot.getDomainAxis().setTickLabelFont(new Font(fontName, Font.PLAIN, 12)); 095 plot.getRangeAxis().setLabelFont(new Font(fontName, Font.BOLD, 14)); 096 plot.getRangeAxis().setTickLabelFont(new Font(fontName, Font.PLAIN, 12)); 097 chart.getLegend().setItemFont(new Font(fontName, Font.PLAIN, 14)); 098 chart.getLegend().setFrame(BlockBorder.NONE); 099 chart.getLegend().setHorizontalAlignment(HorizontalAlignment.CENTER); 100 XYItemRenderer r = plot.getRenderer(); 101 if (r instanceof XYLineAndShapeRenderer) { 102 XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r; 103 renderer.setBaseShapesVisible(false); 104 renderer.setDrawSeriesLineAsPath(true); 105 // set the default stroke for all series 106 renderer.setAutoPopulateSeriesStroke(false); 107 renderer.setBaseStroke(new BasicStroke(3.0f, BasicStroke.CAP_ROUND, 108 BasicStroke.JOIN_BEVEL), false); 109 renderer.setSeriesPaint(0, Color.RED); 110 renderer.setSeriesPaint(1, new Color(24, 123, 58)); 111 renderer.setSeriesPaint(2, new Color(149, 201, 136)); 112 renderer.setSeriesPaint(3, new Color(1, 62, 29)); 113 renderer.setSeriesPaint(4, new Color(81, 176, 86)); 114 renderer.setSeriesPaint(5, new Color(0, 55, 122)); 115 renderer.setSeriesPaint(6, new Color(0, 92, 165)); 116 } 117 118 return chart; 119 120 } 121 122 /** 123 * Creates a dataset, consisting of two series of monthly data. 124 * 125 * @return the dataset. 126 */ 127 private static XYDataset createDataset() { 128 129 TimeSeries s1 = new TimeSeries("Indicator Price"); 130 s1.add(new Month(1, 2010), 126.80); 131 s1.add(new Month(2, 2010), 123.37); 132 s1.add(new Month(3, 2010), 125.30); 133 s1.add(new Month(4, 2010), 126.89); 134 s1.add(new Month(5, 2010), 128.10); 135 s1.add(new Month(6, 2010), 142.20); 136 s1.add(new Month(7, 2010), 153.41); 137 s1.add(new Month(8, 2010), 157.46); 138 s1.add(new Month(9, 2010), 163.61); 139 s1.add(new Month(10, 2010), 161.56); 140 s1.add(new Month(11, 2010), 173.90); 141 s1.add(new Month(12, 2010), 184.26); 142 s1.add(new Month(1, 2011), 197.35); 143 s1.add(new Month(2, 2011), 216.03); 144 s1.add(new Month(3, 2011), 224.33); 145 s1.add(new Month(4, 2011), 231.24); 146 s1.add(new Month(5, 2011), 227.97); 147 s1.add(new Month(6, 2011), 215.58); 148 s1.add(new Month(7, 2011), 210.36); 149 s1.add(new Month(8, 2011), 212.19); 150 s1.add(new Month(9, 2011), 213.04); 151 s1.add(new Month(10, 2011), 193.90); 152 s1.add(new Month(11, 2011), 193.66); 153 s1.add(new Month(12, 2011), 189.02); 154 s1.add(new Month(1, 2012), 188.90); 155 s1.add(new Month(2, 2012), 182.29); 156 s1.add(new Month(3, 2012), 167.77); 157 s1.add(new Month(4, 2012), 160.46); 158 s1.add(new Month(5, 2012), 157.68); 159 s1.add(new Month(6, 2012), 145.31); 160 s1.add(new Month(7, 2012), 159.07); 161 s1.add(new Month(8, 2012), 148.50); 162 s1.add(new Month(9, 2012), 151.28); 163 s1.add(new Month(10, 2012), 147.12); 164 s1.add(new Month(11, 2012), 136.35); 165 s1.add(new Month(12, 2012), 131.31); 166 s1.add(new Month(1, 2013), 135.38); 167 s1.add(new Month(2, 2013), 131.51); 168 s1.add(new Month(3, 2013), 131.38); 169 170 TimeSeries s2 = new TimeSeries("Columbian Milds"); 171 s2.add(new Month(1, 2010), 207.51); 172 s2.add(new Month(2, 2010), 204.71); 173 s2.add(new Month(3, 2010), 205.71); 174 s2.add(new Month(4, 2010), 200.00); 175 s2.add(new Month(5, 2010), 200.54); 176 s2.add(new Month(6, 2010), 224.49); 177 s2.add(new Month(7, 2010), 235.52); 178 s2.add(new Month(8, 2010), 243.98); 179 s2.add(new Month(9, 2010), 247.77); 180 s2.add(new Month(10, 2010), 230.02); 181 s2.add(new Month(11, 2010), 244.02); 182 s2.add(new Month(12, 2010), 261.97); 183 s2.add(new Month(1, 2011), 279.88); 184 s2.add(new Month(2, 2011), 296.44); 185 s2.add(new Month(3, 2011), 300.68); 186 s2.add(new Month(4, 2011), 312.95); 187 s2.add(new Month(5, 2011), 302.17); 188 s2.add(new Month(6, 2011), 287.95); 189 s2.add(new Month(7, 2011), 285.21); 190 s2.add(new Month(8, 2011), 286.97); 191 s2.add(new Month(9, 2011), 287.54); 192 s2.add(new Month(10, 2011), 257.66); 193 s2.add(new Month(11, 2011), 256.99); 194 s2.add(new Month(12, 2011), 251.60); 195 s2.add(new Month(1, 2012), 255.91); 196 s2.add(new Month(2, 2012), 244.14); 197 s2.add(new Month(3, 2012), 222.84); 198 s2.add(new Month(4, 2012), 214.46); 199 s2.add(new Month(5, 2012), 207.32); 200 s2.add(new Month(6, 2012), 184.67); 201 s2.add(new Month(7, 2012), 202.56); 202 s2.add(new Month(8, 2012), 187.14); 203 s2.add(new Month(9, 2012), 190.10); 204 s2.add(new Month(10, 2012), 181.39); 205 s2.add(new Month(11, 2012), 170.08); 206 s2.add(new Month(12, 2012), 164.40); 207 s2.add(new Month(1, 2013), 169.19); 208 s2.add(new Month(2, 2013), 161.70); 209 s2.add(new Month(3, 2013), 161.53); 210 211 TimeSeries s3 = new TimeSeries("Other Milds"); 212 s3.add(new Month(1, 2010), 158.90); 213 s3.add(new Month(2, 2010), 157.86); 214 s3.add(new Month(3, 2010), 164.50); 215 s3.add(new Month(4, 2010), 169.55); 216 s3.add(new Month(5, 2010), 173.38); 217 s3.add(new Month(6, 2010), 190.90); 218 s3.add(new Month(7, 2010), 203.21); 219 s3.add(new Month(8, 2010), 211.59); 220 s3.add(new Month(9, 2010), 222.71); 221 s3.add(new Month(10, 2010), 217.64); 222 s3.add(new Month(11, 2010), 233.48); 223 s3.add(new Month(12, 2010), 248.17); 224 s3.add(new Month(1, 2011), 263.77); 225 s3.add(new Month(2, 2011), 287.89); 226 s3.add(new Month(3, 2011), 292.07); 227 s3.add(new Month(4, 2011), 300.12); 228 s3.add(new Month(5, 2011), 291.09); 229 s3.add(new Month(6, 2011), 274.98); 230 s3.add(new Month(7, 2011), 268.02); 231 s3.add(new Month(8, 2011), 270.44); 232 s3.add(new Month(9, 2011), 274.88); 233 s3.add(new Month(10, 2011), 247.82); 234 s3.add(new Month(11, 2011), 245.09); 235 s3.add(new Month(12, 2011), 236.71); 236 s3.add(new Month(1, 2012), 237.21); 237 s3.add(new Month(2, 2012), 224.16); 238 s3.add(new Month(3, 2012), 201.26); 239 s3.add(new Month(4, 2012), 191.45); 240 s3.add(new Month(5, 2012), 184.65); 241 s3.add(new Month(6, 2012), 168.69); 242 s3.add(new Month(7, 2012), 190.45); 243 s3.add(new Month(8, 2012), 174.82); 244 s3.add(new Month(9, 2012), 178.98); 245 s3.add(new Month(10, 2012), 173.32); 246 s3.add(new Month(11, 2012), 159.91); 247 s3.add(new Month(12, 2012), 152.74); 248 s3.add(new Month(1, 2013), 157.29); 249 s3.add(new Month(2, 2013), 149.46); 250 s3.add(new Month(3, 2013), 149.78); 251 252 TimeSeries s4 = new TimeSeries("Brazilian Naturals"); 253 s4.add(new Month(1, 2010), 131.67); 254 s4.add(new Month(2, 2010), 124.57); 255 s4.add(new Month(3, 2010), 126.21); 256 s4.add(new Month(4, 2010), 126.07); 257 s4.add(new Month(5, 2010), 127.45); 258 s4.add(new Month(6, 2010), 143.20); 259 s4.add(new Month(7, 2010), 156.87); 260 s4.add(new Month(8, 2010), 163.21); 261 s4.add(new Month(9, 2010), 175.15); 262 s4.add(new Month(10, 2010), 175.38); 263 s4.add(new Month(11, 2010), 190.62); 264 s4.add(new Month(12, 2010), 204.25); 265 s4.add(new Month(1, 2011), 219.77); 266 s4.add(new Month(2, 2011), 247.00); 267 s4.add(new Month(3, 2011), 260.98); 268 s4.add(new Month(4, 2011), 273.40); 269 s4.add(new Month(5, 2011), 268.66); 270 s4.add(new Month(6, 2011), 250.59); 271 s4.add(new Month(7, 2011), 245.69); 272 s4.add(new Month(8, 2011), 249.83); 273 s4.add(new Month(9, 2011), 255.64); 274 s4.add(new Month(10, 2011), 234.28); 275 s4.add(new Month(11, 2011), 236.75); 276 s4.add(new Month(12, 2011), 228.79); 277 s4.add(new Month(1, 2012), 228.21); 278 s4.add(new Month(2, 2012), 215.40); 279 s4.add(new Month(3, 2012), 192.03); 280 s4.add(new Month(4, 2012), 180.90); 281 s4.add(new Month(5, 2012), 174.17); 282 s4.add(new Month(6, 2012), 156.17); 283 s4.add(new Month(7, 2012), 175.98); 284 s4.add(new Month(8, 2012), 160.05); 285 s4.add(new Month(9, 2012), 166.53); 286 s4.add(new Month(10, 2012), 161.20); 287 s4.add(new Month(11, 2012), 148.25); 288 s4.add(new Month(12, 2012), 140.69); 289 s4.add(new Month(1, 2013), 145.17); 290 s4.add(new Month(2, 2013), 136.63); 291 s4.add(new Month(3, 2013), 133.61); 292 293 TimeSeries s5 = new TimeSeries("Robustas"); 294 s5.add(new Month(1, 2010), 69.92); 295 s5.add(new Month(2, 2010), 67.88); 296 s5.add(new Month(3, 2010), 67.25); 297 s5.add(new Month(4, 2010), 71.59); 298 s5.add(new Month(5, 2010), 70.70); 299 s5.add(new Month(6, 2010), 76.92); 300 s5.add(new Month(7, 2010), 85.27); 301 s5.add(new Month(8, 2010), 82.68); 302 s5.add(new Month(9, 2010), 81.28); 303 s5.add(new Month(10, 2010), 85.27); 304 s5.add(new Month(11, 2010), 92.04); 305 s5.add(new Month(12, 2010), 94.09); 306 s5.add(new Month(1, 2011), 101.09); 307 s5.add(new Month(2, 2011), 109.35); 308 s5.add(new Month(3, 2011), 118.13); 309 s5.add(new Month(4, 2011), 117.37); 310 s5.add(new Month(5, 2011), 121.98); 311 s5.add(new Month(6, 2011), 117.95); 312 s5.add(new Month(7, 2011), 112.73); 313 s5.add(new Month(8, 2011), 112.07); 314 s5.add(new Month(9, 2011), 106.06); 315 s5.add(new Month(10, 2011), 98.10); 316 s5.add(new Month(11, 2011), 97.24); 317 s5.add(new Month(12, 2011), 98.41); 318 s5.add(new Month(1, 2012), 96.72); 319 s5.add(new Month(2, 2012), 101.93); 320 s5.add(new Month(3, 2012), 103.57); 321 s5.add(new Month(4, 2012), 101.80); 322 s5.add(new Month(5, 2012), 106.88); 323 s5.add(new Month(6, 2012), 105.70); 324 s5.add(new Month(7, 2012), 107.06); 325 s5.add(new Month(8, 2012), 106.52); 326 s5.add(new Month(9, 2012), 104.95); 327 s5.add(new Month(10, 2012), 104.47); 328 s5.add(new Month(11, 2012), 97.67); 329 s5.add(new Month(12, 2012), 96.59); 330 s5.add(new Month(1, 2013), 99.69); 331 s5.add(new Month(2, 2013), 104.03); 332 s5.add(new Month(3, 2013), 106.26); 333 334 TimeSeries s6 = new TimeSeries("Futures (London)"); 335 s6.add(new Month(1, 2010), 62.66); 336 s6.add(new Month(2, 2010), 60.37); 337 s6.add(new Month(3, 2010), 58.64); 338 s6.add(new Month(4, 2010), 62.21); 339 s6.add(new Month(5, 2010), 62.46); 340 s6.add(new Month(6, 2010), 69.72); 341 s6.add(new Month(7, 2010), 78.17); 342 s6.add(new Month(8, 2010), 78.42); 343 s6.add(new Month(9, 2010), 75.87); 344 s6.add(new Month(10, 2010), 80.08); 345 s6.add(new Month(11, 2010), 86.40); 346 s6.add(new Month(12, 2010), 88.70); 347 s6.add(new Month(1, 2011), 96.02); 348 s6.add(new Month(2, 2011), 104.53); 349 s6.add(new Month(3, 2011), 111.36); 350 s6.add(new Month(4, 2011), 111.34); 351 s6.add(new Month(5, 2011), 116.76); 352 s6.add(new Month(6, 2011), 110.51); 353 s6.add(new Month(7, 2011), 103.36); 354 s6.add(new Month(8, 2011), 102.71); 355 s6.add(new Month(9, 2011), 96.10); 356 s6.add(new Month(10, 2011), 88.64); 357 s6.add(new Month(11, 2011), 85.78); 358 s6.add(new Month(12, 2011), 87.65); 359 s6.add(new Month(1, 2012), 84.19); 360 s6.add(new Month(2, 2012), 88.69); 361 s6.add(new Month(3, 2012), 91.37); 362 s6.add(new Month(4, 2012), 91.81); 363 s6.add(new Month(5, 2012), 96.82); 364 s6.add(new Month(6, 2012), 94.75); 365 s6.add(new Month(7, 2012), 96.14); 366 s6.add(new Month(8, 2012), 96.12); 367 s6.add(new Month(9, 2012), 94.65); 368 s6.add(new Month(10, 2012), 94.66); 369 s6.add(new Month(11, 2012), 87.32); 370 s6.add(new Month(12, 2012), 85.94); 371 s6.add(new Month(1, 2013), 88.85); 372 s6.add(new Month(2, 2013), 94.41); 373 s6.add(new Month(3, 2013), 97.22); 374 375 TimeSeries s7 = new TimeSeries("Futures (New York)"); 376 s7.add(new Month(1, 2010), 142.76); 377 s7.add(new Month(2, 2010), 134.35); 378 s7.add(new Month(3, 2010), 134.97); 379 s7.add(new Month(4, 2010), 135.12); 380 s7.add(new Month(5, 2010), 135.81); 381 s7.add(new Month(6, 2010), 152.36); 382 s7.add(new Month(7, 2010), 165.23); 383 s7.add(new Month(8, 2010), 175.10); 384 s7.add(new Month(9, 2010), 187.80); 385 s7.add(new Month(10, 2010), 190.43); 386 s7.add(new Month(11, 2010), 206.92); 387 s7.add(new Month(12, 2010), 221.51); 388 s7.add(new Month(1, 2011), 238.05); 389 s7.add(new Month(2, 2011), 261.41); 390 s7.add(new Month(3, 2011), 274.10); 391 s7.add(new Month(4, 2011), 285.58); 392 s7.add(new Month(5, 2011), 277.72); 393 s7.add(new Month(6, 2011), 262.52); 394 s7.add(new Month(7, 2011), 255.90); 395 s7.add(new Month(8, 2011), 260.39); 396 s7.add(new Month(9, 2011), 261.39); 397 s7.add(new Month(10, 2011), 236.74); 398 s7.add(new Month(11, 2011), 235.25); 399 s7.add(new Month(12, 2011), 227.23); 400 s7.add(new Month(1, 2012), 227.50); 401 s7.add(new Month(2, 2012), 212.09); 402 s7.add(new Month(3, 2012), 188.78); 403 s7.add(new Month(4, 2012), 181.75); 404 s7.add(new Month(5, 2012), 176.50); 405 s7.add(new Month(6, 2012), 159.93); 406 s7.add(new Month(7, 2012), 183.20); 407 s7.add(new Month(8, 2012), 169.77); 408 s7.add(new Month(9, 2012), 175.36); 409 s7.add(new Month(10, 2012), 170.43); 410 s7.add(new Month(11, 2012), 155.72); 411 s7.add(new Month(12, 2012), 149.58); 412 s7.add(new Month(1, 2013), 154.28); 413 s7.add(new Month(2, 2013), 144.89); 414 s7.add(new Month(3, 2013), 141.43); 415 416 TimeSeriesCollection dataset = new TimeSeriesCollection(); 417 dataset.addSeries(s1); 418 dataset.addSeries(s2); 419 dataset.addSeries(s3); 420 dataset.addSeries(s4); 421 dataset.addSeries(s5); 422 dataset.addSeries(s6); 423 dataset.addSeries(s7); 424 return dataset; 425 } 426 427 @Override 428 public void start(Stage stage) throws Exception { 429 XYDataset dataset = createDataset(); 430 JFreeChart chart = createChart(dataset); 431 ChartViewer viewer = new ChartViewer(chart); 432 433 stage.setScene(new Scene(viewer)); 434 stage.setTitle("JFreeChart: TimeSeriesFXDemo1.java"); 435 stage.setWidth(700); 436 stage.setHeight(390); 437 stage.show(); 438 } 439 440 /** 441 * @param args the command line arguments 442 */ 443 public static void main(String[] args) { 444 launch(args); 445 } 446 447}