Skip to content

Commit 38d85da

Browse files
TranHoangThanhDuyduy.tran.yb
authored andcommitted
Refactor tooltip handling in TmfCommonXLineChart
Refactor TmfCommonXLineChartTooltipProvider to make the tooltip population logic extensible for subclasses via protected hooks, while preserving the default tooltip behavior. Also remove unused maxLen calculation that was left over from an earlier implementation. Files: - org.eclipse.tracecompass.tmf.ui.viewers.xychart.linechart.TmfCommonXLineChartTooltipProvider
1 parent d2fde0d commit 38d85da

1 file changed

Lines changed: 98 additions & 77 deletions

File tree

tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/viewers/xychart/linechart/TmfCommonXLineChartTooltipProvider.java

Lines changed: 98 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@
2323
import org.eclipse.swt.graphics.Point;
2424
import org.eclipse.swt.graphics.RGBA;
2525
import org.eclipse.swt.widgets.Control;
26-
import org.eclipse.tracecompass.internal.tmf.ui.Messages;
2726
import org.eclipse.tracecompass.tmf.core.presentation.RGBAColor;
28-
import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
29-
import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
3027
import org.eclipse.tracecompass.tmf.ui.viewers.TmfAbstractToolTipHandler;
28+
import org.eclipse.tracecompass.tmf.ui.viewers.TmfAbstractToolTipHandler.ToolTipString;
3129
import org.eclipse.tracecompass.tmf.ui.viewers.xychart.IAxis;
3230
import org.eclipse.tracecompass.tmf.ui.viewers.xychart.ITmfChartTimeProvider;
3331
import org.eclipse.tracecompass.tmf.ui.viewers.xychart.IXYSeries;
3432
import org.eclipse.tracecompass.tmf.ui.viewers.xychart.TmfBaseProvider;
33+
import org.eclipse.tracecompass.tmf.ui.viewers.xychart.linechart.TmfCommonXAxisChartViewer;
3534

