1 /*
   2  * Copyright (c) 2011, 2016, 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 java.util.*;
  30 
  31 import javafx.animation.*;
  32 import javafx.application.Platform;
  33 import javafx.beans.NamedArg;
  34 import javafx.beans.property.DoubleProperty;
  35 import javafx.beans.property.SimpleDoubleProperty;
  36 import javafx.collections.FXCollections;
  37 import javafx.collections.ListChangeListener;
  38 import javafx.collections.ObservableList;
  39 import javafx.scene.AccessibleRole;
  40 import javafx.scene.Group;
  41 import javafx.scene.Node;
  42 import javafx.scene.layout.StackPane;
  43 import javafx.scene.shape.*;
  44 import javafx.util.Duration;
  45 
  46 import com.sun.javafx.charts.Legend.LegendItem;
  47 import javafx.css.converter.BooleanConverter;
  48 
  49 import javafx.beans.property.BooleanProperty;
  50 import javafx.beans.value.WritableValue;
  51 import javafx.css.CssMetaData;
  52 import javafx.css.Styleable;
  53 import javafx.css.StyleableBooleanProperty;
  54 import javafx.css.StyleableProperty;
  55 
  56 /**
  57  * StackedAreaChart is a variation of {@link AreaChart} that displays trends of the
  58  * contribution of each value. (over time e.g.) The areas are stacked so that each
  59  * series adjoins but does not overlap the preceding series. This contrasts with
  60  * the Area chart where each series overlays the preceding series.
  61  *
  62  * The cumulative nature of the StackedAreaChart gives an idea of the total Y data
  63  * value at any given point along the X axis.
  64  *
  65  * Since data points across multiple series may not be common, StackedAreaChart
  66  * interpolates values along the line joining the data points whenever necessary.
  67  *
  68  * @since JavaFX 2.1
  69  */
  70 public class StackedAreaChart<X,Y> extends XYChart<X,Y> {
  71 
  72     // -------------- PRIVATE FIELDS ------------------------------------------
  73 
  74     /** A multiplier for teh Y values that we store for each series, it is used to animate in a new series */
  75     private Map<Series<X,Y>, DoubleProperty> seriesYMultiplierMap = new HashMap<>();
  76 
  77     // -------------- PUBLIC PROPERTIES ----------------------------------------
  78     /**
  79      * When true, CSS styleable symbols are created for any data items that
  80      * don't have a symbol node specified.
  81      * @since JavaFX 8.0
  82      */
  83     private BooleanProperty createSymbols = new StyleableBooleanProperty(true) {
  84         @Override
  85         protected void invalidated() {
  86             for (int seriesIndex = 0; seriesIndex < getData().size(); seriesIndex++) {
  87                 Series<X, Y> series = getData().get(seriesIndex);
  88                 for (int itemIndex = 0; itemIndex < series.getData().size(); itemIndex++) {
  89                     Data<X, Y> item = series.getData().get(itemIndex);
  90                     Node symbol = item.getNode();
  91                     if (get() && symbol == null) { // create any symbols
  92                         symbol = createSymbol(series, getData().indexOf(series), item, itemIndex);
  93                         if (null != symbol) {
  94                             getPlotChildren().add(symbol);
  95                         }
  96                     } else if (!get() && symbol != null) { // remove symbols
  97                         getPlotChildren().remove(symbol);
  98                         symbol = null;
  99                         item.setNode(null);
 100                     }
 101                 }
 102             }
 103             requestChartLayout();
 104         }
 105 
 106         public Object getBean() {
 107             return this;
 108         }
 109 
 110         public String getName() {
 111             return "createSymbols";
 112         }
 113 
 114         public CssMetaData<StackedAreaChart<?, ?>,Boolean> getCssMetaData() {
 115             return StyleableProperties.CREATE_SYMBOLS;
 116         }
 117     };
 118 
 119     /**
 120      * Indicates whether symbols for data points will be created or not.
 121      *
 122      * @return true if symbols for data points will be created and false otherwise.
 123      * @since JavaFX 8.0
 124      */
 125     public final boolean getCreateSymbols() { return createSymbols.getValue(); }
 126     public final void setCreateSymbols(boolean value) { createSymbols.setValue(value); }
 127     public final BooleanProperty createSymbolsProperty() { return createSymbols; }
 128 
 129     // -------------- CONSTRUCTORS ----------------------------------------------
 130 
 131     /**
 132      * Construct a new Area Chart with the given axis
 133      *
 134      * @param xAxis The x axis to use
 135      * @param yAxis The y axis to use
 136      */
 137     public StackedAreaChart(@NamedArg("xAxis") Axis<X> xAxis, @NamedArg("yAxis") Axis<Y> yAxis) {
 138         this(xAxis,yAxis, FXCollections.<Series<X,Y>>observableArrayList());
 139     }
 140 
 141     /**
 142      * Construct a new Area Chart with the given axis and data.
 143      * <p>
 144      * Note: yAxis must be a ValueAxis, otherwise {@code IllegalArgumentException} is thrown.
 145      *
 146      * @param xAxis The x axis to use
 147      * @param yAxis The y axis to use
 148      * @param data The data to use, this is the actual list used so any changes to it will be reflected in the chart
 149      *
 150      * @throws java.lang.IllegalArgumentException if yAxis is not a ValueAxis
 151      */
 152     public StackedAreaChart(@NamedArg("xAxis") Axis<X> xAxis, @NamedArg("yAxis") Axis<Y> yAxis, @NamedArg("data") ObservableList<Series<X,Y>> data) {
 153         super(xAxis,yAxis);
 154         if (!(yAxis instanceof ValueAxis)) {
 155             throw new IllegalArgumentException("Axis type incorrect, yAxis must be of ValueAxis type.");
 156         }
 157         setData(data);
 158     }
 159 
 160     // -------------- METHODS ------------------------------------------------------------------------------------------
 161 
 162     private static double doubleValue(Number number) { return doubleValue(number, 0); }
 163     private static double doubleValue(Number number, double nullDefault) {
 164         return (number == null) ? nullDefault : number.doubleValue();
 165     }
 166 
 167     @Override protected void dataItemAdded(Series<X,Y> series, int itemIndex, Data<X,Y> item) {
 168         final Node symbol = createSymbol(series, getData().indexOf(series), item, itemIndex);
 169         if (shouldAnimate()) {
 170             boolean animate = false;
 171             if (itemIndex > 0 && itemIndex < (series.getData().size()-1)) {
 172                 animate = true;
 173                 Data<X,Y> p1 = series.getData().get(itemIndex - 1);
 174                 Data<X,Y> p2 = series.getData().get(itemIndex + 1);
 175                 double x1 = getXAxis().toNumericValue(p1.getXValue());
 176                 double y1 = getYAxis().toNumericValue(p1.getYValue());
 177                 double x3 = getXAxis().toNumericValue(p2.getXValue());
 178                 double y3 = getYAxis().toNumericValue(p2.getYValue());
 179 
 180                 double x2 = getXAxis().toNumericValue(item.getXValue());
 181                 double y2 = getYAxis().toNumericValue(item.getYValue());
 182 
 183 //                //1. y intercept of the line : y = ((y3-y1)/(x3-x1)) * x2 + (x3y1 - y3x1)/(x3 -x1)
 184                 double y = ((y3-y1)/(x3-x1)) * x2 + (x3*y1 - y3*x1)/(x3-x1);
 185                 item.setCurrentY(getYAxis().toRealValue(y));
 186                 item.setCurrentX(getXAxis().toRealValue(x2));
 187                 //2. we can simply use the midpoint on the line as well..
 188 //                double x = (x3 + x1)/2;
 189 //                double y = (y3 + y1)/2;
 190 //                item.setCurrentX(x);
 191 //                item.setCurrentY(y);
 192             } else if (itemIndex == 0 && series.getData().size() > 1) {
 193                 animate = true;
 194                 item.setCurrentX(series.getData().get(1).getXValue());
 195                 item.setCurrentY(series.getData().get(1).getYValue());
 196             } else if (itemIndex == (series.getData().size() - 1) && series.getData().size() > 1) {
 197                 animate = true;
 198                 int last = series.getData().size() - 2;
 199                 item.setCurrentX(series.getData().get(last).getXValue());
 200                 item.setCurrentY(series.getData().get(last).getYValue());
 201             } else if (symbol != null) {
 202                 // fade in new symbol
 203                 symbol.setOpacity(0);
 204                 getPlotChildren().add(symbol);
 205                 FadeTransition ft = new FadeTransition(Duration.millis(500),symbol);
 206                 ft.setToValue(1);
 207                 ft.play();
 208             }
 209             if (animate) {
 210                 animate(
 211                     new KeyFrame(Duration.ZERO,
 212                             (e) -> {
 213                                 if (symbol != null && !getPlotChildren().contains(symbol)) {
 214                                     getPlotChildren().add(symbol);
 215                                 } },
 216                             new KeyValue(item.currentYProperty(),
 217                                     item.getCurrentY()),
 218                             new KeyValue(item.currentXProperty(),
 219                                     item.getCurrentX())
 220                     ),
 221                     new KeyFrame(Duration.millis(800), new KeyValue(item.currentYProperty(),
 222                                         item.getYValue(), Interpolator.EASE_BOTH),
 223                                         new KeyValue(item.currentXProperty(),
 224                                         item.getXValue(), Interpolator.EASE_BOTH))
 225                 );
 226             }
 227 
 228         } else if (symbol != null) {
 229             getPlotChildren().add(symbol);
 230         }
 231     }
 232 
 233     @Override protected  void dataItemRemoved(final Data<X,Y> item, final Series<X,Y> series) {
 234         final Node symbol = item.getNode();
 235 
 236         if (symbol != null) {
 237             symbol.focusTraversableProperty().unbind();
 238         }
 239 
 240         // remove item from sorted list
 241         int itemIndex = series.getItemIndex(item);
 242         if (shouldAnimate()) {
 243             boolean animate = false;
 244             // dataSize represents size of currently visible data. After this operation, the number will decrement by 1
 245             final int dataSize = series.getDataSize();
 246             // This is the size of current data list in Series. Note that it might be totaly different from dataSize as
 247             // some big operation might have happened on the list.
 248             final int dataListSize = series.getData().size();
 249             if (itemIndex > 0 && itemIndex < dataSize - 1) {
 250                 animate = true;
 251                 Data<X,Y> p1 = series.getItem(itemIndex - 1);
 252                 Data<X,Y> p2 = series.getItem(itemIndex + 1);
 253                 double x1 = getXAxis().toNumericValue(p1.getXValue());
 254                 double y1 = getYAxis().toNumericValue(p1.getYValue());
 255                 double x3 = getXAxis().toNumericValue(p2.getXValue());
 256                 double y3 = getYAxis().toNumericValue(p2.getYValue());
 257 
 258                 double x2 = getXAxis().toNumericValue(item.getXValue());
 259                 double y2 = getYAxis().toNumericValue(item.getYValue());
 260 
 261 //                //1.  y intercept of the line : y = ((y3-y1)/(x3-x1)) * x2 + (x3y1 - y3x1)/(x3 -x1)
 262                 double y = ((y3-y1)/(x3-x1)) * x2 + (x3*y1 - y3*x1)/(x3-x1);
 263                 item.setCurrentX(getXAxis().toRealValue(x2));
 264                 item.setCurrentY(getYAxis().toRealValue(y2));
 265                 item.setXValue(getXAxis().toRealValue(x2));
 266                 item.setYValue(getYAxis().toRealValue(y));
 267                 //2.  we can simply use the midpoint on the line as well..
 268 //                double x = (x3 + x1)/2;
 269 //                double y = (y3 + y1)/2;
 270 //                item.setCurrentX(x);
 271 //                item.setCurrentY(y);
 272             } else if (itemIndex == 0 && dataListSize > 1) {
 273                 animate = true;
 274                 item.setXValue(series.getData().get(0).getXValue());
 275                 item.setYValue(series.getData().get(0).getYValue());
 276             } else if (itemIndex == (dataSize - 1) && dataListSize > 1) {
 277                 animate = true;
 278                 int last = dataListSize - 1;
 279                 item.setXValue(series.getData().get(last).getXValue());
 280                 item.setYValue(series.getData().get(last).getYValue());
 281             } else if (symbol != null) {
 282                 // fade out symbol
 283                 symbol.setOpacity(0);
 284                 FadeTransition ft = new FadeTransition(Duration.millis(500),symbol);
 285                 ft.setToValue(0);
 286                 ft.setOnFinished(actionEvent -> {
 287                     getPlotChildren().remove(symbol);
 288                     removeDataItemFromDisplay(series, item);
 289                     symbol.setOpacity(1.0);
 290                 });
 291                 ft.play();
 292             } else {
 293                 item.setSeries(null);
 294                 removeDataItemFromDisplay(series, item);
 295             }
 296             if (animate) {
 297                 animate( new KeyFrame(Duration.ZERO, new KeyValue(item.currentYProperty(),
 298                             item.getCurrentY()), new KeyValue(item.currentXProperty(),
 299                             item.getCurrentX())),
 300                             new KeyFrame(Duration.millis(800), actionEvent -> {
 301                                 getPlotChildren().remove(symbol);
 302                                 removeDataItemFromDisplay(series, item);
 303                             },
 304                             new KeyValue(item.currentYProperty(),
 305                             item.getYValue(), Interpolator.EASE_BOTH),
 306                             new KeyValue(item.currentXProperty(),
 307                             item.getXValue(), Interpolator.EASE_BOTH))
 308                 );
 309             }
 310         } else {
 311             getPlotChildren().remove(symbol);
 312             removeDataItemFromDisplay(series, item);
 313         }
 314         //Note: better animation here, point should move from old position to new position at center point between prev and next symbols
 315     }
 316 
 317     /** @inheritDoc */
 318     @Override protected void dataItemChanged(Data<X, Y> item) {
 319     }
 320 
 321     @Override protected void seriesChanged(ListChangeListener.Change<? extends Series> c) {
 322         // Update style classes for all series lines and symbols
 323         for (int i = 0; i < getDataSize(); i++) {
 324             final Series<X,Y> s = getData().get(i);
 325             Path seriesLine = (Path)((Group)s.getNode()).getChildren().get(1);
 326             Path fillPath = (Path)((Group)s.getNode()).getChildren().get(0);
 327             seriesLine.getStyleClass().setAll("chart-series-area-line", "series" + i, s.defaultColorStyleClass);
 328             fillPath.getStyleClass().setAll("chart-series-area-fill", "series" + i, s.defaultColorStyleClass);
 329             for (int j=0; j < s.getData().size(); j++) {
 330                 final Data<X,Y> item = s.getData().get(j);
 331                 final Node node = item.getNode();
 332                 if(node!=null) node.getStyleClass().setAll("chart-area-symbol", "series" + i, "data" + j, s.defaultColorStyleClass);
 333             }
 334         }
 335     }
 336 
 337     @Override protected  void seriesAdded(Series<X,Y> series, int seriesIndex) {
 338         // create new paths for series
 339         Path seriesLine = new Path();
 340         Path fillPath = new Path();
 341         seriesLine.setStrokeLineJoin(StrokeLineJoin.BEVEL);
 342         fillPath.setStrokeLineJoin(StrokeLineJoin.BEVEL);
 343         Group areaGroup = new Group(fillPath,seriesLine);
 344         series.setNode(areaGroup);
 345         // create series Y multiplier
 346         DoubleProperty seriesYAnimMultiplier = new SimpleDoubleProperty(this, "seriesYMultiplier");
 347         seriesYMultiplierMap.put(series, seriesYAnimMultiplier);
 348         // handle any data already in series
 349         if (shouldAnimate()) {
 350             seriesYAnimMultiplier.setValue(0d);
 351         } else {
 352             seriesYAnimMultiplier.setValue(1d);
 353         }
 354         getPlotChildren().add(areaGroup);
 355         List<KeyFrame> keyFrames = new ArrayList<KeyFrame>();
 356         if (shouldAnimate()) {
 357             // animate in new series
 358             keyFrames.add(new KeyFrame(Duration.ZERO,
 359                 new KeyValue(areaGroup.opacityProperty(), 0),
 360                 new KeyValue(seriesYAnimMultiplier, 0)
 361             ));
 362             keyFrames.add(new KeyFrame(Duration.millis(200),
 363                 new KeyValue(areaGroup.opacityProperty(), 1)
 364             ));
 365             keyFrames.add(new KeyFrame(Duration.millis(500),
 366                 new KeyValue(seriesYAnimMultiplier, 1)
 367             ));
 368         }
 369         for (int j=0; j<series.getData().size(); j++) {
 370             Data<X,Y> item = series.getData().get(j);
 371             final Node symbol = createSymbol(series, seriesIndex, item, j);
 372             if (symbol != null) {
 373                 if (shouldAnimate()) symbol.setOpacity(0);
 374                 getPlotChildren().add(symbol);
 375                 if (shouldAnimate()) {
 376                     // fade in new symbol
 377                     keyFrames.add(new KeyFrame(Duration.ZERO, new KeyValue(symbol.opacityProperty(), 0)));
 378                     keyFrames.add(new KeyFrame(Duration.millis(200), new KeyValue(symbol.opacityProperty(), 1)));
 379                 }
 380             }
 381         }
 382         if (shouldAnimate()) animate(keyFrames.toArray(new KeyFrame[keyFrames.size()]));
 383     }
 384 
 385     @Override protected  void seriesRemoved(final Series<X,Y> series) {
 386         // remove series Y multiplier
 387         seriesYMultiplierMap.remove(series);
 388         // remove all symbol nodes
 389         if (shouldAnimate()) {
 390             Timeline tl = new Timeline(createSeriesRemoveTimeLine(series, 400));
 391             tl.play();
 392         } else {
 393             getPlotChildren().remove(series.getNode());
 394             for (Data<X,Y> d:series.getData()) getPlotChildren().remove(d.getNode());
 395             removeSeriesFromDisplay(series);
 396         }
 397     }
 398 
 399     /** @inheritDoc */
 400     @Override protected void updateAxisRange() {
 401         // This override is necessary to update axis range based on cumulative Y value for the
 402         // Y axis instead of the normal way where max value in the data range is used.
 403         final Axis<X> xa = getXAxis();
 404         final Axis<Y> ya = getYAxis();
 405         if (xa.isAutoRanging()) {
 406             List xData = new ArrayList<Number>();
 407             for(Series<X,Y> series : getData()) {
 408                 for(Data<X,Y> data: series.getData()) {
 409                     xData.add(data.getXValue());
 410                 }
 411             }
 412             xa.invalidateRange(xData);
 413         }
 414         if (ya.isAutoRanging()) {
 415             double totalMinY = Double.MAX_VALUE;
 416             Iterator<Series<X, Y>> seriesIterator = getDisplayedSeriesIterator();
 417             boolean first = true;
 418             NavigableMap<Double, Double> accum = new TreeMap<>();
 419             NavigableMap<Double, Double> prevAccum = new TreeMap<>();
 420             NavigableMap<Double, Double> currentValues = new TreeMap<>();
 421             while (seriesIterator.hasNext()) {
 422                 currentValues.clear();
 423                 Series<X, Y> series = seriesIterator.next();
 424                 for(Data<X,Y> item : series.getData()) {
 425                     if(item != null) {
 426                         final double xv = xa.toNumericValue(item.getXValue());
 427                         final double yv = ya.toNumericValue(item.getYValue());
 428                         currentValues.put(xv, yv);
 429                         if (first) {
 430                             // On the first pass, just fill the map
 431                             accum.put(xv, yv);
 432                             // minimum is applicable only in the first series
 433                             totalMinY = Math.min(totalMinY, yv);
 434                         } else {
 435                             if (prevAccum.containsKey(xv)) {
 436                                 accum.put(xv, prevAccum.get(xv) + yv);
 437                             } else {
 438                                 // If the point wasn't yet in the previous (accumulated) series
 439                                 Map.Entry<Double, Double> he = prevAccum.higherEntry(xv);
 440                                 Map.Entry<Double, Double> le = prevAccum.lowerEntry(xv);
 441                                 if (he != null && le != null) {
 442                                     // If there's both point above and below this point, interpolate
 443                                     accum.put(xv, ((xv - le.getKey()) / (he.getKey() - le.getKey())) *
 444                                             (le.getValue() + he.getValue()) + yv);
 445                                 } else if (he != null) {
 446                                     // The point is before the first point in the previously accumulated series
 447                                     accum.put(xv, he.getValue() + yv);
 448                                 } else if (le != null) {
 449                                     // The point is after the last point in the previously accumulated series
 450                                     accum.put(xv, le.getValue() + yv);
 451                                 } else {
 452                                     // The previously accumulated series is empty
 453                                     accum.put(xv, yv);
 454                                 }
 455                             }
 456                         }
 457                     }
 458                 }
 459                 // Now update all the keys that were in the previous series, but not in the new one
 460                 for (Map.Entry<Double, Double> e : prevAccum.entrySet()) {
 461                     if (accum.keySet().contains(e.getKey())) {
 462                         continue;
 463                     }
 464                     Double k = e.getKey();
 465                     final Double v = e.getValue();
 466                     // Look at the values of the current series
 467                     Map.Entry<Double, Double> he = currentValues.higherEntry(k);
 468                     Map.Entry<Double, Double> le = currentValues.lowerEntry(k);
 469                     if (he != null && le != null) {
 470                         // Interpolate the for the point from current series and add the accumulated value
 471                         accum.put(k, ((k - le.getKey()) / (he.getKey() - le.getKey())) *
 472                                 (le.getValue() + he.getValue()) + v);
 473                     } else if (he != null) {
 474                         // There accumulated value is before the first value in the current series
 475                         accum.put(k, he.getValue() + v);
 476                     } else if (le != null) {
 477                         // There accumulated value is after the last value in the current series
 478                         accum.put(k, le.getValue() + v);
 479                     } else {
 480                         // The current series are empty
 481                         accum.put(k, v);
 482                     }
 483 
 484                 }
 485 
 486                 prevAccum.clear();
 487                 prevAccum.putAll(accum);
 488                 accum.clear();
 489                 first = (totalMinY == Double.MAX_VALUE); // If there was already some value in the series, we can consider as
 490                                                          // being past the first series
 491 
 492             }
 493             if(totalMinY != Double.MAX_VALUE) ya.invalidateRange(Arrays.asList(ya.toRealValue(totalMinY),
 494                     ya.toRealValue(Collections.max(prevAccum.values()))));
 495 
 496         }
 497     }
 498 
 499 
 500     /** @inheritDoc */
 501     @Override protected void layoutPlotChildren() {
 502         ArrayList<DataPointInfo<X, Y>> currentSeriesData = new ArrayList<>();
 503         // AggregateData hold the data points of both the current and the previous series.
 504             // The goal is to collect all the data, sort it and iterate.
 505         ArrayList<DataPointInfo<X, Y>> aggregateData = new ArrayList<>();
 506         for (int seriesIndex=0; seriesIndex < getDataSize(); seriesIndex++) { // for every series
 507             Series<X, Y> series = getData().get(seriesIndex);
 508             aggregateData.clear();
 509             // copy currentSeriesData accumulated in the previous iteration to aggregate.
 510             for(DataPointInfo<X, Y> data : currentSeriesData) {
 511                 data.partOf = PartOf.PREVIOUS;
 512                 aggregateData.add(data);
 513             }
 514             currentSeriesData.clear();
 515             // now copy actual data of the current series.
 516             for (Iterator<Data<X, Y>> it = getDisplayedDataIterator(series); it.hasNext(); ) {
 517                 Data<X, Y> item = it.next();
 518                 DataPointInfo<X, Y> itemInfo = new DataPointInfo<>(item, item.getXValue(),
 519                         item.getYValue(), PartOf.CURRENT);
 520                 aggregateData.add(itemInfo);
 521             }
 522             DoubleProperty seriesYAnimMultiplier = seriesYMultiplierMap.get(series);
 523             Path seriesLine = (Path)((Group)series.getNode()).getChildren().get(1);
 524             Path fillPath = (Path)((Group)series.getNode()).getChildren().get(0);
 525             seriesLine.getElements().clear();
 526             fillPath.getElements().clear();
 527             int dataIndex = 0;
 528             // Sort data points from prev and current series
 529             sortAggregateList(aggregateData);
 530 
 531             Axis<Y> yAxis = getYAxis();
 532             Axis<X> xAxis = getXAxis();
 533             boolean firstCurrent = false;
 534             boolean lastCurrent = false;
 535             int firstCurrentIndex = findNextCurrent(aggregateData, -1);
 536             int lastCurrentIndex = findPreviousCurrent(aggregateData, aggregateData.size());
 537             double basePosition = yAxis.getZeroPosition();
 538             if (Double.isNaN(basePosition)) {
 539                 ValueAxis<Number> valueYAxis = (ValueAxis<Number>) yAxis;
 540                 if (valueYAxis.getLowerBound() > 0) {
 541                     basePosition = valueYAxis.getDisplayPosition(valueYAxis.getLowerBound());
 542                 } else {
 543                     basePosition = valueYAxis.getDisplayPosition(valueYAxis.getUpperBound());
 544                 }
 545             }
 546             // Iterate over the aggregate data : this process accumulates data points
 547             // cumulatively from the bottom to top of stack
 548 
 549             for (DataPointInfo<X, Y> dataInfo : aggregateData) {
 550                 if (dataIndex == lastCurrentIndex) lastCurrent = true;
 551                 if (dataIndex == firstCurrentIndex) firstCurrent = true;
 552                 final Data<X,Y> item = dataInfo.dataItem;
 553                 if (dataInfo.partOf.equals(PartOf.CURRENT)) { // handle data from current series
 554                     int pIndex = findPreviousPrevious(aggregateData, dataIndex);
 555                     int nIndex = findNextPrevious(aggregateData, dataIndex);
 556                     DataPointInfo<X, Y> prevPoint;
 557                     DataPointInfo<X, Y> nextPoint;
 558                     if (pIndex == -1 || (nIndex == -1 && !(aggregateData.get(pIndex).x.equals(dataInfo.x)))) {
 559                         if (firstCurrent) {
 560                             // Need to add the drop down point.
 561                             Data<X, Y> ddItem = new Data(dataInfo.x, 0);
 562                             addDropDown(currentSeriesData, ddItem, ddItem.getXValue(), ddItem.getYValue(),
 563                                     xAxis.getDisplayPosition(ddItem.getCurrentX()), basePosition);
 564                         }
 565                         double x = xAxis.getDisplayPosition(item.getCurrentX());
 566                         double y = yAxis.getDisplayPosition(
 567                                 yAxis.toRealValue(yAxis.toNumericValue(item.getCurrentY()) * seriesYAnimMultiplier.getValue()));
 568                         addPoint(currentSeriesData, item, item.getXValue(), item.getYValue(), x, y,
 569                                 PartOf.CURRENT, false, (firstCurrent) ? false : true);
 570                         if (dataIndex == lastCurrentIndex) {
 571                             // need to add drop down point
 572                             Data<X, Y> ddItem = new Data(dataInfo.x, 0);
 573                             addDropDown(currentSeriesData, ddItem, ddItem.getXValue(), ddItem.getYValue(),
 574                                     xAxis.getDisplayPosition(ddItem.getCurrentX()), basePosition);
 575                         }
 576                     } else {
 577                         prevPoint = aggregateData.get(pIndex);
 578                         if (prevPoint.x.equals(dataInfo.x)) { // Need to add Y values
 579                             // Check if prevPoint is a dropdown - as the stable sort preserves the order.
 580                             // If so, find the non dropdown previous point on previous series.
 581                             if (prevPoint.dropDown) {
 582                                 pIndex = findPreviousPrevious(aggregateData, pIndex);
 583                                 prevPoint = aggregateData.get(pIndex);
 584                                 // If lastCurrent - add this drop down
 585                             }
 586                             if (prevPoint.x.equals(dataInfo.x)) { // simply add
 587                                 double x = xAxis.getDisplayPosition(item.getCurrentX());
 588                                 final double yv = yAxis.toNumericValue(item.getCurrentY()) + yAxis.toNumericValue(prevPoint.y);
 589                                 double y = yAxis.getDisplayPosition(
 590                                         yAxis.toRealValue(yv * seriesYAnimMultiplier.getValue()));
 591                                 addPoint(currentSeriesData, item, dataInfo.x, yAxis.toRealValue(yv), x, y, PartOf.CURRENT, false,
 592                                         (firstCurrent) ? false : true);
 593                             }
 594                             if (lastCurrent) {
 595                                 addDropDown(currentSeriesData, item, prevPoint.x, prevPoint.y, prevPoint.displayX, prevPoint.displayY);
 596                             }
 597                         } else {
 598                             // interpolate
 599                             nextPoint = (nIndex == -1) ? null : aggregateData.get(nIndex);
 600                             prevPoint = (pIndex == -1) ? null : aggregateData.get(pIndex);
 601                             final double yValue = yAxis.toNumericValue(item.getCurrentY());
 602                             if (prevPoint != null && nextPoint != null) {
 603                                 double x = xAxis.getDisplayPosition(item.getCurrentX());
 604                                 double displayY = interpolate(prevPoint.displayX,
 605                                         prevPoint.displayY, nextPoint.displayX, nextPoint.displayY, x);
 606                                 double dataY = interpolate(xAxis.toNumericValue(prevPoint.x),
 607                                         yAxis.toNumericValue(prevPoint.y),
 608                                         xAxis.toNumericValue(nextPoint.x),
 609                                         yAxis.toNumericValue(nextPoint.y),
 610                                         xAxis.toNumericValue(dataInfo.x));
 611                                 if (firstCurrent) {
 612                                     // now create the drop down point
 613                                     Data<X, Y> ddItem = new Data(dataInfo.x, dataY);
 614                                     addDropDown(currentSeriesData, ddItem, dataInfo.x, yAxis.toRealValue(dataY), x, displayY);
 615                                 }
 616                                 double y = yAxis.getDisplayPosition(yAxis.toRealValue((yValue + dataY) * seriesYAnimMultiplier.getValue()));
 617                                 // Add the current point
 618                                 addPoint(currentSeriesData, item, dataInfo.x, yAxis.toRealValue(yValue + dataY), x, y, PartOf.CURRENT, false,
 619                                         (firstCurrent) ? false : true);
 620                                 if (dataIndex == lastCurrentIndex) {
 621                                     // add drop down point
 622                                     Data<X, Y> ddItem = new Data(dataInfo.x, dataY);
 623                                     addDropDown(currentSeriesData, ddItem, dataInfo.x, yAxis.toRealValue(dataY), x, displayY);
 624                                 }
 625                                 // Note: add drop down if last current
 626                             }
 627                             else {
 628                                 // we do not need to take care of this as it is
 629                                 // already handled above with check of if(pIndex == -1 or nIndex == -1)
 630                             }
 631                         }
 632                     }
 633 
 634                 } else { // handle data from Previous series.
 635                     int pIndex = findPreviousCurrent(aggregateData, dataIndex);
 636                     int nIndex = findNextCurrent(aggregateData, dataIndex);
 637                     DataPointInfo<X, Y> prevPoint;
 638                     DataPointInfo<X, Y> nextPoint;
 639                     if (dataInfo.dropDown) {
 640                         if (xAxis.toNumericValue(dataInfo.x) <=
 641                                 xAxis.toNumericValue(aggregateData.get(firstCurrentIndex).x) ||
 642                                 xAxis.toNumericValue(dataInfo.x) > xAxis.toNumericValue(aggregateData.get(lastCurrentIndex).x)) {
 643                             addDropDown(currentSeriesData, item, dataInfo.x, dataInfo.y, dataInfo.displayX, dataInfo.displayY);
 644                         }
 645                     } else {
 646                         if (pIndex == -1 || nIndex == -1) {
 647                             addPoint(currentSeriesData, item, dataInfo.x, dataInfo.y, dataInfo.displayX, dataInfo.displayY,
 648                                     PartOf.CURRENT, true, false);
 649                         } else {
 650                             nextPoint = aggregateData.get(nIndex);
 651                             if (nextPoint.x.equals(dataInfo.x)) {
 652                                 // do nothing as the current point is already there.
 653                             } else {
 654                                 // interpolate on the current series.
 655                                 prevPoint = aggregateData.get(pIndex);
 656                                 double x = xAxis.getDisplayPosition(item.getCurrentX());
 657                                   double dataY = interpolate(xAxis.toNumericValue(prevPoint.x),
 658                                           yAxis.toNumericValue(prevPoint.y),
 659                                           xAxis.toNumericValue(nextPoint.x),
 660                                           yAxis.toNumericValue(nextPoint.y),
 661                                           xAxis.toNumericValue(dataInfo.x));
 662                                 final double yv = yAxis.toNumericValue(dataInfo.y) + dataY;
 663                                 double y = yAxis.getDisplayPosition(
 664                                         yAxis.toRealValue(yv * seriesYAnimMultiplier.getValue()));
 665                                 addPoint(currentSeriesData, new Data(dataInfo.x, dataY), dataInfo.x, yAxis.toRealValue(yv), x, y, PartOf.CURRENT, true, true);
 666                             }
 667                         }
 668                     }
 669                 }
 670                 dataIndex++;
 671                 if (firstCurrent) firstCurrent = false;
 672                 if (lastCurrent) lastCurrent = false;
 673             } // end of inner for loop
 674 
 675             // Draw the SeriesLine and Series fill
 676             if (!currentSeriesData.isEmpty()) {
 677                 seriesLine.getElements().add(new MoveTo(currentSeriesData.get(0).displayX, currentSeriesData.get(0).displayY));
 678                 fillPath.getElements().add(new MoveTo(currentSeriesData.get(0).displayX, currentSeriesData.get(0).displayY));
 679             }
 680             for (DataPointInfo<X, Y> point : currentSeriesData) {
 681                 if (point.lineTo) {
 682                     seriesLine.getElements().add(new LineTo(point.displayX, point.displayY));
 683                 } else {
 684                     seriesLine.getElements().add(new MoveTo(point.displayX, point.displayY));
 685                 }
 686                 fillPath.getElements().add(new  LineTo(point.displayX, point.displayY));
 687                 // draw symbols only for actual data points and skip for interpolated points.
 688                 if (!point.skipSymbol) {
 689                     Node symbol = point.dataItem.getNode();
 690                     if (symbol != null) {
 691                         final double w = symbol.prefWidth(-1);
 692                         final double h = symbol.prefHeight(-1);
 693                         symbol.resizeRelocate(point.displayX-(w/2), point.displayY-(h/2),w,h);
 694                     }
 695                 }
 696             }
 697             for(int i = aggregateData.size()-1; i > 0; i--) {
 698                 DataPointInfo<X, Y> point = aggregateData.get(i);
 699                 if (PartOf.PREVIOUS.equals(point.partOf)) {
 700                     fillPath.getElements().add(new  LineTo(point.displayX, point.displayY));
 701                 }
 702             }
 703             if (!fillPath.getElements().isEmpty()) {
 704                 fillPath.getElements().add(new ClosePath());
 705             }
 706 
 707         }  // end of out for loop
 708      }
 709 
 710     private void addDropDown(ArrayList<DataPointInfo<X, Y>> currentSeriesData, Data<X, Y> item, X xValue, Y yValue, double x, double y) {
 711         DataPointInfo<X, Y> dropDownDataPoint = new DataPointInfo<>(true);
 712         dropDownDataPoint.setValues(item, xValue, yValue, x, y, PartOf.CURRENT, true, false);
 713         currentSeriesData.add(dropDownDataPoint);
 714     }
 715 
 716     private void addPoint(ArrayList<DataPointInfo<X, Y>> currentSeriesData, Data<X, Y> item, X xValue, Y yValue, double x, double y, PartOf partof,
 717                           boolean symbol, boolean lineTo) {
 718         DataPointInfo<X, Y> currentDataPoint = new DataPointInfo<>();
 719         currentDataPoint.setValues(item, xValue, yValue, x, y, partof, symbol, lineTo);
 720         currentSeriesData.add(currentDataPoint);
 721     }
 722 
 723     //-------------------- helper methods to retrieve data points from the previous
 724      // or current data series.
 725      private int findNextCurrent(ArrayList<DataPointInfo<X, Y>> points, int index) {
 726         for(int i = index+1; i < points.size(); i++) {
 727             if (points.get(i).partOf.equals(PartOf.CURRENT)) {
 728                 return i;
 729             }
 730         }
 731         return -1;
 732      }
 733 
 734      private int findPreviousCurrent(ArrayList<DataPointInfo<X, Y>> points, int index) {
 735         for(int i = index-1; i >= 0; i--) {
 736             if (points.get(i).partOf.equals(PartOf.CURRENT)) {
 737                 return i;
 738             }
 739         }
 740         return -1;
 741      }
 742 
 743 
 744     private int findPreviousPrevious(ArrayList<DataPointInfo<X, Y>> points, int index) {
 745        for(int i = index-1; i >= 0; i--) {
 746             if (points.get(i).partOf.equals(PartOf.PREVIOUS)) {
 747                 return i;
 748             }
 749         }
 750         return -1;
 751     }
 752     private int findNextPrevious(ArrayList<DataPointInfo<X, Y>> points, int index) {
 753         for(int i = index+1; i < points.size(); i++) {
 754             if (points.get(i).partOf.equals(PartOf.PREVIOUS)) {
 755                 return i;
 756             }
 757         }
 758         return -1;
 759     }
 760 
 761 
 762      private void sortAggregateList(ArrayList<DataPointInfo<X, Y>> aggregateList) {
 763         Collections.sort(aggregateList, (o1, o2) -> {
 764             Data<X,Y> d1 = o1.dataItem;
 765             Data<X,Y> d2 = o2.dataItem;
 766             double val1 = getXAxis().toNumericValue(d1.getXValue());
 767             double val2 = getXAxis().toNumericValue(d2.getXValue());
 768             return (val1 < val2 ? -1 : ( val1 == val2) ? 0 : 1);
 769         });
 770      }
 771 
 772     private double interpolate(double lowX, double lowY, double highX, double highY, double x) {
 773          // using y = mx+c find the y for the given x.
 774          return (((highY - lowY)/(highX - lowX))*(x - lowX))+lowY;
 775     }
 776 
 777     private Node createSymbol(Series<X,Y> series, int seriesIndex, final Data<X,Y> item, int itemIndex) {
 778         Node symbol = item.getNode();
 779         // check if symbol has already been created
 780         if (symbol == null && getCreateSymbols()) {
 781             symbol = new StackPane();
 782             symbol.setAccessibleRole(AccessibleRole.TEXT);
 783             symbol.setAccessibleRoleDescription("Point");
 784             symbol.focusTraversableProperty().bind(Platform.accessibilityActiveProperty());
 785             item.setNode(symbol);
 786         }
 787         // set symbol styles
 788         // Note not sure if we want to add or check, ie be more careful and efficient here
 789         if (symbol != null) symbol.getStyleClass().setAll("chart-area-symbol", "series" + seriesIndex, "data" + itemIndex,
 790                 series.defaultColorStyleClass);
 791         return symbol;
 792     }
 793 
 794     @Override
 795     LegendItem createLegendItemForSeries(Series<X, Y> series, int seriesIndex) {
 796         LegendItem legendItem = new LegendItem(series.getName());
 797         legendItem.getSymbol().getStyleClass().addAll("chart-area-symbol", "series" + seriesIndex,
 798                 "area-legend-symbol", series.defaultColorStyleClass);
 799         return legendItem;
 800     }
 801 
 802     // -------------- INNER CLASSES --------------------------------------------
 803     /*
 804      * Helper class to hold data and display and other information for each
 805      * data point
 806      */
 807     final static class DataPointInfo<X, Y> {
 808         X x;
 809         Y y;
 810         double displayX;
 811         double displayY;
 812         Data<X,Y> dataItem;
 813         PartOf partOf;
 814         boolean skipSymbol = false; // interpolated point - skip drawing symbol
 815         boolean lineTo = false; // should there be a lineTo to this point on SeriesLine.
 816         boolean dropDown = false; // Is this a drop down point ( non data point).
 817 
 818         //----- Constructors --------------------
 819         DataPointInfo() {}
 820 
 821         DataPointInfo(Data<X,Y> item, X x, Y y, PartOf partOf) {
 822             this.dataItem = item;
 823             this.x = x;
 824             this.y = y;
 825             this.partOf = partOf;
 826         }
 827 
 828         DataPointInfo(boolean dropDown) {
 829             this.dropDown = dropDown;
 830         }
 831 
 832         void setValues(Data<X,Y> item, X x, Y y, double dx, double dy,
 833                         PartOf partOf, boolean skipSymbol, boolean lineTo) {
 834             this.dataItem = item;
 835             this.x = x;
 836             this.y = y;
 837             this.displayX = dx;
 838             this.displayY = dy;
 839             this.partOf = partOf;
 840             this.skipSymbol = skipSymbol;
 841             this.lineTo = lineTo;
 842         }
 843 
 844         public final X getX() {
 845             return x;
 846         }
 847 
 848         public final Y getY() {
 849             return y;
 850         }
 851     }
 852 
 853     // To indicate if the data point belongs to the current or the previous series.
 854     private static enum PartOf {
 855         CURRENT,
 856         PREVIOUS
 857     }
 858 
 859     // -------------- STYLESHEET HANDLING --------------------------------------
 860 
 861     private static class StyleableProperties {
 862 
 863         private static final CssMetaData<StackedAreaChart<?, ?>, Boolean> CREATE_SYMBOLS =
 864                 new CssMetaData<StackedAreaChart<?, ?>, Boolean>("-fx-create-symbols",
 865                 BooleanConverter.getInstance(), Boolean.TRUE) {
 866             @Override
 867             public boolean isSettable(StackedAreaChart<?,?> node) {
 868                 return node.createSymbols == null || !node.createSymbols.isBound();
 869             }
 870 
 871             @Override
 872             public StyleableProperty<Boolean> getStyleableProperty(StackedAreaChart<?,?> node) {
 873                 return (StyleableProperty<Boolean>)(WritableValue<Boolean>)node.createSymbolsProperty();
 874             }
 875         };
 876 
 877         private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;
 878 
 879         static {
 880             final List<CssMetaData<? extends Styleable, ?>> styleables =
 881                 new ArrayList<CssMetaData<? extends Styleable, ?>>(XYChart.getClassCssMetaData());
 882             styleables.add(CREATE_SYMBOLS);
 883             STYLEABLES = Collections.unmodifiableList(styleables);
 884 
 885         }
 886     }
 887 
 888     /**
 889      * @return The CssMetaData associated with this class, which may include the
 890      * CssMetaData of its superclasses.
 891      * @since JavaFX 8.0
 892      */
 893      public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
 894          return StyleableProperties.STYLEABLES;
 895      }
 896 
 897     /**
 898      * {@inheritDoc}
 899      * @since JavaFX 8.0
 900      */
 901     @Override
 902     public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() {
 903         return getClassCssMetaData();
 904     }
 905 
 906 }