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