| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2014, 2015 École Polytechnique de Montréal and others. |
| 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 | |
| 13 | package org.eclipse.tracecompass.tmf.ui.viewers.xycharts.linecharts; |
| 14 | |
| 15 | import java.util.LinkedHashMap; |
| 16 | import java.util.Map; |
| 17 | import java.util.Map.Entry; |
| 18 | |
| 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.TmfUiRefreshHandler; |
| 28 | import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentInfo; |
| 29 | import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentSignal; |
| 30 | import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.TmfChartTimeStampFormat; |
| 31 | import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.TmfXYChartViewer; |
| 32 | import org.swtchart.IAxisTick; |
| 33 | import org.swtchart.ILineSeries; |
| 34 | import org.swtchart.ILineSeries.PlotSymbolType; |
| 35 | import org.swtchart.ISeries; |
| 36 | import org.swtchart.ISeries.SeriesType; |
| 37 | import org.swtchart.ISeriesSet; |
| 38 | import org.swtchart.LineStyle; |
| 39 | import org.swtchart.Range; |
| 40 | |
| 41 | /** |
| 42 | * Abstract line chart viewer class implementation. All series in this viewer |
| 43 | * use the same X axis values. They are automatically created as values are |
| 44 | * provided for a key. Series by default will be displayed as a line. Each |
| 45 | * series appearance can be overridden when creating it. |
| 46 | * |
| 47 | * @author - Geneviève Bastien |
| 48 | */ |
| 49 | public abstract class TmfCommonXLineChartViewer extends TmfXYChartViewer { |
| 50 | |
| 51 | private static final double DEFAULT_MAXY = Double.MIN_VALUE; |
| 52 | private static final double DEFAULT_MINY = Double.MAX_VALUE; |
| 53 | |
| 54 | /* The desired number of points per pixel */ |
| 55 | private static final double RESOLUTION = 1.0; |
| 56 | |
| 57 | private static final int[] LINE_COLORS = { SWT.COLOR_BLUE, SWT.COLOR_RED, SWT.COLOR_GREEN, |
| 58 | SWT.COLOR_MAGENTA, SWT.COLOR_CYAN, |
| 59 | SWT.COLOR_DARK_BLUE, SWT.COLOR_DARK_RED, SWT.COLOR_DARK_GREEN, |
| 60 | SWT.COLOR_DARK_MAGENTA, SWT.COLOR_DARK_CYAN, SWT.COLOR_DARK_YELLOW, |
| 61 | SWT.COLOR_BLACK, SWT.COLOR_GRAY }; |
| 62 | private static final LineStyle[] LINE_STYLES = { LineStyle.SOLID, LineStyle.DASH, LineStyle.DOT, LineStyle.DASHDOT }; |
| 63 | |
| 64 | private final Map<String, double[]> fSeriesValues = new LinkedHashMap<>(); |
| 65 | private double[] fXValues; |
| 66 | private double fResolution; |
| 67 | |
| 68 | private UpdateThread fUpdateThread; |
| 69 | |
| 70 | /** |
| 71 | * Constructor |
| 72 | * |
| 73 | * @param parent |
| 74 | * The parent composite |
| 75 | * @param title |
| 76 | * The title of the viewer |
| 77 | * @param xLabel |
| 78 | * The label of the xAxis |
| 79 | * @param yLabel |
| 80 | * The label of the yAXIS |
| 81 | */ |
| 82 | public TmfCommonXLineChartViewer(Composite parent, String title, String xLabel, String yLabel) { |
| 83 | super(parent, title, xLabel, yLabel); |
| 84 | |
| 85 | setResolution(RESOLUTION); |
| 86 | setTooltipProvider(new TmfCommonXLineChartTooltipProvider(this)); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Set the number of requests per pixel that should be done on this chart |
| 91 | * |
| 92 | * @param resolution |
| 93 | * The number of points per pixels |
| 94 | */ |
| 95 | protected void setResolution(double resolution) { |
| 96 | fResolution = resolution; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void loadTrace(ITmfTrace trace) { |
| 101 | super.loadTrace(trace); |
| 102 | reinitialize(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Forces a reinitialization of the data sources, even if it has already |
| 107 | * been initialized for this trace before |
| 108 | */ |
| 109 | protected void reinitialize() { |
| 110 | fSeriesValues.clear(); |
| 111 | Thread thread = new Thread() { |
| 112 | @Override |
| 113 | public void run() { |
| 114 | initializeDataSource(); |
| 115 | TmfUiRefreshHandler.getInstance().queueUpdate(TmfCommonXLineChartViewer.this, |
| 116 | new Runnable() { |
| 117 | @Override |
| 118 | public void run() { |
| 119 | if (!getSwtChart().isDisposed()) { |
| 120 | /* Delete the old series */ |
| 121 | clearContent(); |
| 122 | createSeries(); |
| 123 | } |
| 124 | } |
| 125 | }); |
| 126 | } |
| 127 | }; |
| 128 | thread.start(); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Initialize the source of the data for this viewer. This method is run in |
| 133 | * a separate thread, so this is where for example one can execute an |
| 134 | * analysis module and wait for its completion to initialize the series |
| 135 | */ |
| 136 | protected void initializeDataSource() { |
| 137 | |
| 138 | } |
| 139 | |
| 140 | private class UpdateThread extends Thread { |
| 141 | private final IProgressMonitor fMonitor; |
| 142 | private final int fNumRequests; |
| 143 | |
| 144 | public UpdateThread(int numRequests) { |
| 145 | super("Line chart update"); //$NON-NLS-1$ |
| 146 | fNumRequests = numRequests; |
| 147 | fMonitor = new NullProgressMonitor(); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void run() { |
| 152 | Display.getDefault().syncExec(new Runnable() { |
| 153 | @Override |
| 154 | public void run() { |
| 155 | updateData(getWindowStartTime(), getWindowEndTime(), fNumRequests, fMonitor); |
| 156 | } |
| 157 | }); |
| 158 | updateThreadFinished(this); |
| 159 | } |
| 160 | |
| 161 | public void cancel() { |
| 162 | fMonitor.setCanceled(true); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | private synchronized void newUpdateThread() { |
| 167 | cancelUpdate(); |
| 168 | if (!getSwtChart().isDisposed()) { |
| 169 | final int numRequests = (int) (getSwtChart().getPlotArea().getBounds().width * fResolution); |
| 170 | fUpdateThread = new UpdateThread(numRequests); |
| 171 | fUpdateThread.start(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | private synchronized void updateThreadFinished(UpdateThread thread) { |
| 176 | if (thread == fUpdateThread) { |
| 177 | fUpdateThread = null; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Cancels the currently running update thread. It is automatically called |
| 183 | * when the content is updated, but child viewers may want to call it |
| 184 | * manually to do some operations before calling |
| 185 | * {@link TmfCommonXLineChartViewer#updateContent} |
| 186 | */ |
| 187 | protected synchronized void cancelUpdate() { |
| 188 | if (fUpdateThread != null) { |
| 189 | fUpdateThread.cancel(); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | protected void updateContent() { |
| 195 | getDisplay().asyncExec(new Runnable() { |
| 196 | @Override |
| 197 | public void run() { |
| 198 | newUpdateThread(); |
| 199 | } |
| 200 | }); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Convenience method to compute the values of the X axis for a given time |
| 205 | * range. This method will return nb values depending, equally separated |
| 206 | * from start to end. |
| 207 | * |
| 208 | * The returned time values are in internal time, ie to get trace time, the |
| 209 | * time offset needs to be added to those values. |
| 210 | * |
| 211 | * @param start |
| 212 | * The start time of the time range |
| 213 | * @param end |
| 214 | * End time of the range |
| 215 | * @param nb |
| 216 | * The number of steps in the x axis. |
| 217 | * @return The time values (converted to double) to match every step. |
| 218 | */ |
| 219 | protected static final double[] getXAxis(long start, long end, int nb) { |
| 220 | |
| 221 | double timestamps[] = new double[nb]; |
| 222 | long steps = (end - start); |
| 223 | double step = steps / (double) nb; |
| 224 | |
| 225 | double curTime = 1; |
| 226 | for (int i = 0; i < nb; i++) { |
| 227 | timestamps[i] = curTime; |
| 228 | curTime += step; |
| 229 | } |
| 230 | return timestamps; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Set the values of the x axis. There is only one array of values for the x |
| 235 | * axis for all series of a line chart so it needs to be set once here. |
| 236 | * |
| 237 | * @param xaxis |
| 238 | * The values for the x axis. The values must be in internal |
| 239 | * time, ie time offset have been subtracted from trace time |
| 240 | * values. |
| 241 | */ |
| 242 | protected final void setXAxis(double[] xaxis) { |
| 243 | fXValues = xaxis; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Update the series data because the time range has changed. The x axis |
| 248 | * values for this data update can be computed using the |
| 249 | * {@link TmfCommonXLineChartViewer#getXAxis(long, long, int)} method which |
| 250 | * will return a list of uniformely separated time values. |
| 251 | * |
| 252 | * Each series values should be set by calling the |
| 253 | * {@link TmfCommonXLineChartViewer#setSeries(String, double[])}. |
| 254 | * |
| 255 | * This method is responsible for calling the |
| 256 | * {@link TmfCommonXLineChartViewer#updateDisplay()} when needed for the new |
| 257 | * values to be displayed. |
| 258 | * |
| 259 | * @param start |
| 260 | * The start time of the range for which the get the data |
| 261 | * @param end |
| 262 | * The end time of the range |
| 263 | * @param nb |
| 264 | * The number of 'points' in the chart. |
| 265 | * @param monitor |
| 266 | * The progress monitor object |
| 267 | */ |
| 268 | protected abstract void updateData(long start, long end, int nb, IProgressMonitor monitor); |
| 269 | |
| 270 | /** |
| 271 | * Set the data for a given series of the graph. The series does not need to |
| 272 | * be created before calling this, but it needs to have at least as many |
| 273 | * values as the x axis. |
| 274 | * |
| 275 | * If the series does not exist, it will automatically be created at display |
| 276 | * time, with the default values. |
| 277 | * |
| 278 | * @param seriesName |
| 279 | * The name of the series for which to set the values |
| 280 | * @param seriesValues |
| 281 | * The array of values for the series |
| 282 | */ |
| 283 | protected void setSeries(String seriesName, double[] seriesValues) { |
| 284 | if (fXValues.length > seriesValues.length) { |
| 285 | throw new IllegalStateException(); |
| 286 | } |
| 287 | fSeriesValues.put(seriesName, seriesValues); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Add a new series to the XY line chart. By default, it is a simple solid |
| 292 | * line. |
| 293 | * |
| 294 | * @param seriesName |
| 295 | * The name of the series to create |
| 296 | * @return The series so that the concrete viewer can modify its properties |
| 297 | * if required |
| 298 | */ |
| 299 | protected ILineSeries addSeries(String seriesName) { |
| 300 | ISeriesSet seriesSet = getSwtChart().getSeriesSet(); |
| 301 | int seriesCount = seriesSet.getSeries().length; |
| 302 | ILineSeries series = (ILineSeries) seriesSet.createSeries(SeriesType.LINE, seriesName); |
| 303 | series.setVisible(true); |
| 304 | series.enableArea(false); |
| 305 | series.setLineStyle(LINE_STYLES[(seriesCount / (LINE_COLORS.length)) % LINE_STYLES.length]); |
| 306 | series.setSymbolType(PlotSymbolType.NONE); |
| 307 | series.setLineColor(Display.getDefault().getSystemColor(LINE_COLORS[seriesCount % LINE_COLORS.length])); |
| 308 | return series; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Delete a series from the chart and its values from the viewer. |
| 313 | * |
| 314 | * @param seriesName |
| 315 | * Name of the series to delete |
| 316 | */ |
| 317 | protected void deleteSeries(String seriesName) { |
| 318 | ISeries series = getSwtChart().getSeriesSet().getSeries(seriesName); |
| 319 | if (series != null) { |
| 320 | getSwtChart().getSeriesSet().deleteSeries(series.getId()); |
| 321 | } |
| 322 | fSeriesValues.remove(seriesName); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Update the chart's values before refreshing the viewer |
| 327 | */ |
| 328 | protected void updateDisplay() { |
| 329 | Display.getDefault().asyncExec(new Runnable() { |
| 330 | final TmfChartTimeStampFormat tmfChartTimeStampFormat = new TmfChartTimeStampFormat(getTimeOffset()); |
| 331 | |
| 332 | @Override |
| 333 | public void run() { |
| 334 | if (!getSwtChart().isDisposed()) { |
| 335 | double[] xValues = fXValues; |
| 336 | if (xValues.length < 1) { |
| 337 | return; |
| 338 | } |
| 339 | double maxy = DEFAULT_MAXY; |
| 340 | double miny = DEFAULT_MINY; |
| 341 | for (Entry<String, double[]> entry : fSeriesValues.entrySet()) { |
| 342 | ILineSeries series = (ILineSeries) getSwtChart().getSeriesSet().getSeries(entry.getKey()); |
| 343 | if (series == null) { |
| 344 | series = addSeries(entry.getKey()); |
| 345 | } |
| 346 | series.setXSeries(xValues); |
| 347 | /* Find the minimal and maximum values in this series */ |
| 348 | for (double value : entry.getValue()) { |
| 349 | maxy = Math.max(maxy, value); |
| 350 | miny = Math.min(miny, value); |
| 351 | } |
| 352 | series.setYSeries(entry.getValue()); |
| 353 | } |
| 354 | if (maxy == DEFAULT_MAXY) { |
| 355 | maxy = 1.0; |
| 356 | } |
| 357 | |
| 358 | IAxisTick xTick = getSwtChart().getAxisSet().getXAxis(0).getTick(); |
| 359 | xTick.setFormat(tmfChartTimeStampFormat); |
| 360 | |
| 361 | final double start = xValues[0]; |
| 362 | int lastX = xValues.length - 1; |
| 363 | double end = (start == xValues[lastX]) ? start + 1 : xValues[lastX]; |
| 364 | getSwtChart().getAxisSet().getXAxis(0).setRange(new Range(start, end)); |
| 365 | getSwtChart().getAxisSet().getXAxis(0).adjustRange(); |
| 366 | if (maxy > miny) { |
| 367 | getSwtChart().getAxisSet().getYAxis(0).setRange(new Range(miny, maxy)); |
| 368 | } |
| 369 | getSwtChart().redraw(); |
| 370 | |
| 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)); |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | }); |
| 383 | } |
| 384 | |
| 385 | /** |
| 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. |
| 390 | */ |
| 391 | protected void createSeries() { |
| 392 | |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | protected void clearContent() { |
| 397 | getSwtChart().getAxisSet().getXAxis(0).getTick().setFormat(null); |
| 398 | super.clearContent(); |
| 399 | } |
| 400 | |
| 401 | } |