tmf: Fix trace type id in extension for Generic CTF traces
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / xycharts / linecharts / TmfCommonXLineChartViewer.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 java.util.LinkedHashMap;
16import java.util.Map;
17import java.util.Map.Entry;
18
19import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
20import org.eclipse.linuxtools.tmf.ui.viewers.xycharts.TmfChartTimeStampFormat;
21import org.eclipse.linuxtools.tmf.ui.viewers.xycharts.TmfXYChartViewer;
c85a741e 22import org.eclipse.swt.SWT;
2e427755
GB
23import org.eclipse.swt.widgets.Composite;
24import org.eclipse.swt.widgets.Display;
25import org.swtchart.IAxisTick;
26import org.swtchart.ILineSeries;
27import org.swtchart.ILineSeries.PlotSymbolType;
28import org.swtchart.ISeries;
29import org.swtchart.ISeries.SeriesType;
c85a741e 30import org.swtchart.ISeriesSet;
2e427755
GB
31import org.swtchart.LineStyle;
32import org.swtchart.Range;
33
34/**
35 * Abstract line chart viewer class implementation. All series in this viewer
36 * use the same X axis values. They are automatically created as values are
37 * provided for a key. Series by default will be displayed as a line. Each
38 * series appearance can be overridden when creating it.
39 *
40 * @author - Geneviève Bastien
41 * @since 3.0
42 */
43public abstract class TmfCommonXLineChartViewer extends TmfXYChartViewer {
44
45 private static final double DEFAULT_MAXY = Double.MIN_VALUE;
46 private static final double DEFAULT_MINY = Double.MAX_VALUE;
47
48 /* The desired number of points per pixel */
49 private static final double RESOLUTION = 1.0;
50
c85a741e
GB
51 private static final int[] LINE_COLORS = { SWT.COLOR_BLUE, SWT.COLOR_RED, SWT.COLOR_GREEN,
52 SWT.COLOR_MAGENTA, SWT.COLOR_CYAN,
53 SWT.COLOR_DARK_BLUE, SWT.COLOR_DARK_RED, SWT.COLOR_DARK_GREEN,
54 SWT.COLOR_DARK_MAGENTA, SWT.COLOR_DARK_CYAN, SWT.COLOR_DARK_YELLOW,
55 SWT.COLOR_BLACK, SWT.COLOR_GRAY };
56 private static final LineStyle[] LINE_STYLES = { LineStyle.SOLID, LineStyle.DASH, LineStyle.DOT, LineStyle.DASHDOT };
57
2e427755
GB
58 private final Map<String, double[]> fSeriesValues = new LinkedHashMap<>();
59 private double[] fXValues;
9392459a 60 private double fResolution;
2e427755
GB
61
62 /**
63 * Constructor
64 *
65 * @param parent
66 * The parent composite
67 * @param title
68 * The title of the viewer
69 * @param xLabel
70 * The label of the xAxis
71 * @param yLabel
72 * The label of the yAXIS
73 */
74 public TmfCommonXLineChartViewer(Composite parent, String title, String xLabel, String yLabel) {
75 super(parent, title, xLabel, yLabel);
76
9392459a 77 setResolution(RESOLUTION);
2e427755
GB
78 setTooltipProvider(new TmfCommonXLineChartTooltipProvider(this));
79 }
80
9392459a
GB
81 /**
82 * Set the number of requests per pixel that should be done on this chart
83 *
84 * @param resolution
85 * The number of points per pixels
86 */
87 protected void setResolution(double resolution) {
88 fResolution = resolution;
89 }
90
2e427755
GB
91 @Override
92 public void loadTrace(ITmfTrace trace) {
93 super.loadTrace(trace);
94 fSeriesValues.clear();
95 Thread thread = new Thread() {
96 @Override
97 public void run() {
98 initializeDataSource();
99 Display.getDefault().asyncExec(new Runnable() {
100
101 @Override
102 public void run() {
103 if (!getSwtChart().isDisposed()) {
104 /* Delete the old series */
105 clearContent();
106 createSeries();
107 }
108 }
109 });
110 }
111 };
112 thread.start();
113 }
114
115 /**
116 * Initialize the source of the data for this viewer. This method is run in
117 * a separate thread, so this is where for example one can execute an
118 * analysis module and wait for its completion to initialize the series
119 */
120 protected void initializeDataSource() {
121
122 }
123
124 @Override
125 protected void updateContent() {
126 getDisplay().asyncExec(new Runnable() {
127
128 @Override
129 public void run() {
9392459a 130 final int numRequests = (int) (getSwtChart().getPlotArea().getBounds().width * fResolution);
2e427755
GB
131 Thread thread = new Thread("Line chart update") { //$NON-NLS-1$
132 @Override
133 public void run() {
134 updateData(getWindowStartTime(), getWindowEndTime(), numRequests);
135 }
136 };
137 thread.start();
138 }
139 });
140 }
141
142 /**
143 * Convenience method to compute the values of the X axis for a given time
144 * range. This method will return nb values depending, equally separated
145 * from start to end.
146 *
147 * The returned time values are in internal time, ie to get trace time, the
148 * time offset needs to be added to those values.
149 *
150 * @param start
151 * The start time of the time range
152 * @param end
153 * End time of the range
154 * @param nb
155 * The number of steps in the x axis.
156 * @return The time values (converted to double) to match every step.
157 */
158 protected final double[] getXAxis(long start, long end, int nb) {
159 setTimeOffset(start - 1);
160
161 double timestamps[] = new double[nb];
162 long steps = (end - start);
163 double step = steps / (double) nb;
164
165 double curTime = 1;
166 for (int i = 0; i < nb; i++) {
167 timestamps[i] = curTime;
168 curTime += step;
169 }
170 return timestamps;
171 }
172
173 /**
174 * Set the values of the x axis. There is only one array of values for the x
175 * axis for all series of a line chart so it needs to be set once here.
176 *
177 * @param xaxis
178 * The values for the x axis. The values must be in internal
179 * time, ie time offset have been subtracted from trace time
180 * values.
181 */
182 protected final void setXAxis(double[] xaxis) {
183 fXValues = xaxis;
184 }
185
186 /**
187 * Update the series data because the time range has changed. The x axis
188 * values for this data update can be computed using the
c85a741e
GB
189 * {@link TmfCommonXLineChartViewer#getXAxis(long, long, int)} method which
190 * will return a list of uniformely separated time values.
2e427755
GB
191 *
192 * Each series values should be set by calling the
193 * {@link TmfCommonXLineChartViewer#setSeries(String, double[])}.
194 *
195 * This method is responsible for calling the
c85a741e
GB
196 * {@link TmfCommonXLineChartViewer#updateDisplay()} when needed for the new
197 * values to be displayed.
2e427755
GB
198 *
199 * @param start
200 * The start time of the range for which the get the data
201 * @param end
202 * The end time of the range
203 * @param nb
204 * The number of 'points' in the chart.
205 */
206 protected abstract void updateData(long start, long end, int nb);
207
208 /**
209 * Set the data for a given series of the graph. The series does not need to
210 * be created before calling this, but it needs to have at least as many
211 * values as the x axis.
212 *
213 * If the series does not exist, it will automatically be created at display
214 * time, with the default values.
215 *
216 * @param seriesName
217 * The name of the series for which to set the values
218 * @param seriesValues
219 * The array of values for the series
220 */
221 protected void setSeries(String seriesName, double[] seriesValues) {
222 if (fXValues.length > seriesValues.length) {
223 throw new IllegalStateException();
224 }
225 fSeriesValues.put(seriesName, seriesValues);
226 }
227
228 /**
229 * Add a new series to the XY line chart. By default, it is a simple solid
230 * line.
231 *
2e427755
GB
232 * @param seriesName
233 * The name of the series to create
234 * @return The series so that the concrete viewer can modify its properties
235 * if required
236 */
237 protected ILineSeries addSeries(String seriesName) {
c85a741e
GB
238 ISeriesSet seriesSet = getSwtChart().getSeriesSet();
239 int seriesCount = seriesSet.getSeries().length;
240 ILineSeries series = (ILineSeries) seriesSet.createSeries(SeriesType.LINE, seriesName);
2e427755
GB
241 series.setVisible(true);
242 series.enableArea(false);
c85a741e 243 series.setLineStyle(LINE_STYLES[(seriesCount / (LINE_COLORS.length)) % LINE_STYLES.length]);
2e427755 244 series.setSymbolType(PlotSymbolType.NONE);
c85a741e 245 series.setLineColor(Display.getDefault().getSystemColor(LINE_COLORS[seriesCount % LINE_COLORS.length]));
2e427755
GB
246 return series;
247 }
248
249 /**
250 * Delete a series from the chart and its values from the viewer.
251 *
252 * @param seriesName
253 * Name of the series to delete
254 */
255 protected void deleteSeries(String seriesName) {
256 ISeries series = getSwtChart().getSeriesSet().getSeries(seriesName);
257 if (series != null) {
258 getSwtChart().getSeriesSet().deleteSeries(series.getId());
259 }
260 fSeriesValues.remove(seriesName);
261 }
262
263 /**
264 * Update the chart's values before refreshing the viewer
265 */
266 protected void updateDisplay() {
267 Display.getDefault().asyncExec(new Runnable() {
268 final TmfChartTimeStampFormat tmfChartTimeStampFormat = new TmfChartTimeStampFormat(getTimeOffset());
269
270 @Override
271 public void run() {
272 if (!getSwtChart().isDisposed()) {
273 double maxy = DEFAULT_MAXY;
274 double miny = DEFAULT_MINY;
275 for (Entry<String, double[]> entry : fSeriesValues.entrySet()) {
276 ILineSeries series = (ILineSeries) getSwtChart().getSeriesSet().getSeries(entry.getKey());
277 if (series == null) {
278 series = addSeries(entry.getKey());
279 }
280 series.setXSeries(fXValues);
281 /* Find the minimal and maximum values in this series */
282 for (double value : entry.getValue()) {
283 maxy = Math.max(maxy, value);
284 miny = Math.min(miny, value);
285 }
286 series.setYSeries(entry.getValue());
287 }
9392459a
GB
288 if (maxy == DEFAULT_MAXY) {
289 maxy = 1.0;
290 }
2e427755
GB
291
292 IAxisTick xTick = getSwtChart().getAxisSet().getXAxis(0).getTick();
293 xTick.setFormat(tmfChartTimeStampFormat);
294
295 final double start = fXValues[0];
296 int lastX = fXValues.length - 1;
297 double end = (start == fXValues[lastX]) ? start + 1 : fXValues[lastX];
298 getSwtChart().getAxisSet().getXAxis(0).setRange(new Range(start, end));
299 getSwtChart().getAxisSet().getXAxis(0).adjustRange();
300 if (maxy > miny) {
301 getSwtChart().getAxisSet().getYAxis(0).setRange(new Range(miny, maxy));
302 }
303 getSwtChart().redraw();
304 }
305 }
306 });
307 }
308
309 /**
310 * Create the series once the initialization of the viewer's data source is
311 * done. Series do not need to be created before setting their values, but
312 * if their appearance needs to be customized, this method is a good place
313 * to do so. It is called only once per trace.
314 */
315 protected void createSeries() {
316
317 }
318
319}
This page took 0.040225 seconds and 5 git commands to generate.