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 
  29 import org.junit.Test;
  30 import static org.junit.Assert.assertEquals;
  31 import javafx.collections.*;
  32 
  33 import test.com.sun.javafx.pgstub.StubToolkit;
  34 import java.util.Arrays;
  35 
  36 import javafx.scene.Node;
  37 import javafx.scene.Scene;
  38 import javafx.scene.chart.CategoryAxis;
  39 import javafx.scene.chart.Chart;
  40 import javafx.scene.chart.NumberAxis;
  41 import javafx.scene.chart.StackedBarChart;
  42 import javafx.scene.chart.XYChart;
  43 import javafx.scene.chart.XYChartShim;
  44 import javafx.stage.Stage;
  45 import javafx.scene.layout.Region;
  46 
  47 import org.junit.Ignore;
  48 
  49 @Ignore
  50 public class StackedBarChartTest extends XYChartTestBase {
  51 
  52     private Scene scene;
  53     private StubToolkit toolkit;
  54     private Stage stage;
  55     StackedBarChart<String,Number> sbc;
  56     final XYChart.Series<String,Number> series1 = new XYChart.Series<String,Number>();
  57     final XYChart.Series<String,Number> series2 = new XYChart.Series<String,Number>();
  58     final XYChart.Series<String,Number> series3 = new XYChart.Series<String,Number>();
  59     final String[] years = {"2007", "2008", "2009"};
  60 
  61     protected Chart createChart() {
  62         final CategoryAxis xAxis = new CategoryAxis();
  63         final NumberAxis yAxis = new NumberAxis();
  64         sbc = new StackedBarChart<String,Number>(xAxis,yAxis);
  65         xAxis.setLabel("Year");
  66         xAxis.setCategories(FXCollections.<String>observableArrayList(Arrays.asList(years)));
  67         yAxis.setLabel("Price");
  68         sbc.setTitle("HelloStackedBarChart");
  69         // Populate Chart data
  70         ObservableList<XYChart.Data> data = FXCollections.observableArrayList();
  71         series1.getData().add(new XYChart.Data<String,Number>(years[0], 567));
  72         series1.getData().add(new XYChart.Data<String,Number>(years[1], 1292));
  73         series1.getData().add(new XYChart.Data<String,Number>(years[2], 2180));
  74         series2.getData().add(new XYChart.Data<String,Number>(years[0], 956));
  75         series2.getData().add(new XYChart.Data<String,Number>(years[1], 1665));
  76         series2.getData().add(new XYChart.Data<String,Number>(years[2], 2450));
  77         series3.getData().add(new XYChart.Data<String,Number>(years[0], 800));
  78         series3.getData().add(new XYChart.Data<String,Number>(years[1], 1000));
  79         series3.getData().add(new XYChart.Data<String,Number>(years[2], 2800));
  80         return sbc;
  81     }
  82 
  83     @Test
  84     public void testAddingAutoRangingCategoryAxis() {
  85         CategoryAxis xAxis = new CategoryAxis();
  86         String[] years = {"2007", "2008", "2009"};
  87         NumberAxis yAxis = new NumberAxis();
  88         ObservableList<StackedBarChart.Series> barChartData = FXCollections.observableArrayList(
  89             new StackedBarChart.Series("Region 1", FXCollections.observableArrayList(
  90                new StackedBarChart.Data(years[0], 567d),
  91                new StackedBarChart.Data(years[1], 1292d),
  92                new StackedBarChart.Data(years[2], 1292d)
  93             )),
  94             new StackedBarChart.Series("Region 2", FXCollections.observableArrayList(
  95                new StackedBarChart.Data(years[0], 956),
  96                new StackedBarChart.Data(years[1], 1665),
  97                new StackedBarChart.Data(years[2], 2559)
  98             ))
  99         );
 100         StackedBarChart sbc = new StackedBarChart(xAxis, yAxis, barChartData, 25.0d);
 101         scene = new Scene(sbc,800,600);
 102         stage = new Stage();
 103         stage.setScene(scene);
 104         stage.show();
 105         pulse();
 106          assertEquals(6, XYChartShim.getPlotChildren(sbc).size());
 107     }
 108 
 109     @Test
 110     public void testSeriesAdd() {
 111         startApp();
 112         sbc.getData().addAll(series1, series2, series3);
 113         pulse();
 114         ObservableList<Node> childrenList = XYChartShim.getPlotChildren(sbc);
 115         StringBuffer sb = new StringBuffer();
 116         assertEquals(9, childrenList.size());
 117         for (Node n : childrenList) {
 118             assertEquals("chart-bar", n.getStyleClass().get(0));
 119         }
 120         // compute bounds for the first series
 121         sb = computeBoundsString((Region)childrenList.get(0), (Region)childrenList.get(1),
 122                 (Region)childrenList.get(2));
 123         assertEquals("10 479 234 36 254 432 234 83 499 375 234 140 ", sb.toString());
 124 
 125         // compute bounds for the second series
 126 //        sb = computeBoundsString((Region)childrenList.get(3), (Region)childrenList.get(4),
 127 //                (Region)childrenList.get(5));
 128 //        assertEquals("10 421 236 62 256 328 236 108 501 220 236 158 ", sb.toString());
 129 //
 130 //        // compute bounds for the third series
 131 //        sb = computeBoundsString((Region)childrenList.get(6), (Region)childrenList.get(7),
 132 //                (Region)childrenList.get(8));
 133 //        assertEquals("10 370 236 51 256 264 236 64 501 39 236 181 ", sb.toString());
 134     }
 135 
 136     @Test
 137     public void testSeriesRemove() {
 138         startApp();
 139         sbc.getData().addAll(series1);
 140         pulse();
 141         assertEquals(3, XYChartShim.getPlotChildren(sbc).size());
 142         if (!sbc.getData().isEmpty()) {
 143             sbc.getData().remove(0);
 144             pulse();
 145             assertEquals(0, XYChartShim.getPlotChildren(sbc).size());
 146         }
 147     }
 148 
 149     @Test
 150     public void testDataItemAdd() {
 151         startApp();
 152         sbc.getData().addAll(series1);
 153         pulse();
 154         series1.getData().add(new XYChart.Data<String, Number>("2010", 750));
 155         pulse();
 156         assertEquals(4, XYChartShim.getPlotChildren(sbc).size());
 157     }
 158 
 159     @Test
 160     public void testDataItemInsert() {
 161         startApp();
 162         sbc.getData().addAll(series1);
 163         pulse();
 164         series1.getData().add(1, new XYChart.Data<String, Number>("2010", 750));
 165         pulse();
 166         assertEquals(4, XYChartShim.getPlotChildren(sbc).size());
 167     }
 168 
 169     @Test
 170     public void testDataItemRemove() {
 171         startApp();
 172         sbc.getData().addAll(series1);
 173         pulse();
 174         if (!sbc.getData().isEmpty()) {
 175             series1.getData().remove(0);
 176             pulse();
 177             assertEquals(2, XYChartShim.getPlotChildren(sbc).size());
 178         }
 179     }
 180 
 181     @Test @Ignore
 182     public void testDataItemChange() {
 183         startApp();
 184         sbc.getData().addAll(series1);
 185         pulse();
 186         if (!sbc.getData().isEmpty()) {
 187             StringBuffer sb = new StringBuffer();
 188             ObservableList<Node> childrenList = XYChartShim.getPlotChildren(sbc);
 189             // compute bounds for the first series before data change
 190             sb = computeBoundsString((Region)childrenList.get(0), (Region)childrenList.get(1),
 191                 (Region)childrenList.get(2));
 192             assertEquals("10 380 216 127 236 216 216 291 461 16 216 491 ", sb.toString());
 193 
 194             XYChart.Data<String, Number> d = series1.getData().get(2);
 195             d.setYValue(500d);
 196             pulse();
 197 
 198             // compute bounds for the first series after data change
 199             sb = computeBoundsString((Region)childrenList.get(0), (Region)childrenList.get(1),
 200                 (Region)childrenList.get(2));
 201             assertEquals("10 302 216 205 236 40 216 467 461 326 216 181 ", sb.toString());
 202         }
 203     }
 204 }