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