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 import org.junit.Ignore;
  44 
  45 public class BarChartTest extends XYChartTestBase {
  46 
  47     static String[] years = {"2010", "2011", "2012"};
  48     static double[] anvilsSold = { 567, 1292, 2423 };
  49     static double[] skatesSold = { 956, 1665, 2559 };
  50     static double[] pillsSold = { 1154, 1927, 2774 };
  51     final CategoryAxis xAxis = new CategoryAxis();
  52     final NumberAxis yAxis = new NumberAxis();
  53     final BarChart<String,Number> bc = new BarChart<String,Number>(xAxis,yAxis);
  54 
  55     @Override
  56     protected Chart createChart() {
  57         xAxis.setLabel("X Axis");
  58         xAxis.setCategories(FXCollections.<String>observableArrayList(Arrays.asList(years)));
  59         yAxis.setLabel("Y Axis");
  60         // add starting data
  61         XYChart.Series<String,Number> series1 = new XYChart.Series<String,Number>();
  62         series1.setName("Data Series 1");
  63         XYChart.Series<String,Number> series2 = new XYChart.Series<String,Number>();
  64         series2.setName("Data Series 2");
  65         series1.getData().add(new XYChart.Data<String,Number>(years[0], 567));
  66         series1.getData().add(new XYChart.Data<String,Number>(years[1], 1292));
  67         series1.getData().add(new XYChart.Data<String,Number>(years[2], 2180));
  68 
  69         series2.getData().add(new XYChart.Data<String,Number>(years[0], 956));
  70         series2.getData().add(new XYChart.Data<String,Number>(years[1], 1665));
  71         series2.getData().add(new XYChart.Data<String,Number>(years[2], 2450));
  72         bc.getData().add(series1);
  73         bc.getData().add(series2);
  74         return bc;
  75     }
  76 
  77     @Ignore("JDK-8162547")
  78     @Test
  79     public void testAddingCustomStyleClassToBarChartBarNodes() {
  80         startApp();
  81         XYChart.Series<String, Number> series = new XYChart.Series();
  82         XYChart.Data<String, Number> item = new XYChart.Data("A", 20);
  83         Node bar = item.getNode();
  84         if (bar == null) {
  85             bar = new StackPane();
  86         }
  87         String myStyleClass = "my-style";
  88         bar.getStyleClass().add(myStyleClass);
  89         item.setNode(bar);
  90         series.getData().add(item); 
  91         bc.getData().add(series);
  92         checkStyleClass(bar, myStyleClass);
  93     }
  94 
  95     @Test
  96     public void testCategoryAxisCategoriesOnAddDataAtIndex() {
  97         startApp();
  98         bc.getData().clear();
  99         xAxis.getCategories().clear();
 100         xAxis.setAutoRanging(true);
 101         XYChart.Series<String,Number> series = new XYChart.Series<String,Number>();
 102         series.getData().clear();
 103         series.getData().add(new XYChart.Data<String, Number>("1", 1));
 104         series.getData().add(new XYChart.Data<String, Number>("2", 2));
 105         series.getData().add(new XYChart.Data<String, Number>("3", 3));
 106         bc.getData().add(series);
 107         pulse();
 108         // category at index 0 = "1"
 109         assertEquals("1", xAxis.getCategories().get(0));
 110         series.getData().add(0, new XYChart.Data<String, Number>("0", 5));
 111         pulse();
 112         // item inserted at 0; category at index 0 = 0
 113         assertEquals("0", xAxis.getCategories().get(0));
 114     }
 115 
 116     @Test
 117     public void testRemoveAndAddSameSeriesBeforeAnimationCompletes() {
 118         startApp();
 119         assertEquals(2, bc.getData().size());
 120         // remove and add the same series.
 121         bc.getData().add(bc.getData().remove(0));
 122         pulse();
 123         assertEquals(2, bc.getData().size());
 124     }
 125 
 126     @Test
 127     public void testRemoveAndAddSameDataBeforeAnimationCompletes() {
 128         startApp();
 129         Series s = bc.getData().get(0);
 130         assertEquals(3, XYChartShim.Series_getDataSize(s));
 131         s.getData().add(s.getData().remove(0));
 132         assertEquals(3, XYChartShim.Series_getDataSize(s));
 133     }
 134 
 135     @Test
 136     public void testRemoveNotAnimated() {
 137         startApp();
 138         bc.setAnimated(false);
 139         Series s = bc.getData().get(0);
 140         assertEquals(3, XYChartShim.Series_getDataSize(s));
 141         s.getData().remove(0);
 142         assertEquals(2, XYChartShim.Series_getDataSize(s));
 143     }
 144 
 145     @Override
 146     ObservableList<XYChart.Series<?, ?>> createTestSeries() {
 147         ObservableList<XYChart.Series<?, ?>> list = FXCollections.observableArrayList();
 148         for (int i = 0; i != 10; i++) {
 149             XYChart.Series<String, Number> series = new XYChart.Series<>();
 150             series.getData().add(new XYChart.Data<>(Integer.toString(i*10), i*10));
 151             series.getData().add(new XYChart.Data<>(Integer.toString(i*20), i*20));
 152             series.getData().add(new XYChart.Data<>(Integer.toString(i*30), i*30));
 153             list.add(series);
 154         }
 155         return list;
 156     }
 157 
 158     @Override
 159     void checkSeriesStyleClasses(XYChart.Series<?, ?> series,
 160             int seriesIndex, int colorIndex) {
 161         // TODO: legend
 162     }
 163 
 164     @Override
 165     void checkDataStyleClasses(XYChart.Data<?, ?> data,
 166             int seriesIndex, int dataIndex, int colorIndex) {
 167         Node bar = data.getNode();
 168         checkStyleClass(bar, "series"+seriesIndex, "data"+dataIndex, "default-color"+colorIndex);
 169     }
 170 
 171     @Test
 172     public void testSeriesRemoveAnimatedStyleClasses() {
 173         startApp();
 174         bc.getData().clear();
 175         xAxis.getCategories().clear();
 176         xAxis.setAutoRanging(true);
 177         pulse();
 178         int nodesPerSeries = 3; // 3 bars
 179         checkSeriesRemoveAnimatedStyleClasses(bc, nodesPerSeries, 700);
 180     }
 181 }