1
/**********************************************************************
2
* Copyright (c) 2013, 2014 Ericsson
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
10
* Alexandre Montplaisir - Initial API and implementation
11
* Bernd Hufmann - Updated for TMF base chart viewer
12
**********************************************************************/
13
package org.eclipse.linuxtools.tmf.ui.viewers.xycharts.barcharts;
15
import java.util.ArrayList;
16
import java.util.List;
18
import org.eclipse.linuxtools.tmf.ui.viewers.xycharts.TmfChartTimeStampFormat;
19
import org.eclipse.linuxtools.tmf.ui.viewers.xycharts.TmfXYChartViewer;
20
import org.eclipse.swt.graphics.Color;
21
import org.eclipse.swt.graphics.RGB;
22
import org.eclipse.swt.widgets.Composite;
23
import org.eclipse.swt.widgets.Display;
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;
31
* Abstract bar chart viewer class implementation. Used for displaying
34
* @author Alexandre Montplaisir
35
* @author Bernd Hufmann
38
public abstract class TmfBarChartViewer extends TmfXYChartViewer {
40
// ------------------------------------------------------------------------
42
// ------------------------------------------------------------------------
43
/** Width of each histogram bar, in pixels */
44
public static final int MINIMUM_BAR_WIDTH = 1;
46
// ------------------------------------------------------------------------
48
// ------------------------------------------------------------------------
50
private final List<String> seriesNames = new ArrayList<>();
52
private final List<RGB> colors = new ArrayList<>();
54
private int fBarWidth = MINIMUM_BAR_WIDTH;
56
// ------------------------------------------------------------------------
58
// ------------------------------------------------------------------------
60
* Constructs a TmfXYChartViewer.
63
* The parent composite
65
* The title of the viewer
67
* The label of the xAxis
69
* The label of the yAXIS
73
public TmfBarChartViewer(Composite parent, String title, String xLabel, String yLabel, int barWidth) {
74
super(parent, title, xLabel, yLabel);
77
setTooltipProvider(new TmfHistogramTooltipProvider(this));
80
// ------------------------------------------------------------------------
82
// ------------------------------------------------------------------------
84
protected void updateContent() {
86
getDisplay().asyncExec(new Runnable() {
90
Chart swtChart = getSwtChart();
91
int numRequests = swtChart.getPlotArea().getBounds().width / fBarWidth;
93
for (int i = 0; i < seriesNames.size(); i++) {
94
ISeries series = swtChart.getSeriesSet().getSeries(seriesNames.get(i));
96
series = initSeries(seriesNames.get(i), colors.get(i));
98
readData(series, getWindowStartTime(), getWindowEndTime(), numRequests);
105
* Method to add a series to the chart.
110
* color to use for series
112
protected void addSeries(String name, RGB color) {
113
seriesNames.add(name);
120
protected void clearSeries() {
126
* Draw the given series on the chart
129
* The series to display
131
* The X values. It can be computed with
132
* {@link TmfBarChartViewer#getXAxis}
133
* The values are stored in the internal time representation.
134
* To get the trace time one has to add the time offset
135
* {@link #getTimeOffset()}.
137
* The Y values that were computed by the extended class
139
protected void drawChart(final ISeries series, final double[] x, final double[] y) {
140
// Run in GUI thread to make sure that chart is ready after restart
141
final Display display = getDisplay();
142
if (display.isDisposed()) {
146
display.syncExec(new Runnable() {
149
if (display.isDisposed()) {
152
Chart swtChart = getSwtChart();
153
IAxisTick xTick = swtChart.getAxisSet().getXAxis(0).getTick();
154
xTick.setFormat(new TmfChartTimeStampFormat(getTimeOffset()));
155
series.setXSeries(x);
156
series.setYSeries(y);
157
xTick.setTickMarkStepHint(256);
159
swtChart.getAxisSet().adjustRange();
166
* Convenience method to compute the X axis values for a given time range.
169
* Start of the time range
173
* Number of steps. This will be the size of the returned array.
174
* @return The time values (converted to double) that match every step
176
protected final double[] getXAxis(long start, long end, int nb) {
177
setTimeOffset(start - 1);
178
double timestamps[] = new double[nb];
179
long steps = (end - start);
180
double step = steps / (double) nb;
183
for (int i = 0; i < nb; i++) {
184
timestamps[i] = curTime;
191
* Load the data for the given series. This method should call
192
* {@link TmfBarChartViewer#drawChart} to return the results when done.
194
* Careful, this method is called by a signal handler which also happens to
195
* be in the main UI thread. This means any processing will block the UI! In
196
* most cases it's probably better to start a separate Thread/Job to do the
197
* processing, and that one can call drawChart() when done to update the
201
* Which series of the chart should the viewer update
203
* The start time (in nanoseconds) of the range to display
205
* The end time of the range to display.
207
* The number of 'steps' in the bar chart (fewer steps means each
210
protected abstract void readData(ISeries series, long start, long end, int nb);
212
// initializes a series
213
private IBarSeries initSeries(String name, RGB color) {
214
IBarSeries bs = (IBarSeries) getSwtChart().getSeriesSet().createSeries(SeriesType.BAR, name);
215
bs.enableStack(true);
216
bs.setBarColor(new Color(Display.getDefault(), color));
b'\\ No newline at end of file'