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 java
.util
.LinkedHashMap
;
17 import java
.util
.Map
.Entry
;
19 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
20 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
22 import org
.eclipse
.linuxtools
.tmf
.ui
.TmfUiRefreshHandler
;
23 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.xycharts
.TmfChartTimeStampFormat
;
24 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.xycharts
.TmfXYChartViewer
;
25 import org
.eclipse
.swt
.SWT
;
26 import org
.eclipse
.swt
.widgets
.Composite
;
27 import org
.eclipse
.swt
.widgets
.Display
;
28 import org
.swtchart
.IAxisTick
;
29 import org
.swtchart
.ILineSeries
;
30 import org
.swtchart
.ILineSeries
.PlotSymbolType
;
31 import org
.swtchart
.ISeries
;
32 import org
.swtchart
.ISeries
.SeriesType
;
33 import org
.swtchart
.ISeriesSet
;
34 import org
.swtchart
.LineStyle
;
35 import org
.swtchart
.Range
;
38 * Abstract line chart viewer class implementation. All series in this viewer
39 * use the same X axis values. They are automatically created as values are
40 * provided for a key. Series by default will be displayed as a line. Each
41 * series appearance can be overridden when creating it.
43 * @author - Geneviève Bastien
46 public abstract class TmfCommonXLineChartViewer
extends TmfXYChartViewer
{
48 private static final double DEFAULT_MAXY
= Double
.MIN_VALUE
;
49 private static final double DEFAULT_MINY
= Double
.MAX_VALUE
;
51 /* The desired number of points per pixel */
52 private static final double RESOLUTION
= 1.0;
54 private static final int[] LINE_COLORS
= { SWT
.COLOR_BLUE
, SWT
.COLOR_RED
, SWT
.COLOR_GREEN
,
55 SWT
.COLOR_MAGENTA
, SWT
.COLOR_CYAN
,
56 SWT
.COLOR_DARK_BLUE
, SWT
.COLOR_DARK_RED
, SWT
.COLOR_DARK_GREEN
,
57 SWT
.COLOR_DARK_MAGENTA
, SWT
.COLOR_DARK_CYAN
, SWT
.COLOR_DARK_YELLOW
,
58 SWT
.COLOR_BLACK
, SWT
.COLOR_GRAY
};
59 private static final LineStyle
[] LINE_STYLES
= { LineStyle
.SOLID
, LineStyle
.DASH
, LineStyle
.DOT
, LineStyle
.DASHDOT
};
61 private final Map
<String
, double[]> fSeriesValues
= new LinkedHashMap
<>();
62 private double[] fXValues
;
63 private double fResolution
;
65 private UpdateThread fUpdateThread
;
71 * The parent composite
73 * The title of the viewer
75 * The label of the xAxis
77 * The label of the yAXIS
79 public TmfCommonXLineChartViewer(Composite parent
, String title
, String xLabel
, String yLabel
) {
80 super(parent
, title
, xLabel
, yLabel
);
82 setResolution(RESOLUTION
);
83 setTooltipProvider(new TmfCommonXLineChartTooltipProvider(this));
87 * Set the number of requests per pixel that should be done on this chart
90 * The number of points per pixels
92 protected void setResolution(double resolution
) {
93 fResolution
= resolution
;
97 public void loadTrace(ITmfTrace trace
) {
98 super.loadTrace(trace
);
103 * Forces a reinitialization of the data sources, even if it has already
104 * been initialized for this trace before
108 protected void reinitialize() {
109 fSeriesValues
.clear();
110 Thread thread
= new Thread() {
113 initializeDataSource();
114 TmfUiRefreshHandler
.getInstance().queueUpdate(TmfCommonXLineChartViewer
.this,
118 if (!getSwtChart().isDisposed()) {
119 /* Delete the old series */
131 * Initialize the source of the data for this viewer. This method is run in
132 * a separate thread, so this is where for example one can execute an
133 * analysis module and wait for its completion to initialize the series
135 protected void initializeDataSource() {
139 private class UpdateThread
extends Thread
{
140 private final IProgressMonitor fMonitor
;
141 private final int fNumRequests
;
143 public UpdateThread(int numRequests
) {
144 super("Line chart update"); //$NON-NLS-1$
145 fNumRequests
= numRequests
;
146 fMonitor
= new NullProgressMonitor();
151 updateData(getWindowStartTime(), getWindowEndTime(), fNumRequests
, fMonitor
);
152 updateThreadFinished(this);
155 public void cancel() {
156 fMonitor
.setCanceled(true);
160 private synchronized void newUpdateThread() {
162 final int numRequests
= (int) (getSwtChart().getPlotArea().getBounds().width
* fResolution
);
163 fUpdateThread
= new UpdateThread(numRequests
);
164 fUpdateThread
.start();
167 private synchronized void updateThreadFinished(UpdateThread thread
) {
168 if (thread
== fUpdateThread
) {
169 fUpdateThread
= null;
174 * Cancels the currently running update thread. It is automatically called
175 * when the content is updated, but child viewers may want to call it
176 * manually to do some operations before calling
177 * {@link TmfCommonXLineChartViewer#updateContent}
179 protected synchronized void cancelUpdate() {
180 if (fUpdateThread
!= null) {
181 fUpdateThread
.cancel();
186 protected void updateContent() {
187 getDisplay().asyncExec(new Runnable() {
196 * Convenience method to compute the values of the X axis for a given time
197 * range. This method will return nb values depending, equally separated
200 * The returned time values are in internal time, ie to get trace time, the
201 * time offset needs to be added to those values.
204 * The start time of the time range
206 * End time of the range
208 * The number of steps in the x axis.
209 * @return The time values (converted to double) to match every step.
211 protected final double[] getXAxis(long start
, long end
, int nb
) {
212 setTimeOffset(start
- 1);
214 double timestamps
[] = new double[nb
];
215 long steps
= (end
- start
);
216 double step
= steps
/ (double) nb
;
219 for (int i
= 0; i
< nb
; i
++) {
220 timestamps
[i
] = curTime
;
227 * Set the values of the x axis. There is only one array of values for the x
228 * axis for all series of a line chart so it needs to be set once here.
231 * The values for the x axis. The values must be in internal
232 * time, ie time offset have been subtracted from trace time
235 protected final void setXAxis(double[] xaxis
) {
240 * Update the series data because the time range has changed. The x axis
241 * values for this data update can be computed using the
242 * {@link TmfCommonXLineChartViewer#getXAxis(long, long, int)} method which
243 * will return a list of uniformely separated time values.
245 * Each series values should be set by calling the
246 * {@link TmfCommonXLineChartViewer#setSeries(String, double[])}.
248 * This method is responsible for calling the
249 * {@link TmfCommonXLineChartViewer#updateDisplay()} when needed for the new
250 * values to be displayed.
253 * The start time of the range for which the get the data
255 * The end time of the range
257 * The number of 'points' in the chart.
259 * The progress monitor object
261 protected abstract void updateData(long start
, long end
, int nb
, IProgressMonitor monitor
);
264 * Set the data for a given series of the graph. The series does not need to
265 * be created before calling this, but it needs to have at least as many
266 * values as the x axis.
268 * If the series does not exist, it will automatically be created at display
269 * time, with the default values.
272 * The name of the series for which to set the values
273 * @param seriesValues
274 * The array of values for the series
276 protected void setSeries(String seriesName
, double[] seriesValues
) {
277 if (fXValues
.length
> seriesValues
.length
) {
278 throw new IllegalStateException();
280 fSeriesValues
.put(seriesName
, seriesValues
);
284 * Add a new series to the XY line chart. By default, it is a simple solid
288 * The name of the series to create
289 * @return The series so that the concrete viewer can modify its properties
292 protected ILineSeries
addSeries(String seriesName
) {
293 ISeriesSet seriesSet
= getSwtChart().getSeriesSet();
294 int seriesCount
= seriesSet
.getSeries().length
;
295 ILineSeries series
= (ILineSeries
) seriesSet
.createSeries(SeriesType
.LINE
, seriesName
);
296 series
.setVisible(true);
297 series
.enableArea(false);
298 series
.setLineStyle(LINE_STYLES
[(seriesCount
/ (LINE_COLORS
.length
)) % LINE_STYLES
.length
]);
299 series
.setSymbolType(PlotSymbolType
.NONE
);
300 series
.setLineColor(Display
.getDefault().getSystemColor(LINE_COLORS
[seriesCount
% LINE_COLORS
.length
]));
305 * Delete a series from the chart and its values from the viewer.
308 * Name of the series to delete
310 protected void deleteSeries(String seriesName
) {
311 ISeries series
= getSwtChart().getSeriesSet().getSeries(seriesName
);
312 if (series
!= null) {
313 getSwtChart().getSeriesSet().deleteSeries(series
.getId());
315 fSeriesValues
.remove(seriesName
);
319 * Update the chart's values before refreshing the viewer
321 protected void updateDisplay() {
322 Display
.getDefault().asyncExec(new Runnable() {
323 final TmfChartTimeStampFormat tmfChartTimeStampFormat
= new TmfChartTimeStampFormat(getTimeOffset());
327 if (!getSwtChart().isDisposed()) {
328 double maxy
= DEFAULT_MAXY
;
329 double miny
= DEFAULT_MINY
;
330 for (Entry
<String
, double[]> entry
: fSeriesValues
.entrySet()) {
331 ILineSeries series
= (ILineSeries
) getSwtChart().getSeriesSet().getSeries(entry
.getKey());
332 if (series
== null) {
333 series
= addSeries(entry
.getKey());
335 series
.setXSeries(fXValues
);
336 /* Find the minimal and maximum values in this series */
337 for (double value
: entry
.getValue()) {
338 maxy
= Math
.max(maxy
, value
);
339 miny
= Math
.min(miny
, value
);
341 series
.setYSeries(entry
.getValue());
343 if (maxy
== DEFAULT_MAXY
) {
347 IAxisTick xTick
= getSwtChart().getAxisSet().getXAxis(0).getTick();
348 xTick
.setFormat(tmfChartTimeStampFormat
);
350 final double start
= fXValues
[0];
351 int lastX
= fXValues
.length
- 1;
352 double end
= (start
== fXValues
[lastX
]) ? start
+ 1 : fXValues
[lastX
];
353 getSwtChart().getAxisSet().getXAxis(0).setRange(new Range(start
, end
));
354 getSwtChart().getAxisSet().getXAxis(0).adjustRange();
356 getSwtChart().getAxisSet().getYAxis(0).setRange(new Range(miny
, maxy
));
358 getSwtChart().redraw();
365 * Create the series once the initialization of the viewer's data source is
366 * done. Series do not need to be created before setting their values, but
367 * if their appearance needs to be customized, this method is a good place
368 * to do so. It is called only once per trace.
370 protected void createSeries() {