1 /*
   2  * Copyright (c) 2010, 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 com.sun.javafx.charts.Legend;
  30 import java.util.ArrayList;
  31 import java.util.BitSet;
  32 import java.util.Collections;
  33 import java.util.HashMap;
  34 import java.util.HashSet;
  35 import java.util.Iterator;
  36 import java.util.List;
  37 import java.util.Map;
  38 import java.util.Set;
  39 
  40 import javafx.animation.Interpolator;
  41 import javafx.animation.KeyFrame;
  42 import javafx.animation.KeyValue;
  43 import javafx.beans.binding.StringBinding;
  44 import javafx.beans.property.BooleanProperty;
  45 import javafx.beans.property.ObjectProperty;
  46 import javafx.beans.property.ObjectPropertyBase;
  47 import javafx.beans.property.ReadOnlyObjectProperty;
  48 import javafx.beans.property.ReadOnlyObjectWrapper;
  49 import javafx.beans.property.SimpleObjectProperty;
  50 import javafx.beans.property.StringProperty;
  51 import javafx.beans.property.StringPropertyBase;
  52 import javafx.beans.value.WritableValue;
  53 import javafx.collections.FXCollections;
  54 import javafx.collections.ListChangeListener;
  55 import javafx.collections.ListChangeListener.Change;
  56 import javafx.collections.ObservableList;
  57 import javafx.css.CssMetaData;
  58 import javafx.css.Styleable;
  59 import javafx.css.StyleableBooleanProperty;
  60 import javafx.css.StyleableProperty;
  61 import javafx.geometry.Orientation;
  62 import javafx.geometry.Side;
  63 import javafx.scene.Group;
  64 import javafx.scene.Node;
  65 import javafx.scene.layout.Region;
  66 import javafx.scene.shape.ClosePath;
  67 import javafx.scene.shape.Line;
  68 import javafx.scene.shape.LineTo;
  69 import javafx.scene.shape.MoveTo;
  70 import javafx.scene.shape.Path;
  71 import javafx.scene.shape.Rectangle;
  72 import javafx.util.Duration;
  73 
  74 import com.sun.javafx.collections.NonIterableChange;
  75 import javafx.css.converter.BooleanConverter;
  76 
  77 
  78 
  79 /**
  80  * Chart base class for all 2 axis charts. It is responsible for drawing the two
  81  * axes and the plot content. It contains a list of all content in the plot and
  82  * implementations of XYChart can add nodes to this list that need to be rendered.
  83  *
  84  * <p>It is possible to install Tooltips on data items / symbols.
  85  * For example the following code snippet installs Tooltip on the 1st data item.
  86  *
  87  * <pre><code>
  88  *  XYChart.Data item = ( XYChart.Data)series.getData().get(0);
  89  *  Tooltip.install(item.getNode(), new Tooltip("Symbol-0"));
  90  * </code></pre>
  91  *
  92  * @since JavaFX 2.0
  93  */
  94 public abstract class XYChart<X,Y> extends Chart {
  95 
  96     // -------------- PRIVATE FIELDS -----------------------------------------------------------------------------------
  97 
  98     // to indicate which colors are being used for the series
  99     private final BitSet colorBits = new BitSet(8);
 100     static String DEFAULT_COLOR = "default-color";
 101     final Map<Series<X,Y>, Integer> seriesColorMap = new HashMap<>();
 102     private boolean rangeValid = false;
 103     private final Line verticalZeroLine = new Line();
 104     private final Line horizontalZeroLine = new Line();
 105     private final Path verticalGridLines = new Path();
 106     private final Path horizontalGridLines = new Path();
 107     private final Path horizontalRowFill = new Path();
 108     private final Path verticalRowFill = new Path();
 109     private final Region plotBackground = new Region();
 110     private final Group plotArea = new Group(){
 111         @Override public void requestLayout() {} // suppress layout requests
 112     };
 113     private final Group plotContent = new Group();
 114     private final Rectangle plotAreaClip = new Rectangle();
 115 
 116     private final List<Series<X, Y>> displayedSeries = new ArrayList<>();
 117     private Legend legend = new Legend();
 118 
 119     /** This is called when a series is added or removed from the chart */
 120     private final ListChangeListener<Series<X,Y>> seriesChanged = c -> {
 121         ObservableList<? extends Series<X, Y>> series = c.getList();
 122         while (c.next()) {
 123             // RT-12069, linked list pointers should update when list is permutated.
 124             if (c.wasPermutated()) {
 125                 displayedSeries.sort((o1, o2) -> series.indexOf(o2) - series.indexOf(o1));
 126 
 127             }
 128 
 129             if (c.getRemoved().size() > 0) updateLegend();
 130 
 131             Set<Series<X, Y>> dupCheck = new HashSet<>(displayedSeries);
 132             dupCheck.removeAll(c.getRemoved());
 133             for (Series<X, Y> d : c.getAddedSubList()) {
 134                 if (!dupCheck.add(d)) {
 135                     throw new IllegalArgumentException("Duplicate series added");
 136                 }
 137             }
 138 
 139             for (Series<X,Y> s : c.getRemoved()) {
 140                 s.setToRemove = true;
 141                 seriesRemoved(s);
 142             }
 143 
 144             for(int i=c.getFrom(); i<c.getTo() && !c.wasPermutated(); i++) {
 145                 final Series<X,Y> s = c.getList().get(i);
 146                 // add new listener to data
 147                 s.setChart(XYChart.this);
 148                 if (s.setToRemove) {
 149                     s.setToRemove = false;
 150                     s.getChart().seriesBeingRemovedIsAdded(s);
 151                 }
 152                 // update linkedList Pointers for series
 153                 displayedSeries.add(s);
 154                 // update default color style class
 155                 int nextClearBit = colorBits.nextClearBit(0);
 156                 colorBits.set(nextClearBit, true);
 157                 s.defaultColorStyleClass = DEFAULT_COLOR+(nextClearBit%8);
 158                 seriesColorMap.put(s, nextClearBit%8);
 159                 // inform sub-classes of series added
 160                 seriesAdded(s, i);
 161             }
 162             if (c.getFrom() < c.getTo()) updateLegend();
 163             seriesChanged(c);
 164 
 165         }
 166         // update axis ranges
 167         invalidateRange();
 168         // lay everything out
 169         requestChartLayout();
 170     };
 171 
 172     // -------------- PUBLIC PROPERTIES --------------------------------------------------------------------------------
 173 
 174     private final Axis<X> xAxis;
 175     /** Get the X axis, by default it is along the bottom of the plot */
 176     public Axis<X> getXAxis() { return xAxis; }
 177 
 178     private final Axis<Y> yAxis;
 179     /** Get the Y axis, by default it is along the left of the plot */
 180     public Axis<Y> getYAxis() { return yAxis; }
 181 
 182     /** XYCharts data */
 183     private ObjectProperty<ObservableList<Series<X,Y>>> data = new ObjectPropertyBase<ObservableList<Series<X,Y>>>() {
 184         private ObservableList<Series<X,Y>> old;
 185         @Override protected void invalidated() {
 186             final ObservableList<Series<X,Y>> current = getValue();
 187             if (current == old) return;
 188             int saveAnimationState = -1;
 189             // add remove listeners
 190             if(old != null) {
 191                 old.removeListener(seriesChanged);
 192                 // Set animated to false so we don't animate both remove and add
 193                 // at the same time. RT-14163
 194                 // RT-21295 - disable animated only when current is also not null.
 195                 if (current != null && old.size() > 0) {
 196                     saveAnimationState = (old.get(0).getChart().getAnimated()) ? 1 : 2;
 197                     old.get(0).getChart().setAnimated(false);
 198                 }
 199             }
 200             if(current != null) current.addListener(seriesChanged);
 201             // fire series change event if series are added or removed
 202             if(old != null || current != null) {
 203                 final List<Series<X,Y>> removed = (old != null) ? old : Collections.<Series<X,Y>>emptyList();
 204                 final int toIndex = (current != null) ? current.size() : 0;
 205                 // let series listener know all old series have been removed and new that have been added
 206                 if (toIndex > 0 || !removed.isEmpty()) {
 207                     seriesChanged.onChanged(new NonIterableChange<Series<X,Y>>(0, toIndex, current){
 208                         @Override public List<Series<X,Y>> getRemoved() { return removed; }
 209                         @Override protected int[] getPermutation() {
 210                             return new int[0];
 211                         }
 212                     });
 213                 }
 214             } else if (old != null && old.size() > 0) {
 215                 // let series listener know all old series have been removed
 216                 seriesChanged.onChanged(new NonIterableChange<Series<X,Y>>(0, 0, current){
 217                     @Override public List<Series<X,Y>> getRemoved() { return old; }
 218                     @Override protected int[] getPermutation() {
 219                         return new int[0];
 220                     }
 221                 });
 222             }
 223             // restore animated on chart.
 224             if (current != null && current.size() > 0 && saveAnimationState != -1) {
 225                 current.get(0).getChart().setAnimated((saveAnimationState == 1) ? true : false);
 226             }
 227             old = current;
 228         }
 229 
 230         public Object getBean() {
 231             return XYChart.this;
 232         }
 233 
 234         public String getName() {
 235             return "data";
 236         }
 237     };
 238     public final ObservableList<Series<X,Y>> getData() { return data.getValue(); }
 239     public final void setData(ObservableList<Series<X,Y>> value) { data.setValue(value); }
 240     public final ObjectProperty<ObservableList<Series<X,Y>>> dataProperty() { return data; }
 241 
 242     /** True if vertical grid lines should be drawn */
 243     private BooleanProperty verticalGridLinesVisible = new StyleableBooleanProperty(true) {
 244         @Override protected void invalidated() {
 245             requestChartLayout();
 246         }
 247 
 248         @Override
 249         public Object getBean() {
 250             return XYChart.this;
 251         }
 252 
 253         @Override
 254         public String getName() {
 255             return "verticalGridLinesVisible";
 256         }
 257 
 258         @Override
 259         public CssMetaData<XYChart<?,?>,Boolean> getCssMetaData() {
 260             return StyleableProperties.VERTICAL_GRID_LINE_VISIBLE;
 261         }
 262     };
 263     /**
 264      * Indicates whether vertical grid lines are visible or not.
 265      *
 266      * @return true if verticalGridLines are visible else false.
 267      * @see #verticalGridLinesVisible
 268      */
 269     public final boolean getVerticalGridLinesVisible() { return verticalGridLinesVisible.get(); }
 270     public final void setVerticalGridLinesVisible(boolean value) { verticalGridLinesVisible.set(value); }
 271     public final BooleanProperty verticalGridLinesVisibleProperty() { return verticalGridLinesVisible; }
 272 
 273     /** True if horizontal grid lines should be drawn */
 274     private BooleanProperty horizontalGridLinesVisible = new StyleableBooleanProperty(true) {
 275         @Override protected void invalidated() {
 276             requestChartLayout();
 277         }
 278 
 279         @Override
 280         public Object getBean() {
 281             return XYChart.this;
 282         }
 283 
 284         @Override
 285         public String getName() {
 286             return "horizontalGridLinesVisible";
 287         }
 288 
 289         @Override
 290         public CssMetaData<XYChart<?,?>,Boolean> getCssMetaData() {
 291             return StyleableProperties.HORIZONTAL_GRID_LINE_VISIBLE;
 292         }
 293     };
 294     public final boolean isHorizontalGridLinesVisible() { return horizontalGridLinesVisible.get(); }
 295     public final void setHorizontalGridLinesVisible(boolean value) { horizontalGridLinesVisible.set(value); }
 296     public final BooleanProperty horizontalGridLinesVisibleProperty() { return horizontalGridLinesVisible; }
 297 
 298     /** If true then alternative vertical columns will have fills */
 299     private BooleanProperty alternativeColumnFillVisible = new StyleableBooleanProperty(false) {
 300         @Override protected void invalidated() {
 301             requestChartLayout();
 302         }
 303 
 304         @Override
 305         public Object getBean() {
 306             return XYChart.this;
 307         }
 308 
 309         @Override
 310         public String getName() {
 311             return "alternativeColumnFillVisible";
 312         }
 313 
 314         @Override
 315         public CssMetaData<XYChart<?,?>,Boolean> getCssMetaData() {
 316             return StyleableProperties.ALTERNATIVE_COLUMN_FILL_VISIBLE;
 317         }
 318     };
 319     public final boolean isAlternativeColumnFillVisible() { return alternativeColumnFillVisible.getValue(); }
 320     public final void setAlternativeColumnFillVisible(boolean value) { alternativeColumnFillVisible.setValue(value); }
 321     public final BooleanProperty alternativeColumnFillVisibleProperty() { return alternativeColumnFillVisible; }
 322 
 323     /** If true then alternative horizontal rows will have fills */
 324     private BooleanProperty alternativeRowFillVisible = new StyleableBooleanProperty(true) {
 325         @Override protected void invalidated() {
 326             requestChartLayout();
 327         }
 328 
 329         @Override
 330         public Object getBean() {
 331             return XYChart.this;
 332         }
 333 
 334         @Override
 335         public String getName() {
 336             return "alternativeRowFillVisible";
 337         }
 338 
 339         @Override
 340         public CssMetaData<XYChart<?,?>,Boolean> getCssMetaData() {
 341             return StyleableProperties.ALTERNATIVE_ROW_FILL_VISIBLE;
 342         }
 343     };
 344     public final boolean isAlternativeRowFillVisible() { return alternativeRowFillVisible.getValue(); }
 345     public final void setAlternativeRowFillVisible(boolean value) { alternativeRowFillVisible.setValue(value); }
 346     public final BooleanProperty alternativeRowFillVisibleProperty() { return alternativeRowFillVisible; }
 347 
 348     /**
 349      * If this is true and the vertical axis has both positive and negative values then a additional axis line
 350      * will be drawn at the zero point
 351      *
 352      * @defaultValue true
 353      */
 354     private BooleanProperty verticalZeroLineVisible = new StyleableBooleanProperty(true) {
 355         @Override protected void invalidated() {
 356             requestChartLayout();
 357         }
 358 
 359         @Override
 360         public Object getBean() {
 361             return XYChart.this;
 362         }
 363 
 364         @Override
 365         public String getName() {
 366             return "verticalZeroLineVisible";
 367         }
 368 
 369         @Override
 370         public CssMetaData<XYChart<?,?>,Boolean> getCssMetaData() {
 371             return StyleableProperties.VERTICAL_ZERO_LINE_VISIBLE;
 372         }
 373     };
 374     public final boolean isVerticalZeroLineVisible() { return verticalZeroLineVisible.get(); }
 375     public final void setVerticalZeroLineVisible(boolean value) { verticalZeroLineVisible.set(value); }
 376     public final BooleanProperty verticalZeroLineVisibleProperty() { return verticalZeroLineVisible; }
 377 
 378     /**
 379      * If this is true and the horizontal axis has both positive and negative values then a additional axis line
 380      * will be drawn at the zero point
 381      *
 382      * @defaultValue true
 383      */
 384     private BooleanProperty horizontalZeroLineVisible = new StyleableBooleanProperty(true) {
 385         @Override protected void invalidated() {
 386             requestChartLayout();
 387         }
 388 
 389         @Override
 390         public Object getBean() {
 391             return XYChart.this;
 392         }
 393 
 394         @Override
 395         public String getName() {
 396             return "horizontalZeroLineVisible";
 397         }
 398 
 399         @Override
 400         public CssMetaData<XYChart<?,?>,Boolean> getCssMetaData() {
 401             return StyleableProperties.HORIZONTAL_ZERO_LINE_VISIBLE;
 402         }
 403     };
 404     public final boolean isHorizontalZeroLineVisible() { return horizontalZeroLineVisible.get(); }
 405     public final void setHorizontalZeroLineVisible(boolean value) { horizontalZeroLineVisible.set(value); }
 406     public final BooleanProperty horizontalZeroLineVisibleProperty() { return horizontalZeroLineVisible; }
 407 
 408     // -------------- PROTECTED PROPERTIES -----------------------------------------------------------------------------
 409 
 410     /**
 411      * Modifiable and observable list of all content in the plot. This is where implementations of XYChart should add
 412      * any nodes they use to draw their plot.
 413      *
 414      * @return Observable list of plot children
 415      */
 416     protected ObservableList<Node> getPlotChildren() {
 417         return plotContent.getChildren();
 418     }
 419 
 420     // -------------- CONSTRUCTOR --------------------------------------------------------------------------------------
 421 
 422     /**
 423      * Constructs a XYChart given the two axes. The initial content for the chart
 424      * plot background and plot area that includes vertical and horizontal grid
 425      * lines and fills, are added.
 426      *
 427      * @param xAxis X Axis for this XY chart
 428      * @param yAxis Y Axis for this XY chart
 429      */
 430     public XYChart(Axis<X> xAxis, Axis<Y> yAxis) {
 431         this.xAxis = xAxis;
 432         if (xAxis.getSide() == null) xAxis.setSide(Side.BOTTOM);
 433         xAxis.setEffectiveOrientation(Orientation.HORIZONTAL);
 434         this.yAxis = yAxis;
 435         if (yAxis.getSide() == null) yAxis.setSide(Side.LEFT);
 436         yAxis.setEffectiveOrientation(Orientation.VERTICAL);
 437         // RT-23123 autoranging leads to charts incorrect appearance.
 438         xAxis.autoRangingProperty().addListener((ov, t, t1) -> {
 439             updateAxisRange();
 440         });
 441         yAxis.autoRangingProperty().addListener((ov, t, t1) -> {
 442             updateAxisRange();
 443         });
 444         // add initial content to chart content
 445         getChartChildren().addAll(plotBackground,plotArea,xAxis,yAxis);
 446         // We don't want plotArea or plotContent to autoSize or do layout
 447         plotArea.setAutoSizeChildren(false);
 448         plotContent.setAutoSizeChildren(false);
 449         // setup clipping on plot area
 450         plotAreaClip.setSmooth(false);
 451         plotArea.setClip(plotAreaClip);
 452         // add children to plot area
 453         plotArea.getChildren().addAll(
 454                 verticalRowFill, horizontalRowFill,
 455                 verticalGridLines, horizontalGridLines,
 456                 verticalZeroLine, horizontalZeroLine,
 457                 plotContent);
 458         // setup css style classes
 459         plotContent.getStyleClass().setAll("plot-content");
 460         plotBackground.getStyleClass().setAll("chart-plot-background");
 461         verticalRowFill.getStyleClass().setAll("chart-alternative-column-fill");
 462         horizontalRowFill.getStyleClass().setAll("chart-alternative-row-fill");
 463         verticalGridLines.getStyleClass().setAll("chart-vertical-grid-lines");
 464         horizontalGridLines.getStyleClass().setAll("chart-horizontal-grid-lines");
 465         verticalZeroLine.getStyleClass().setAll("chart-vertical-zero-line");
 466         horizontalZeroLine.getStyleClass().setAll("chart-horizontal-zero-line");
 467         // mark plotContent as unmanaged as its preferred size changes do not effect our layout
 468         plotContent.setManaged(false);
 469         plotArea.setManaged(false);
 470         // listen to animation on/off and sync to axis
 471         animatedProperty().addListener((valueModel, oldValue, newValue) -> {
 472             if(getXAxis() != null) getXAxis().setAnimated(newValue);
 473             if(getYAxis() != null) getYAxis().setAnimated(newValue);
 474         });
 475         setLegend(legend);
 476     }
 477 
 478     // -------------- METHODS ------------------------------------------------------------------------------------------
 479 
 480     /**
 481      * Gets the size of the data returning 0 if the data is null
 482      *
 483      * @return The number of items in data, or null if data is null
 484      */
 485     final int getDataSize() {
 486         final ObservableList<Series<X,Y>> data = getData();
 487         return (data!=null) ? data.size() : 0;
 488     }
 489 
 490     /** Called when a series's name has changed */
 491     private void seriesNameChanged() {
 492         updateLegend();
 493         requestChartLayout();
 494     }
 495 
 496     @SuppressWarnings({"UnusedParameters"})
 497     private void dataItemsChanged(Series<X,Y> series, List<Data<X,Y>> removed, int addedFrom, int addedTo, boolean permutation) {
 498         for (Data<X,Y> item : removed) {
 499             dataItemRemoved(item, series);
 500         }
 501         for(int i=addedFrom; i<addedTo; i++) {
 502             Data<X,Y> item = series.getData().get(i);
 503             dataItemAdded(series, i, item);
 504         }
 505         invalidateRange();
 506         requestChartLayout();
 507     }
 508 
 509     private <T> void dataValueChanged(Data<X,Y> item, T newValue, ObjectProperty<T> currentValueProperty) {
 510         if (currentValueProperty.get() != newValue) invalidateRange();
 511         dataItemChanged(item);
 512         if (shouldAnimate()) {
 513             animate(
 514                     new KeyFrame(Duration.ZERO, new KeyValue(currentValueProperty, currentValueProperty.get())),
 515                     new KeyFrame(Duration.millis(700), new KeyValue(currentValueProperty, newValue, Interpolator.EASE_BOTH))
 516             );
 517         } else {
 518             currentValueProperty.set(newValue);
 519             requestChartLayout();
 520         }
 521     }
 522 
 523     /**
 524      * This is called whenever a series is added or removed and the legend needs to be updated
 525      */
 526     protected void updateLegend() {
 527         List<Legend.LegendItem> legendList = new ArrayList<>();
 528         if (getData() != null) {
 529             for (int seriesIndex = 0; seriesIndex < getData().size(); seriesIndex++) {
 530                 Series<X, Y> series = getData().get(seriesIndex);
 531                 legendList.add(createLegendItemForSeries(series, seriesIndex));
 532             }
 533         }
 534         legend.getItems().setAll(legendList);
 535         if (legendList.size() > 0) {
 536             if (getLegend() == null) {
 537                 setLegend(legend);
 538             }
 539         } else {
 540             setLegend(null);
 541         }
 542     }
 543 
 544     /**
 545      * Called by the updateLegend for each series in the chart in order to
 546      * create new legend item
 547      * @param series the series for this legend item
 548      * @param seriesIndex the index of the series
 549      * @return new legend item for this series
 550      */
 551     Legend.LegendItem createLegendItemForSeries(Series<X, Y> series, int seriesIndex) {
 552         return new Legend.LegendItem(series.getName());
 553     }
 554 
 555     /**
 556      * This method is called when there is an attempt to add series that was
 557      * set to be removed, and the removal might not have completed.
 558      * @param series
 559      */
 560     void seriesBeingRemovedIsAdded(Series<X,Y> series) {}
 561 
 562     /**
 563      * This method is called when there is an attempt to add a Data item that was
 564      * set to be removed, and the removal might not have completed.
 565      * @param data
 566      */
 567     void dataBeingRemovedIsAdded(Data<X,Y> item, Series<X,Y> series) {}
 568     /**
 569      * Called when a data item has been added to a series. This is where implementations of XYChart can create/add new
 570      * nodes to getPlotChildren to represent this data item. They also may animate that data add with a fade in or
 571      * similar if animated = true.
 572      *
 573      * @param series    The series the data item was added to
 574      * @param itemIndex The index of the new item within the series
 575      * @param item      The new data item that was added
 576      */
 577     protected abstract void dataItemAdded(Series<X,Y> series, int itemIndex, Data<X,Y> item);
 578 
 579     /**
 580      * Called when a data item has been removed from data model but it is still visible on the chart. Its still visible
 581      * so that you can handle animation for removing it in this method. After you are done animating the data item you
 582      * must call removeDataItemFromDisplay() to remove the items node from being displayed on the chart.
 583      *
 584      * @param item   The item that has been removed from the series
 585      * @param series The series the item was removed from
 586      */
 587     protected abstract void dataItemRemoved(Data<X, Y> item, Series<X, Y> series);
 588 
 589     /**
 590      * Called when a data item has changed, ie its xValue, yValue or extraValue has changed.
 591      *
 592      * @param item    The data item who was changed
 593      */
 594     protected abstract void dataItemChanged(Data<X, Y> item);
 595     /**
 596      * A series has been added to the charts data model. This is where implementations of XYChart can create/add new
 597      * nodes to getPlotChildren to represent this series. Also you have to handle adding any data items that are
 598      * already in the series. You may simply call dataItemAdded() for each one or provide some different animation for
 599      * a whole series being added.
 600      *
 601      * @param series      The series that has been added
 602      * @param seriesIndex The index of the new series
 603      */
 604     protected abstract void seriesAdded(Series<X, Y> series, int seriesIndex);
 605 
 606     /**
 607      * A series has been removed from the data model but it is still visible on the chart. Its still visible
 608      * so that you can handle animation for removing it in this method. After you are done animating the data item you
 609      * must call removeSeriesFromDisplay() to remove the series from the display list.
 610      *
 611      * @param series The series that has been removed
 612      */
 613     protected abstract void seriesRemoved(Series<X,Y> series);
 614 
 615     /** Called when each atomic change is made to the list of series for this chart */
 616     protected void seriesChanged(Change<? extends Series> c) {}
 617 
 618     /**
 619      * This is called when a data change has happened that may cause the range to be invalid.
 620      */
 621     private void invalidateRange() {
 622         rangeValid = false;
 623     }
 624 
 625     /**
 626      * This is called when the range has been invalidated and we need to update it. If the axis are auto
 627      * ranging then we compile a list of all data that the given axis has to plot and call invalidateRange() on the
 628      * axis passing it that data.
 629      */
 630     protected void updateAxisRange() {
 631         final Axis<X> xa = getXAxis();
 632         final Axis<Y> ya = getYAxis();
 633         List<X> xData = null;
 634         List<Y> yData = null;
 635         if(xa.isAutoRanging()) xData = new ArrayList<X>();
 636         if(ya.isAutoRanging()) yData = new ArrayList<Y>();
 637         if(xData != null || yData != null) {
 638             for(Series<X,Y> series : getData()) {
 639                 for(Data<X,Y> data: series.getData()) {
 640                     if(xData != null) xData.add(data.getXValue());
 641                     if(yData != null) yData.add(data.getYValue());
 642                 }
 643             }
 644             if(xData != null) xa.invalidateRange(xData);
 645             if(yData != null) ya.invalidateRange(yData);
 646         }
 647     }
 648 
 649     /**
 650      * Called to update and layout the plot children. This should include all work to updates nodes representing
 651      * the plot on top of the axis and grid lines etc. The origin is the top left of the plot area, the plot area with
 652      * can be got by getting the width of the x axis and its height from the height of the y axis.
 653      */
 654     protected abstract void layoutPlotChildren();
 655 
 656     /** @inheritDoc */
 657     @Override protected final void layoutChartChildren(double top, double left, double width, double height) {
 658         if(getData() == null) return;
 659         if (!rangeValid) {
 660             rangeValid = true;
 661             if(getData() != null) updateAxisRange();
 662         }
 663         // snap top and left to pixels
 664         top = snapPositionY(top);
 665         left = snapPositionX(left);
 666         // get starting stuff
 667         final Axis<X> xa = getXAxis();
 668         final ObservableList<Axis.TickMark<X>> xaTickMarks = xa.getTickMarks();
 669         final Axis<Y> ya = getYAxis();
 670         final ObservableList<Axis.TickMark<Y>> yaTickMarks = ya.getTickMarks();
 671         // check we have 2 axises and know their sides
 672         if (xa == null || ya == null) return;
 673         // try and work out width and height of axises
 674         double xAxisWidth = 0;
 675         double xAxisHeight = 30; // guess x axis height to start with
 676         double yAxisWidth = 0;
 677         double yAxisHeight = 0;
 678         for (int count=0; count<5; count ++) {
 679             yAxisHeight = snapSizeY(height - xAxisHeight);
 680             if (yAxisHeight < 0) {
 681                 yAxisHeight = 0;
 682             }
 683             yAxisWidth = ya.prefWidth(yAxisHeight);
 684             xAxisWidth = snapSizeX(width - yAxisWidth);
 685             if (xAxisWidth < 0) {
 686                 xAxisWidth = 0;
 687             }
 688             double newXAxisHeight = xa.prefHeight(xAxisWidth);
 689             if (newXAxisHeight == xAxisHeight) break;
 690             xAxisHeight = newXAxisHeight;
 691         }
 692         // round axis sizes up to whole integers to snap to pixel
 693         xAxisWidth = Math.ceil(xAxisWidth);
 694         xAxisHeight = Math.ceil(xAxisHeight);
 695         yAxisWidth = Math.ceil(yAxisWidth);
 696         yAxisHeight = Math.ceil(yAxisHeight);
 697         // calc xAxis height
 698         double xAxisY = 0;
 699         switch(xa.getEffectiveSide()) {
 700             case TOP:
 701                 xa.setVisible(true);
 702                 xAxisY = top+1;
 703                 top += xAxisHeight;
 704                 break;
 705             case BOTTOM:
 706                 xa.setVisible(true);
 707                 xAxisY = top + yAxisHeight;
 708         }
 709 
 710         // calc yAxis width
 711         double yAxisX = 0;
 712         switch(ya.getEffectiveSide()) {
 713             case LEFT:
 714                 ya.setVisible(true);
 715                 yAxisX = left +1;
 716                 left += yAxisWidth;
 717                 break;
 718             case RIGHT:
 719                 ya.setVisible(true);
 720                 yAxisX = left + xAxisWidth;
 721         }
 722         // resize axises
 723         xa.resizeRelocate(left, xAxisY, xAxisWidth, xAxisHeight);
 724         ya.resizeRelocate(yAxisX, top, yAxisWidth, yAxisHeight);
 725         // When the chart is resized, need to specifically call out the axises
 726         // to lay out as they are unmanaged.
 727         xa.requestAxisLayout();
 728         xa.layout();
 729         ya.requestAxisLayout();
 730         ya.layout();
 731         // layout plot content
 732         layoutPlotChildren();
 733         // get axis zero points
 734         final double xAxisZero = snapPositionX(xa.getZeroPosition());
 735         final double yAxisZero = snapPositionY(ya.getZeroPosition());
 736         // position vertical and horizontal zero lines
 737         if(Double.isNaN(xAxisZero) || !isVerticalZeroLineVisible()) {
 738             verticalZeroLine.setVisible(false);
 739         } else {
 740             verticalZeroLine.setStartX(left+xAxisZero+0.5);
 741             verticalZeroLine.setStartY(top);
 742             verticalZeroLine.setEndX(left+xAxisZero+0.5);
 743             verticalZeroLine.setEndY(top+yAxisHeight);
 744             verticalZeroLine.setVisible(true);
 745         }
 746         if(Double.isNaN(yAxisZero) || !isHorizontalZeroLineVisible()) {
 747             horizontalZeroLine.setVisible(false);
 748         } else {
 749             horizontalZeroLine.setStartX(left);
 750             horizontalZeroLine.setStartY(top+yAxisZero+0.5);
 751             horizontalZeroLine.setEndX(left+xAxisWidth);
 752             horizontalZeroLine.setEndY(top+yAxisZero+0.5);
 753             horizontalZeroLine.setVisible(true);
 754         }
 755         // layout plot background
 756         plotBackground.resizeRelocate(left, top, xAxisWidth, yAxisHeight);
 757         // update clip
 758         plotAreaClip.setX(left);
 759         plotAreaClip.setY(top);
 760         plotAreaClip.setWidth(xAxisWidth+1);
 761         plotAreaClip.setHeight(yAxisHeight+1);
 762 //        plotArea.setClip(new Rectangle(left, top, xAxisWidth, yAxisHeight));
 763         // position plot group, its origin is the bottom left corner of the plot area
 764         plotContent.setLayoutX(left);
 765         plotContent.setLayoutY(top);
 766         plotContent.requestLayout(); // Note: not sure this is right, maybe plotContent should be resizeable
 767         // update vertical grid lines
 768         verticalGridLines.getElements().clear();
 769         if (getVerticalGridLinesVisible()) {
 770             for (Axis.TickMark<X> tick : xaTickMarks) {
 771                 final double x = tick.getPosition();
 772                 if ((x != xAxisZero || !isVerticalZeroLineVisible()) && x > 0 && x <= xAxisWidth) {
 773                     verticalGridLines.getElements().add(new MoveTo(left+x+0.5, top));
 774                     verticalGridLines.getElements().add(new LineTo(left+x+0.5, top+yAxisHeight));
 775                 }
 776             }
 777         }
 778         // update horizontal grid lines
 779         horizontalGridLines.getElements().clear();
 780         if (isHorizontalGridLinesVisible()) {
 781             for (Axis.TickMark<Y> tick : yaTickMarks) {
 782                 final double y = tick.getPosition();
 783                 if ((y != yAxisZero || !isHorizontalZeroLineVisible()) && y >= 0 && y < yAxisHeight) {
 784                     horizontalGridLines.getElements().add(new MoveTo(left, top+y+0.5));
 785                     horizontalGridLines.getElements().add(new LineTo(left+xAxisWidth, top+y+0.5));
 786                 }
 787             }
 788         }
 789         // Note: is there a more efficient way to calculate horizontal and vertical row fills?
 790         // update vertical row fill
 791         verticalRowFill.getElements().clear();
 792         if (isAlternativeColumnFillVisible()) {
 793             // tick marks are not sorted so get all the positions and sort them
 794             final List<Double> tickPositionsPositive = new ArrayList<Double>();
 795             final List<Double> tickPositionsNegative = new ArrayList<Double>();
 796             for (Axis.TickMark<X> tick : xaTickMarks) {
 797                 double pos = tick.getPosition();
 798                 if (pos == xAxisZero) {
 799                     tickPositionsPositive.add(pos);
 800                     tickPositionsNegative.add(pos);
 801                 } else if (pos < xAxisZero) {
 802                     tickPositionsPositive.add(pos);
 803                 } else {
 804                     tickPositionsNegative.add(pos);
 805                 }
 806             }
 807             Collections.sort(tickPositionsPositive);
 808             Collections.sort(tickPositionsNegative);
 809             // iterate over every pair of positive tick marks and create fill
 810             for(int i=1; i < tickPositionsPositive.size(); i+=2) {
 811                 if((i+1) < tickPositionsPositive.size()) {
 812                     final double x1 = tickPositionsPositive.get(i);
 813                     final double x2 = tickPositionsPositive.get(i+1);
 814                     verticalRowFill.getElements().addAll(
 815                             new MoveTo(left+x1,top),
 816                             new LineTo(left+x1,top+yAxisHeight),
 817                             new LineTo(left+x2,top+yAxisHeight),
 818                             new LineTo(left+x2,top),
 819                             new ClosePath());
 820                 }
 821             }
 822             // iterate over every pair of positive tick marks and create fill
 823             for(int i=0; i < tickPositionsNegative.size(); i+=2) {
 824                 if((i+1) < tickPositionsNegative.size()) {
 825                     final double x1 = tickPositionsNegative.get(i);
 826                     final double x2 = tickPositionsNegative.get(i+1);
 827                     verticalRowFill.getElements().addAll(
 828                             new MoveTo(left+x1,top),
 829                             new LineTo(left+x1,top+yAxisHeight),
 830                             new LineTo(left+x2,top+yAxisHeight),
 831                             new LineTo(left+x2,top),
 832                             new ClosePath());
 833                 }
 834             }
 835         }
 836         // update horizontal row fill
 837         horizontalRowFill.getElements().clear();
 838         if (isAlternativeRowFillVisible()) {
 839             // tick marks are not sorted so get all the positions and sort them
 840             final List<Double> tickPositionsPositive = new ArrayList<Double>();
 841             final List<Double> tickPositionsNegative = new ArrayList<Double>();
 842             for (Axis.TickMark<Y> tick : yaTickMarks) {
 843                 double pos = tick.getPosition();
 844                 if (pos == yAxisZero) {
 845                     tickPositionsPositive.add(pos);
 846                     tickPositionsNegative.add(pos);
 847                 } else if (pos < yAxisZero) {
 848                     tickPositionsPositive.add(pos);
 849                 } else {
 850                     tickPositionsNegative.add(pos);
 851                 }
 852             }
 853             Collections.sort(tickPositionsPositive);
 854             Collections.sort(tickPositionsNegative);
 855             // iterate over every pair of positive tick marks and create fill
 856             for(int i=1; i < tickPositionsPositive.size(); i+=2) {
 857                 if((i+1) < tickPositionsPositive.size()) {
 858                     final double y1 = tickPositionsPositive.get(i);
 859                     final double y2 = tickPositionsPositive.get(i+1);
 860                     horizontalRowFill.getElements().addAll(
 861                             new MoveTo(left, top + y1),
 862                             new LineTo(left + xAxisWidth, top + y1),
 863                             new LineTo(left + xAxisWidth, top + y2),
 864                             new LineTo(left, top + y2),
 865                             new ClosePath());
 866                 }
 867             }
 868             // iterate over every pair of positive tick marks and create fill
 869             for(int i=0; i < tickPositionsNegative.size(); i+=2) {
 870                 if((i+1) < tickPositionsNegative.size()) {
 871                     final double y1 = tickPositionsNegative.get(i);
 872                     final double y2 = tickPositionsNegative.get(i+1);
 873                     horizontalRowFill.getElements().addAll(
 874                             new MoveTo(left, top + y1),
 875                             new LineTo(left + xAxisWidth, top + y1),
 876                             new LineTo(left + xAxisWidth, top + y2),
 877                             new LineTo(left, top + y2),
 878                             new ClosePath());
 879                 }
 880             }
 881         }
 882 //
 883     }
 884 
 885     /**
 886      * Get the index of the series in the series linked list.
 887      *
 888      * @param series The series to find index for
 889      * @return index of the series in series list
 890      */
 891     int getSeriesIndex(Series<X,Y> series) {
 892         return displayedSeries.indexOf(series);
 893     }
 894 
 895     /**
 896      * Computes the size of series linked list
 897      * @return size of series linked list
 898      */
 899     int getSeriesSize() {
 900         return displayedSeries.size();
 901     }
 902 
 903     /**
 904      * This should be called from seriesRemoved() when you are finished with any animation for deleting the series from
 905      * the chart. It will remove the series from showing up in the Iterator returned by getDisplayedSeriesIterator().
 906      *
 907      * @param series The series to remove
 908      */
 909     protected final void removeSeriesFromDisplay(Series<X, Y> series) {
 910         if (series != null) series.setToRemove = false;
 911         series.setChart(null);
 912         displayedSeries.remove(series);
 913         int idx = seriesColorMap.remove(series);
 914         colorBits.clear(idx);
 915     }
 916 
 917     /**
 918      * XYChart maintains a list of all series currently displayed this includes all current series + any series that
 919      * have recently been deleted that are in the process of being faded(animated) out. This creates and returns a
 920      * iterator over that list. This is what implementations of XYChart should use when plotting data.
 921      *
 922      * @return iterator over currently displayed series
 923      */
 924     protected final Iterator<Series<X,Y>> getDisplayedSeriesIterator() {
 925         return Collections.unmodifiableList(displayedSeries).iterator();
 926     }
 927 
 928     /**
 929      * Creates an array of KeyFrames for fading out nodes representing a series
 930      *
 931      * @param series The series to remove
 932      * @param fadeOutTime Time to fade out, in milliseconds
 933      * @return array of two KeyFrames from zero to fadeOutTime
 934      */
 935     final KeyFrame[] createSeriesRemoveTimeLine(Series<X, Y> series, long fadeOutTime) {
 936         final List<Node> nodes = new ArrayList<>();
 937         nodes.add(series.getNode());
 938         for (Data<X, Y> d : series.getData()) {
 939             if (d.getNode() != null) {
 940                 nodes.add(d.getNode());
 941             }
 942         }
 943         // fade out series node and symbols
 944         KeyValue[] startValues = new KeyValue[nodes.size()];
 945         KeyValue[] endValues = new KeyValue[nodes.size()];
 946         for (int j = 0; j < nodes.size(); j++) {
 947             startValues[j] = new KeyValue(nodes.get(j).opacityProperty(), 1);
 948             endValues[j] = new KeyValue(nodes.get(j).opacityProperty(), 0);
 949         }
 950         return new KeyFrame[] {
 951             new KeyFrame(Duration.ZERO, startValues),
 952             new KeyFrame(Duration.millis(fadeOutTime), actionEvent -> {
 953                 getPlotChildren().removeAll(nodes);
 954                 removeSeriesFromDisplay(series);
 955             }, endValues)
 956         };
 957     }
 958 
 959     /**
 960      * The current displayed data value plotted on the X axis. This may be the same as xValue or different. It is
 961      * used by XYChart to animate the xValue from the old value to the new value. This is what you should plot
 962      * in any custom XYChart implementations. Some XYChart chart implementations such as LineChart also use this
 963      * to animate when data is added or removed.
 964      */
 965     protected final X getCurrentDisplayedXValue(Data<X,Y> item) { return item.getCurrentX(); }
 966 
 967     /** Set the current displayed data value plotted on X axis.
 968      *
 969      * @param item The XYChart.Data item from which the current X axis data value is obtained.
 970      * @see #getCurrentDisplayedXValue
 971      */
 972     protected final void setCurrentDisplayedXValue(Data<X,Y> item, X value) { item.setCurrentX(value); }
 973 
 974     /** The current displayed data value property that is plotted on X axis.
 975      *
 976      * @param item The XYChart.Data item from which the current X axis data value property object is obtained.
 977      * @return The current displayed X data value ObjectProperty.
 978      * @see #getCurrentDisplayedXValue
 979      */
 980     protected final ObjectProperty<X> currentDisplayedXValueProperty(Data<X,Y> item) { return item.currentXProperty(); }
 981 
 982     /**
 983      * The current displayed data value plotted on the Y axis. This may be the same as yValue or different. It is
 984      * used by XYChart to animate the yValue from the old value to the new value. This is what you should plot
 985      * in any custom XYChart implementations. Some XYChart chart implementations such as LineChart also use this
 986      * to animate when data is added or removed.
 987      */
 988     protected final Y getCurrentDisplayedYValue(Data<X,Y> item) { return item.getCurrentY(); }
 989 
 990     /**
 991      * Set the current displayed data value plotted on Y axis.
 992      *
 993      * @param item The XYChart.Data item from which the current Y axis data value is obtained.
 994      * @see #getCurrentDisplayedYValue
 995      */
 996     protected final void setCurrentDisplayedYValue(Data<X,Y> item, Y value) { item.setCurrentY(value); }
 997 
 998     /** The current displayed data value property that is plotted on Y axis.
 999      *
1000      * @param item The XYChart.Data item from which the current Y axis data value property object is obtained.
1001      * @return The current displayed Y data value ObjectProperty.
1002      * @see #getCurrentDisplayedYValue
1003      */
1004     protected final ObjectProperty<Y> currentDisplayedYValueProperty(Data<X,Y> item) { return item.currentYProperty(); }
1005 
1006     /**
1007      * The current displayed data extra value. This may be the same as extraValue or different. It is
1008      * used by XYChart to animate the extraValue from the old value to the new value. This is what you should plot
1009      * in any custom XYChart implementations.
1010      */
1011     protected final Object getCurrentDisplayedExtraValue(Data<X,Y> item) { return item.getCurrentExtraValue(); }
1012 
1013     /**
1014      * Set the current displayed data extra value.
1015      *
1016      * @param item The XYChart.Data item from which the current extra value is obtained.
1017      * @see #getCurrentDisplayedExtraValue
1018      */
1019     protected final void setCurrentDisplayedExtraValue(Data<X,Y> item, Object value) { item.setCurrentExtraValue(value); }
1020 
1021     /**
1022      * The current displayed extra value property.
1023      *
1024      * @param item The XYChart.Data item from which the current extra value property object is obtained.
1025      * @return ObjectProperty<Object> The current extra value ObjectProperty
1026      * @see #getCurrentDisplayedExtraValue
1027      */
1028     protected final ObjectProperty<Object> currentDisplayedExtraValueProperty(Data<X,Y> item) { return item.currentExtraValueProperty(); }
1029 
1030     /**
1031      * XYChart maintains a list of all items currently displayed this includes all current data + any data items
1032      * recently deleted that are in the process of being faded out. This creates and returns a iterator over
1033      * that list. This is what implementations of XYChart should use when plotting data.
1034      *
1035      * @param series The series to get displayed data for
1036      * @return iterator over currently displayed items from this series
1037      */
1038     protected final Iterator<Data<X,Y>> getDisplayedDataIterator(final Series<X,Y> series) {
1039         return Collections.unmodifiableList(series.displayedData).iterator();
1040     }
1041 
1042     /**
1043      * This should be called from dataItemRemoved() when you are finished with any animation for deleting the item from the
1044      * chart. It will remove the data item from showing up in the Iterator returned by getDisplayedDataIterator().
1045      *
1046      * @param series The series to remove
1047      * @param item   The item to remove from series's display list
1048      */
1049     protected final void removeDataItemFromDisplay(Series<X, Y> series, Data<X, Y> item) {
1050         series.removeDataItemRef(item);
1051     }
1052 
1053     // -------------- STYLESHEET HANDLING ------------------------------------------------------------------------------
1054 
1055     private static class StyleableProperties {
1056         private static final CssMetaData<XYChart<?,?>,Boolean> HORIZONTAL_GRID_LINE_VISIBLE =
1057             new CssMetaData<XYChart<?,?>,Boolean>("-fx-horizontal-grid-lines-visible",
1058                 BooleanConverter.getInstance(), Boolean.TRUE) {
1059 
1060             @Override
1061             public boolean isSettable(XYChart<?,?> node) {
1062                 return node.horizontalGridLinesVisible == null ||
1063                         !node.horizontalGridLinesVisible.isBound();
1064             }
1065 
1066             @Override
1067             public StyleableProperty<Boolean> getStyleableProperty(XYChart<?,?> node) {
1068                 return (StyleableProperty<Boolean>)(WritableValue<Boolean>)node.horizontalGridLinesVisibleProperty();
1069             }
1070         };
1071 
1072         private static final CssMetaData<XYChart<?,?>,Boolean> HORIZONTAL_ZERO_LINE_VISIBLE =
1073             new CssMetaData<XYChart<?,?>,Boolean>("-fx-horizontal-zero-line-visible",
1074                 BooleanConverter.getInstance(), Boolean.TRUE) {
1075 
1076             @Override
1077             public boolean isSettable(XYChart<?,?> node) {
1078                 return node.horizontalZeroLineVisible == null ||
1079                         !node.horizontalZeroLineVisible.isBound();
1080             }
1081 
1082             @Override
1083             public StyleableProperty<Boolean> getStyleableProperty(XYChart<?,?> node) {
1084                 return (StyleableProperty<Boolean>)(WritableValue<Boolean>)node.horizontalZeroLineVisibleProperty();
1085             }
1086         };
1087 
1088         private static final CssMetaData<XYChart<?,?>,Boolean> ALTERNATIVE_ROW_FILL_VISIBLE =
1089             new CssMetaData<XYChart<?,?>,Boolean>("-fx-alternative-row-fill-visible",
1090                 BooleanConverter.getInstance(), Boolean.TRUE) {
1091 
1092             @Override
1093             public boolean isSettable(XYChart<?,?> node) {
1094                 return node.alternativeRowFillVisible == null ||
1095                         !node.alternativeRowFillVisible.isBound();
1096             }
1097 
1098             @Override
1099             public StyleableProperty<Boolean> getStyleableProperty(XYChart<?,?> node) {
1100                 return (StyleableProperty<Boolean>)(WritableValue<Boolean>)node.alternativeRowFillVisibleProperty();
1101             }
1102         };
1103 
1104         private static final CssMetaData<XYChart<?,?>,Boolean> VERTICAL_GRID_LINE_VISIBLE =
1105             new CssMetaData<XYChart<?,?>,Boolean>("-fx-vertical-grid-lines-visible",
1106                 BooleanConverter.getInstance(), Boolean.TRUE) {
1107 
1108             @Override
1109             public boolean isSettable(XYChart<?,?> node) {
1110                 return node.verticalGridLinesVisible == null ||
1111                         !node.verticalGridLinesVisible.isBound();
1112             }
1113 
1114             @Override
1115             public StyleableProperty<Boolean> getStyleableProperty(XYChart<?,?> node) {
1116                 return (StyleableProperty<Boolean>)(WritableValue<Boolean>)node.verticalGridLinesVisibleProperty();
1117             }
1118         };
1119 
1120         private static final CssMetaData<XYChart<?,?>,Boolean> VERTICAL_ZERO_LINE_VISIBLE =
1121             new CssMetaData<XYChart<?,?>,Boolean>("-fx-vertical-zero-line-visible",
1122                 BooleanConverter.getInstance(), Boolean.TRUE) {
1123 
1124             @Override
1125             public boolean isSettable(XYChart<?,?> node) {
1126                 return node.verticalZeroLineVisible == null ||
1127                         !node.verticalZeroLineVisible.isBound();
1128             }
1129 
1130             @Override
1131             public StyleableProperty<Boolean> getStyleableProperty(XYChart<?,?> node) {
1132                 return (StyleableProperty<Boolean>)(WritableValue<Boolean>)node.verticalZeroLineVisibleProperty();
1133             }
1134         };
1135 
1136         private static final CssMetaData<XYChart<?,?>,Boolean> ALTERNATIVE_COLUMN_FILL_VISIBLE =
1137             new CssMetaData<XYChart<?,?>,Boolean>("-fx-alternative-column-fill-visible",
1138                 BooleanConverter.getInstance(), Boolean.TRUE) {
1139 
1140             @Override
1141             public boolean isSettable(XYChart<?,?> node) {
1142                 return node.alternativeColumnFillVisible == null ||
1143                         !node.alternativeColumnFillVisible.isBound();
1144             }
1145 
1146             @Override
1147             public StyleableProperty<Boolean> getStyleableProperty(XYChart<?,?> node) {
1148                 return (StyleableProperty<Boolean>)(WritableValue<Boolean>)node.alternativeColumnFillVisibleProperty();
1149             }
1150         };
1151 
1152         private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;
1153         static {
1154             final List<CssMetaData<? extends Styleable, ?>> styleables =
1155                 new ArrayList<CssMetaData<? extends Styleable, ?>>(Chart.getClassCssMetaData());
1156             styleables.add(HORIZONTAL_GRID_LINE_VISIBLE);
1157             styleables.add(HORIZONTAL_ZERO_LINE_VISIBLE);
1158             styleables.add(ALTERNATIVE_ROW_FILL_VISIBLE);
1159             styleables.add(VERTICAL_GRID_LINE_VISIBLE);
1160             styleables.add(VERTICAL_ZERO_LINE_VISIBLE);
1161             styleables.add(ALTERNATIVE_COLUMN_FILL_VISIBLE);
1162             STYLEABLES = Collections.unmodifiableList(styleables);
1163         }
1164     }
1165 
1166     /**
1167      * @return The CssMetaData associated with this class, which may include the
1168      * CssMetaData of its super classes.
1169      * @since JavaFX 8.0
1170      */
1171     public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
1172         return StyleableProperties.STYLEABLES;
1173     }
1174 
1175     /**
1176      * {@inheritDoc}
1177      * @since JavaFX 8.0
1178      */
1179     @Override
1180     public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() {
1181         return getClassCssMetaData();
1182     }
1183 
1184     // -------------- INNER CLASSES ------------------------------------------------------------------------------------
1185 
1186     /**
1187      * A single data item with data for 2 axis charts
1188      * @since JavaFX 2.0
1189      */
1190     public final static class Data<X,Y> {
1191         // -------------- PUBLIC PROPERTIES ----------------------------------------
1192 
1193         private boolean setToRemove = false;
1194         /** The series this data belongs to */
1195         private Series<X,Y> series;
1196         void setSeries(Series<X,Y> series) {
1197             this.series = series;
1198         }
1199 
1200         /** The generic data value to be plotted on the X axis */
1201         private ObjectProperty<X> xValue = new SimpleObjectProperty<X>(Data.this, "XValue") {
1202             @Override protected void invalidated() {
1203                 if (series!=null) {
1204                     XYChart<X,Y> chart = series.getChart();
1205                     if(chart!=null) chart.dataValueChanged(Data.this, get(), currentXProperty());
1206                 } else {
1207                     // data has not been added to series yet :
1208                     // so currentX and X should be the same
1209                     setCurrentX(get());
1210                 }
1211             }
1212         };
1213         /**
1214          * Gets the generic data value to be plotted on the X axis.
1215          * @return the generic data value to be plotted on the X axis.
1216          */
1217         public final X getXValue() { return xValue.get(); }
1218         /**
1219          * Sets the generic data value to be plotted on the X axis.
1220          * @param value the generic data value to be plotted on the X axis.
1221          */
1222         public final void setXValue(X value) {
1223             xValue.set(value);
1224             // handle the case where this is a init because the default constructor was used
1225             // and the case when series is not associated to a chart due to a remove series
1226             if (currentX.get() == null ||
1227                     (series != null && series.getChart() == null)) currentX.setValue(value);
1228         }
1229         /**
1230          * The generic data value to be plotted on the X axis.
1231          * @return The XValue property
1232          */
1233         public final ObjectProperty<X> XValueProperty() { return xValue; }
1234 
1235         /** The generic data value to be plotted on the Y axis */
1236         private ObjectProperty<Y> yValue = new SimpleObjectProperty<Y>(Data.this, "YValue") {
1237             @Override protected void invalidated() {
1238                 if (series!=null) {
1239                     XYChart<X,Y> chart = series.getChart();
1240                     if(chart!=null) chart.dataValueChanged(Data.this, get(), currentYProperty());
1241                 } else {
1242                     // data has not been added to series yet :
1243                     // so currentY and Y should be the same
1244                     setCurrentY(get());
1245                 }
1246             }
1247         };
1248         /**
1249          * Gets the generic data value to be plotted on the Y axis.
1250          * @return the generic data value to be plotted on the Y axis.
1251          */
1252         public final Y getYValue() { return yValue.get(); }
1253         /**
1254          * Sets the generic data value to be plotted on the Y axis.
1255          * @param value the generic data value to be plotted on the Y axis.
1256          */
1257         public final void setYValue(Y value) {
1258             yValue.set(value);
1259             // handle the case where this is a init because the default constructor was used
1260             // and the case when series is not associated to a chart due to a remove series
1261             if (currentY.get() == null ||
1262                     (series != null && series.getChart() == null)) currentY.setValue(value);
1263 
1264         }
1265         /**
1266          * The generic data value to be plotted on the Y axis.
1267          * @return the YValue property
1268          */
1269         public final ObjectProperty<Y> YValueProperty() { return yValue; }
1270 
1271         /**
1272          * The generic data value to be plotted in any way the chart needs. For example used as the radius
1273          * for BubbleChart.
1274          */
1275         private ObjectProperty<Object> extraValue = new SimpleObjectProperty<Object>(Data.this, "extraValue") {
1276             @Override protected void invalidated() {
1277                 if (series!=null) {
1278                     XYChart<X,Y> chart = series.getChart();
1279                     if(chart!=null) chart.dataValueChanged(Data.this, get(), currentExtraValueProperty());
1280                 }
1281             }
1282         };
1283         public final Object getExtraValue() { return extraValue.get(); }
1284         public final void setExtraValue(Object value) { extraValue.set(value); }
1285         public final ObjectProperty<Object> extraValueProperty() { return extraValue; }
1286 
1287         /**
1288          * The node to display for this data item. You can either create your own node and set it on the data item
1289          * before you add the item to the chart. Otherwise the chart will create a node for you that has the default
1290          * representation for the chart type. This node will be set as soon as the data is added to the chart. You can
1291          * then get it to add mouse listeners etc. Charts will do their best to position and size the node
1292          * appropriately, for example on a Line or Scatter chart this node will be positioned centered on the data
1293          * values position. For a bar chart this is positioned and resized as the bar for this data item.
1294          */
1295         private ObjectProperty<Node> node = new SimpleObjectProperty<Node>(this, "node") {
1296             protected void invalidated() {
1297                 Node node = get();
1298                 if (node != null) {
1299                     node.accessibleTextProperty().unbind();
1300                     node.accessibleTextProperty().bind(new StringBinding() {
1301                         {bind(currentXProperty(), currentYProperty());}
1302                         @Override protected String computeValue() {
1303                             String seriesName = series != null ? series.getName() : "";
1304                             return seriesName + " X Axis is " + getCurrentX() + " Y Axis is " + getCurrentY();
1305                         }
1306                     });
1307                 }
1308             };
1309         };
1310         public final Node getNode() { return node.get(); }
1311         public final void setNode(Node value) { node.set(value); }
1312         public final ObjectProperty<Node> nodeProperty() { return node; }
1313 
1314         /**
1315          * The current displayed data value plotted on the X axis. This may be the same as xValue or different. It is
1316          * used by XYChart to animate the xValue from the old value to the new value. This is what you should plot
1317          * in any custom XYChart implementations. Some XYChart chart implementations such as LineChart also use this
1318          * to animate when data is added or removed.
1319          */
1320         private ObjectProperty<X> currentX = new SimpleObjectProperty<X>(this, "currentX");
1321         final X getCurrentX() { return currentX.get(); }
1322         final void setCurrentX(X value) { currentX.set(value); }
1323         final ObjectProperty<X> currentXProperty() { return currentX; }
1324 
1325         /**
1326          * The current displayed data value plotted on the Y axis. This may be the same as yValue or different. It is
1327          * used by XYChart to animate the yValue from the old value to the new value. This is what you should plot
1328          * in any custom XYChart implementations. Some XYChart chart implementations such as LineChart also use this
1329          * to animate when data is added or removed.
1330          */
1331         private ObjectProperty<Y> currentY = new SimpleObjectProperty<Y>(this, "currentY");
1332         final Y getCurrentY() { return currentY.get(); }
1333         final void setCurrentY(Y value) { currentY.set(value); }
1334         final ObjectProperty<Y> currentYProperty() { return currentY; }
1335 
1336         /**
1337          * The current displayed data extra value. This may be the same as extraValue or different. It is
1338          * used by XYChart to animate the extraValue from the old value to the new value. This is what you should plot
1339          * in any custom XYChart implementations.
1340          */
1341         private ObjectProperty<Object> currentExtraValue = new SimpleObjectProperty<Object>(this, "currentExtraValue");
1342         final Object getCurrentExtraValue() { return currentExtraValue.getValue(); }
1343         final void setCurrentExtraValue(Object value) { currentExtraValue.setValue(value); }
1344         final ObjectProperty<Object> currentExtraValueProperty() { return currentExtraValue; }
1345 
1346         // -------------- CONSTRUCTOR -------------------------------------------------
1347 
1348         /**
1349          * Creates an empty XYChart.Data object.
1350          */
1351         public Data() {}
1352 
1353         /**
1354          * Creates an instance of XYChart.Data object and initializes the X,Y
1355          * data values.
1356          *
1357          * @param xValue The X axis data value
1358          * @param yValue The Y axis data value
1359          */
1360         public Data(X xValue, Y yValue) {
1361             setXValue(xValue);
1362             setYValue(yValue);
1363             setCurrentX(xValue);
1364             setCurrentY(yValue);
1365         }
1366 
1367         /**
1368          * Creates an instance of XYChart.Data object and initializes the X,Y
1369          * data values and extraValue.
1370          *
1371          * @param xValue The X axis data value.
1372          * @param yValue The Y axis data value.
1373          * @param extraValue Chart extra value.
1374          */
1375         public Data(X xValue, Y yValue, Object extraValue) {
1376             setXValue(xValue);
1377             setYValue(yValue);
1378             setExtraValue(extraValue);
1379             setCurrentX(xValue);
1380             setCurrentY(yValue);
1381             setCurrentExtraValue(extraValue);
1382         }
1383 
1384         // -------------- PUBLIC METHODS ----------------------------------------------
1385 
1386         /**
1387          * Returns a string representation of this {@code Data} object.
1388          * @return a string representation of this {@code Data} object.
1389          */
1390         @Override public String toString() {
1391             return "Data["+getXValue()+","+getYValue()+","+getExtraValue()+"]";
1392         }
1393 
1394     }
1395 
1396     /**
1397      * A named series of data items
1398      * @since JavaFX 2.0
1399      */
1400     public static final class Series<X,Y> {
1401 
1402         // -------------- PRIVATE PROPERTIES ----------------------------------------
1403 
1404         /** the style class for default color for this series */
1405         String defaultColorStyleClass;
1406         boolean setToRemove = false;
1407 
1408         private List<Data<X, Y>> displayedData = new ArrayList<>();
1409 
1410         private final ListChangeListener<Data<X,Y>> dataChangeListener = new ListChangeListener<Data<X, Y>>() {
1411             @Override public void onChanged(Change<? extends Data<X, Y>> c) {
1412                 ObservableList<? extends Data<X, Y>> data = c.getList();
1413                 final XYChart<X, Y> chart = getChart();
1414                 while (c.next()) {
1415                     if (chart != null) {
1416                         // RT-25187 Probably a sort happened, just reorder the pointers and return.
1417                         if (c.wasPermutated()) {
1418                             displayedData.sort((o1, o2) -> data.indexOf(o2) - data.indexOf(o1));
1419                             return;
1420                         }
1421 
1422                         Set<Data<X, Y>> dupCheck = new HashSet<>(displayedData);
1423                         dupCheck.removeAll(c.getRemoved());
1424                         for (Data<X, Y> d : c.getAddedSubList()) {
1425                             if (!dupCheck.add(d)) {
1426                                 throw new IllegalArgumentException("Duplicate data added");
1427                             }
1428                         }
1429 
1430                         // update data items reference to series
1431                         for (Data<X, Y> item : c.getRemoved()) {
1432                             item.setToRemove = true;
1433                         }
1434 
1435                         if (c.getAddedSize() > 0) {
1436                             for (Data<X, Y> itemPtr : c.getAddedSubList()) {
1437                                 if (itemPtr.setToRemove) {
1438                                     if (chart != null) chart.dataBeingRemovedIsAdded(itemPtr, Series.this);
1439                                     itemPtr.setToRemove = false;
1440                                 }
1441                             }
1442 
1443                             for (Data<X, Y> d : c.getAddedSubList()) {
1444                                 d.setSeries(Series.this);
1445                             }
1446                             if (c.getFrom() == 0) {
1447                                 displayedData.addAll(0, c.getAddedSubList());
1448                             } else {
1449                                 displayedData.addAll(displayedData.indexOf(data.get(c.getFrom() - 1)) + 1, c.getAddedSubList());
1450                             }
1451                         }
1452                         // inform chart
1453                         chart.dataItemsChanged(Series.this,
1454                                 (List<Data<X, Y>>) c.getRemoved(), c.getFrom(), c.getTo(), c.wasPermutated());
1455                     } else {
1456                         Set<Data<X, Y>> dupCheck = new HashSet<>();
1457                         for (Data<X, Y> d : data) {
1458                             if (!dupCheck.add(d)) {
1459                                 throw new IllegalArgumentException("Duplicate data added");
1460                             }
1461                         }
1462 
1463                         for (Data<X, Y> d : c.getAddedSubList()) {
1464                             d.setSeries(Series.this);
1465                         }
1466 
1467                     }
1468                 }
1469             }
1470         };
1471 
1472         // -------------- PUBLIC PROPERTIES ----------------------------------------
1473 
1474         /** Reference to the chart this series belongs to */
1475         private final ReadOnlyObjectWrapper<XYChart<X,Y>> chart = new ReadOnlyObjectWrapper<XYChart<X,Y>>(this, "chart") {
1476             @Override
1477             protected void invalidated() {
1478                 if (get() == null) {
1479                     displayedData.clear();
1480                 } else {
1481                     displayedData.addAll(getData());
1482                 }
1483             }
1484         };
1485         public final XYChart<X,Y> getChart() { return chart.get(); }
1486         private void setChart(XYChart<X,Y> value) { chart.set(value); }
1487         public final ReadOnlyObjectProperty<XYChart<X,Y>> chartProperty() { return chart.getReadOnlyProperty(); }
1488 
1489         /** The user displayable name for this series */
1490         private final StringProperty name = new StringPropertyBase() {
1491             @Override protected void invalidated() {
1492                 get(); // make non-lazy
1493                 if(getChart() != null) getChart().seriesNameChanged();
1494             }
1495 
1496             @Override
1497             public Object getBean() {
1498                 return Series.this;
1499             }
1500 
1501             @Override
1502             public String getName() {
1503                 return "name";
1504             }
1505         };
1506         public final String getName() { return name.get(); }
1507         public final void setName(String value) { name.set(value); }
1508         public final StringProperty nameProperty() { return name; }
1509 
1510         /**
1511          * The node to display for this series. This is created by the chart if it uses nodes to represent the whole
1512          * series. For example line chart uses this for the line but scatter chart does not use it. This node will be
1513          * set as soon as the series is added to the chart. You can then get it to add mouse listeners etc.
1514          */
1515         private ObjectProperty<Node> node = new SimpleObjectProperty<Node>(this, "node");
1516         public final Node getNode() { return node.get(); }
1517         public final void setNode(Node value) { node.set(value); }
1518         public final ObjectProperty<Node> nodeProperty() { return node; }
1519 
1520         /** ObservableList of data items that make up this series */
1521         private final ObjectProperty<ObservableList<Data<X,Y>>> data = new ObjectPropertyBase<ObservableList<Data<X,Y>>>() {
1522             private ObservableList<Data<X,Y>> old;
1523             @Override protected void invalidated() {
1524                 final ObservableList<Data<X,Y>> current = getValue();
1525                 // add remove listeners
1526                 if(old != null) old.removeListener(dataChangeListener);
1527                 if(current != null) current.addListener(dataChangeListener);
1528                 // fire data change event if series are added or removed
1529                 if(old != null || current != null) {
1530                     final List<Data<X,Y>> removed = (old != null) ? old : Collections.<Data<X,Y>>emptyList();
1531                     final int toIndex = (current != null) ? current.size() : 0;
1532                     // let data listener know all old data have been removed and new data that has been added
1533                     if (toIndex > 0 || !removed.isEmpty()) {
1534                         dataChangeListener.onChanged(new NonIterableChange<Data<X,Y>>(0, toIndex, current){
1535                             @Override public List<Data<X,Y>> getRemoved() { return removed; }
1536 
1537                             @Override protected int[] getPermutation() {
1538                                 return new int[0];
1539                             }
1540                         });
1541                     }
1542                 } else if (old != null && old.size() > 0) {
1543                     // let series listener know all old series have been removed
1544                     dataChangeListener.onChanged(new NonIterableChange<Data<X,Y>>(0, 0, current){
1545                         @Override public List<Data<X,Y>> getRemoved() { return old; }
1546                         @Override protected int[] getPermutation() {
1547                             return new int[0];
1548                         }
1549                     });
1550                 }
1551                 old = current;
1552             }
1553 
1554             @Override
1555             public Object getBean() {
1556                 return Series.this;
1557             }
1558 
1559             @Override
1560             public String getName() {
1561                 return "data";
1562             }
1563         };
1564         public final ObservableList<Data<X,Y>> getData() { return data.getValue(); }
1565         public final void setData(ObservableList<Data<X,Y>> value) { data.setValue(value); }
1566         public final ObjectProperty<ObservableList<Data<X,Y>>> dataProperty() { return data; }
1567 
1568         // -------------- CONSTRUCTORS ----------------------------------------------
1569 
1570         /**
1571          * Construct a empty series
1572          */
1573         public Series() {
1574             this(FXCollections.<Data<X,Y>>observableArrayList());
1575         }
1576 
1577         /**
1578          * Constructs a Series and populates it with the given {@link ObservableList} data.
1579          *
1580          * @param data ObservableList of XYChart.Data
1581          */
1582         public Series(ObservableList<Data<X,Y>> data) {
1583             setData(data);
1584             for(Data<X,Y> item:data) item.setSeries(this);
1585         }
1586 
1587         /**
1588          * Constructs a named Series and populates it with the given {@link ObservableList} data.
1589          *
1590          * @param name a name for the series
1591          * @param data ObservableList of XYChart.Data
1592          */
1593         public Series(String name, ObservableList<Data<X,Y>> data) {
1594             this(data);
1595             setName(name);
1596         }
1597 
1598         // -------------- PUBLIC METHODS ----------------------------------------------
1599 
1600         /**
1601          * Returns a string representation of this {@code Series} object.
1602          * @return a string representation of this {@code Series} object.
1603          */
1604         @Override public String toString() {
1605             return "Series["+getName()+"]";
1606         }
1607 
1608         // -------------- PRIVATE/PROTECTED METHODS -----------------------------------
1609 
1610         /*
1611          * The following methods are for manipulating the pointers in the linked list
1612          * when data is deleted.
1613          */
1614         private void removeDataItemRef(Data<X,Y> item) {
1615             if (item != null) item.setToRemove = false;
1616             displayedData.remove(item);
1617         }
1618 
1619         int getItemIndex(Data<X,Y> item) {
1620             return displayedData.indexOf(item);
1621         }
1622 
1623         Data<X, Y> getItem(int i) {
1624             return displayedData.get(i);
1625         }
1626 
1627         int getDataSize() {
1628             return displayedData.size();
1629         }
1630     }
1631 
1632 }