doc: Move the examples plugin to the doc/ directory
[deliverable/tracecompass.git] / doc / org.eclipse.tracecompass.examples / src / org / eclipse / tracecompass / examples / ui / viewers / histogram / NewHistogramViewer.java
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 static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.util.Arrays;
18 import java.util.List;
19
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Display;
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;
27 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
28 import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.barcharts.TmfBarChartViewer;
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 */
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())) {
79 trace = checkNotNull(trace);
80 /* Retrieve the statistics object */
81 final TmfStatisticsModule statsMod =
82 TmfTraceUtils.getAnalysisModuleOfClass(trace, TmfStatisticsModule.class, TmfStatisticsModule.ID);
83 if (statsMod == null) {
84 /* No statistics module available for this trace */
85 continue;
86 }
87 statsMod.waitForInitialization();
88 final ITmfStatistics stats = statsMod.getStatistics();
89 if (stats == null) {
90 /*
91 * Should not be null after waitForInitialization()
92 * is called.
93 */
94 throw new IllegalStateException();
95 }
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 }
This page took 0.037293 seconds and 5 git commands to generate.