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