Remove all existing @since annotations
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / viewers / xycharts / barcharts / TmfBarChartViewer.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 for TMF base chart viewer
12 **********************************************************************/
13 package org.eclipse.tracecompass.tmf.ui.viewers.xycharts.barcharts;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.swt.graphics.Color;
19 import org.eclipse.swt.graphics.RGB;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.TmfChartTimeStampFormat;
23 import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.TmfXYChartViewer;
24 import org.swtchart.Chart;
25 import org.swtchart.IAxisTick;
26 import org.swtchart.IBarSeries;
27 import org.swtchart.ISeries;
28 import org.swtchart.ISeries.SeriesType;
29
30 /**
31 * Abstract bar chart viewer class implementation. Used for displaying
32 * histograms.
33 *
34 * @author Alexandre Montplaisir
35 * @author Bernd Hufmann
36 */
37 public abstract class TmfBarChartViewer extends TmfXYChartViewer {
38
39 // ------------------------------------------------------------------------
40 // Constants
41 // ------------------------------------------------------------------------
42 /** Width of each histogram bar, in pixels */
43 public static final int MINIMUM_BAR_WIDTH = 1;
44
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48 /** List of series */
49 private final List<String> seriesNames = new ArrayList<>();
50 /** List of colors */
51 private final List<RGB> colors = new ArrayList<>();
52 /** the bar width */
53 private int fBarWidth = MINIMUM_BAR_WIDTH;
54
55 // ------------------------------------------------------------------------
56 // Constructors
57 // ------------------------------------------------------------------------
58 /**
59 * Constructs a TmfXYChartViewer.
60 *
61 * @param parent
62 * The parent composite
63 * @param title
64 * The title of the viewer
65 * @param xLabel
66 * The label of the xAxis
67 * @param yLabel
68 * The label of the yAXIS
69 * @param barWidth
70 * The bar width
71 */
72 public TmfBarChartViewer(Composite parent, String title, String xLabel, String yLabel, int barWidth) {
73 super(parent, title, xLabel, yLabel);
74 fBarWidth = barWidth;
75
76 setTooltipProvider(new TmfHistogramTooltipProvider(this));
77 }
78
79 // ------------------------------------------------------------------------
80 // Operations
81 // ------------------------------------------------------------------------
82 @Override
83 protected void updateContent() {
84
85 getDisplay().asyncExec(new Runnable() {
86
87 @Override
88 public void run() {
89 Chart swtChart = getSwtChart();
90 int numRequests = swtChart.getPlotArea().getBounds().width / fBarWidth;
91
92 for (int i = 0; i < seriesNames.size(); i++) {
93 ISeries series = swtChart.getSeriesSet().getSeries(seriesNames.get(i));
94 if (series == null) {
95 series = initSeries(seriesNames.get(i), colors.get(i));
96 }
97 readData(series, getWindowStartTime(), getWindowEndTime(), numRequests);
98 }
99 }
100 });
101 }
102
103 /**
104 * Method to add a series to the chart.
105 *
106 * @param name
107 * Name of series
108 * @param color
109 * color to use for series
110 */
111 protected void addSeries(String name, RGB color) {
112 seriesNames.add(name);
113 colors.add(color);
114 }
115
116 /**
117 * Clears all series
118 */
119 protected void clearSeries() {
120 seriesNames.clear();
121 colors.clear();
122 }
123
124 /**
125 * Draw the given series on the chart
126 *
127 * @param series
128 * The series to display
129 * @param x
130 * The X values. It can be computed with
131 * {@link TmfBarChartViewer#getXAxis}
132 * The values are stored in the internal time representation.
133 * To get the trace time one has to add the time offset
134 * {@link #getTimeOffset()}.
135 * @param y
136 * The Y values that were computed by the extended class
137 */
138 protected void drawChart(final ISeries series, final double[] x, final double[] y) {
139 // Run in GUI thread to make sure that chart is ready after restart
140 final Display display = getDisplay();
141 if (display.isDisposed()) {
142 return;
143 }
144
145 display.syncExec(new Runnable() {
146 @Override
147 public void run() {
148 if (display.isDisposed()) {
149 return;
150 }
151 Chart swtChart = getSwtChart();
152 IAxisTick xTick = swtChart.getAxisSet().getXAxis(0).getTick();
153 xTick.setFormat(new TmfChartTimeStampFormat(getTimeOffset()));
154 series.setXSeries(x);
155 series.setYSeries(y);
156 xTick.setTickMarkStepHint(256);
157
158 swtChart.getAxisSet().adjustRange();
159 swtChart.redraw();
160 }
161 });
162 }
163
164 /**
165 * Convenience method to compute the X axis values for a given time range.
166 *
167 * @param start
168 * Start of the time range
169 * @param end
170 * End of the range
171 * @param nb
172 * Number of steps. This will be the size of the returned array.
173 * @return The time values (converted to double) that match every step
174 */
175 protected final double[] getXAxis(long start, long end, int nb) {
176 setTimeOffset(start - 1);
177 double timestamps[] = new double[nb];
178 long steps = (end - start);
179 double step = steps / (double) nb;
180
181 double curTime = 1;
182 for (int i = 0; i < nb; i++) {
183 timestamps[i] = curTime;
184 curTime += step;
185 }
186 return timestamps;
187 }
188
189 /**
190 * Load the data for the given series. This method should call
191 * {@link TmfBarChartViewer#drawChart} to return the results when done.
192 *
193 * Careful, this method is called by a signal handler which also happens to
194 * be in the main UI thread. This means any processing will block the UI! In
195 * most cases it's probably better to start a separate Thread/Job to do the
196 * processing, and that one can call drawChart() when done to update the
197 * view.
198 *
199 * @param series
200 * Which series of the chart should the viewer update
201 * @param start
202 * The start time (in nanoseconds) of the range to display
203 * @param end
204 * The end time of the range to display.
205 * @param nb
206 * The number of 'steps' in the bar chart (fewer steps means each
207 * bar is wider).
208 */
209 protected abstract void readData(ISeries series, long start, long end, int nb);
210
211 // initializes a series
212 private IBarSeries initSeries(String name, RGB color) {
213 IBarSeries bs = (IBarSeries) getSwtChart().getSeriesSet().createSeries(SeriesType.BAR, name);
214 bs.enableStack(true);
215 bs.setBarColor(new Color(Display.getDefault(), color));
216 bs.setBarPadding(0);
217 return bs;
218 }
219 }
This page took 0.036737 seconds and 5 git commands to generate.