Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0d316cd3412da7b77c25770f0cb1632edc5c608f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*******************************************************************************
 * Copyright (c) 2010, 2018 IBM Corporation and others.
 * 
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    IBM Corporation - initial API and implementation
 *    Red Hat Inc. - modified to handle SWTChart 0.9.0 vs 0.8.0; ongoing maintenance
 *******************************************************************************/

package org.eclipse.linuxtools.internal.systemtap.graphing.ui.charts;

import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.linuxtools.internal.systemtap.graphing.ui.charts.listeners.ChartWithAxisMouseMoveListener;
import org.eclipse.linuxtools.internal.systemtap.graphing.ui.preferences.GraphingPreferenceConstants;
import org.eclipse.linuxtools.systemtap.graphing.core.adapters.IAdapter;
import org.eclipse.linuxtools.systemtap.graphing.ui.charts.AbstractChartBuilder;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.swtchart.IAxis;
import org.swtchart.ISeries;
import org.swtchart.ITitle;
import org.swtchart.LineStyle;
import org.swtchart.Range;

/**
 * A {@link AbstractChartBuilder} for building a chart with axes.
 * @author Qi Liang
 */
public abstract class AbstractChartWithAxisBuilder extends AbstractChartBuilder {

    private PaintListener titleBoundsPaintListener;
    private double defaultMargin = 0.04;
    /**
     * @return The size of the chart's left margin.
     * @since 3.0
     */
    protected double getChartMarginXL() {
        return defaultMargin;
    }
    /**
     * @return The size of the chart's right margin.
     * @since 3.0
     */
    protected double getChartMarginXU() {
        return defaultMargin;
    }
    /**
     * @return The size of the chart's top margin.
     * @since 3.0
     */
    protected double getChartMarginYL() {
        return defaultMargin;
    }
    /**
     * @return The size of the chart's bottom margin.
     * @since 3.0
     */
    protected double getChartMarginYU() {
        return defaultMargin;
    }

    protected boolean xLineGrid, yLineGrid;
    /**
     * @since 3.0
     */
    protected int xSeriesTicks, ySeriesTicks;

    /**
     * Creates a chart series for this chart.
     * @param i The index of the series to create.
     * @return The newly created series.
     */
    protected abstract ISeries createChartISeries(int i);

    @Override
    protected void updateProperties(PropertyChangeEvent event) {
        super.updateProperties(event);
        String eventName = event.getProperty();
        if (eventName.equals(GraphingPreferenceConstants.P_SHOW_X_GRID_LINES)) {
            xLineGrid = store.getBoolean(GraphingPreferenceConstants.P_SHOW_X_GRID_LINES);
            buildXAxis();
        } else if (eventName.equals(GraphingPreferenceConstants.P_SHOW_Y_GRID_LINES)) {
            yLineGrid = store.getBoolean(GraphingPreferenceConstants.P_SHOW_Y_GRID_LINES);
            buildYAxis();
        } else if (eventName.equals(GraphingPreferenceConstants.P_X_SERIES_TICKS)) {
            xSeriesTicks = store.getInt(GraphingPreferenceConstants.P_X_SERIES_TICKS);
            buildXAxis();
        } else if (eventName.equals(GraphingPreferenceConstants.P_Y_SERIES_TICKS)) {
            ySeriesTicks = store.getInt(GraphingPreferenceConstants.P_Y_SERIES_TICKS);
            buildYAxis();
        }
    }

    /**
     * Constructs a builder for a chart with axes and associates it to one data set.
     * @param adapter An {@link IAdapter} for reading from the chart's data set.
     * @param parent The parent {@link Composite} that will contain this chart builder.
     * @param style The style of the chart to construct.
     * @param title The title of the chart to construct.
     */
    public AbstractChartWithAxisBuilder(IAdapter adapter, Composite parent, int style, String title) {
        super(adapter, parent, style, title);
        xLineGrid = store.getBoolean(GraphingPreferenceConstants.P_SHOW_X_GRID_LINES);
        yLineGrid = store.getBoolean(GraphingPreferenceConstants.P_SHOW_Y_GRID_LINES);
        xSeriesTicks = store.getInt(GraphingPreferenceConstants.P_X_SERIES_TICKS);
        ySeriesTicks = store.getInt(GraphingPreferenceConstants.P_Y_SERIES_TICKS);
    }

