-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimelineChart.java
455 lines (373 loc) · 15.3 KB
/
TimelineChart.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package Timeline;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javafx.beans.NamedArg;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.Axis;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.effect.Bloom;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ValueAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import wormguides.controllers.RootLayoutController;
import wormguides.stories.Note;
import wormguides.stories.Story;
import wormguides.layers.StoriesLayer;
import wormguides.resources.ProductionInfo;
/**
* Created by mcmullic on 7/11/2017.
*/
/**
*
* This class is a graphical timeline of (@link Note)s. It displays the active Story and it's notes for each cell.
*
*/
public class TimelineChart<X,Y> extends XYChart<X,Y> {
/**
* Class that gets the length of each block
*/
public static class ExtraData {
public long length;
public String styleClass;
public ExtraData(long length, String styleClass) {
super();
this.length = length;
this.styleClass = styleClass;
}
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
public void setStyleClass(String styleClass) {
this.styleClass = styleClass;
}
public String getStyleClass() {
return styleClass;
}
}
private double blockHeight = 10;
/**
*
* @param xAxis
* @param yAxis
*/
public TimelineChart(@NamedArg("xAxis") Axis<X> xAxis, @NamedArg("yAxis") Axis<Y> yAxis) {
this(xAxis, yAxis, FXCollections.<Series<X, Y>>observableArrayList());
}
/**
*
* @param xAxis
* @param yAxis
* @param data
*/
public TimelineChart(@NamedArg("xAxis") Axis<X> xAxis, @NamedArg("yAxis") Axis<Y> yAxis, @NamedArg("data") ObservableList<Series<X, Y>> data) {
super(xAxis, yAxis);
if (!(xAxis instanceof ValueAxis && yAxis instanceof CategoryAxis)) {
throw new IllegalArgumentException("Axis type incorrect");
}
setData(data);
}
/**
*
* @param obj
* @return
*/
private static String getStyleClass(Object obj) {
return ((ExtraData) obj).getStyleClass();
}
private static double getLength(Object obj) {
return ((ExtraData) obj).getLength();
}
/**
* method to plot the bars for each cell
*/
@Override
protected void layoutPlotChildren() {
for (int seriesIndex = 0; seriesIndex < getData().size(); seriesIndex++) {
Series<X, Y> series = getData().get(seriesIndex);
Iterator<Data<X, Y>> iter = getDisplayedDataIterator(series);
while (iter.hasNext()) {
Data<X, Y> item = iter.next();
double x = getXAxis().getDisplayPosition(item.getXValue());
double y = getYAxis().getDisplayPosition(item.getYValue());
if (Double.isNaN(x) || Double.isNaN(y)) {
continue;
}
Node block = item.getNode();
Rectangle ellipse;
if (block != null) {
if (block instanceof StackPane) {
StackPane region = (StackPane) item.getNode();
if (region.getShape() == null) {
ellipse = new Rectangle(getLength(item.getExtraValue()), getBlockHeight());
} else if (region.getShape() instanceof Rectangle) {
ellipse = (Rectangle) region.getShape();
} else {
return;
}
ellipse.setWidth(getLength(item.getExtraValue()) * ((getXAxis() instanceof NumberAxis) ? Math.abs(((NumberAxis) getXAxis()).getScale()) : 1));
ellipse.setHeight(getBlockHeight() * ((getYAxis() instanceof NumberAxis) ? Math.abs(((NumberAxis) getYAxis()).getScale()) : 1));
y -= getBlockHeight() / 2.0;
region.setShape(null);
region.setShape(ellipse);
region.setScaleShape(false);
region.setCenterShape(false);
region.setCacheShape(false);
block.setLayoutX(x);
block.setLayoutY(y);
}
}
}
}
}
/**
* Intializes timelinechart with activeStory from storiesLayer
* If no active story- return blank chart
* @param storiesLayer
* @return
*/
public static TimelineChart<Number, String> intialize(StoriesLayer storiesLayer, ProductionInfo productionInfo) {
int movieTimeOffset = productionInfo.getMovieTimeOffset();
Story activeStory = storiesLayer.getActiveStory();
if (activeStory == null) {
List<String> cellNames = new ArrayList<String>();
HashMap<String, Series> hmap = new HashMap<String, XYChart.Series>();
final NumberAxis xAxis = new NumberAxis();
xAxis.setAutoRanging(false);
xAxis.setLowerBound(250);
xAxis.setUpperBound(400);
xAxis.setLabel("Time (min)");
xAxis.setMinorTickCount(0);
final CategoryAxis yAxis = new CategoryAxis();
yAxis.setLabel("Cell/Item");
yAxis.setTickLabelGap(10);
yAxis.setCategories(FXCollections.<String>observableArrayList(cellNames));
final TimelineChart<Number, String> chart = new TimelineChart<Number, String>(xAxis, yAxis);
chart.setTitle("Timeline of Story Notes");
chart.setLegendVisible(false);
chart.setBlockHeight(15);
for (XYChart.Series thisSeries : hmap.values()) {
chart.getData().addAll(thisSeries);
}
return chart;
}
int min = 250;
int max = 0;
List<String> cellNames = new ArrayList<String>();
List<String> cellTags = new ArrayList<String>();
HashMap<String, Series> cellSeries = new HashMap<String, XYChart.Series>();
HashMap<String, String> tagMap = new HashMap<String, String>();
HashMap<XYChart.Data, Note> noteDisplay = new HashMap<XYChart.Data, Note>();
for (int i = 0; i < (activeStory.getNotes().size()); i++) {
Note note = activeStory.getNotes().get(i);
String cellName = activeStory.getNotes().get(i).getCellName();
int start = activeStory.getNotes().get(i).getStartTime() + movieTimeOffset;
int end = activeStory.getNotes().get(i).getEndTime() + movieTimeOffset;
if (start < min && !(start < 0)){
min = start;
}
if (end > max){
max = end;
}
if (cellSeries.containsKey(cellName)) {
XYChart.Series currentSeries = cellSeries.get(cellName);
if (end - start == 0) {
XYChart.Data d = (new XYChart.Data(start, tagMap.get(cellName), new TimelineChart.ExtraData((end - start) + 1, "status-purple")));
currentSeries.getData().add(d);
noteDisplay.put(d, note);
} else {
XYChart.Data d = new XYChart.Data(start, tagMap.get(cellName), new TimelineChart.ExtraData((end - start) + 1, "status-lightPurple"));
currentSeries.getData().add(d);
noteDisplay.put(d,note);
}
} else {
cellNames.add(cellName);
String cellTag = activeStory.getNotes().get(i).getTagName();
cellTags.add(cellTag);
tagMap.put(cellName, cellTag);
XYChart.Series currentSeries = new XYChart.Series();
currentSeries.getData().add(new XYChart.Data(200, tagMap.get(cellName), new TimelineChart.ExtraData(300, "status-lightGray")));
if (end - start == 0) {
XYChart.Data d = new XYChart.Data(start, tagMap.get(cellName), new TimelineChart.ExtraData((end - start) + 1, "status-purple"));
currentSeries.getData().add(d);
noteDisplay.put(d,note);
} else {
XYChart.Data d = new XYChart.Data(start, tagMap.get(cellName), new TimelineChart.ExtraData((end - start) + 1, "status-lightPurple"));
currentSeries.getData().add(d);
noteDisplay.put(d,note);
}
cellSeries.put(cellName, currentSeries);
}
}
final NumberAxis xAxis = new NumberAxis();
xAxis.setAutoRanging(false);
xAxis.setLowerBound(min-10);
xAxis.setUpperBound(max+10);
xAxis.setLabel("Time (min)");
xAxis.setMinorTickCount(0);
final CategoryAxis yAxis = new CategoryAxis();
yAxis.setLabel("Cell/ Item");
yAxis.setTickLabelGap(10);
yAxis.setCategories(FXCollections.<String>observableArrayList(cellTags));
final TimelineChart<Number, String> chart = new TimelineChart<Number, String>(xAxis, yAxis);
chart.setTitle("Timeline of Story Notes");
chart.setLegendVisible(false);
chart.setBlockHeight(15);
for (XYChart.Series thisSeries : cellSeries.values()) {
chart.getData().addAll(thisSeries);
}
for (XYChart.Series<Number, String> s : chart.getData()) {
for (XYChart.Data<Number, String> d : s.getData()) {
for (int i = 0; i < (activeStory.getNotes().size()); i++) {
final Note note = activeStory.getNotes().get(i);
int start = note.getStartTime() + movieTimeOffset;
int end = note.getEndTime()+movieTimeOffset;
if (noteDisplay.get(d) == note) {
Tooltip.install(d.getNode(), new Tooltip(
Integer.valueOf(start) + "-" + Integer.valueOf(end) + "\n" + note.getTagName() + "\n" + note.getTagContents()));
// add effect
Bloom bloom = new Bloom();
d.getNode().setEffect(bloom);
//Adding class on hover
d.getNode().setOnMouseEntered(event -> d.getNode().getStyleClass().add("onHover"));
//Removing class on exit
d.getNode().setOnMouseExited(event -> d.getNode().getStyleClass().remove("onHover"));
// rebuild 3d window on node pressed
d.getNode().setOnMousePressed(event -> storiesLayer.setActiveNoteWithSubsceneRebuild(note));
}
}
}
}
return chart;
}
/**
* getter and setter for height of bars on graph
* @return
*/
public double getBlockHeight() {
return blockHeight;
}
public void setBlockHeight(double blockHeight) {
this.blockHeight = blockHeight;
}
/**
*
* @param series
* @param itemIndex
* @param item
*/
@Override
protected void dataItemAdded(Series<X, Y> series, int itemIndex, Data<X, Y> item) {
Node block = createContainer(series, getData().indexOf(series), item, itemIndex);
getPlotChildren().add(block);
}
/**
*
* @param item
* @param series
*/
@Override
protected void dataItemRemoved(final Data<X, Y> item, final Series<X, Y> series) {
final Node block = item.getNode();
getPlotChildren().remove(block);
removeDataItemFromDisplay(series, item);
}
/**
*
* @param item
*/
@Override
protected void dataItemChanged(Data<X, Y> item) {
}
/**
*
* @param series
* @param seriesIndex
*/
@Override
protected void seriesAdded(Series<X, Y> series, int seriesIndex) {
for (int j = 0; j < series.getData().size(); j++) {
Data<X, Y> item = series.getData().get(j);
Node container = createContainer(series, seriesIndex, item, j);
getPlotChildren().add(container);
}
}
/**
*
* @param series
*/
@Override
protected void seriesRemoved(final Series<X, Y> series) {
for (XYChart.Data<X, Y> d : series.getData()) {
final Node container = d.getNode();
getPlotChildren().remove(container);
}
removeSeriesFromDisplay(series);
}
/**
*
* @param series
* @param seriesIndex
* @param item
* @param itemIndex
* @return
*/
private Node createContainer(Series<X, Y> series, int seriesIndex, final Data<X, Y> item, int itemIndex) {
Node container = item.getNode();
if (container == null) {
container = new StackPane();
item.setNode(container);
}
container.getStyleClass().add(getStyleClass(item.getExtraValue()));
return container;
}
/**
* used to update x and y axis when new story/note is added
*/
@Override
protected void updateAxisRange() {
final Axis<X> xa = getXAxis();
final Axis<Y> ya = getYAxis();
List<X> xData = null;
List<Y> yData = null;
if (xa.isAutoRanging()) xData = new ArrayList<X>();
if (ya.isAutoRanging()) yData = new ArrayList<Y>();
if (xData != null || yData != null) {
for (Series<X, Y> series : getData()) {
for (Data<X, Y> data : series.getData()) {
if (xData != null) {
xData.add(data.getXValue());
xData.add(xa.toRealValue(xa.toNumericValue(data.getXValue()) + getLength(data.getExtraValue())));
}
if (yData != null) {
yData.add(data.getYValue());
}
}
}
if (xData != null) xa.invalidateRange(xData);
if (yData != null) ya.invalidateRange(yData);
}
}
/**
* rebuilds timeline when new note or story is active
* @Window3DController
* @param chart
* @param timelineStage
*/
public void rebuildTimeline(TimelineChart chart, Stage timelineStage) {
chart.getStylesheets().add(getClass().getResource("/Timeline/chart.css").toExternalForm());
Scene scene = new Scene(chart, 1500, 700);
timelineStage.setScene(scene);
}
}