36
36
import javafx .scene .Group ;
37
37
import javafx .scene .Node ;
38
38
import javafx .scene .chart .Axis ;
39
+ import javafx .scene .chart .LineChart .SortingPolicy ;
40
+ import javafx .scene .chart .NumberAxis ;
39
41
import javafx .scene .chart .XYChart ;
40
42
import javafx .scene .control .Label ;
41
43
import javafx .scene .effect .ColorAdjust ;
42
44
import javafx .scene .input .MouseEvent ;
43
- import javafx .scene .chart .LineChart .SortingPolicy ;
44
- import javafx .scene .chart .NumberAxis ;
45
45
import javafx .scene .layout .Background ;
46
46
import javafx .scene .layout .BackgroundFill ;
47
47
import javafx .scene .layout .CornerRadii ;
48
48
import javafx .scene .layout .StackPane ;
49
- import javafx .scene .paint .Color ;
50
49
import javafx .scene .shape .ClosePath ;
51
50
import javafx .scene .shape .LineTo ;
52
51
import javafx .scene .shape .MoveTo ;
@@ -76,7 +75,7 @@ public enum SeriesType {
76
75
77
76
// Global chart properties
78
77
private static final String COLOR_CSS_CLASS = "default-color" ;
79
- private static final String LINE_CHART_LINE_CSS_CLASS = "chart- series-line" ;
78
+ private static final String LINE_CHART_LINE_CSS_CLASS = "series-line" ;
80
79
private static final String LINE_CHART_SYMBOL_CSS_CLASS = "chart-line-symbol" ;
81
80
private static final String SCATTER_CHART_SYMBOL_CSS_CLASS = "chart-symbol" ;
82
81
@@ -93,17 +92,18 @@ public enum SeriesType {
93
92
94
93
protected HashMap <Series <X , Y >, TypedSeries <X , Y >> typedSeries = new LinkedHashMap <>();
95
94
protected HashMap <Series <X , Y >, Boolean > seriesVisibility = new HashMap <>();
95
+ protected HashMap <Series <X , Y >, SymbolType > seriesSymbolType = new HashMap <>();
96
96
97
97
/** A package private hashmap in the chart object */
98
98
protected Map <Series <X , Y >, Integer > seriesColorMap = new HashMap <>();
99
99
protected BitSet availableColors = new BitSet (8 );
100
100
101
- //TODO
101
+ // TODO
102
102
// private static final Color[] availableColorsExtended = new Color[164];
103
103
// static {
104
104
//
105
105
// }
106
-
106
+
107
107
// Area chart
108
108
/** Remember the current animation state of area charts for layout */
109
109
private Map <Series <X , Y >, DoubleProperty > seriesYMultiplierMap = new HashMap <>();
@@ -201,9 +201,6 @@ protected Insets computeValue() {
201
201
for (var entry : additionalXAxis .entrySet ()) {
202
202
double height = entry .getValue ().getHeight ();
203
203
204
- System .out .println ("X Axis: " + height + " " + entry .getValue () + " " + entry .getValue ().getWidth ()
205
- + entry .getValue ().getSide ());
206
-
207
204
switch (entry .getValue ().getSide ()) {
208
205
case TOP :
209
206
top += (height + AXIS_PADDING );
@@ -215,11 +212,8 @@ protected Insets computeValue() {
215
212
throw new IllegalStateException ("X Axis may only be positioned top or bottom" );
216
213
}
217
214
}
218
- System .out .println (top + " " + right + " " + bottom + " " + left );
219
215
return new Insets (top , right , bottom , left );
220
-
221
216
}
222
-
223
217
};
224
218
225
219
this .paddingProperty ().bind (paddingBinding );
@@ -274,6 +268,9 @@ public boolean addSeries(TypedSeries<X, Y> series) throws IllegalArgumentExcepti
274
268
275
269
// we need to change the series
276
270
Series <X , Y > ser = series .getSeries ();
271
+ SymbolType t = seriesSymbolType .get (series );
272
+ String symTypeCss = (t != null )? t .name () : "" ;
273
+
277
274
for (int itemIndex = 0 ; itemIndex < ser .getData ().size (); itemIndex ++) {
278
275
Data <X , Y > item = ser .getData ().get (itemIndex );
279
276
Node symbol = item .getNode ();
@@ -288,7 +285,7 @@ public boolean addSeries(TypedSeries<X, Y> series) throws IllegalArgumentExcepti
288
285
identifier = AREA_CHART_SYMBOL_CSS_CLASS ;
289
286
break ;
290
287
}
291
- this .createSymbols (ser , itemIndex , item , identifier );
288
+ this .createSymbols (ser , itemIndex , item , identifier , symTypeCss );
292
289
293
290
symbol = item .getNode ();
294
291
getPlotChildren ().add (symbol );
@@ -489,37 +486,47 @@ protected void seriesChanged(ListChangeListener.Change<? extends Series> c) {
489
486
490
487
String seriesColor = COLOR_CSS_CLASS + seriesColorMap .get (s );
491
488
489
+ SymbolType symType = seriesSymbolType .get (s );
490
+ String symTypeCss = (symType != null ) ? symType .name ():"" ;
491
+
492
+
492
493
switch (type .getSeriesType ()) {
493
494
case AREA :
494
495
Path seriesLine = (Path ) ((Group ) s .getNode ()).getChildren ().get (1 );
495
496
Path fillPath = (Path ) ((Group ) s .getNode ()).getChildren ().get (0 );
496
497
seriesLine .getStyleClass ().setAll (AREA_CHART_SERIES_LINE_CSS_CLASS , "series" + i , seriesColor );
497
498
fillPath .getStyleClass ().setAll (AREA_CHART_FILL_CSS_CLASS , "series" + i , seriesColor );
499
+
500
+
498
501
for (int j = 0 ; j < s .getData ().size (); j ++) {
499
502
final Data <X , Y > item = s .getData ().get (j );
500
503
final Node node = item .getNode ();
501
504
if (node != null )
502
- node .getStyleClass ().setAll (AREA_CHART_SYMBOL_CSS_CLASS , "series" + i , "data" + j , seriesColor );
505
+ node .getStyleClass ().setAll (AREA_CHART_SYMBOL_CSS_CLASS , "series" + i , "data" + j , seriesColor , symTypeCss );
503
506
}
504
507
break ;
505
508
case LINE :
506
509
Node seriesNode = s .getNode ();
507
510
if (seriesNode != null )
508
511
seriesNode .getStyleClass ().setAll (LINE_CHART_LINE_CSS_CLASS , "series" + i , seriesColor );
512
+
509
513
for (int j = 0 ; j < s .getData ().size (); j ++) {
510
514
final Node symbol = s .getData ().get (j ).getNode ();
511
515
if (symbol != null )
512
516
symbol .getStyleClass ().setAll (LINE_CHART_SYMBOL_CSS_CLASS , "series" + i , "data" + j ,
513
- seriesColor );
517
+ seriesColor , symTypeCss );
514
518
}
515
519
break ;
516
520
517
521
case SCATTER :
522
+
518
523
for (int j = 0 ; j < s .getData ().size (); j ++) {
519
524
final Node symbol = s .getData ().get (j ).getNode ();
520
- if (symbol != null )
525
+ if (symbol != null ) {
521
526
symbol .getStyleClass ().setAll (SCATTER_CHART_SYMBOL_CSS_CLASS , "series" + i , "data" + j ,
522
- seriesColor );
527
+ seriesColor ,symTypeCss );
528
+ }
529
+
523
530
}
524
531
break ;
525
532
case STACKED_AREA :
@@ -831,13 +838,20 @@ protected void layoutScatterSeries(Series<X, Y> series, Axis<X> xAxis, Axis<Y> y
831
838
// Add data item
832
839
protected void addAreaItem (Series <X , Y > series , int itemIndex , Data <X , Y > item ) {
833
840
// TODO add animation code
834
- createSymbols (series , itemIndex , item , AREA_CHART_SYMBOL_CSS_CLASS );
841
+
842
+ SymbolType t = seriesSymbolType .get (series );
843
+ String symTypeCss = (t != null )? t .name () : "" ;
844
+
845
+ createSymbols (series , itemIndex , item , AREA_CHART_SYMBOL_CSS_CLASS ,symTypeCss );
835
846
getPlotChildren ().add (item .getNode ());
836
847
}
837
848
838
849
protected void addScatterItem (Series <X , Y > series , int itemIndex , Data <X , Y > item ) {
839
850
840
- createSymbols (series , itemIndex , item , SCATTER_CHART_SYMBOL_CSS_CLASS );
851
+ SymbolType t = seriesSymbolType .get (series );
852
+ String symTypeCss = (t != null )? t .name () : "" ;
853
+
854
+ createSymbols (series , itemIndex , item , SCATTER_CHART_SYMBOL_CSS_CLASS ,symTypeCss );
841
855
Node symbol = item .getNode ();
842
856
843
857
// add and fade in new symbol if animated
@@ -854,9 +868,12 @@ protected void addScatterItem(Series<X, Y> series, int itemIndex, Data<X, Y> ite
854
868
855
869
protected void addLineItem (Series <X , Y > series , int itemIndex , Data <X , Y > item ) {
856
870
871
+ SymbolType t = seriesSymbolType .get (series );
872
+ String symTypeCss = (t != null )? t .name () : "" ;
873
+
857
874
// Create the node
858
875
if (typedSeries .get (series ).showSymbolsProperty ().get ()) {
859
- createSymbols (series , itemIndex , item , LINE_CHART_SYMBOL_CSS_CLASS );
876
+ createSymbols (series , itemIndex , item , LINE_CHART_SYMBOL_CSS_CLASS , symTypeCss );
860
877
}
861
878
Node symbol = item .getNode ();
862
879
@@ -867,7 +884,7 @@ protected void addLineItem(Series<X, Y> series, int itemIndex, Data<X, Y> item)
867
884
// TODO add animation code
868
885
}
869
886
870
- private void createSymbols (Series <X , Y > series , int itemIndex , Data <X , Y > item , String symbolCSSIdentifier ) {
887
+ private void createSymbols (Series <X , Y > series , int itemIndex , Data <X , Y > item , String symbolCSSIdentifier , String additionalCss ) {
871
888
Node symbol = item .getNode ();
872
889
// TODO should we also check here is e.g. scatter chart ticks should be drawn?
873
890
if (symbol == null ) {
@@ -880,7 +897,7 @@ private void createSymbols(Series<X, Y> series, int itemIndex, Data<X, Y> item,
880
897
881
898
if (symbol != null ) {
882
899
symbol .getStyleClass ().addAll (symbolCSSIdentifier , "series" + getData ().indexOf (series ), "data" + itemIndex ,
883
- COLOR_CSS_CLASS + seriesColorMap .get (series ));
900
+ COLOR_CSS_CLASS + seriesColorMap .get (series ), additionalCss );
884
901
}
885
902
}
886
903
@@ -1116,7 +1133,6 @@ public void setSeriesVisibility(TypedSeries series, boolean b) {
1116
1133
1117
1134
protected void toggleSeriesVisability (Series <X , Y > series , boolean b ) {
1118
1135
seriesVisibility .put (series , b );
1119
- System .out .println ("Toogle Series" );
1120
1136
this .requestChartLayout ();
1121
1137
}
1122
1138
@@ -1141,9 +1157,12 @@ protected void updateLegend() {
1141
1157
name = "Series " + seriesIndex ;
1142
1158
}
1143
1159
1160
+ SymbolType t = seriesSymbolType .get (series );
1161
+ String symTypeCss = (t != null )? t .name () : "" ;
1162
+
1144
1163
Node symbol = new StackPane ();
1145
1164
symbol .getStyleClass ().addAll ("chart-legend-symbol" , "default-color" + seriesColorMap .get (series ),
1146
- "chart-symbol" ); // This css class contains background-color of the chart. Just go ahead and use
1165
+ "chart-symbol" , symTypeCss ); // This css class contains background-color of the chart. Just go ahead and use
1147
1166
// it
1148
1167
LegendItem lItem = new LegendItem (name , symbol );
1149
1168
@@ -1259,11 +1278,66 @@ public void removeValueMarker(ValueMarker<?> markerToRemove) {
1259
1278
1260
1279
markerLock .unlock ();
1261
1280
}
1262
-
1263
1281
1282
+ /*
1283
+ * Series can be used in multiple chart. How styling is performed is bound to a
1284
+ * chart object, therefore use these functions to set the display type
1285
+ */
1264
1286
1265
-
1266
-
1267
-
1287
+ /**
1288
+ * Set the series to the specified color. The colors can be found in the
1289
+ * chart.css file.
1290
+ *
1291
+ * @param seriesIndex the index of the series
1292
+ * @param colorIndex in range of [0-168]
1293
+ */
1294
+ public void setSeriesColor (int seriesIndex , int colorIndex ) {
1295
+ // Gettings the index in a linked hashset is expensive. This won't be performed
1296
+ // many times!
1297
+ int index = 0 ;
1298
+ for (var entry : typedSeries .entrySet ()) {
1299
+ if (index ++ == seriesIndex ) {
1300
+ setSeriesColor (entry .getKey (), colorIndex );
1301
+ break ;
1302
+ }
1303
+ }
1304
+ }
1305
+
1306
+ /**
1307
+ * Set the series to the specified color. The colors can be found in the
1308
+ * chart.css file.
1309
+ *
1310
+ * @param series the series to set the color for
1311
+ * @param colorIndex in range of [0-146]
1312
+ */
1313
+ public void setSeriesColor (Series series , int colorIndex ) {
1314
+ if (series != null ) {
1315
+ seriesColorMap .put (series , colorIndex );
1316
+ legendMap .get (series );
1317
+ updateLegend ();
1318
+ this .seriesChanged (null );
1319
+ }
1320
+ }
1321
+
1322
+ public void setSeriesSymbol (int seriesIndex , SymbolType type ) {
1323
+ // Gettings the index in a linked hashset is expensive. This won't be performed
1324
+ // many times!
1325
+ int index = 0 ;
1326
+ for (var entry : typedSeries .entrySet ()) {
1327
+ if (index ++ == seriesIndex ) {
1328
+ setSeriesSymbol (entry .getKey (), type );
1329
+ break ;
1330
+ }
1331
+ }
1332
+ }
1333
+
1334
+ public void setSeriesSymbol (Series series , SymbolType type ) {
1335
+ if (series != null ) {
1336
+ seriesSymbolType .put (series ,type );
1337
+ legendMap .get (series );
1338
+ updateLegend ();
1339
+ this .seriesChanged (null );
1340
+ }
1341
+ }
1268
1342
1269
1343
}
0 commit comments