    @Override
    protected void createChart() {
        super.createChart();
        applyTitleBoundsListener();
        chartMouseMoveListener = new ChartWithAxisMouseMoveListener(chart, chart.getPlotArea());
    }

    /**
     * After this method is called, the chart's title will (from then on) be centered with the plot area.
     * @since 3.0
     */
    protected void applyTitleBoundsListener() {
        ITitle title = chart.getTitle();
        // Underlying SWT Chart implementation changes from the title being a Control to just
        // a PaintListener.  In the Control class case, we can move it's location to
        // center over a PieChart, but in the latter case, we need to alter the title
        // with blanks in the PaintListener and have the title paint after it
        // once the title has been altered.
        if (title instanceof Control) {
            titleBoundsPaintListener = e -> {
			    Rectangle bounds = chart.getPlotArea().getBounds();
			    Control title1 = (Control) chart.getTitle();
			    Rectangle titleBounds = title1.getBounds();
			    title1.setLocation(new Point(bounds.x + (bounds.width - titleBounds.width) / 2, title1.getLocation().y));
			};
            chart.addPaintListener(titleBoundsPaintListener);
        } else {
            // move title paint listener to end
            chart.removePaintListener((PaintListener)title);
            titleBoundsPaintListener = e -> {
			    ITitle title1 = chart.getTitle();
			    Font font = title1.getFont();
			    Font oldFont = e.gc.getFont();
			    e.gc.setFont(font);
			    Control legend = (Control)chart.getLegend();
			    Rectangle legendBounds = legend.getBounds();
			    int adjustment = legendBounds.width - 15;
			    Point blankSize = e.gc.textExtent(" "); //$NON-NLS-1$
			    int numBlanks = ((adjustment / blankSize.x) >> 1) << 1;
			    String text = title1.getText().trim();
			    for (int i = 0; i < numBlanks; ++i) {
			        text += " "; //$NON-NLS-1$
			    }
			    e.gc.setFont(oldFont);
			    title1.setText(text);
			};
            chart.addPaintListener(titleBoundsPaintListener);
            chart.addPaintListener((PaintListener)title);
        }
    }

    /**
     * Removes the chart's paint listener for repositioning the title, if one has been applied.
     * @since 3.0
     */
    protected void removeTitleBoundsListener() {
        if (titleBoundsPaintListener != null) {
            chart.removePaintListener(titleBoundsPaintListener);
            titleBoundsPaintListener = null;
        }
    }

    /**
     * Builds X axis.
     */
    @Override
    protected void buildXAxis() {
        String labels[] = adapter.getLabels();
        IAxis xAxis = this.chart.getAxisSet().getXAxis(0);
        if (xLineGrid) {
            xAxis.getGrid().setStyle(LineStyle.SOLID);
        } else {
            xAxis.getGrid().setStyle(LineStyle.NONE);
        }
        xAxis.getTick().setForeground(BLACK);
        xAxis.getTick().setTickMarkStepHint(xSeriesTicks);
        ITitle xTitle = xAxis.getTitle();
        xTitle.setForeground(BLACK);

        if (labels.length > 0) {
            xTitle.setText(labels[0]);
        }
        else {
            xTitle.setText(""); //$NON-NLS-1$
        }
    }

    /**
     * Builds Y axis.
     */
    @Override
    protected void buildYAxis() {
        IAxis yAxis = this.chart.getAxisSet().getYAxis(0);
        yAxis.getTitle().setText(""); //$NON-NLS-1$
        if (yLineGrid) {
            yAxis.getGrid().setStyle(LineStyle.SOLID);
        } else {
            yAxis.getGrid().setStyle(LineStyle.NONE);
        }
        yAxis.getTick().setForeground(BLACK);
        yAxis.getTick().setTickMarkStepHint(ySeriesTicks);
    }

