1515package org .eclipse .tracecompass .tmf .ui .viewers .xychart .linechart ;
1616
1717import java .text .Format ;
18+ import java .util .ArrayList ;
1819import java .util .Arrays ;
1920import java .util .List ;
2021
2829import org .eclipse .tracecompass .tmf .core .timestamp .ITmfTimestamp ;
2930import org .eclipse .tracecompass .tmf .core .timestamp .TmfTimestamp ;
3031import org .eclipse .tracecompass .tmf .ui .viewers .TmfAbstractToolTipHandler ;
31- import org .eclipse .tracecompass .tmf .ui .viewers .TmfAbstractToolTipHandler .ToolTipString ;
3232import org .eclipse .tracecompass .tmf .ui .viewers .xychart .IAxis ;
3333import org .eclipse .tracecompass .tmf .ui .viewers .xychart .ITmfChartTimeProvider ;
3434import org .eclipse .tracecompass .tmf .ui .viewers .xychart .IXYSeries ;
3535import org .eclipse .tracecompass .tmf .ui .viewers .xychart .TmfBaseProvider ;
36+ import org .eclipse .tracecompass .tmf .ui .viewers .xychart .linechart .TmfCommonXAxisChartViewer ;
3637
3738/**
3839 * Displays a tooltip on line charts. For each series, it shows the y value at
@@ -72,62 +73,97 @@ public void refresh() {
7273 // nothing to do
7374 }
7475
75- protected boolean isTooltipAvailable () {
76- return getChartViewer ().getWindowDuration () != 0 ;
77- }
78-
79- protected int getHoveredIndex (List <IXYSeries > series , double xCoordinate ) {
80- if (series .isEmpty ()) {
81- return -1 ;
82- }
83- double [] xSeries = series .get (0 ).getXSeries ();
84- if ((xSeries == null ) || (xSeries .length == 0 )) {
85- return -1 ;
86- }
87- int index = Arrays .binarySearch (xSeries , xCoordinate );
88- index = (index >= 0 ) ? index : -index - 1 ;
89- return (index < xSeries .length ) ? index : -1 ;
90- }
76+ // ======================================================================
77+ // HOOK METHODS
78+ // ======================================================================
9179
92- protected boolean isValidSeriesIndex ( IXYSeries series , int index ) {
93- double [] ySeries = series . getYSeries ();
94- return series . isVisible () && ySeries != null && index >= 0 && index < ySeries . length ;
80+ @ FunctionalInterface
81+ protected interface ITooltipItemAdder {
82+ void addItem ( ToolTipString key , ToolTipString value ) ;
9583 }
9684
97- protected void addAdditionalTooltipItems (double xCoordinate , String seriesKey ) {
98- ITmfTimestamp time = TmfTimestamp .fromNanos ((long ) xCoordinate + getChartViewer ().getTimeOffset ());
99- addItem (null ,
85+ protected void addAdditionalTooltipItems (ITooltipItemAdder adder , double xCoordinate , String key ) {
86+ long timeNanos = Math .round (xCoordinate ) + getChartViewer ().getTimeOffset ();
87+ ITmfTimestamp time = TmfTimestamp .fromNanos (timeNanos );
88+ adder .addItem (
10089 ToolTipString .fromString (Messages .TmfCommonXLineChartTooltipProvider_time ),
10190 ToolTipString .fromTimestamp (time .toString (), time .toNanos ()));
10291 }
10392
104- protected void addSeriesTooltipItem (IXYSeries series , int index , Format format ) {
105- double [] ySeries = series .getYSeries ();
93+ protected void addSeriesTooltipItem (ITooltipItemAdder adder , IXYSeries xySeries , int index , Format format ) {
94+ double [] ySeries = xySeries .getYSeries ();
10695 if (ySeries == null || index < 0 || index >= ySeries .length ) {
10796 return ;
10897 }
10998
110- String label = formatSeriesLabel (series );
99+ String label = formatSeriesLabel (xySeries );
111100 double yValue = ySeries [index ];
112101 if (format == null ) {
113- addItem (null , ToolTipString .fromHtml (label ), ToolTipString .fromDecimal (yValue ));
102+ adder .addItem (
103+ ToolTipString .fromHtml (label ),
104+ ToolTipString .fromDecimal (yValue ));
114105 } else {
115- addItem (null , ToolTipString .fromHtml (label ), ToolTipString .fromString (format .format (yValue )));
106+ adder .addItem (
107+ ToolTipString .fromHtml (label ),
108+ ToolTipString .fromString (format .format (yValue )));
116109 }
117110 }
118111
119- protected String formatSeriesLabel (IXYSeries series ) {
120- String key = series .getId ();
121- String label = (key == null ) ? "" : key ; //$NON-NLS-1$
122- Color color = series .getColor ();
112+ // ======================================================================
113+ // PRIVATE METHODS
114+ // ======================================================================
115+
116+ private String formatSeriesLabel (IXYSeries xySeries ) {
117+ String label = xySeries .getId ();
118+ if (label == null ) {
119+ label = "" ; //$NON-NLS-1$
120+ }
121+
122+ Color color = xySeries .getColor ();
123123 if (color != null ) {
124124 RGBA rgba = color .getRGBA ();
125125 RGBAColor rgbaColor = new RGBAColor (rgba .rgb .red , rgba .rgb .green , rgba .rgb .blue , rgba .alpha );
126- label = String .format (TmfCommonXLineChartTooltipProvider .HTML_COLOR_TOOLTIP , rgbaColor , label );
126+ label = String .format (TmfCommonXLineChartTooltipProviderClone .HTML_COLOR_TOOLTIP , rgbaColor , label );
127127 }
128+
128129 return label ;
129130 }
130131
132+ private boolean isTooltipAvailable () {
133+ return getChartViewer ().getWindowDuration () != 0 ;
134+ }
135+
136+ private int getHoveredIndex (List <IXYSeries > series , double xCoordinate ) {
137+ if (series .isEmpty ()) {
138+ return -1 ;
139+ }
140+
141+ double [] xSeries = series .get (0 ).getXSeries ();
142+ if (xSeries == null || xSeries .length == 0 ) {
143+ return -1 ;
144+ }
145+
146+ int index = Arrays .binarySearch (xSeries , xCoordinate );
147+ if (index < 0 ) {
148+ index = -index - 1 ;
149+ index = Math .max (0 , index - 1 );
150+ }
151+
152+ return index < xSeries .length ? index : -1 ;
153+ }
154+
155+ private boolean isValidSeriesIndex (IXYSeries series , int index ) {
156+ double [] ySeries = series .getYSeries ();
157+ return series .isVisible ()
158+ && ySeries != null
159+ && index >= 0
160+ && index < ySeries .length ;
161+ }
162+
163+ // ======================================================================
164+ // TOOLTIP HANDLER
165+ // ======================================================================
166+
131167 private final class CommonToolTipHandler extends TmfAbstractToolTipHandler {
132168
133169 @ Override
@@ -153,20 +189,18 @@ public void fill(Control control, MouseEvent event, Point pt) {
153189 format = chartViewer .getSwtChart ().getAxisSet ().getYAxes ()[0 ].getTick ().getFormat ();
154190 }
155191
156- String firstValidSeriesKey = null ;
192+ List < IXYSeries > validXYSeries = new ArrayList <>() ;
157193 for (IXYSeries xySeries : series ) {
158194 if (isValidSeriesIndex (xySeries , index )) {
159- firstValidSeriesKey = xySeries .getId ();
160- break ;
195+ validXYSeries .add (xySeries );
161196 }
162197 }
163- addAdditionalTooltipItems (xCoordinate , firstValidSeriesKey );
164198
165- for ( IXYSeries xySeries : series ) {
166- if (! isValidSeriesIndex ( xySeries , index )) {
167- continue ;
168- }
169- addSeriesTooltipItem (xySeries , index , format );
199+ String firstValidSeriesKey = validXYSeries . isEmpty () ? null : validXYSeries . get ( 0 ). getId ();
200+ addAdditionalTooltipItems (( key , value ) -> addItem ( null , key , value ), xCoordinate , firstValidSeriesKey );
201+
202+ for ( IXYSeries xySeries : validXYSeries ) {
203+ addSeriesTooltipItem (( key , value ) -> addItem ( null , key , value ), xySeries , index , format );
170204 }
171205 }
172206 }
0 commit comments