Skip to content

Commit 79afdc2

Browse files
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 79afdc2

1 file changed

Lines changed: 102 additions & 74 deletions

File tree

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

Lines changed: 102 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
2929
import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
3030
import org.eclipse.tracecompass.tmf.ui.viewers.TmfAbstractToolTipHandler;
31+
import org.eclipse.tracecompass.tmf.ui.viewers.TmfAbstractToolTipHandler.ToolTipString;
3132
import org.eclipse.tracecompass.tmf.ui.viewers.xychart.IAxis;
3233
import org.eclipse.tracecompass.tmf.ui.viewers.xychart.ITmfChartTimeProvider;
3334
import org.eclipse.tracecompass.tmf.ui.viewers.xychart.IXYSeries;
@@ -43,80 +44,8 @@
4344
*/
4445
public class TmfCommonXLineChartTooltipProvider extends TmfBaseProvider {
4546

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();
47+
private static final String HTML_COLOR_TOOLTIP = "<span style=\"color:%s;\">%s</span>"; //$NON-NLS-1$
48+
private final CommonToolTipHandler fToolTipHandler = new CommonToolTipHandler();
12049

12150
/**
12251
* Constructor for the tooltip provider
@@ -142,4 +71,103 @@ public TmfAbstractToolTipHandler getTooltipHandler() {
14271
public void refresh() {
14372
// nothing to do
14473
}
74+
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+
}
91+
92+
protected boolean isValidSeriesIndex(IXYSeries series, int index) {
93+
double[] ySeries = series.getYSeries();
94+
return series.isVisible() && ySeries != null && index >= 0 && index < ySeries.length;
95+
}
96+
97+
protected void addAdditionalTooltipItems(double xCoordinate, String seriesKey) {
98+
ITmfTimestamp time = TmfTimestamp.fromNanos((long) xCoordinate + getChartViewer().getTimeOffset());
99+
addItem(null,
100+
ToolTipString.fromString(Messages.TmfCommonXLineChartTooltipProvider_time),
101+
ToolTipString.fromTimestamp(time.toString(), time.toNanos()));
102+
}
103+
104+
protected void addSeriesTooltipItem(IXYSeries series, int index, Format format) {
105+
double[] ySeries = series.getYSeries();
106+
if (ySeries == null || index < 0 || index >= ySeries.length) {
107+
return;
108+
}
109+
110+
String label = formatSeriesLabel(series);
111+
double yValue = ySeries[index];
112+
if (format == null) {
113+
addItem(null, ToolTipString.fromHtml(label), ToolTipString.fromDecimal(yValue));
114+
} else {
115+
addItem(null, ToolTipString.fromHtml(label), ToolTipString.fromString(format.format(yValue)));
116+
}
117+
}
118+
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();
123+
if (color != null) {
124+
RGBA rgba = color.getRGBA();
125+
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);
127+
}
128+
return label;
129+
}
130+
131+
private final class CommonToolTipHandler extends TmfAbstractToolTipHandler {
132+
133+
@Override
134+
public void fill(Control control, MouseEvent event, Point pt) {
135+
if (!isTooltipAvailable()) {
136+
return;
137+
}
138+
139+
IAxis xAxis = getXAxis();
140+
double xCoordinate = xAxis.getDataCoordinate(pt.x);
141+
if (xCoordinate < 0) {
142+
return;
143+
}
144+
145+
List<IXYSeries> series = getSeries();
146+
int index = getHoveredIndex(series, xCoordinate);
147+
if (index < 0) {
148+
return;
149+
}
150+
151+
Format format = null;
152+
if (getChartViewer() instanceof TmfCommonXAxisChartViewer chartViewer) {
153+
format = chartViewer.getSwtChart().getAxisSet().getYAxes()[0].getTick().getFormat();
154+
}
155+
156+
String firstValidSeriesKey = null;
157+
for (IXYSeries xySeries : series) {
158+
if (isValidSeriesIndex(xySeries, index)) {
159+
firstValidSeriesKey = xySeries.getId();
160+
break;
161+
}
162+
}
163+
addAdditionalTooltipItems(xCoordinate, firstValidSeriesKey);
164+
165+
for (IXYSeries xySeries : series) {
166+
if (!isValidSeriesIndex(xySeries, index)) {
167+
continue;
168+
}
169+
addSeriesTooltipItem(xySeries, index, format);
170+
}
171+
}
172+
}
145173
}

0 commit comments

Comments
 (0)