Commit | Line | Data |
---|---|---|
2e427755 GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 École Polytechnique de Montréal | |
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 | * Geneviève Bastien - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.viewers.xycharts.linecharts; | |
14 | ||
15 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; | |
16 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; | |
17 | import org.eclipse.linuxtools.tmf.ui.viewers.xycharts.ITmfChartTimeProvider; | |
18 | import org.eclipse.linuxtools.tmf.ui.viewers.xycharts.TmfBaseProvider; | |
19 | import org.eclipse.swt.events.MouseEvent; | |
20 | import org.eclipse.swt.events.MouseTrackListener; | |
21 | import org.swtchart.IAxis; | |
22 | import org.swtchart.ISeries; | |
23 | ||
24 | /** | |
25 | * Displays a tooltip on line charts. For each series, it shows the y value at | |
26 | * the selected x value. This tooltip assumes that all series share a common set | |
27 | * of X axis values. If the X series is not common, the tooltip text may not be | |
28 | * accurate. | |
29 | * | |
30 | * @author Geneviève Bastien | |
210bd7da | 31 | * @since 3.0 |
2e427755 GB |
32 | */ |
33 | public class TmfCommonXLineChartTooltipProvider extends TmfBaseProvider implements MouseTrackListener { | |
34 | ||
35 | /** | |
36 | * Constructor for the tooltip provider | |
37 | * | |
38 | * @param tmfChartViewer | |
39 | * The parent chart viewer | |
40 | */ | |
41 | public TmfCommonXLineChartTooltipProvider(ITmfChartTimeProvider tmfChartViewer) { | |
42 | super(tmfChartViewer); | |
43 | register(); | |
44 | } | |
45 | ||
46 | // ------------------------------------------------------------------------ | |
47 | // TmfBaseProvider | |
48 | // ------------------------------------------------------------------------ | |
49 | ||
50 | @Override | |
51 | public void register() { | |
52 | getChart().getPlotArea().addMouseTrackListener(this); | |
53 | } | |
54 | ||
55 | @Override | |
56 | public void deregister() { | |
57 | if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) { | |
58 | getChart().getPlotArea().removeMouseTrackListener(this); | |
59 | } | |
60 | } | |
61 | ||
62 | @Override | |
63 | public void refresh() { | |
64 | // nothing to do | |
65 | } | |
66 | ||
67 | // ------------------------------------------------------------------------ | |
68 | // MouseTrackListener | |
69 | // ------------------------------------------------------------------------ | |
70 | ||
71 | @Override | |
72 | public void mouseEnter(MouseEvent e) { | |
73 | } | |
74 | ||
75 | @Override | |
76 | public void mouseExit(MouseEvent e) { | |
77 | } | |
78 | ||
79 | @Override | |
80 | public void mouseHover(MouseEvent e) { | |
81 | if (getChartViewer().getWindowDuration() != 0) { | |
82 | IAxis xAxis = getChart().getAxisSet().getXAxis(0); | |
83 | ||
84 | double xCoordinate = xAxis.getDataCoordinate(e.x); | |
85 | ||
86 | ISeries[] series = getChart().getSeriesSet().getSeries(); | |
87 | ||
88 | if ((xCoordinate < 0) || (series.length == 0)) { | |
89 | return; | |
90 | } | |
91 | ||
92 | /* Find the index of the value we want */ | |
93 | double[] xS = series[0].getXSeries(); | |
94 | if (xS == null) { | |
95 | return; | |
96 | } | |
97 | int index = 0; | |
98 | for (int i = 0; i < xS.length; i++) { | |
99 | if (xS[i] > xCoordinate) { | |
100 | break; | |
101 | } | |
102 | index = i; | |
103 | } | |
104 | ||
105 | /* set tooltip of closest data point */ | |
106 | StringBuffer buffer = new StringBuffer(); | |
107 | buffer.append("time="); //$NON-NLS-1$ | |
108 | buffer.append(new TmfTimestamp((long) xCoordinate + getChartViewer().getTimeOffset(), ITmfTimestamp.NANOSECOND_SCALE).toString()); | |
109 | buffer.append('\n'); | |
110 | ||
111 | /* For each series, get the value at the index */ | |
112 | for (ISeries serie : series) { | |
113 | double[] yS = serie.getYSeries(); | |
114 | /* Make sure the series values and the value at index exist */ | |
115 | if (yS == null || yS.length <= index) { | |
116 | continue; | |
117 | } | |
118 | buffer.append(serie.getId()); | |
119 | buffer.append('='); | |
120 | buffer.append(yS[index]); | |
121 | buffer.append('\n'); | |
122 | } | |
123 | ||
124 | getChart().getPlotArea().setToolTipText(buffer.toString()); | |
125 | getChart().redraw(); | |
126 | } | |
127 | } | |
128 | ||
129 | } |