1 /* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.scene.chart; 27 28 29 import com.sun.javafx.scene.control.infrastructure.ControlTestUtils; 30 import java.io.IOException; 31 import java.io.OutputStream; 32 import java.io.PrintStream; 33 import org.junit.Test; 34 import static org.junit.Assert.assertEquals; 35 import javafx.collections.*; 36 37 38 import javafx.scene.Node; 39 import javafx.scene.Group; 40 import javafx.scene.shape.*; 41 import static org.junit.Assert.assertTrue; 42 43 import org.junit.Ignore; 44 45 46 public class StackedAreaChartTest extends XYChartTestBase { 47 StackedAreaChart<Number,Number> ac; 48 final XYChart.Series<Number, Number> series1 = new XYChart.Series<Number, Number>(); 49 boolean useCategoryAxis = false; 50 final String[] countries = {"USA", "Italy", "France", "China", "India"}; 51 protected Chart createChart() { 52 final NumberAxis yAxis = new NumberAxis(); 53 ObservableList<XYChart.Data> data = FXCollections.observableArrayList(); 54 Axis xAxis; 55 if (useCategoryAxis) { 56 xAxis = new CategoryAxis(); 57 ((CategoryAxis)xAxis).setCategories(FXCollections.observableArrayList(countries)); 58 // add starting data 59 series1.getData().add(new XYChart.Data(countries[0], 10d)); 60 series1.getData().add(new XYChart.Data(countries[1], 20d)); 61 series1.getData().add(new XYChart.Data(countries[2], 15d)); 62 series1.getData().add(new XYChart.Data(countries[3], 15d)); 63 series1.getData().add(new XYChart.Data(countries[4], 10d)); 64 } else { 65 xAxis = new NumberAxis(); 66 ac = new StackedAreaChart<Number,Number>(xAxis,yAxis); 67 // add starting data 68 series1.getData().add(new XYChart.Data(10d, 10d)); 69 series1.getData().add(new XYChart.Data(25d, 20d)); 70 series1.getData().add(new XYChart.Data(30d, 15d)); 71 series1.getData().add(new XYChart.Data(50d, 15d)); 72 series1.getData().add(new XYChart.Data(80d, 10d)); 73 } 74 75 xAxis.setLabel("X Axis"); 76 yAxis.setLabel("Y Axis"); 77 ac.setTitle("HelloStackedAreaChart"); 78 79 return ac; 80 } 81 82 private StringBuffer getSeriesLineFromPlot() { 83 ObservableList<Node> childrenList = ac.getPlotChildren(); 84 StringBuffer sb = new StringBuffer(); 85 for (Node n : childrenList) { 86 if (n instanceof Group) { 87 for (Node gn : ((Group)n).getChildren()) { 88 if (gn instanceof Path && "chart-series-area-line".equals(gn.getStyleClass().get(0))) { 89 Path line = (Path)gn; 90 sb = computeSVGPath(line); 91 return sb; 92 } 93 } 94 } 95 } 96 return sb; 97 } 98 99 @Test @Ignore("pending RT-28373") 100 public void testSeriesAdd() { 101 startApp(); 102 ac.getData().addAll(series1); 103 pulse(); 104 StringBuffer sb = getSeriesLineFromPlot(); 105 // assertEquals("L220.0 59.0 L264.0 175.0 L440.0 175.0 L704.0 291.0 ", sb.toString()); 106 assertEquals("L219.0 58.0 L263.0 173.0 L438.0 173.0 L700.0 289.0 ", sb.toString()); 107 108 } 109 110 @Test 111 public void testSeriesRemove() { 112 startApp(); 113 ac.getData().addAll(series1); 114 pulse(); 115 if (!ac.getData().isEmpty()) { 116 ac.getData().remove(0); 117 pulse(); 118 StringBuffer sb = getSeriesLineFromPlot(); 119 assertEquals(sb.toString(), ""); 120 } 121 } 122 123 @Test @Ignore 124 public void testDataItemRemove() { 125 startApp(); 126 ac.getData().addAll(series1); 127 pulse(); 128 if (!ac.getData().isEmpty()) { 129 series1.getData().remove(0); 130 pulse(); 131 StringBuffer sb = getSeriesLineFromPlot(); 132 assertEquals(sb.toString(), "L247.0 171.0 L412.0 171.0 L658.0 284.0 "); 133 } 134 } 135 136 @Test @Ignore 137 public void testDataItemAdd() { 138 startApp(); 139 ac.getData().addAll(series1); 140 pulse(); 141 if (!ac.getData().isEmpty()) { 142 series1.getData().add(new XYChart.Data(40d,10d)); 143 pulse(); 144 StringBuffer sb = getSeriesLineFromPlot(); 145 assertEquals(sb.toString(), "L206.0 57.0 L247.0 171.0 L329.0 284.0 L412.0 171.0 L658.0 284.0 "); 146 } 147 } 148 149 @Test @Ignore 150 public void testDataItemInsert() { 151 startApp(); 152 ac.getData().addAll(series1); 153 pulse(); 154 if (!ac.getData().isEmpty()) { 155 series1.getData().add(2, new XYChart.Data(40d,10d)); 156 pulse(); 157 StringBuffer sb = getSeriesLineFromPlot(); 158 assertEquals(sb.toString(), "L206.0 57.0 L247.0 171.0 L329.0 284.0 L412.0 171.0 L658.0 284.0 "); 159 } 160 } 161 162 @Test @Ignore 163 public void testDataItemChange() { 164 startApp(); 165 ac.getData().addAll(series1); 166 pulse(); 167 if (!ac.getData().isEmpty()) { 168 XYChart.Data<Number,Number> d = series1.getData().get(2); 169 d.setXValue(40d); 170 d.setYValue(30d); 171 pulse(); 172 StringBuffer sb = getSeriesLineFromPlot(); 173 assertEquals(sb.toString(), "L206.0 197.0 L329.0 40.0 L412.0 276.0 L658.0 354.0 "); 174 } 175 } 176 177 @Test 178 public void testStackedAreaChartWithCategoryAxis() { 179 useCategoryAxis = true; 180 startApp(); 181 useCategoryAxis = false; 182 } 183 184 @Test public void testCreateSymbols() { 185 startApp(); 186 ac.setCreateSymbols(false); 187 pulse(); 188 ac.getData().addAll(series1); 189 pulse(); 190 assertEquals(0, countSymbols(ac, "chart-area-symbol")); 191 192 ac.getData().clear(); 193 pulse(); 194 ac.setCreateSymbols(true); 195 pulse(); 196 ac.getData().addAll(series1); 197 assertEquals(5, countSymbols(ac, "chart-area-symbol")); 198 } 199 200 @Test 201 public void testAutoRange_AdditionalPointInSeries1() { 202 ac.getData().clear(); 203 final NumberAxis yAxis = (NumberAxis) ac.getYAxis(); 204 yAxis.setAutoRanging(true); 205 yAxis.setForceZeroInRange(false); 206 ac.getData().add(new StackedAreaChart.Series<Number, Number>(FXCollections.observableArrayList( 207 new StackedAreaChart.Data(0, 4), 208 new StackedAreaChart.Data(2, 5), 209 new StackedAreaChart.Data(4, 4), 210 new StackedAreaChart.Data(6, 2), 211 new StackedAreaChart.Data(7, 12), 212 new StackedAreaChart.Data(8, 6), 213 new StackedAreaChart.Data(10, 8) 214 ))); 215 ac.getData().add(new StackedAreaChart.Series<Number, Number>(FXCollections.observableArrayList( 216 new StackedAreaChart.Data(0,8), 217 new StackedAreaChart.Data(2,1), 218 new StackedAreaChart.Data(4,9), 219 new StackedAreaChart.Data(6,7), 220 new StackedAreaChart.Data(8,5), 221 new StackedAreaChart.Data(10,7) 222 ))); 223 224 ac.updateAxisRange(); 225 226 assertEquals(2, yAxis.dataMinValue, 1e-100); 227 assertEquals(18, yAxis.dataMaxValue, 1e-100); 228 } 229 230 @Test 231 public void testAutoRange_AdditionalPointInSeries1AtTheEnd() { 232 ac.getData().clear(); 233 final NumberAxis yAxis = (NumberAxis) ac.getYAxis(); 234 yAxis.setAutoRanging(true); 235 yAxis.setForceZeroInRange(false); 236 ac.getData().add(new StackedAreaChart.Series<Number, Number>(FXCollections.observableArrayList( 237 new StackedAreaChart.Data(0, 4), 238 new StackedAreaChart.Data(2, 5), 239 new StackedAreaChart.Data(4, 4), 240 new StackedAreaChart.Data(6, 2), 241 new StackedAreaChart.Data(8, 6), 242 new StackedAreaChart.Data(10, 8), 243 new StackedAreaChart.Data(11, 12) 244 ))); 245 ac.getData().add(new StackedAreaChart.Series<Number, Number>(FXCollections.observableArrayList( 246 new StackedAreaChart.Data(0,8), 247 new StackedAreaChart.Data(2,1), 248 new StackedAreaChart.Data(4,9), 249 new StackedAreaChart.Data(6,7), 250 new StackedAreaChart.Data(8,5), 251 new StackedAreaChart.Data(10,7) 252 ))); 253 254 ac.updateAxisRange(); 255 256 assertEquals(2, yAxis.dataMinValue, 1e-100); 257 assertEquals(19, yAxis.dataMaxValue, 1e-100); 258 } 259 260 @Test 261 public void testAutoRange_AdditionalPointInSeries1AtTheBeginning() { 262 ac.getData().clear(); 263 final NumberAxis yAxis = (NumberAxis) ac.getYAxis(); 264 yAxis.setAutoRanging(true); 265 yAxis.setForceZeroInRange(false); 266 ac.getData().add(new StackedAreaChart.Series<Number, Number>(FXCollections.observableArrayList( 267 new StackedAreaChart.Data(-1, 12), 268 new StackedAreaChart.Data(0, 4), 269 new StackedAreaChart.Data(2, 5), 270 new StackedAreaChart.Data(4, 4), 271 new StackedAreaChart.Data(6, 2), 272 new StackedAreaChart.Data(8, 6), 273 new StackedAreaChart.Data(10, 8) 274 ))); 275 ac.getData().add(new StackedAreaChart.Series<Number, Number>(FXCollections.observableArrayList( 276 new StackedAreaChart.Data(0,8), 277 new StackedAreaChart.Data(2,1), 278 new StackedAreaChart.Data(4,9), 279 new StackedAreaChart.Data(6,7), 280 new StackedAreaChart.Data(8,5), 281 new StackedAreaChart.Data(10,7) 282 ))); 283 284 ac.updateAxisRange(); 285 286 assertEquals(2, yAxis.dataMinValue, 1e-100); 287 assertEquals(20, yAxis.dataMaxValue, 1e-100); 288 } 289 290 @Test 291 public void testAutoRange_AdditionalPointInSeries2() { 292 ac.getData().clear(); 293 final NumberAxis yAxis = (NumberAxis) ac.getYAxis(); 294 yAxis.setAutoRanging(true); 295 yAxis.setForceZeroInRange(false); 296 ac.getData().add(new StackedAreaChart.Series<Number, Number>(FXCollections.observableArrayList( 297 new StackedAreaChart.Data(0, 4), 298 new StackedAreaChart.Data(2, 5), 299 new StackedAreaChart.Data(4, 4), 300 new StackedAreaChart.Data(6, 2), 301 new StackedAreaChart.Data(8, 6), 302 new StackedAreaChart.Data(10, 8) 303 ))); 304 ac.getData().add(new StackedAreaChart.Series<Number, Number>(FXCollections.observableArrayList( 305 new StackedAreaChart.Data(0,8), 306 new StackedAreaChart.Data(2,1), 307 new StackedAreaChart.Data(4,9), 308 new StackedAreaChart.Data(6,7), 309 new StackedAreaChart.Data(7, 12), 310 new StackedAreaChart.Data(8,5), 311 new StackedAreaChart.Data(10,7) 312 ))); 313 314 ac.updateAxisRange(); 315 316 assertEquals(2, yAxis.dataMinValue, 1e-100); 317 assertEquals(16, yAxis.dataMaxValue, 1e-100); 318 } 319 320 @Test 321 public void testAutoRange_AdditionalPointInSeries2AtTheEnd() { 322 ac.getData().clear(); 323 final NumberAxis yAxis = (NumberAxis) ac.getYAxis(); 324 yAxis.setAutoRanging(true); 325 yAxis.setForceZeroInRange(false); 326 ac.getData().add(new StackedAreaChart.Series<Number, Number>(FXCollections.observableArrayList( 327 new StackedAreaChart.Data(0, 4), 328 new StackedAreaChart.Data(2, 5), 329 new StackedAreaChart.Data(4, 4), 330 new StackedAreaChart.Data(6, 2), 331 new StackedAreaChart.Data(8, 6), 332 new StackedAreaChart.Data(10, 8) 333 ))); 334 ac.getData().add(new StackedAreaChart.Series<Number, Number>(FXCollections.observableArrayList( 335 new StackedAreaChart.Data(0,8), 336 new StackedAreaChart.Data(2,1), 337 new StackedAreaChart.Data(4,9), 338 new StackedAreaChart.Data(6,7), 339 new StackedAreaChart.Data(8,5), 340 new StackedAreaChart.Data(10,7), 341 new StackedAreaChart.Data(12, 12) 342 ))); 343 344 ac.updateAxisRange(); 345 346 assertEquals(2, yAxis.dataMinValue, 1e-100); 347 assertEquals(20, yAxis.dataMaxValue, 1e-100); 348 } 349 350 @Test 351 public void testAutoRange_AdditionalPointInSeries2AtTheBeginning() { 352 ac.getData().clear(); 353 final NumberAxis yAxis = (NumberAxis) ac.getYAxis(); 354 yAxis.setAutoRanging(true); 355 yAxis.setForceZeroInRange(false); 356 ac.getData().add(new StackedAreaChart.Series<Number, Number>(FXCollections.observableArrayList( 357 new StackedAreaChart.Data(0, 4), 358 new StackedAreaChart.Data(2, 5), 359 new StackedAreaChart.Data(4, 4), 360 new StackedAreaChart.Data(6, 2), 361 new StackedAreaChart.Data(8, 6), 362 new StackedAreaChart.Data(10, 8) 363 ))); 364 ac.getData().add(new StackedAreaChart.Series<Number, Number>(FXCollections.observableArrayList( 365 new StackedAreaChart.Data(-1, 15), 366 new StackedAreaChart.Data(0,8), 367 new StackedAreaChart.Data(2,1), 368 new StackedAreaChart.Data(4,9), 369 new StackedAreaChart.Data(6,7), 370 new StackedAreaChart.Data(8,5), 371 new StackedAreaChart.Data(10,7) 372 ))); 373 374 ac.updateAxisRange(); 375 376 assertEquals(2, yAxis.dataMinValue, 1e-100); 377 assertEquals(19, yAxis.dataMaxValue, 1e-100); 378 } 379 380 @Test 381 public void testAutoRange_EmptySeries1() { 382 ac.getData().clear(); 383 final NumberAxis yAxis = (NumberAxis) ac.getYAxis(); 384 yAxis.setAutoRanging(true); 385 yAxis.setForceZeroInRange(false); 386 ac.getData().add(new StackedAreaChart.Series<Number, Number>(FXCollections.observableArrayList( 387 ))); 388 ac.getData().add(new StackedAreaChart.Series<Number, Number>(FXCollections.observableArrayList( 389 new StackedAreaChart.Data(0,8), 390 new StackedAreaChart.Data(2,1), 391 new StackedAreaChart.Data(4,9), 392 new StackedAreaChart.Data(6,7), 393 new StackedAreaChart.Data(8,5), 394 new StackedAreaChart.Data(10,7) 395 ))); 396 397 ac.updateAxisRange(); 398 399 assertEquals(1, yAxis.dataMinValue, 1e-100); 400 assertEquals(9, yAxis.dataMaxValue, 1e-100); 401 } 402 403 @Test 404 public void testAutoRange_EmptySeries2() { 405 ac.getData().clear(); 406 final NumberAxis yAxis = (NumberAxis) ac.getYAxis(); 407 yAxis.setAutoRanging(true); 408 yAxis.setForceZeroInRange(false); 409 ac.getData().add(new StackedAreaChart.Series<Number, Number>(FXCollections.observableArrayList( 410 new StackedAreaChart.Data(0, 4), 411 new StackedAreaChart.Data(2, 5), 412 new StackedAreaChart.Data(4, 4), 413 new StackedAreaChart.Data(6, 2), 414 new StackedAreaChart.Data(8, 6), 415 new StackedAreaChart.Data(10, 8) 416 ))); 417 ac.getData().add(new StackedAreaChart.Series<>()); 418 ac.getData().add(new StackedAreaChart.Series<Number, Number>(FXCollections.observableArrayList( 419 new StackedAreaChart.Data(0,8), 420 new StackedAreaChart.Data(2,1), 421 new StackedAreaChart.Data(4,9), 422 new StackedAreaChart.Data(6,7), 423 new StackedAreaChart.Data(8,5), 424 new StackedAreaChart.Data(10,7) 425 ))); 426 427 ac.updateAxisRange(); 428 429 assertEquals(2, yAxis.dataMinValue, 1e-100); 430 assertEquals(15, yAxis.dataMaxValue, 1e-100); 431 } 432 433 @Test 434 public void testDataWithoutSymbolsAddWithAnimation_rt_39353() { 435 startApp(); 436 ac.getData().addAll(series1); 437 ac.setAnimated(true); 438 ac.setCreateSymbols(false); 439 series1.getData().add(new XYChart.Data(40d,10d)); 440 Thread.UncaughtExceptionHandler exceptionHandler = ControlTestUtils.setHandler(); 441 try { 442 toolkit.setAnimationTime(0); 443 // check remove just in case 444 series1.getData().remove(0); 445 toolkit.setAnimationTime(800); 446 } finally { 447 ControlTestUtils.resetHandler(exceptionHandler); 448 } 449 } 450 }