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