1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal and others.
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
.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
.swt
.SWT
;
22 import org
.eclipse
.swt
.graphics
.Point
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.swt
.widgets
.Display
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
27 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
28 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
29 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.TmfChartTimeStampFormat
;
30 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.TmfXYChartViewer
;
31 import org
.swtchart
.IAxisTick
;
32 import org
.swtchart
.ILineSeries
;
33 import org
.swtchart
.ILineSeries
.PlotSymbolType
;
34 import org
.swtchart
.ISeries
;
35 import org
.swtchart
.ISeries
.SeriesType
;
36 import org
.swtchart
.ISeriesSet
;
37 import org
.swtchart
.LineStyle
;
38 import org
.swtchart
.Range
;
41 * Abstract line chart viewer class implementation. All series in this viewer
42 * use the same X axis values. They are automatically created as values are
43 * provided for a key. Series by default will be displayed as a line. Each
44 * series appearance can be overridden when creating it.
46 * @author - Geneviève Bastien
48 public abstract class TmfCommonXLineChartViewer
extends TmfXYChartViewer
{
50 private static final double DEFAULT_MAXY
= Double
.MIN_VALUE
;
51 private static final double DEFAULT_MINY
= Double
.MAX_VALUE
;
53 /* The desired number of points per pixel */
54 private static final double RESOLUTION
= 1.0;
56 private static final int[] LINE_COLORS
= { SWT
.COLOR_BLUE
, SWT
.COLOR_RED
, SWT
.COLOR_GREEN
,
57 SWT
.COLOR_MAGENTA
, SWT
.COLOR_CYAN
,
58 SWT
.COLOR_DARK_BLUE
, SWT
.COLOR_DARK_RED
, SWT
.COLOR_DARK_GREEN
,
59 SWT
.COLOR_DARK_MAGENTA
, SWT
.COLOR_DARK_CYAN
, SWT
.COLOR_DARK_YELLOW
,
60 SWT
.COLOR_BLACK
, SWT
.COLOR_GRAY
};
61 private static final LineStyle
[] LINE_STYLES
= { LineStyle
.SOLID
, LineStyle
.DASH
, LineStyle
.DOT
, LineStyle
.DASHDOT
};
63 private final Map
<String
, double[]> fSeriesValues
= new LinkedHashMap
<>();
64 private double[] fXValues
;
65 private double fResolution
;
67 private UpdateThread fUpdateThread
;
73 * The parent composite
75 * The title of the viewer
77 * The label of the xAxis
79 * The label of the yAXIS
81 public TmfCommonXLineChartViewer(Composite parent
, String title
, String xLabel
, String yLabel
) {
82 super(parent
, title
, xLabel
, yLabel
);
84 setResolution(RESOLUTION
);
85 setTooltipProvider(new TmfCommonXLineChartTooltipProvider(this));
89 * Set the number of requests per pixel that should be done on this chart
92 * The number of points per pixels
94 protected void setResolution(double resolution
) {
95 fResolution
= resolution
;
99 public void loadTrace(ITmfTrace trace
) {
100 super.loadTrace(trace
);
105 * Forces a reinitialization of the data sources, even if it has already
106 * been initialized for this trace before
108 protected void reinitialize() {
109 fSeriesValues
.clear();
110 Thread thread
= new Thread() {
111 // Don't use TmfUiRefreshHandler (bug 467751)
114 initializeDataSource();
115 if (!getSwtChart().isDisposed()) {
116 getDisplay().asyncExec(new Runnable() {
119 if (!getSwtChart().isDisposed()) {
120 /* Delete the old series */
133 * Initialize the source of the data for this viewer. This method is run in
134 * a separate thread, so this is where for example one can execute an
135 * analysis module and wait for its completion to initialize the series
137 protected void initializeDataSource() {
141 private class UpdateThread
extends Thread
{
142 private final IProgressMonitor fMonitor
;
143 private final int fNumRequests
;
145 public UpdateThread(int numRequests
) {
146 super("Line chart update"); //$NON-NLS-1$
147 fNumRequests
= numRequests
;
148 fMonitor
= new NullProgressMonitor();
153 Display
.getDefault().syncExec(new Runnable() {
156 updateData(getWindowStartTime(), getWindowEndTime(), fNumRequests
, fMonitor
);
159 updateThreadFinished(this);
162 public void cancel() {
163 fMonitor
.setCanceled(true);
167 private synchronized void newUpdateThread() {
169 if (!getSwtChart().isDisposed()) {
170 final int numRequests
= (int) (getSwtChart().getPlotArea().getBounds().width
* fResolution
);
171 fUpdateThread
= new UpdateThread(numRequests
);
172 fUpdateThread
.start();
176 private synchronized void updateThreadFinished(UpdateThread thread
) {
177 if (thread
== fUpdateThread
) {
178 fUpdateThread
= null;
183 * Cancels the currently running update thread. It is automatically called
184 * when the content is updated, but child viewers may want to call it
185 * manually to do some operations before calling
186 * {@link TmfCommonXLineChartViewer#updateContent}
188 protected synchronized void cancelUpdate() {
189 if (fUpdateThread
!= null) {
190 fUpdateThread
.cancel();
195 protected void updateContent() {
196 getDisplay().asyncExec(new Runnable() {
205 * Convenience method to compute the values of the X axis for a given time
206 * range. This method will return nb values depending, equally separated
209 * The returned time values are in internal time, ie to get trace time, the
210 * time offset needs to be added to those values.
213 * The start time of the time range
215 * End time of the range
217 * The number of steps in the x axis.
218 * @return The time values (converted to double) to match every step.
220 protected static final double[] getXAxis(long start
, long end
, int nb
) {
222 double timestamps
[] = new double[nb
];
223 long steps
= (end
- start
);
224 double step
= steps
/ (double) nb
;
227 for (int i
= 0; i
< nb
; i
++) {
228 timestamps
[i
] = curTime
;
235 * Set the values of the x axis. There is only one array of values for the x
236 * axis for all series of a line chart so it needs to be set once here.
239 * The values for the x axis. The values must be in internal
240 * time, ie time offset have been subtracted from trace time
243 protected final void setXAxis(double[] xaxis
) {
248 * Update the series data because the time range has changed. The x axis
249 * values for this data update can be computed using the
250 * {@link TmfCommonXLineChartViewer#getXAxis(long, long, int)} method which
251 * will return a list of uniformely separated time values.
253 * Each series values should be set by calling the
254 * {@link TmfCommonXLineChartViewer#setSeries(String, double[])}.
256 * This method is responsible for calling the
257 * {@link TmfCommonXLineChartViewer#updateDisplay()} when needed for the new
258 * values to be displayed.
261 * The start time of the range for which the get the data
263 * The end time of the range
265 * The number of 'points' in the chart.
267 * The progress monitor object
269 protected abstract void updateData(long start
, long end
, int nb
, IProgressMonitor monitor
);
272 * Set the data for a given series of the graph. The series does not need to
273 * be created before calling this, but it needs to have at least as many
274 * values as the x axis.
276 * If the series does not exist, it will automatically be created at display
277 * time, with the default values.
280 * The name of the series for which to set the values
281 * @param seriesValues
282 * The array of values for the series
284 protected void setSeries(String seriesName
, double[] seriesValues
) {
285 if (fXValues
.length
> seriesValues
.length
) {
286 throw new IllegalStateException();
288 fSeriesValues
.put(seriesName
, seriesValues
);
292 * Add a new series to the XY line chart. By default, it is a simple solid
296 * The name of the series to create
297 * @return The series so that the concrete viewer can modify its properties
300 protected ILineSeries
addSeries(String seriesName
) {
301 ISeriesSet seriesSet
= getSwtChart().getSeriesSet();
302 int seriesCount
= seriesSet
.getSeries().length
;
303 ILineSeries series
= (ILineSeries
) seriesSet
.createSeries(SeriesType
.LINE
, seriesName
);
304 series
.setVisible(true);
305 series
.enableArea(false);
306 series
.setLineStyle(LINE_STYLES
[(seriesCount
/ (LINE_COLORS
.length
)) % LINE_STYLES
.length
]);
307 series
.setSymbolType(PlotSymbolType
.NONE
);
308 series
.setLineColor(Display
.getDefault().getSystemColor(LINE_COLORS
[seriesCount
% LINE_COLORS
.length
]));
313 * Delete a series from the chart and its values from the viewer.
316 * Name of the series to delete
318 protected void deleteSeries(String seriesName
) {
319 ISeries series
= getSwtChart().getSeriesSet().getSeries(seriesName
);
320 if (series
!= null) {
321 getSwtChart().getSeriesSet().deleteSeries(series
.getId());
323 fSeriesValues
.remove(seriesName
);
327 * Update the chart's values before refreshing the viewer
329 protected void updateDisplay() {
330 Display
.getDefault().asyncExec(new Runnable() {
331 final TmfChartTimeStampFormat tmfChartTimeStampFormat
= new TmfChartTimeStampFormat(getTimeOffset());
335 if (!getSwtChart().isDisposed()) {
336 double[] xValues
= fXValues
;
337 if (xValues
.length
< 1) {
340 double maxy
= DEFAULT_MAXY
;
341 double miny
= DEFAULT_MINY
;
342 for (Entry
<String
, double[]> entry
: fSeriesValues
.entrySet()) {
343 ILineSeries series
= (ILineSeries
) getSwtChart().getSeriesSet().getSeries(entry
.getKey());
344 if (series
== null) {
345 series
= addSeries(entry
.getKey());
347 series
.setXSeries(xValues
);
348 /* Find the minimal and maximum values in this series */
349 for (double value
: entry
.getValue()) {
350 maxy
= Math
.max(maxy
, value
);
351 miny
= Math
.min(miny
, value
);
353 series
.setYSeries(entry
.getValue());
355 if (maxy
== DEFAULT_MAXY
) {
359 IAxisTick xTick
= getSwtChart().getAxisSet().getXAxis(0).getTick();
360 xTick
.setFormat(tmfChartTimeStampFormat
);
362 final double start
= xValues
[0];
363 int lastX
= xValues
.length
- 1;
364 double end
= (start
== xValues
[lastX
]) ? start
+ 1 : xValues
[lastX
];
365 getSwtChart().getAxisSet().getXAxis(0).setRange(new Range(start
, end
));
367 getSwtChart().getAxisSet().getYAxis(0).setRange(new Range(miny
, maxy
));
369 getSwtChart().redraw();
371 if (isSendTimeAlignSignals()) {
372 // The width of the chart might have changed and its
373 // time axis might be misaligned with the other views
374 Point viewPos
= TmfCommonXLineChartViewer
.this.getParent().getParent().toDisplay(0, 0);
375 int axisPos
= getSwtChart().toDisplay(0, 0).x
+ getPointAreaOffset();
376 int timeAxisOffset
= axisPos
- viewPos
.x
;
377 TmfTimeViewAlignmentInfo timeAlignmentInfo
= new TmfTimeViewAlignmentInfo(getControl().getShell(), viewPos
, timeAxisOffset
);
378 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(TmfCommonXLineChartViewer
.this, timeAlignmentInfo
, true));
386 * Create the series once the initialization of the viewer's data source is
387 * done. Series do not need to be created before setting their values, but
388 * if their appearance needs to be customized, this method is a good place
389 * to do so. It is called only once per trace.
391 protected void createSeries() {
396 protected void clearContent() {
397 getSwtChart().getAxisSet().getXAxis(0).getTick().setFormat(null);
398 super.clearContent();