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
.tracecompass
.tmf
.ui
.viewers
.xycharts
.barcharts
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
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
;
31 * Abstract bar chart viewer class implementation. Used for displaying
34 * @author Alexandre Montplaisir
35 * @author Bernd Hufmann
37 public abstract class TmfBarChartViewer
extends TmfXYChartViewer
{
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
42 /** Width of each histogram bar, in pixels */
43 public static final int MINIMUM_BAR_WIDTH
= 1;
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
49 private final List
<String
> seriesNames
= new ArrayList
<>();
51 private final List
<RGB
> colors
= new ArrayList
<>();
53 private int fBarWidth
= MINIMUM_BAR_WIDTH
;
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
59 * Constructs a TmfXYChartViewer.
62 * The parent composite
64 * The title of the viewer
66 * The label of the xAxis
68 * The label of the yAXIS
72 public TmfBarChartViewer(Composite parent
, String title
, String xLabel
, String yLabel
, int barWidth
) {
73 super(parent
, title
, xLabel
, yLabel
);
76 setTooltipProvider(new TmfHistogramTooltipProvider(this));
79 // ------------------------------------------------------------------------
81 // ------------------------------------------------------------------------
83 protected void updateContent() {
85 getDisplay().asyncExec(new Runnable() {
89 Chart swtChart
= getSwtChart();
90 int numRequests
= swtChart
.getPlotArea().getBounds().width
/ fBarWidth
;
92 for (int i
= 0; i
< seriesNames
.size(); i
++) {
93 ISeries series
= swtChart
.getSeriesSet().getSeries(seriesNames
.get(i
));
95 series
= initSeries(seriesNames
.get(i
), colors
.get(i
));
97 readData(series
, getWindowStartTime(), getWindowEndTime(), numRequests
);
104 * Method to add a series to the chart.
109 * color to use for series
111 protected void addSeries(String name
, RGB color
) {
112 seriesNames
.add(name
);
119 protected void clearSeries() {
125 * Draw the given series on the chart
128 * The series to display
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()}.
136 * The Y values that were computed by the extended class
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()) {
145 display
.syncExec(new Runnable() {
148 if (display
.isDisposed()) {
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);
158 swtChart
.getAxisSet().adjustRange();
165 * Convenience method to compute the X axis values for a given time range.
168 * Start of the time range
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
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
;
182 for (int i
= 0; i
< nb
; i
++) {
183 timestamps
[i
] = curTime
;
190 * Load the data for the given series. This method should call
191 * {@link TmfBarChartViewer#drawChart} to return the results when done.
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
200 * Which series of the chart should the viewer update
202 * The start time (in nanoseconds) of the range to display
204 * The end time of the range to display.
206 * The number of 'steps' in the bar chart (fewer steps means each
209 protected abstract void readData(ISeries series
, long start
, long end
, int nb
);
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
));
This page took 0.03988 seconds and 6 git commands to generate.