TMF: Add abstract XY line chart viewer on top of SWT chart
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / xycharts / linecharts / TmfCommonXLineChartTooltipProvider.java
CommitLineData
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
13package org.eclipse.linuxtools.tmf.ui.viewers.xycharts.linecharts;
14
15import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
16import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
17import org.eclipse.linuxtools.tmf.ui.viewers.xycharts.ITmfChartTimeProvider;
18import org.eclipse.linuxtools.tmf.ui.viewers.xycharts.TmfBaseProvider;
19import org.eclipse.swt.events.MouseEvent;
20import org.eclipse.swt.events.MouseTrackListener;
21import org.swtchart.IAxis;
22import 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
31 */
32public class TmfCommonXLineChartTooltipProvider extends TmfBaseProvider implements MouseTrackListener {
33
34 /**
35 * Constructor for the tooltip provider
36 *
37 * @param tmfChartViewer
38 * The parent chart viewer
39 */
40 public TmfCommonXLineChartTooltipProvider(ITmfChartTimeProvider tmfChartViewer) {
41 super(tmfChartViewer);
42 register();
43 }
44
45 // ------------------------------------------------------------------------
46 // TmfBaseProvider
47 // ------------------------------------------------------------------------
48
49 @Override
50 public void register() {
51 getChart().getPlotArea().addMouseTrackListener(this);
52 }
53
54 @Override
55 public void deregister() {
56 if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) {
57 getChart().getPlotArea().removeMouseTrackListener(this);
58 }
59 }
60
61 @Override
62 public void refresh() {
63 // nothing to do
64 }
65
66 // ------------------------------------------------------------------------
67 // MouseTrackListener
68 // ------------------------------------------------------------------------
69
70 @Override
71 public void mouseEnter(MouseEvent e) {
72 }
73
74 @Override
75 public void mouseExit(MouseEvent e) {
76 }
77
78 @Override
79 public void mouseHover(MouseEvent e) {
80 if (getChartViewer().getWindowDuration() != 0) {
81 IAxis xAxis = getChart().getAxisSet().getXAxis(0);
82
83 double xCoordinate = xAxis.getDataCoordinate(e.x);
84
85 ISeries[] series = getChart().getSeriesSet().getSeries();
86
87 if ((xCoordinate < 0) || (series.length == 0)) {
88 return;
89 }
90
91 /* Find the index of the value we want */
92 double[] xS = series[0].getXSeries();
93 if (xS == null) {
94 return;
95 }
96 int index = 0;
97 for (int i = 0; i < xS.length; i++) {
98 if (xS[i] > xCoordinate) {
99 break;
100 }
101 index = i;
102 }
103
104 /* set tooltip of closest data point */
105 StringBuffer buffer = new StringBuffer();
106 buffer.append("time="); //$NON-NLS-1$
107 buffer.append(new TmfTimestamp((long) xCoordinate + getChartViewer().getTimeOffset(), ITmfTimestamp.NANOSECOND_SCALE).toString());
108 buffer.append('\n');
109
110 /* For each series, get the value at the index */
111 for (ISeries serie : series) {
112 double[] yS = serie.getYSeries();
113 /* Make sure the series values and the value at index exist */
114 if (yS == null || yS.length <= index) {
115 continue;
116 }
117 buffer.append(serie.getId());
118 buffer.append('=');
119 buffer.append(yS[index]);
120 buffer.append('\n');
121 }
122
123 getChart().getPlotArea().setToolTipText(buffer.toString());
124 getChart().redraw();
125 }
126 }
127
128}
This page took 0.029634 seconds and 5 git commands to generate.