3635
/**
3736
* Displays a tooltip on line charts. For each series, it shows the y value at
@@ -43,80 +42,8 @@
4342
*/
4443
public class TmfCommonXLineChartTooltipProvider extends TmfBaseProvider {
4544

46-
private final class XYToolTipHandler extends TmfAbstractToolTipHandler {
47-
private static final String HTML_COLOR_TOOLTIP = "<span style=\"color:%s;\">%s</span>"; //$NON-NLS-1$
48-
49-
private boolean isValid(int index, IXYSeries serie) {
50-
double[] ySeries = serie.getYSeries();
51-
return serie.isVisible() && ySeries != null && ySeries.length > index;
52-
}
53-
54-
@Override
55-
public void fill(Control control, MouseEvent event, Point pt) {
56-
if (getChartViewer().getWindowDuration() != 0) {
57-
IAxis xAxis = getXAxis();
58-
59-
double xCoordinate = xAxis.getDataCoordinate(pt.x);
60-
61-
List<IXYSeries> series = getSeries();
62-
63-
if ((xCoordinate < 0) || (series.isEmpty())) {
64-
return;
65-
}
66-
67-
/* Find the index of the value we want */
68-
double[] xS = series.get(0).getXSeries();
69-
if (xS == null) {
70-
return;
71-
}
72-
int index = Arrays.binarySearch(xS, xCoordinate);
73-
index = index >= 0 ? index : -index - 1;
74-
int maxLen = 0;
75-
for (IXYSeries serie : series) {
76-
/* Make sure the series values and the value at index exist */
77-
if (isValid(index, serie)) {
78-
maxLen = Math.max(maxLen, serie.getId().length());
79-
}
80-
}
81-
82-
TmfCommonXAxisChartViewer viewer = null;
83-
Format format = null;
84-
ITmfChartTimeProvider timeProvider = getChartViewer();
85-
if (timeProvider instanceof TmfCommonXAxisChartViewer) {
86-
viewer = (TmfCommonXAxisChartViewer) timeProvider;
87-
format = viewer.getSwtChart().getAxisSet().getYAxes()[0].getTick().getFormat();
88-
}
89-
ITmfTimestamp time = TmfTimestamp.fromNanos((long) xCoordinate + getChartViewer().getTimeOffset());
90-
addItem(null, ToolTipString.fromString(Messages.TmfCommonXLineChartTooltipProvider_time), ToolTipString.fromTimestamp(time.toString(), time.toNanos()));
91-
/* For each series, get the value at the index */
92-
for (IXYSeries serie : series) {
93-
double[] yS = serie.getYSeries();
94-
/* Make sure the series values and the value at index exist */
95-
if (isValid(index, serie)) {
96-
String key = serie.getId();
97-
Color color = serie.getColor();
98-
if (key != null && color != null && viewer != null) {
99-
RGBA rgba = color.getRGBA();
100-
RGBAColor rgbaColor = new RGBAColor(rgba.rgb.red, rgba.rgb.green, rgba.rgb.blue, rgba.alpha);
101-
key = String.format(HTML_COLOR_TOOLTIP, rgbaColor, key);
102-
}
103-
if (key == null) {
104-
key = ""; //$NON-NLS-1$
105-
}
106-
double yValue = yS[index];
107-
if (format == null) {
108-
addItem(null, ToolTipString.fromHtml(key), ToolTipString.fromDecimal(yValue));
109-
} else {
110-
addItem(null, ToolTipString.fromHtml(key), ToolTipString.fromString(format.format(yValue)));
111-
}
112-
}
113-
}
114-
}
115-
}
116-
117-
}
118-
119-
private XYToolTipHandler fToolTipHandler = new XYToolTipHandler();
45+
private static final String HTML_COLOR_TOOLTIP = "<span style=\"color:%s;\">%s</span>"; //$NON-NLS-1$
46+
private final CommonToolTipHandler fToolTipHandler = new CommonToolTipHandler();
12047

12148
/**
12249
* Constructor for the tooltip provider
@@ -142,4 +69,98 @@ public TmfAbstractToolTipHandler getTooltipHandler() {
14269
public void refresh() {
14370
// nothing to do
14471
}
72+
73+
protected boolean isTooltipAvailable() {
74+
return getChartViewer().getWindowDuration() != 0;
75+
}
76+
77+
protected int getHoveredIndex(List<IXYSeries> series, double xCoordinate) {
78+
if (series.isEmpty()) {
79+
return -1;
80+
}
81+
double[] xSeries = series.get(0).getXSeries();
82+
if ((xSeries == null) || (xSeries.length == 0)) {
83+
return -1;
84+
}
85+
int index = Arrays.binarySearch(xSeries, xCoordinate);
86+
index = (index >= 0) ? index : -index - 1;
87+
return (index < xSeries.length) ? index : -1;
88+
}
89+
90+
protected boolean isValidSeriesIndex(IXYSeries series, int index) {
91+
double[] ySeries = series.getYSeries();
92+
return series.isVisible() && ySeries != null && index >= 0 && index < ySeries.length;
93+
}
94+
95+
protected void addAdditionalTooltipItems(double xCoordinate, String seriesKey) {
96+
// Default: no-op
97+
}
98+
99+
protected void addSeriesTooltipItem(IXYSeries series, String key, int index, Format format) {
100+
double[] ySeries = series.getYSeries();
101+
if (ySeries == null || index < 0 || index >= ySeries.length) {
102+
return;
103+
}
104+
105+
String label = formatSeriesLabel(series, key);
106+
double yValue = ySeries[index];
107+
if (format == null) {
108+
addItem(null, ToolTipString.fromHtml(label), ToolTipString.fromDecimal(yValue));
109+
} else {
110+
addItem(null, ToolTipString.fromHtml(label), ToolTipString.fromString(format.format(yValue)));
111+
}
112+
}
113+
114+
protected String formatSeriesLabel(IXYSeries series, String key) {
115+
String label = (key == null) ? "" : key; //$NON-NLS-1$
116+
Color color = series.getColor();
117+
if (color != null) {
118+
RGBA rgba = color.getRGBA();
119+
RGBAColor rgbaColor = new RGBAColor(rgba.rgb.red, rgba.rgb.green, rgba.rgb.blue, rgba.alpha);
120+
label = String.format(TmfCommonXLineChartTooltipProvider.HTML_COLOR_TOOLTIP, rgbaColor, label);
121+
}
122+
return label;
123+
}
124+
125+
private final class CommonToolTipHandler extends TmfAbstractToolTipHandler {
126+
127+
@Override
128+
public void fill(Control control, MouseEvent event, Point pt) {
129+
if (!isTooltipAvailable()) {
130+
return;
131+
}
132+
133+
IAxis xAxis = getXAxis();
134+
double xCoordinate = xAxis.getDataCoordinate(pt.x);
135+
if (xCoordinate < 0) {
136+
return;
137+
}
138+
139+
List<IXYSeries> series = getSeries();
140+
int index = getHoveredIndex(series, xCoordinate);
141+
if (index < 0) {
142+
return;
143+
}
144+
145+
Format format = null;
146+
if (getChartViewer() instanceof TmfCommonXAxisChartViewer chartViewer) {
147+
format = chartViewer.getSwtChart().getAxisSet().getYAxes()[0].getTick().getFormat();
148+
}
149+
150+
boolean additionalItemsAdded = false;
151+
for (IXYSeries xySeries : series) {
152+
if (!isValidSeriesIndex(xySeries, index)) {
153+
continue;
154+
}
155+
156+
String key = xySeries.getId();
157+
if (!additionalItemsAdded) {
158+
addAdditionalTooltipItems(xCoordinate, key);
159+
additionalItemsAdded = true;
160+
}
161+
162+
addSeriesTooltipItem(xySeries, key, index, format);
163+
}
164+
}
165+
}
145166
}

0 commit comments

Comments
 (0)