1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.xycharts
.linecharts
;
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
;
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
30 * @author Geneviève Bastien
33 public class TmfCommonXLineChartTooltipProvider
extends TmfBaseProvider
implements MouseTrackListener
{
36 * Constructor for the tooltip provider
38 * @param tmfChartViewer
39 * The parent chart viewer
41 public TmfCommonXLineChartTooltipProvider(ITmfChartTimeProvider tmfChartViewer
) {
42 super(tmfChartViewer
);
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
51 public void register() {
52 getChart().getPlotArea().addMouseTrackListener(this);
56 public void deregister() {
57 if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) {
58 getChart().getPlotArea().removeMouseTrackListener(this);
63 public void refresh() {
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
72 public void mouseEnter(MouseEvent e
) {
76 public void mouseExit(MouseEvent e
) {
80 public void mouseHover(MouseEvent e
) {
81 if (getChartViewer().getWindowDuration() != 0) {
82 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
84 double xCoordinate
= xAxis
.getDataCoordinate(e
.x
);
86 ISeries
[] series
= getChart().getSeriesSet().getSeries();
88 if ((xCoordinate
< 0) || (series
.length
== 0)) {
92 /* Find the index of the value we want */
93 double[] xS
= series
[0].getXSeries();
98 for (int i
= 0; i
< xS
.length
; i
++) {
99 if (xS
[i
] > xCoordinate
) {
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());
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
) {
118 buffer
.append(serie
.getId());
120 buffer
.append(yS
[index
]);
124 getChart().getPlotArea().setToolTipText(buffer
.toString());