| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013, 2014 Ericsson |
| 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 | * Alexandre Montplaisir - Initial API and implementation |
| 11 | * Bernd Hufmann - Updated to new TMF chart framework |
| 12 | *******************************************************************************/ |
| 13 | package org.eclipse.tracecompass.examples.ui.viewers.histogram; |
| 14 | |
| 15 | import java.util.Arrays; |
| 16 | import java.util.List; |
| 17 | |
| 18 | import org.eclipse.swt.SWT; |
| 19 | import org.eclipse.swt.widgets.Composite; |
| 20 | import org.eclipse.swt.widgets.Display; |
| 21 | import org.eclipse.tracecompass.tmf.core.statistics.ITmfStatistics; |
| 22 | import org.eclipse.tracecompass.tmf.core.statistics.TmfStatisticsModule; |
| 23 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 24 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
| 25 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; |
| 26 | import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.barcharts.TmfBarChartViewer; |
| 27 | import org.swtchart.Chart; |
| 28 | import org.swtchart.IAxis; |
| 29 | import org.swtchart.ISeries; |
| 30 | import org.swtchart.LineStyle; |
| 31 | |
| 32 | /** |
| 33 | * Histogram Viewer implementation based on TmfBarChartViewer. |
| 34 | * |
| 35 | * @author Alexandre Montplaisir |
| 36 | * @author Bernd Hufmann |
| 37 | */ |
| 38 | public class NewHistogramViewer extends TmfBarChartViewer { |
| 39 | |
| 40 | /** |
| 41 | * Creates a Histogram Viewer instance. |
| 42 | * @param parent |
| 43 | * The parent composite to draw in. |
| 44 | */ |
| 45 | public NewHistogramViewer(Composite parent) { |
| 46 | super(parent, null, null, null, TmfBarChartViewer.MINIMUM_BAR_WIDTH); |
| 47 | |
| 48 | Chart swtChart = getSwtChart(); |
| 49 | |
| 50 | IAxis xAxis = swtChart.getAxisSet().getXAxis(0); |
| 51 | IAxis yAxis = swtChart.getAxisSet().getYAxis(0); |
| 52 | |
| 53 | /* Hide the grid */ |
| 54 | xAxis.getGrid().setStyle(LineStyle.NONE); |
| 55 | yAxis.getGrid().setStyle(LineStyle.NONE); |
| 56 | |
| 57 | /* Hide the legend */ |
| 58 | swtChart.getLegend().setVisible(false); |
| 59 | |
| 60 | addSeries("Number of events", Display.getDefault().getSystemColor(SWT.COLOR_BLUE).getRGB()); //$NON-NLS-1$ |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | protected void readData(final ISeries series, final long start, final long end, final int nb) { |
| 65 | if (getTrace() != null) { |
| 66 | final double y[] = new double[nb]; |
| 67 | |
| 68 | Thread thread = new Thread("Histogram viewer update") { //$NON-NLS-1$ |
| 69 | @Override |
| 70 | public void run() { |
| 71 | double x[] = getXAxis(start, end, nb); |
| 72 | final long yLong[] = new long[nb]; |
| 73 | Arrays.fill(y, 0.0); |
| 74 | |
| 75 | /* Add the values for each trace */ |
| 76 | for (ITmfTrace trace : TmfTraceManager.getTraceSet(getTrace())) { |
| 77 | /* Retrieve the statistics object */ |
| 78 | final TmfStatisticsModule statsMod = |
| 79 | TmfTraceUtils.getAnalysisModuleOfClass(trace, TmfStatisticsModule.class, TmfStatisticsModule.ID); |
| 80 | if (statsMod == null) { |
| 81 | /* No statistics module available for this trace */ |
| 82 | continue; |
| 83 | } |
| 84 | statsMod.waitForInitialization(); |
| 85 | final ITmfStatistics stats = statsMod.getStatistics(); |
| 86 | if (stats == null) { |
| 87 | /* |
| 88 | * Should not be null after waitForInitialization() |
| 89 | * is called. |
| 90 | */ |
| 91 | throw new IllegalStateException(); |
| 92 | } |
| 93 | List<Long> values = stats.histogramQuery(start, end, nb); |
| 94 | |
| 95 | for (int i = 0; i < nb; i++) { |
| 96 | yLong[i] += values.get(i); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | for (int i = 0; i < nb; i++) { |
| 101 | y[i] += yLong[i]; /* casting from long to double */ |
| 102 | } |
| 103 | |
| 104 | /* Update the viewer */ |
| 105 | drawChart(series, x, y); |
| 106 | } |
| 107 | }; |
| 108 | thread.start(); |
| 109 | } |
| 110 | return; |
| 111 | } |
| 112 | } |