Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ee5a032e88f709cecc7bd08bcf2b0ad829cd2985 (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*******************************************************************************
 * Copyright (c) 2015, 2019 Ericsson
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Alexis Cabana-Loriaux - Initial API and implementation
 *
 *******************************************************************************/

package org.eclipse.tracecompass.internal.tmf.ui.viewers.piecharts;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.linuxtools.dataviewers.piechart.PieChart;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.tracecompass.internal.tmf.ui.viewers.piecharts.model.TmfPieChartStatisticsModel;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.TimeGraphColorScheme;

/**
 * Creates a viewer containing 2 pie charts, one for showing information about
 * the current selection, and the second one for showing information about the
 * current time-range selection. It follows the MVC pattern, being a view.
 *
 * This class is closely related with the IPieChartViewerState interface that
 * acts as a state machine for the general layout of the charts.
 *
 * @author Alexis Cabana-Loriaux
 * @since 2.0
 *
 */
public class TmfPieChartViewer extends Composite {

    /**
     * The pie chart containing global information about the trace
     */
    private PieChart fGlobalPC;

    /**
     * The name of the piechart containing the statistics about the global trace
     */
    private String fGlobalPCname;

    /**
     * The pie chart containing information about the current time-range
     * selection
     */
    private PieChart fTimeRangePC;

    /**
     * The name of the piechart containing the statistics about the current
     * selection
     */
    private String fTimeRangePCname;

    /**
     * The listener for the mouse movement event.
     */
    private Listener fMouseMoveListener;

    /**
     * The listener for the mouse right click event.
     */
    private MouseListener fMouseClickListener;

    /**
     * The list of listener to notify when an event type is selected
     */
    private ListenerList<Listener> fEventTypeSelectedListeners = new ListenerList<>(ListenerList.IDENTITY);

    /**
     * The name of the slice containing the too little slices
     */
    private String fOthersSliceName;

    /**
     * Implementation of the State design pattern to reorder the layout
     * depending on the selection. This variable holds the current state of the
     * layout.
     */
    private IPieChartViewerState fCurrentState;

    /**
     * Represents the minimum percentage a slice of pie must have in order to be
     * shown
     */
    private static final float MIN_PRECENTAGE_TO_SHOW_SLICE = 0.025F;// 2.5%

    /**
     * Represents the maximum number of slices of the pie charts. WE don't want
     * to pollute the viewer with too much slice entries.
     */
    private static final int NB_MAX_SLICES = 10;

    /**
     * The data that has to be presented by the pie charts
     */
    private TmfPieChartStatisticsModel fModel = null;

    /** The color scheme for the chart */
    private @NonNull TimeGraphColorScheme fColorScheme = new TimeGraphColorScheme();
    /**
     * @param parent
     *            The parent composite that will hold the viewer
     */
    public TmfPieChartViewer(Composite parent) {
        super(parent, SWT.NONE);
        fGlobalPCname = Messages.TmfStatisticsView_GlobalSelectionPieChartName;
        fTimeRangePCname = Messages.TmfStatisticsView_TimeRangeSelectionPieChartName;
        fOthersSliceName = Messages.TmfStatisticsView_PieChartOthersSliceName;
        parent.addDisposeListener(e -> {
            fColorScheme.dispose();
        });
        initContent();
    }

    // ------------------------------------------------------------------------
    // Class methods
    // ------------------------------------------------------------------------

    /**
     * Called by this class' constructor. Constructs the basic viewer containing
     * the charts, as well as their listeners
     */
    private synchronized void initContent() {
        setLayout(new FillLayout());

        fGlobalPC = null;
        fTimeRangePC = null;

        // Setup listeners for the tooltips
        fMouseMoveListener = event -> {
            PieChart pc = (PieChart) event.widget;
            switch (event.type) {
            /* Get tooltip information on the slice */
            case SWT.MouseMove:
                int sliceIndex = pc.getSliceIndexFromPosition(0, event.x, event.y);
                if (sliceIndex < 0) {
                    // mouse is outside the chart
                    pc.setToolTipText(null);
                    break;
                }
                float percOfSlice = (float) pc.getSlicePercent(0, sliceIndex);
                String percent = String.format("%.1f", percOfSlice); //$NON-NLS-1$
                Long nbEvents = Long.valueOf((long) pc.getSeriesSet().getSeries()[sliceIndex].getXSeries()[0]);

                String text = Messages.TmfStatisticsView_PieChartToolTipTextName + " = " + //$NON-NLS-1$
                        pc.getSeriesSet().getSeries()[sliceIndex].getId() + "\n"; //$NON-NLS-1$

                text += Messages.TmfStatisticsView_PieChartToolTipTextEventCount + " = "//$NON-NLS-1$
                        + nbEvents.toString() + " (" + percent + "%)"; //$NON-NLS-1$ //$NON-NLS-2$
                pc.setToolTipText(text);
                return;
            default:
            }
        };

        fMouseClickListener = new MouseListener() {

            @Override
            public void mouseUp(MouseEvent e) {
                // Do nothing
            }

            @Override
            public void mouseDown(MouseEvent e) {
                PieChart pc = (PieChart) e.widget;
                int slicenb = pc.getSliceIndexFromPosition(0, e.x, e.y);
                if (slicenb < 0 || slicenb >= pc.getSeriesSet().getSeries().length) {
                    // mouse is outside the chart
                    return;
                }
                Event selectionEvent = new Event();
                selectionEvent.text = pc.getSeriesSet().getSeries()[slicenb].getId();
                notifyEventTypeSelectionListener(selectionEvent);
            }

            @Override
            public void mouseDoubleClick(MouseEvent e) {
                // Do nothing
            }
        };

        // at creation no content is selected
        setCurrentState(new PieChartViewerStateNoContentSelected(this));
    }

    /**
     * Updates the data contained in the Global PieChart by using a Map.
     * Normally, this method is only called by the state machine.
     */
    synchronized void updateGlobalPieChart() {
        if (getGlobalPC() == null) {
            fGlobalPC = new PieChart(this, SWT.NONE);
            Color backgroundColor = fColorScheme.getColor(TimeGraphColorScheme.TOOL_BACKGROUND);
            Color foregroundColor = fColorScheme.getColor(TimeGraphColorScheme.TOOL_FOREGROUND);
            getGlobalPC().getTitle().setText(fGlobalPCname);
            getGlobalPC().getTitle().setForeground(foregroundColor);
            getGlobalPC().setBackground(backgroundColor);
            getGlobalPC().setForeground(foregroundColor);
            getGlobalPC().getAxisSet().getXAxis(0).getTitle().setText(""); //Hide the title over the legend //$NON-NLS-1$
            getGlobalPC().getAxisSet().getXAxis(0).getTitle().setForeground(foregroundColor);
            getGlobalPC().getLegend().setVisible(true);
            getGlobalPC().getLegend().setPosition(SWT.RIGHT);
            getGlobalPC().getLegend().setBackground(backgroundColor);
            getGlobalPC().getLegend().setForeground(foregroundColor);
            getGlobalPC().addListener(SWT.MouseMove, fMouseMoveListener);
            getGlobalPC().addMouseListener(fMouseClickListener);
        } else if (getGlobalPC().isDisposed() || fModel == null || fModel.getPieChartGlobalModel() == null) {
            return;
        }

        Map<String, Long> totalEventCountForChart = getTotalEventCountForChart(true);

        if (totalEventCountForChart == null) {
            return;
        }

        updatePieChartWithData(fGlobalPC, totalEventCountForChart, MIN_PRECENTAGE_TO_SHOW_SLICE, fOthersSliceName);
    }

    /**
     * Updates the data contained in the Time-Range PieChart by using a Map.
     * Normally, this method is only called by the state machine.
     */
    synchronized void updateTimeRangeSelectionPieChart() {
        if (getTimeRangePC() == null) {
            Color backgroundColor = fColorScheme.getColor(TimeGraphColorScheme.TOOL_BACKGROUND);
            Color foregroundColor = fColorScheme.getColor(TimeGraphColorScheme.TOOL_FOREGROUND);
            fTimeRangePC = new PieChart(this, SWT.NONE);
            fTimeRangePC.setBackground(backgroundColor);
            fTimeRangePC.setForeground(foregroundColor);
            getTimeRangePC().getTitle().setText(fTimeRangePCname);
            getTimeRangePC().getTitle().setForeground(foregroundColor);
            getTimeRangePC().getAxisSet().getXAxis(0).getTitle().setText(""); //Hide the title over the legend //$NON-NLS-1$
            getTimeRangePC().getAxisSet().getXAxis(0).getTitle().setForeground(foregroundColor);
            getTimeRangePC().getLegend().setPosition(SWT.BOTTOM);
            getTimeRangePC().getLegend().setVisible(true);
            getTimeRangePC().getLegend().setBackground(backgroundColor);
            getTimeRangePC().getLegend().setForeground(foregroundColor);
            getTimeRangePC().addListener(SWT.MouseMove, fMouseMoveListener);
            getTimeRangePC().addMouseListener(fMouseClickListener);
        }
        else if (getTimeRangePC().isDisposed()) {
            return;
        }

        Map<String, Long> totalEventCountForChart = getTotalEventCountForChart(false);

        if (totalEventCountForChart == null) {
            return;
        }

        updatePieChartWithData(fTimeRangePC, totalEventCountForChart, MIN_PRECENTAGE_TO_SHOW_SLICE, fOthersSliceName);
    }

    /* return the chart-friendly map given by the TmfPieChartStatisticsModel */
    private Map<String, Long> getTotalEventCountForChart(boolean isGlobal) {
        if (fModel == null) {
            return null;
        }
        Map<ITmfTrace, Map<String, Long>> chartModel;
        if (isGlobal) {
            chartModel = fModel.getPieChartGlobalModel();
        } else {
            chartModel = fModel.getPieChartSelectionModel();
        }
        if (chartModel == null) {
            return null;
        }

        Map<String, Long> totalEventCountForChart = new HashMap<>();
        for (Entry<ITmfTrace, Map<String, Long>> entry : chartModel.entrySet()) {
            Map<String, Long> traceEventCount = entry.getValue();
            if (traceEventCount == null) {
                continue;
            }
            for (Entry<String, Long> event : traceEventCount.entrySet()) {
                final Long value = totalEventCountForChart.get(event.getKey());
                if (value != null) {
                    totalEventCountForChart.put(event.getKey(), value + event.getValue());
                } else {
                    totalEventCountForChart.put(event.getKey(), event.getValue());
                }
            }
        }

        return totalEventCountForChart;
    }

    /**
     * Reinitializes the charts to their initial state, without any data
     */
    public synchronized void reinitializeCharts() {
        if (isDisposed()) {
            return;
        }

        if (getGlobalPC() != null && !getGlobalPC().isDisposed()) {
            getGlobalPC().dispose();
        }
        fGlobalPC = new PieChart(this, SWT.NONE);
        getGlobalPC().getTitle().setText(fGlobalPCname);
        getGlobalPC().getAxisSet().getXAxis(0).getTitle().setText(""); //Hide the title over the legend //$NON-NLS-1$
        if (getTimeRangePC() != null && !getTimeRangePC().isDisposed()) {
            getTimeRangePC().dispose();
            fTimeRangePC = null;
        }
        layout();
        setCurrentState(new PieChartViewerStateNoContentSelected(this));
    }

    /**
     * Function used to update or create the slices of a PieChart to match the
     * content of a Map passed in parameter. It also provides a facade to use
     * the PieChart API
     */
    private static void updatePieChartWithData(
            final PieChart chart,
            final Map<String, Long> slices,
            final float minimumSizeOfSlice,
            final String nameOfOthers) {

        List<EventOccurrenceObject> chartValues = new ArrayList<>();
        Long eventTotal = 0L;
        for (Entry<String, Long> entry : slices.entrySet()) {
            eventTotal += entry.getValue();
            chartValues.add(new EventOccurrenceObject(entry.getKey(), entry.getValue()));
        }

        // No events in the selection
        if (eventTotal == 0) {
            // clear the chart and show "NO DATA"

            return;
        }

        /*
         * filter out the event types taking too little space in the chart and
         * label the whole group together. The remaining slices will be showing
         */
        List<EventOccurrenceObject> filteredChartValues = new ArrayList<>();
        Long othersEntryCount = 0L;
        int nbSlices = 0;
        for (EventOccurrenceObject entry : chartValues) {
            if (entry.getNbOccurence() / eventTotal.floatValue() > minimumSizeOfSlice && nbSlices <= NB_MAX_SLICES) {
                filteredChartValues.add(entry);
                nbSlices++;
            } else {
                othersEntryCount += entry.getNbOccurence();
            }
        }

        Collections.sort(filteredChartValues);

        // Add the "Others" slice in the pie if its not empty
        if (othersEntryCount != 0) {
            filteredChartValues.add(new EventOccurrenceObject(nameOfOthers, othersEntryCount));
        }

        // put the entries in the chart and add their percentage
        double[][] tempValues = new double[filteredChartValues.size()][1];
        String[] tempNames = new String[filteredChartValues.size()];
        int index = 0;
        for (EventOccurrenceObject entry : filteredChartValues) {
            tempValues[index][0] = entry.getNbOccurence();
            tempNames[index] = entry.getName();
            index++;
        }

        chart.addPieChartSeries(tempNames, tempValues);
    }

    /**
     * Refresh this viewer
     *
     * @param refreshGlobal
     *            if we have to refresh the global piechart
     * @param refreshSelection
     *            if we have to refresh the selection piechart
     */
    public synchronized void refresh(boolean refreshGlobal, boolean refreshSelection) {
        if (fModel == null) {
            reinitializeCharts();
        } else {
            if (refreshGlobal) {
                /* will update the global pc */
                getCurrentState().newGlobalEntries(this);
            }

            if (refreshSelection) {
                // Check if the selection is empty
                int nbEventsType = 0;
                Map<String, Long> selectionModel = getTotalEventCountForChart(false);
                for (Long l : selectionModel.values()) {
                    if (l != 0) {
                        nbEventsType++;
                    }
                }

                // Check if the selection is empty or if
                // there is enough event types to show in the piecharts
                if (nbEventsType < 2) {
                    getCurrentState().newEmptySelection(this);
                } else {
                    getCurrentState().newSelection(this);
                }
            }
        }
    }

    /**
     * @param l
     *            the listener to add
     */
    public void addEventTypeSelectionListener(Listener l) {
        fEventTypeSelectedListeners.add(l);
    }

    /**
     * @param l
     *            the listener to remove
     */
    public void removeEventTypeSelectionListener(Listener l) {
        fEventTypeSelectedListeners.remove(l);
    }

    /* Notify all listeners that an event type has been selected */
    private void notifyEventTypeSelectionListener(Event e) {
        for (Object o : fEventTypeSelectedListeners.getListeners()) {
            ((Listener) o).handleEvent(e);
        }
    }

    // ------------------------------------------------------------------------
    // Getters
    // ------------------------------------------------------------------------

    /**
     * @return the global piechart
     */
    synchronized PieChart getGlobalPC() {
        return fGlobalPC;
    }

    /**
     * @return the time-range selection piechart
     */
    synchronized PieChart getTimeRangePC() {
        return fTimeRangePC;
    }

    /**
     * @return the current state of the viewer
     */
    synchronized IPieChartViewerState getCurrentState() {
        return fCurrentState;
    }

    // ------------------------------------------------------------------------
    // Setters
    // ------------------------------------------------------------------------

    /**
     * @return the model
     */
    public TmfPieChartStatisticsModel getModel() {
        return fModel;
    }

    /**
     * @param model
     *            the model to set
     */
    public void setInput(TmfPieChartStatisticsModel model) {
        fModel = model;
    }

    /**
     * Normally, this method is only called by the state machine
     *
     * @param newChart
     *            the new PieChart
     */
    public synchronized void setTimeRangePC(PieChart newChart) {
        fTimeRangePC = newChart;
    }

    /**
     * Setter method for the state.
     *
     * @param newState
     *            The new state of the viewer Normally only called by classes
     *            implementing the IPieChartViewerState interface.
     */
    public synchronized void setCurrentState(final IPieChartViewerState newState) {
        fCurrentState = newState;
    }

    /**
     * Nested class used to handle and sort more easily the pair (Name, Number
     * of occurrences)
     *
     * @author Alexis Cabana-Loriaux
     */
    private static class EventOccurrenceObject implements Comparable<EventOccurrenceObject> {

        private String fName;

        private Long fNbOccurrences;

        EventOccurrenceObject(String name, Long nbOccurences) {
            this.fName = name;
            this.fNbOccurrences = nbOccurences;
        }

        @Override
        public int compareTo(EventOccurrenceObject other) {
            // descending order
            return Long.compare(other.getNbOccurence(), this.getNbOccurence());
        }

        public String getName() {
            return fName;
        }

        public Long getNbOccurence() {
            return fNbOccurrences;
        }
    }
}

Back to the top