lttng: Provide tooltip for latency scatter chart
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / viewers / xycharts / TmfClosestDataPointTooltipProvider.java
CommitLineData
a2de198a
BH
1/**********************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12package org.eclipse.tracecompass.tmf.ui.viewers.xycharts;
13
14import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
15
16import org.eclipse.jdt.annotation.NonNull;
17import org.eclipse.jdt.annotation.NonNullByDefault;
18import org.eclipse.jdt.annotation.Nullable;
19import org.eclipse.swt.SWT;
20import org.eclipse.swt.events.MouseEvent;
21import org.eclipse.swt.events.MouseMoveListener;
22import org.eclipse.swt.events.MouseTrackListener;
23import org.eclipse.swt.events.PaintEvent;
24import org.eclipse.swt.events.PaintListener;
25import org.eclipse.swt.widgets.Display;
26import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
27import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
28import org.swtchart.IAxis;
29import org.swtchart.ISeries;
30
31/**
32 * Abstract tooltip provider for xy chart viewers. It displays the y value and y
33 * value of the data point of the mouse position. Extending classes can provide
34 * a custom tooltip text.
35 *
36 * @author Bernd Hufmann
37 * @since 2.0
38 */
39@NonNullByDefault(false)
40public class TmfClosestDataPointTooltipProvider extends TmfBaseProvider implements MouseTrackListener, MouseMoveListener, PaintListener {
41
42 // ------------------------------------------------------------------------
43 // Constants
44 // ------------------------------------------------------------------------
45 private static final int ALPHA = 128;
46 private static final int HIGHLIGHT_RADIUS = 5;
47
48 // ------------------------------------------------------------------------
49 // Attributes
50 // ------------------------------------------------------------------------
51 /** X coordinate for highlighting */
52 private int fHighlightX;
53 /** y coordinate for highlighting */
54 private int fHighlightY;
55 /** Flag to do highlighting or not */
56 private boolean fIsHighlight;
57
58 // ------------------------------------------------------------------------
59 // Constructors
60 // ------------------------------------------------------------------------
61 /**
62 * Constructor for a tool tip provider.
63 *
64 * @param tmfChartViewer
65 * - the parent chart viewer
66 */
67 public TmfClosestDataPointTooltipProvider(ITmfChartTimeProvider tmfChartViewer) {
68 super(tmfChartViewer);
69 register();
70 }
71
72 // ------------------------------------------------------------------------
73 // TmfBaseProvider
74 // ------------------------------------------------------------------------
75 @Override
76 public void register() {
77 getChart().getPlotArea().addMouseTrackListener(this);
78 getChart().getPlotArea().addMouseMoveListener(this);
79 getChart().getPlotArea().addPaintListener(this);
80 }
81
82 @Override
83 public void deregister() {
84 if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) {
85 getChart().getPlotArea().removeMouseTrackListener(this);
86 getChart().getPlotArea().removeMouseMoveListener(this);
87 getChart().getPlotArea().removePaintListener(this);
88 }
89 }
90
91 @Override
92 public void refresh() {
93 // nothing to do
94 }
95
96 // ------------------------------------------------------------------------
97 // MouseTrackListener
98 // ------------------------------------------------------------------------
99 @Override
100 public void mouseEnter(MouseEvent e) {
101 }
102
103 @Override
104 public void mouseExit(MouseEvent e) {
105 }
106
107 @Override
108 public void mouseHover(MouseEvent e) {
109 if ((getChartViewer().getWindowDuration() != 0) && (e != null)) {
110 IAxis xAxis = getChart().getAxisSet().getXAxis(0);
111 IAxis yAxis = getChart().getAxisSet().getYAxis(0);
112
113 ISeries[] series = getChart().getSeriesSet().getSeries();
114
115 double smallestDistance = Double.MAX_VALUE;
116 Parameter param = null;
117
118 // go over all series
119 for (int k = 0; k < series.length; k++) {
120 ISeries serie = series[k];
121 double[] xS = serie.getXSeries();
122 double[] yS = serie.getYSeries();
123
124 if ((xS == null) || (yS == null)) {
125 continue;
126 }
127 // go over all data points
128 for (int i = 0; i < xS.length; i++) {
129 int xs = xAxis.getPixelCoordinate(xS[i]) - e.x;
130 int ys = yAxis.getPixelCoordinate(yS[i]) - e.y;
131 double currentDistance = xs * xs + ys * ys;
132
133 /*
134 * Check for smallest distance to mouse position and
135 * only consider it if the mouse is close the data point.
136 */
137 if ((currentDistance < smallestDistance) && (currentDistance < (HIGHLIGHT_RADIUS * HIGHLIGHT_RADIUS))) {
138 smallestDistance = currentDistance;
139 fHighlightX = xs + e.x;
140 fHighlightY = ys + e.y;
141 if (param == null) {
142 param = new Parameter();
143 }
144 param.setSeriesIndex(k);
145 param.setDataIndex(i);
146 }
147 }
148 }
149 String tooltip = null;
150 if (param != null) {
151 tooltip = createToolTipText(param);
152 if (tooltip != null) {
153 fIsHighlight = true;
154 getChart().redraw();
155 }
156 }
157 /*
158 * Note that tooltip might be null which will clear the
159 * previous tooltip string. This is intentional.
160 */
161 getChart().getPlotArea().setToolTipText(tooltip);
162 }
163 }
164
165 // ------------------------------------------------------------------------
166 // MouseMoveListener
167 // ------------------------------------------------------------------------
168 @Override
169 public void mouseMove(@Nullable MouseEvent e) {
170 fIsHighlight = false;
171 getChart().redraw();
172 }
173
174 // ------------------------------------------------------------------------
175 // PaintListener
176 // ------------------------------------------------------------------------
177 @Override
178 public void paintControl(PaintEvent e) {
179 if (fIsHighlight && e != null) {
180 e.gc.setBackground(Display.getDefault().getSystemColor(
181 SWT.COLOR_RED));
182 e.gc.setAlpha(ALPHA);
183
184 e.gc.fillOval(fHighlightX - HIGHLIGHT_RADIUS, fHighlightY - HIGHLIGHT_RADIUS,
185 2 * HIGHLIGHT_RADIUS, 2 * HIGHLIGHT_RADIUS);
186 }
187 }
188
189 /**
190 * Creates the tooltip based on the given parameter.
191 *
192 * @param param
193 * parameter to create the tooltip string
194 * @return the tooltip based on the given parameter.
195 */
196 protected String createToolTipText(@NonNull Parameter param) {
197 ISeries[] series = getChart().getSeriesSet().getSeries();
198 int seriesIndex = param.getSeriesIndex();
199 int dataIndex = param.getDataIndex();
200 if ((series != null) && (seriesIndex < series.length)) {
201 ISeries serie = series[seriesIndex];
202 double[] xS = serie.getXSeries();
203 double[] yS = serie.getYSeries();
204 if ((xS != null) && (yS != null) && (dataIndex < xS.length) && (dataIndex < yS.length)) {
205 StringBuffer buffer = new StringBuffer();
206 buffer.append(checkNotNull("x=")); //$NON-NLS-1$
207 buffer.append(new TmfTimestamp((long) xS[dataIndex] + getChartViewer().getTimeOffset(), ITmfTimestamp.NANOSECOND_SCALE).toString());
208 buffer.append('\n');
209 buffer.append("y="); //$NON-NLS-1$
210 buffer.append((long) yS[dataIndex]);
211 return buffer.toString();
212 }
213 }
214 return null;
215 }
216
217 /**
218 * Parameter class
219 */
220 protected static class Parameter {
221 /* A series index */
222 private int seriesIndex;
223 /* A data point index within a series */
224 private int dataIndex;
225
226 /**
227 * @return the series index
228 */
229 public int getSeriesIndex() {
230 return seriesIndex;
231 }
232
233 /**
234 * @param seriesIndex
235 * index the seriesIndex to set
236 */
237 public void setSeriesIndex(int seriesIndex) {
238 this.seriesIndex = seriesIndex;
239 }
240
241 /**
242 * @return the data index
243 */
244 public int getDataIndex() {
245 return dataIndex;
246 }
247
248 /**
249 * @param dataIndex
250 * the data index to set
251 */
252 public void setDataIndex(int dataIndex) {
253 this.dataIndex = dataIndex;
254 }
255
256 }
257}
This page took 0.033989 seconds and 5 git commands to generate.