1 /* 2 * Copyright (c) 2012, 2015, 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 test.javafx.scene.chart; 27 28 import java.util.Arrays; 29 import javafx.collections.FXCollections; 30 import org.junit.Test; 31 import static org.junit.Assert.assertEquals; 32 import javafx.collections.*; 33 34 import javafx.scene.Node; 35 import javafx.scene.chart.BarChart; 36 import javafx.scene.chart.CategoryAxis; 37 import javafx.scene.chart.Chart; 38 import javafx.scene.chart.NumberAxis; 39 import javafx.scene.chart.XYChart; 40 import javafx.scene.chart.XYChart.Series; 41 import javafx.scene.chart.XYChartShim; 42 import javafx.scene.layout.StackPane; 43 44 public class BarChartTest extends XYChartTestBase { 45 46 static String[] years = {"2010", "2011", "2012"}; 47 static double[] anvilsSold = { 567, 1292, 2423 }; 48 static double[] skatesSold = { 956, 1665, 2559 }; 49 static double[] pillsSold = { 1154, 1927, 2774 }; 50 final CategoryAxis xAxis = new CategoryAxis(); 51 final NumberAxis yAxis = new NumberAxis(); 52 final BarChart<String,Number> bc = new BarChart<String,Number>(xAxis,yAxis); 53 54 @Override 55 protected Chart createChart() { 56 xAxis.setLabel("X Axis"); 57 xAxis.setCategories(FXCollections.<String>observableArrayList(Arrays.asList(years))); 58 yAxis.setLabel("Y Axis"); 59 // add starting data 60 XYChart.Series<String,Number> series1 = new XYChart.Series<String,Number>(); 61 series1.setName("Data Series 1"); 62 XYChart.Series<String,Number> series2 = new XYChart.Series<String,Number>(); 63 series2.setName("Data Series 2"); 64 series1.getData().add(new XYChart.Data<String,Number>(years[0], 567)); 65 series1.getData().add(new XYChart.Data<String,Number>(years[1], 1292)); 66 series1.getData().add(new XYChart.Data<String,Number>(years[2], 2180)); 67 68 series2.getData().add(new XYChart.Data<String,Number>(years[0], 956)); 69 series2.getData().add(new XYChart.Data<String,Number>(years[1], 1665)); 70 series2.getData().add(new XYChart.Data<String,Number>(years[2], 2450)); 71 bc.getData().add(series1); 72 bc.getData().add(series2); 73 return bc; 74 } 75 76 @Test 77 public void testAddingCustomStyleClassToBarChartBarNodes() { 78 startApp(); 79 XYChart.Series<String, Number> series = new XYChart.Series(); 80 ObservableList<XYChart.Data<String, Number>> seriesData = series.getData(); 81 String xValue = "A"; 82 Number yValue = Integer.valueOf(20); 83 XYChart.Data<String, Number> item = new XYChart.Data(xValue, yValue); 84 Node bar = item.getNode(); 85 if (bar == null) { 86 bar = new StackPane(); 87 } 88 String myStyleClass = "my-style"; 89 bar.getStyleClass().add(myStyleClass); 90 item.setNode(bar); 91 seriesData.add(item); 92 bc.getData().add(series); 93 assertEquals("my-style", bar.getStyleClass().get(0)); 94 } 95 96 @Test 97 public void testCategoryAxisCategoriesOnAddDataAtIndex() { 98 startApp(); 99 bc.getData().clear(); 100 xAxis.getCategories().clear(); 101 xAxis.setAutoRanging(true); 102 XYChart.Series<String,Number> series = new XYChart.Series<String,Number>(); 103 series.getData().clear(); 104 series.getData().add(new XYChart.Data<String, Number>("1", 1)); 105 series.getData().add(new XYChart.Data<String, Number>("2", 2)); 106 series.getData().add(new XYChart.Data<String, Number>("3", 3)); 107 bc.getData().add(series); 108 pulse(); 109 // category at index 0 = "1" 110 assertEquals("1", xAxis.getCategories().get(0)); 111 series.getData().add(0, new XYChart.Data<String, Number>("0", 5)); 112 pulse(); 113 // item inserted at 0; category at index 0 = 0 114 assertEquals("0", xAxis.getCategories().get(0)); 115 } 116 117 @Test 118 public void testRemoveAndAddSameSeriesBeforeAnimationCompletes() { 119 startApp(); 120 assertEquals(2, bc.getData().size()); 121 // remove and add the same series. 122 bc.getData().add(bc.getData().remove(0)); 123 pulse(); 124 assertEquals(2, bc.getData().size()); 125 } 126 127 @Test 128 public void testRemoveAndAddSameDataBeforeAnimationCompletes() { 129 startApp(); 130 Series s = bc.getData().get(0); 131 assertEquals(3, XYChartShim.Series_getDataSize(s)); 132 s.getData().add(s.getData().remove(0)); 133 assertEquals(3, XYChartShim.Series_getDataSize(s)); 134 } 135 136 @Test 137 public void testRemoveNotAnimated() { 138 startApp(); 139 bc.setAnimated(false); 140 Series s = bc.getData().get(0); 141 assertEquals(3, XYChartShim.Series_getDataSize(s)); 142 s.getData().remove(0); 143 assertEquals(2, XYChartShim.Series_getDataSize(s)); 144 } 145 }