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
.tracecompass
.tmf
.ui
.viewers
.xycharts
.linecharts
;
15 import java
.text
.FieldPosition
;
16 import java
.text
.Format
;
17 import java
.text
.ParsePosition
;
19 import org
.eclipse
.swt
.events
.MouseEvent
;
20 import org
.eclipse
.swt
.events
.MouseTrackListener
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
22 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.ITmfChartTimeProvider
;
23 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.TmfBaseProvider
;
24 import org
.swtchart
.IAxis
;
25 import org
.swtchart
.ISeries
;
28 * Displays a tooltip on line charts. For each series, it shows the y value at
29 * the selected x value. This tooltip assumes that all series share a common set
30 * of X axis values. If the X series is not common, the tooltip text may not be
33 * @author Geneviève Bastien
35 public class TmfCommonXLineChartTooltipProvider
extends TmfBaseProvider
implements MouseTrackListener
{
37 private static final Format DEFAULT_FORMAT
= new Format() {
42 private static final long serialVersionUID
= -6130622953193109057L;
45 public StringBuffer
format(Object obj
, StringBuffer toAppendTo
, FieldPosition pos
) {
46 return toAppendTo
.append(obj
);
50 public Object
parseObject(String source
, ParsePosition pos
) {
57 * Constructor for the tooltip provider
59 * @param tmfChartViewer
60 * The parent chart viewer
62 public TmfCommonXLineChartTooltipProvider(ITmfChartTimeProvider tmfChartViewer
) {
63 super(tmfChartViewer
);
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
72 public void register() {
73 getChart().getPlotArea().addMouseTrackListener(this);
77 public void deregister() {
78 if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) {
79 getChart().getPlotArea().removeMouseTrackListener(this);
84 public void refresh() {
88 // ------------------------------------------------------------------------
90 // ------------------------------------------------------------------------
93 public void mouseEnter(MouseEvent e
) {
97 public void mouseExit(MouseEvent e
) {
101 public void mouseHover(MouseEvent e
) {
102 if (getChartViewer().getWindowDuration() != 0) {
103 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
105 double xCoordinate
= xAxis
.getDataCoordinate(e
.x
);
107 ISeries
[] series
= getChart().getSeriesSet().getSeries();
109 if ((xCoordinate
< 0) || (series
.length
== 0)) {
113 /* Find the index of the value we want */
114 double[] xS
= series
[0].getXSeries();
119 for (int i
= 0; i
< xS
.length
; i
++) {
120 if (xS
[i
] > xCoordinate
) {
126 /* set tooltip of closest data point */
127 Format format
= getChart().getAxisSet().getYAxis(0).getTick().getFormat();
128 if (format
== null) {
129 format
= DEFAULT_FORMAT
;
131 StringBuffer buffer
= new StringBuffer();
132 buffer
.append("time="); //$NON-NLS-1$
133 buffer
.append(TmfTimestamp
.fromNanos((long) xCoordinate
+ getChartViewer().getTimeOffset()).toString());
136 /* For each series, get the value at the index */
137 for (ISeries serie
: series
) {
138 double[] yS
= serie
.getYSeries();
139 /* Make sure the series values and the value at index exist */
140 if (yS
== null || yS
.length
<= index
) {
143 buffer
.append(serie
.getId());
145 buffer
.append(format
.format(yS
[index
]));
149 getChart().getPlotArea().setToolTipText(buffer
.toString());