/
ChartComponentGraph.java
10486 lines (7611 loc) · 318 KB
/
ChartComponentGraph.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (C) 2005, 2023 Wolfgang Schramm and Contributors
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*******************************************************************************/
package net.tourbook.chart;
import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;
import java.io.InputStream;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import net.tourbook.chart.preferences.IChartPreferences;
import net.tourbook.common.DPITools;
import net.tourbook.common.PointLong;
import net.tourbook.common.RectangleLong;
import net.tourbook.common.UI;
import net.tourbook.common.color.ColorUtil;
import net.tourbook.common.color.ThemeUtil;
import net.tourbook.common.tooltip.IPinned_ToolTip;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.layout.PixelConverter;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.LineAttributes;
import org.eclipse.swt.graphics.Path;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Widget;
/**
* Draws the graph and axis into the canvas
*
* @author Wolfgang Schramm
*/
public class ChartComponentGraph extends Canvas {
private static final double FLOAT_ALMOST_ZERO = ChartDataYSerie.FLOAT_ALMOST_ZERO;
private static final double ZOOM_RATIO = 1.3;
private static double ZOOM_RATIO_FACTOR = ZOOM_RATIO;
private static final int BAR_MARKER_WIDTH = 16;
private static final int[] DOT_DASHES = new int[] { 1, 1 };
private static final NumberFormat _nf = NumberFormat.getNumberInstance();
private static final int[][] _leftAccelerator = new int[][] {
{ -40, -200 },
{ -30, -50 },
{ -20, -10 },
{ -10, -5 },
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!! move 2 instead of 1, with 1 it would sometimes not move, needs more investigation
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
{ 0, -2 } };
private static final int[][] _rightAccelerator = new int[][] {
{ 10, 2 },
{ 20, 5 },
{ 30, 10 },
{ 40, 50 },
{ Integer.MAX_VALUE, 200 } };
private final LineAttributes LINE_DASHED = new LineAttributes(5);
{
LINE_DASHED.dashOffset = 3;
LINE_DASHED.style = SWT.LINE_CUSTOM;
LINE_DASHED.dash = new float[] { 1f, 2f };
LINE_DASHED.width = 1f;
}
Chart _chart;
private final ChartComponents _chartComponents;
/**
* This image contains one single graph without title and x-axis with units.
* <p>
* This image was created to fix clipping bugs which occurred when gradient filling was painted
* with a path.
*/
private Image _chartImage_10_Graphs;
/**
* This image contains the chart without additional layers.
*/
private Image _chartImage_20_Chart;
/**
* Contains custom layers like the markers or tour segments which are painted in the foreground.
*/
private Image _chartImage_30_Custom;
/**
* Contains layers like the x/y sliders, x-marker, selection or hovered line/bar.
*/
private Image _chartImage_40_Overlay;
/**
*
*/
private ChartDrawingData _chartDrawingData;
/**
* drawing data which is used to draw the chart, when this list is empty, an error is displayed
*/
private ArrayList<GraphDrawingData> _allGraphDrawingData = new ArrayList<>();
private ArrayList<GraphDrawingData> _revertedGraphDrawingData;
/**
* Zoom ratio between the visible and the virtual chart width, is 1.0 when not zoomed, is > 1
* when zoomed.
*/
private double _graphZoomRatio = 1;
/**
* Contains the width for a zoomed graph this includes also the invisible parts.
*/
private long _xxDevGraphWidth;
/**
* When the graph is zoomed, the chart shows only a part of the whole graph in the viewport. This
* value contains the left border of the viewport.
*/
long _xxDevViewPortLeftBorder;
/**
* ratio for the position where the chart starts on the left side within the virtual graph width
*/
private double _zoomRatioLeftBorder;
/**
* Ratio where the mouse was double clicked, this position is used to zoom the chart with the
* mouse
*/
private double _zoomRatioCenter;
/**
* This zoom ratio is used when zooming the next time with the keyboard, when 0 this is ignored.
*/
private double _zoomRatioCenter_WithKeyboard;
/**
* when the slider is dragged and the mouse up event occurs, the graph is zoomed to the sliders
* when set to <code>true</code>
*/
boolean _canAutoZoomToSlider;
/**
* when <code>true</code> the vertical sliders will be moved to the border when the chart is
* zoomed
*/
boolean _canAutoMoveSliders;
/**
* true indicates the graph needs to be redrawn in the paint event
*/
private boolean _isChartDirty;
/**
* true indicates the slider needs to be redrawn in the paint event
*/
private boolean _isSliderDirty;
/**
* when <code>true</code> the custom layers above the graph image needs to be redrawn in the next
* paint event
*/
private boolean _isCustomLayerImageDirty;
/**
* set to <code>true</code> when the selection needs to be redrawn
*/
private boolean _isSelectionDirty;
/**
* status for the x-slider, <code>true</code> indicates, the slider is visible
*/
private boolean _isXSliderVisible;
/**
* true indicates that the y-sliders is visible
*/
private boolean _isYSliderVisible;
/**
* Is <code>true</code> when the area between the vertical x-sliders is filled with a translucent
* background color, this is used for the geo compare area.
*/
private boolean _isXSliderAreaVisible;
/*
* Left/right chart sliders
*/
private final ChartXSlider _xSliderA;
private final ChartXSlider _xSliderB;
/**
* This is set when the x-slider is being dragged, otherwise it is <code>null</code>
*/
private ChartXSlider _xSliderDragged;
/**
* This is the slider which is drawn on top of the other, this is normally the last dragged
* slider
*/
private ChartXSlider _xSliderOnTop;
/**
* This is the slider which is below the top slider
*/
private ChartXSlider _xSliderOnBottom;
/**
* This is a dummy slider to keep the value points
*/
private ChartXSlider _xSliderValuePoint;
/**
* contains the x-slider when the mouse is over it, or <code>null</code> when the mouse is not
* over it
*/
private ChartXSlider _mouseOverXSlider;
/**
* Contains the slider which has the focus.
*/
private ChartXSlider _selectedXSlider;
/**
* Device position of the x-slider line when the slider is dragged. The position can be outside
* of the viewport which causes autoscrolling.
*/
private int _devXDraggedXSliderLine;
/**
* Mouse device position when autoscrolling is done with the mouse but without a x-slider
*/
private int _devXAutoScrollMousePosition = Integer.MIN_VALUE;
/**
* list for all y-sliders
*/
private ArrayList<ChartYSlider> _ySliders;
/**
* contextLeftSlider is set when the right mouse button was clicked and the left slider was hit
*/
private ChartXSlider _contextLeftSlider;
/**
* contextRightSlider is set when the right mouse button was clicked and the right slider was hit
*/
private ChartXSlider _contextRightSlider;
/**
* cursor when the graph can be resizes
*/
private Cursor _cursorResizeLeftRight;
private Cursor _cursorArrow;
private Cursor _cursorDragged;
private Cursor _cursorDragXSlider_ModeZoom;
private Cursor _cursorDragXSlider_ModeSlider;
private Cursor _cursorModeZoom;
private Cursor _cursorModeZoomMove;
private Cursor _cursorModeSlider;
private Cursor _cursorMove1x;
private Cursor _cursorMove2x;
private Cursor _cursorMove3x;
private Cursor _cursorMove4x;
private Cursor _cursorMove5x;
private Cursor _cursorResizeTopDown;
private Cursor _cursorXSliderLeft;
private Cursor _cursorXSliderRight;
/**
* Contains a {@link ChartTitleSegment} when a tour title area is hovered, otherwise
* <code>null</code> .
*/
private ChartTitleSegment _hoveredTitleSegment;
/**
* serie index for the hovered bar, the bar is hidden when -1;
*/
private int _hoveredBarSerieIndex = -1;
private int _hoveredBarValueIndex;
private boolean _isHoveredBarDirty;
private ChartBarToolTip _hoveredBar_ToolTip;
private boolean _isHoveredLineVisible = false;
private int _hoveredValuePointIndex = -1;
private ArrayList<RectangleLong[]> _lineFocusRectangles = new ArrayList<>();
private ArrayList<PointLong[]> _lineDevPositions = new ArrayList<>();
/**
* Tooltip for value points, can be <code>null</code> when not set.
*/
IPinned_ToolTip valuePointToolTip;
private ChartYSlider _hitYSlider;
private ChartYSlider _ySliderDragged;
private int _ySliderGraphX;
private boolean _isSetXSliderPositionLeft;
private boolean _isSetXSliderPositionRight;
/**
* <code>true</code> when the x-marker is moved with the mouse
*/
private boolean _isXMarkerMoved;
/**
* x-position when the x-marker was started to drag
*/
private int _devXMarkerDraggedStartPos;
/**
* x-position when the x-marker is moved
*/
private int _devXMarkerDraggedPos;
private int _movedXMarkerStartValueIndex;
private int _movedXMarkerEndValueIndex;
private double _xMarkerValueDiff;
/**
* <code>true</code> when the chart is dragged with the mouse
*/
private boolean _isChartDragged = false;
/**
* <code>true</code> when the mouse button in down but not moved
*/
private boolean _isChartDraggedStarted = false;
private Point _draggedChartStartPos;
private Point _draggedChartDraggedPos;
private boolean[] _selectedBarItems;
private final int[] _drawAsyncCounter = new int[1];
private boolean _isAutoScroll;
private boolean _isDisableHoveredLineValueIndex;
private int[] _autoScrollCounter = new int[1];
private final ColorCache _colorCache = new ColorCache();
/**
* Default background color
*/
private Color _backgroundColor;
/**
* Default foreground color
*/
private Color _foregroundColor;
private boolean _isSelectionVisible;
/**
* Is <code>true</code> when this chart gained the focus, <code>false</code> when the focus is
* lost.
*/
private boolean _isFocusActive;
private boolean _isOverlayDirty;
/**
* widget relative position of the mouse in the mouse down event
*/
private int _devXMouseDown;
private int _devYMouseDown;
private int _devXMouseMove;
private int _devYMouseMove;
private boolean _isPaintDraggedImage = false;
/**
* is <code>true</code> when data for a graph is available
*/
private boolean _isGraphVisible = false;
/**
* Client area for this canvas
*/
Rectangle _clientArea;
/**
* After a resize the custom overlay must be recomputed
*/
private int _isCustomOverlayInvalid;
private PixelConverter _pc;
/**
* Is <code>true</code> when a chart can be overlapped. The overlap feature is currently
* supported for graphs which all have the chart type ChartType.LINE.
*/
boolean _canChartBeOverlapped;
/**
* Is <code>true</code> when overlapped graphs are enabled.
*/
boolean _isChartOverlapped;
/**
* Cache font to improve performance.
*/
private Font _uiFont;
/**
* Configuration how the chart title segment is displayed.
*/
ChartTitleSegmentConfig chartTitleSegmentConfig = new ChartTitleSegmentConfig();
private ILineSelectionPainter _lineSelectionPainter;
/**
* Constructor
*
* @param parent
* the parent of this control.
* @param style
* the style of this control.
*/
ChartComponentGraph(final Chart chartWidget, final Composite parent, final int style) {
// create composite with horizontal scrollbars
super(parent, SWT.H_SCROLL | SWT.NO_BACKGROUND);
final Display display = getDisplay();
_chart = chartWidget;
_uiFont = _chart.getFont();
_pc = new PixelConverter(_chart);
// SET_FORMATTING_OFF
_cursorResizeLeftRight = new Cursor(display, SWT.CURSOR_SIZEWE);
_cursorResizeTopDown = new Cursor(display, SWT.CURSOR_SIZENS);
_cursorDragged = new Cursor(display, SWT.CURSOR_SIZEALL);
_cursorArrow = new Cursor(display, SWT.CURSOR_ARROW);
_cursorModeSlider = createCursorFromImage(ChartImages.CursorMode_Slider);
_cursorModeZoom = createCursorFromImage(ChartImages.CursorMode_Zoom);
_cursorModeZoomMove = createCursorFromImage(ChartImages.CursorMode_Slider_Move);
_cursorDragXSlider_ModeZoom = createCursorFromImage(ChartImages.Cursor_DragXSlider_ModeZoom);
_cursorDragXSlider_ModeSlider = createCursorFromImage(ChartImages.Cursor_DragXSlider_ModeSlider);
_cursorMove1x = createCursorFromImage(ChartImages.Cursor_Move1x);
_cursorMove2x = createCursorFromImage(ChartImages.Cursor_Move2x);
_cursorMove3x = createCursorFromImage(ChartImages.Cursor_Move3x);
_cursorMove4x = createCursorFromImage(ChartImages.Cursor_Move4x);
_cursorMove5x = createCursorFromImage(ChartImages.Cursor_Move5x);
_cursorXSliderLeft = createCursorFromImage(ChartImages.Cursor_X_Slider_Left);
_cursorXSliderRight = createCursorFromImage(ChartImages.Cursor_X_Slider_Right);
// SET_FORMATTING_ON
_chartComponents = (ChartComponents) parent;
// setup the x-slider
_xSliderA = new ChartXSlider(this, Integer.MIN_VALUE, ChartXSlider.SLIDER_TYPE_LEFT);
_xSliderB = new ChartXSlider(this, Integer.MIN_VALUE, ChartXSlider.SLIDER_TYPE_RIGHT);
_xSliderValuePoint = new ChartXSlider(this, Integer.MIN_VALUE, ChartXSlider.SLIDER_TYPE_VALUE_POINT);
_xSliderOnTop = _xSliderB;
_xSliderOnBottom = _xSliderA;
_hoveredBar_ToolTip = new ChartBarToolTip(_chart);
addListener();
createContextMenu();
setCursorStyle();
}
/**
* Execute the action which is defined when a bar is selected with the left mouse button
*/
private void actionSelectBars() {
if (_hoveredBarSerieIndex < 0) {
return;
}
boolean[] selectedBarItems;
if (_allGraphDrawingData.isEmpty()) {
selectedBarItems = null;
} else {
final GraphDrawingData graphDrawingData = _allGraphDrawingData.get(0);
final ChartDataXSerie xData = graphDrawingData.getXData();
selectedBarItems = new boolean[xData._highValuesDouble[0].length];
selectedBarItems[_hoveredBarValueIndex] = true;
}
setSelectedBars(selectedBarItems);
_chart.fireEvent_BarSelection(_hoveredBarSerieIndex, _hoveredBarValueIndex);
}
/**
* hookup all listeners
*/
private void addListener() {
addPaintListener(paintEvent -> {
if (_isChartDragged) {
drawSync_020_MoveCanvas(paintEvent.gc);
} else {
// final long start = System.nanoTime();
// System.out.println();
// System.out.println("onPaint\tstart\t");
// // TODO remove SYSTEM.OUT.PRINTLN
drawSync_000_onPaint(paintEvent.gc, paintEvent.time & 0xFFFFFFFFL);
// System.out.println("onPaint\tend\t" + (((double) System.nanoTime() - start) / 1000000) + "ms");
// System.out.println();
// // TODO remove SYSTEM.OUT.PRINTLN
}
});
// horizontal scrollbar
final ScrollBar horizontalBar = getHorizontalBar();
horizontalBar.setEnabled(false);
horizontalBar.setVisible(false);
horizontalBar.addSelectionListener(widgetSelectedAdapter(this::onScroll));
addMouseMoveListener(mouseEvent -> {
if (_isGraphVisible) {
onMouseMove(mouseEvent.time & 0xFFFFFFFFL, mouseEvent.x, mouseEvent.y);
}
});
addMouseListener(new MouseListener() {
@Override
public void mouseDoubleClick(final MouseEvent e) {
if (_isGraphVisible) {
onMouseDoubleClick(e);
}
}
@Override
public void mouseDown(final MouseEvent e) {
if (_isGraphVisible) {
onMouseDown(e);
}
}
@Override
public void mouseUp(final MouseEvent e) {
if (_isGraphVisible) {
onMouseUp(e);
}
}
});
addMouseTrackListener(new MouseTrackListener() {
@Override
public void mouseEnter(final MouseEvent e) {
if (_isGraphVisible) {
onMouseEnter();
}
}
@Override
public void mouseExit(final MouseEvent e) {
if (_isGraphVisible) {
onMouseExit(e);
}
}
@Override
public void mouseHover(final MouseEvent e) {}
});
addListener(SWT.MouseWheel, event -> onMouseWheel(event, false, false));
addFocusListener(new FocusListener() {
@Override
public void focusGained(final FocusEvent e) {
setFocusToControl();
_isFocusActive = true;
_isSelectionDirty = true;
redraw();
}
@Override
public void focusLost(final FocusEvent e) {
_isFocusActive = false;
_isSelectionDirty = true;
redraw();
}
});
addListener(SWT.Traverse, event -> {
switch (event.detail) {
case SWT.TRAVERSE_RETURN:
case SWT.TRAVERSE_ESCAPE:
case SWT.TRAVERSE_TAB_NEXT:
case SWT.TRAVERSE_TAB_PREVIOUS:
case SWT.TRAVERSE_PAGE_NEXT:
case SWT.TRAVERSE_PAGE_PREVIOUS:
event.doit = true;
break;
}
});
addControlListener(new ControlListener() {
@Override
public void controlMoved(final ControlEvent e) {}
@Override
public void controlResized(final ControlEvent e) {
_clientArea = getClientArea();
_isDisableHoveredLineValueIndex = true;
}
});
addListener(SWT.KeyDown, this::onKeyDown);
addDisposeListener(disposeEvent -> onDispose());
}
private void adjustYSlider() {
/*
* Check if the y slider was outside of the bounds, recompute the chart when necessary
*/
final GraphDrawingData drawingData = _ySliderDragged.getDrawingData();
final ChartDataYSerie yData = _ySliderDragged.getYData();
final ChartYSlider slider1 = yData.getYSlider1();
final ChartYSlider slider2 = yData.getYSlider2();
final int devYBottom = drawingData.getDevYBottom();
final int devYTop = devYBottom - drawingData.devGraphHeight;
final float graphYBottom = drawingData.getGraphYBottom();
final double scaleY = drawingData.getScaleY();
final int devYSliderLine1 = slider1.getDevYSliderLine();
final int devYSliderLine2 = slider2.getDevYSliderLine();
final boolean isBottomToTop = drawingData.getYData().isYAxisDirection();
double graphValue1 = 0;
double graphValue2 = 0;
if (isBottomToTop) {
graphValue1 = (((double) devYBottom - devYSliderLine1) / scaleY + graphYBottom);
graphValue2 = (((double) devYBottom - devYSliderLine2) / scaleY + graphYBottom);
} else {
graphValue1 = (((double) devYSliderLine1 - devYTop) / scaleY + graphYBottom);
graphValue2 = (((double) devYSliderLine2 - devYTop) / scaleY + graphYBottom);
}
boolean isAdjustedSlider1 = false;
// get value which was adjusted
if (_ySliderDragged == slider1) {
yData.adjustedYValue = (float) graphValue1;
isAdjustedSlider1 = true;
} else if (_ySliderDragged == slider2) {
yData.adjustedYValue = (float) graphValue2;
} else {
// this case should not happen
System.out.println("y-slider is not set correctly");//$NON-NLS-1$
return;
}
double minValue;
double maxValue;
if (graphValue1 < graphValue2) {
minValue = graphValue1;
maxValue = graphValue2;
// position the lower slider to the bottom of the chart
slider1.setDevYSliderLine(devYBottom);
slider2.setDevYSliderLine(devYTop);
} else {
// graphValue1 >= graphValue2
minValue = graphValue2;
maxValue = graphValue1;
// position the upper slider to the top of the chart
slider1.setDevYSliderLine(devYTop);
slider2.setDevYSliderLine(devYBottom);
}
yData.setVisibleMinValue(minValue);
yData.setVisibleMaxValue(maxValue);
/*
* Keep slider positions which are used to force these values when creating new chart data
*/
final float[] sliderMinMaxValue = yData.getSliderMinMaxValue();
if (sliderMinMaxValue != null) {
// set only the adjusted value
if (isBottomToTop) {
if (isAdjustedSlider1) {
sliderMinMaxValue[1] = (float) maxValue;
} else {
sliderMinMaxValue[0] = (float) minValue;
}
} else {
if (isAdjustedSlider1) {
sliderMinMaxValue[0] = (float) minValue;
} else {
sliderMinMaxValue[1] = (float) maxValue;
}
}
}
_ySliderDragged = null;
// the cursor could be outside of the chart, reset it
// setCursorStyle();
/*
* The hited slider could be outsite of the chart, hide the labels on the slider
*/
_hitYSlider = null;
/*
* When the chart is synchronized, the y-slider position is modified, so we overwrite the
* synchronized chart y-slider positions until the zoom in marker is overwritten
*/
final SynchConfiguration synchedChartConfig = _chartComponents._synchConfigSrc;
if (synchedChartConfig != null) {
final ChartYDataMinMaxKeeper synchedChartMinMaxKeeper = synchedChartConfig.getYDataMinMaxKeeper();
// get the id for the changed y-slider
final Integer yDataInfo = (Integer) yData.getCustomData(ChartDataYSerie.YDATA_INFO);
// adjust min value for the changed y-slider
final HashMap<Integer, Double> minKeeper = synchedChartMinMaxKeeper.getMinValues();
final Double synchedChartMinValue = minKeeper.get(yDataInfo);
if (synchedChartMinValue != null) {
minKeeper.put(yDataInfo, minValue);
}
// adjust max value for the changed y-slider
final HashMap<Integer, Double> maxKeeper = synchedChartMinMaxKeeper.getMaxValues();
final Double synchedChartMaxValue = maxKeeper.get(yDataInfo);
if (synchedChartMaxValue != null) {
maxKeeper.put(yDataInfo, maxValue);
}
}
computeChart();
}
/**
* @param allGraphDrawingData
* @return Returns <code>true</code> when a chart can be overlapped.
* <p>
* The overlap feature is currently supported for graphs which all have the chart type
* {@link ChartType#LINE}.
*/
private boolean canChartBeOverlapped(final ArrayList<GraphDrawingData> allGraphDrawingData) {
for (final GraphDrawingData graphDrawingData : allGraphDrawingData) {
final ChartType chartType = graphDrawingData.getChartType();
if (chartType != ChartType.LINE && chartType != ChartType.HORIZONTAL_BAR) {
return false;
}
}
return true;
}
/**
* when the chart was modified, recompute all
*/
private void computeChart() {
getDisplay().asyncExec(() -> {
if (!isDisposed()) {
_chartComponents.onResize();
}
});
}
private void computeSliderForContextMenu(final int devX, final int devY) {
ChartXSlider slider1 = null;
ChartXSlider slider2 = null;
// reset the context slider
_contextLeftSlider = null;
_contextRightSlider = null;
// check if a slider or the slider line was hit
if (_xSliderA.getHitRectangle().contains(devX, devY)) {
slider1 = _xSliderA;
}
if (_xSliderB.getHitRectangle().contains(devX, devY)) {
slider2 = _xSliderB;
}
/*
* check if a slider was hit
*/
if (slider1 == null && slider2 == null) {
// no slider was hit
return;
}
/*
* check if one slider was hit, when yes, the leftslider is set and the right slider is null
*/
if (slider1 != null && slider2 == null) {
// only slider 1 was hit
_contextLeftSlider = slider1;
return;
}
if (slider2 != null && slider1 == null) {
// only slider 2 was hit
_contextLeftSlider = slider2;
return;
}
/*
* both sliders are hit
*/
final long xSlider1Position = slider1.getHitRectangle().x;
final long xSlider2Position = slider2.getHitRectangle().x;
if (xSlider1Position == xSlider2Position) {
// both sliders are at the same position
_contextLeftSlider = slider1;
return;
}
if (xSlider1Position < xSlider2Position) {
_contextLeftSlider = slider1;
_contextRightSlider = slider2;
} else {
_contextLeftSlider = slider2;
_contextRightSlider = slider1;
}
}
private int computeXMarkerValue(final double[] xValues,
final int xmStartIndex,
final double valueDraggingDiff,
final double valueXStartWithOffset) {
int valueIndex;
double valueX = xValues[xmStartIndex];
double valueHalf;
/*
* get the marker positon for the next value
*/
if (valueDraggingDiff > 0) {
// moved to the right
for (valueIndex = xmStartIndex; valueIndex < xValues.length; valueIndex++) {
valueX = xValues[valueIndex];
valueHalf = ((valueX - xValues[Math.min(valueIndex + 1, xValues.length - 1)]) / 2);
if (valueX >= valueXStartWithOffset + valueHalf) {
break;
}
}
} else {
// moved to the left
for (valueIndex = xmStartIndex; valueIndex >= 0; valueIndex--) {
valueX = xValues[valueIndex];
valueHalf = ((valueX - xValues[Math.max(0, valueIndex - 1)]) / 2);
if (valueX < valueXStartWithOffset + valueHalf) {
break;
}
}
}
return Math.max(0, Math.min(valueIndex, xValues.length - 1));
}
/**
* create the context menu
*/
private void createContextMenu() {
final MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(menuManager -> {
actionSelectBars();
_hoveredBar_ToolTip.hide();