    /**
     * Builds X series.
     */
    @Override
    protected void buildXSeries() {
        Object data[][] = adapter.getData();
        if (data == null || data.length == 0) {
            return;
        }

        int start = 0, len = Math.min(this.maxItems, data.length), leny = data[0].length-1;
        if (this.maxItems < data.length) {
            start = data.length - this.maxItems;
        }

        Double[] all_valx = new Double[len];
        Double[][] all_valy = new Double[leny][len];
        // Will want to centre view around points, so be as accurate with max/min as possible.
        double maxX = Double.NEGATIVE_INFINITY;
        double maxY = maxX;
        double minX = Double.POSITIVE_INFINITY;
        double minY = minX;

        // Read in from the data array all x/y points to plot.
        // In the case of an empty (null) value in either axis, ignore both x & y axis data for that point.
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < leny + 1; j++) {
                Double val = getDoubleOrNullValue(data[start + i][j]);
                if (j == 0) {
                    if (val != null) {
                        all_valx[i] = val;
                        maxX = Math.max(val, maxX);
                        minX = Math.min(val, minX);
                    } else {
                        break;
                    }
                } else if (val != null) {
                    all_valy[j-1][i] = val;
                    maxY = Math.max(val, maxY);
                    minY = Math.min(val, minY);
                }
            }
        }

        // Now create dense arrays of x/y values that exclude null values,
        // and plot those values to the chart.

        ISeries allSeries[] = chart.getSeriesSet().getSeries();
        ISeries series = null;
        for (int i = 0; i < leny; i++) {
            if (i >= allSeries.length) {
                series = createChartISeries(i);
            } else {
                series = chart.getSeriesSet().getSeries()[i];
            }

            double[] valx = new double[len];
            double[] valy = new double[len];
            int len_trim = 0;
            for (int j = 0; j < len; j++) {
                if (all_valx[j] != null && all_valy[i][j] != null) {
                    valx[len_trim] = all_valx[j].doubleValue();
                    valy[len_trim] = all_valy[i][j].doubleValue();
                    len_trim++;
                }
            }
            double[] valx_trim = new double[len_trim];
            double[] valy_trim = new double[len_trim];
            for (int j = 0; j < len_trim; j++) {
                valx_trim[j] = valx[j];
                valy_trim[j] = valy[j];
            }
            series.setXSeries(valx_trim);
            series.setYSeries(valy_trim);
        }

        if (series != null && series.getXSeries().length > 0) {
            applyRangeX(minX, maxX);
            applyRangeY(minY, maxY);
        }
        chart.redraw();
    }

    /**
     * Updates the visible range of the chart's x-axis.
     * @param min The smallest x-value that should be in range.
     * @param max The largest x-value that should be in range.
     */
    private void applyRangeX(double min, double max) {
        IAxis axis = chart.getAxisSet().getXAxis(0);
        double actualRange = max - min;
        double scaledRange = actualRange * scale;
        double marginL = scaledRange > 0 ? scaledRange * getChartMarginXL() : 1;
        double marginU = scaledRange > 0 ? scaledRange * getChartMarginXU() : 1;

        double lower = (actualRange - scaledRange) * scroll + min;
        axis.setRange(new Range(lower - marginL, lower + scaledRange + marginU));
    }

    /**
     * Updates the visible range of the chart's y-axis.
     * @param min The smallest y-value that should be in range.
     * @param max The largest y-value that should be in range.
     * @since 3.0
     */
    protected void applyRangeY(double min, double max) {
        IAxis axis = chart.getAxisSet().getYAxis(0);
        double actualRange = max - min;
        double scaledRange = actualRange * scaleY;
        double marginL = scaledRange > 0 ? scaledRange * getChartMarginYL() : 1;
        double marginU = scaledRange > 0 ? scaledRange * getChartMarginYU() : 1;

        double lower = (actualRange - scaledRange) * scrollY + min;
        axis.setRange(new Range(lower - marginL, lower + scaledRange + marginU));
    }

    @Override
    public void updateDataSet() {
        buildXSeries();
        chartMouseMoveListener.update();
    }

    @Override
    protected void buildYSeries() {
    }
}

Back to the top