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 javafx.collections.FXCollections;
  29 import javafx.collections.ObservableList;
  30 import javafx.scene.Group;
  31 import javafx.scene.Node;
  32 import javafx.scene.chart.AreaChart;
  33 import javafx.scene.chart.Axis;
  34 import javafx.scene.chart.CategoryAxis;
  35 import javafx.scene.chart.Chart;
  36 import javafx.scene.chart.NumberAxis;
  37 import javafx.scene.chart.XYChart;
  38 import javafx.scene.chart.XYChartShim;
  39 import javafx.scene.shape.Path;
  40 import static org.junit.Assert.assertEquals;
  41 import org.junit.Ignore;
  42 import org.junit.Test;
  43 
  44 public class AreaChartTest extends XYChartTestBase {
  45     AreaChart<Number,Number> ac;
  46     final XYChart.Series<Number, Number> series1 = new XYChart.Series<Number, Number>();
  47     boolean useCategoryAxis = false;
  48     final String[] countries = {"USA", "Italy", "France", "China", "India"};
  49     protected Chart createChart() {
  50         final NumberAxis yAxis = new NumberAxis();
  51         ObservableList<XYChart.Data> data = FXCollections.observableArrayList();
  52         Axis xAxis;
  53         if (useCategoryAxis) {
  54             xAxis = new CategoryAxis();
  55             ((CategoryAxis)xAxis).setCategories(FXCollections.observableArrayList(countries));
  56             // add starting data
  57         series1.getData().add(new XYChart.Data(countries[0], 10d));
  58         series1.getData().add(new XYChart.Data(countries[1], 20d));
  59         series1.getData().add(new XYChart.Data(countries[2], 15d));
  60         series1.getData().add(new XYChart.Data(countries[3], 15d));
  61         series1.getData().add(new XYChart.Data(countries[4], 10d));
  62         } else {
  63             xAxis = new NumberAxis();
  64             ac = new AreaChart<Number,Number>(xAxis,yAxis);
  65             // add starting data
  66         series1.getData().add(new XYChart.Data(10d, 10d));
  67         series1.getData().add(new XYChart.Data(25d, 20d));
  68         series1.getData().add(new XYChart.Data(30d, 15d));
  69         series1.getData().add(new XYChart.Data(50d, 15d));
  70         series1.getData().add(new XYChart.Data(80d, 10d));
  71         }
  72 
  73         xAxis.setLabel("X Axis");
  74         yAxis.setLabel("Y Axis");
  75         ac.setTitle("HelloAreaChart");
  76 
  77         return ac;
  78     }
  79 
  80     private String getSeriesLineFromPlot() {
  81         for (Node n : XYChartShim.getPlotChildren(ac)) {
  82             if (n instanceof Group) {
  83                 for (Node gn : ((Group)n).getChildren()) {
  84                     if (gn instanceof Path && "chart-series-area-line".equals(gn.getStyleClass().get(0))) {
  85                         Path line = (Path)gn;
  86                         return computeSVGPath(line);
  87                     }
  88                 }
  89             }
  90         }
  91         return "";
  92     }
  93 
  94     @Test @Ignore
  95     public void testDataItemRemove() {
  96         startApp();
  97         ac.getData().addAll(series1);
  98         pulse();
  99         if (!ac.getData().isEmpty()) {
 100             series1.getData().remove(0);
 101             pulse();
 102             assertEquals("L247.0 171.0 L412.0 171.0 L658.0 284.0 ", getSeriesLineFromPlot());
 103         }
 104     }
 105 
 106     @Test
 107     public void testAreaChartWithCategoryAxis() {
 108         useCategoryAxis = true;
 109         startApp();
 110         useCategoryAxis = false;
 111     }
 112 
 113     @Override
 114     void checkSeriesStyleClasses(XYChart.Series<?, ?> series,
 115             int seriesIndex, int colorIndex) {
 116         Group group = (Group) series.getNode();
 117         Node fillPath = group.getChildren().get(0);
 118         Node seriesLine = group.getChildren().get(1);
 119         checkStyleClass(fillPath, "series"+seriesIndex, "default-color"+colorIndex);
 120         checkStyleClass(seriesLine, "series"+seriesIndex, "default-color"+colorIndex);
 121     }
 122 
 123     @Override
 124     void checkDataStyleClasses(XYChart.Data<?, ?> data,
 125             int seriesIndex, int dataIndex, int colorIndex) {
 126         Node symbol = data.getNode();
 127         checkStyleClass(symbol, "series"+seriesIndex, "data"+dataIndex, "default-color"+colorIndex);
 128     }
 129 
 130     @Test
 131     public void testSeriesRemoveAnimatedStyleClasses() {
 132         startApp();
 133         //ac.setCreateSymbols(false);
 134         int nodesPerSeries = 4; // 3 symbols + 1 path
 135         checkSeriesRemoveAnimatedStyleClasses(ac, nodesPerSeries, 400);
 136     }
 137 
 138     @Test public void testCreateSymbols() {
 139          startApp();
 140          ac.getData().clear();
 141          ac.setCreateSymbols(false);
 142          pulse();
 143          ac.getData().addAll(series1);
 144          pulse();
 145          assertEquals(0, countSymbols(ac, "chart-area-symbol"));
 146 
 147          ac.getData().clear();
 148          ac.setCreateSymbols(true);
 149          pulse();
 150          ac.getData().addAll(series1);
 151          pulse();
 152          assertEquals(5, countSymbols(ac, "chart-area-symbol"));
 153      }
 154 }