Project

General

Profile

Download (4.85 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix;
10

    
11
import java.util.List;
12

    
13
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
14
import org.eclipse.jface.window.DefaultToolTip;
15
import org.eclipse.jface.window.ToolTip;
16
import org.eclipse.nebula.widgets.nattable.util.GUIHelper;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.graphics.Point;
19
import org.eclipse.swt.layout.GridData;
20
import org.eclipse.swt.layout.GridLayout;
21
import org.eclipse.swt.widgets.Composite;
22
import org.eclipse.swt.widgets.Event;
23
import org.swtchart.Chart;
24
import org.swtchart.IAxis;
25
import org.swtchart.ILineSeries;
26
import org.swtchart.ILineSeries.PlotSymbolType;
27
import org.swtchart.ISeries.SeriesType;
28
import org.swtchart.LineStyle;
29

    
30
import eu.etaxonomy.cdm.model.description.Feature;
31

    
32
/**
33
 * @author pplitzner
34
 * @since Jul 6, 2018
35
 *
36
 */
37
public class QuantitativeChartTooltip extends DefaultToolTip {
38

    
39
    private CharacterMatrix matrix;
40

    
41
    public QuantitativeChartTooltip(CharacterMatrix matrix) {
42
        super(matrix.getNatTable(), ToolTip.NO_RECREATE, false);
43
        this.matrix = matrix;
44
    }
45

    
46
    @Override
47
    protected Object getToolTipArea(Event event) {
48
        int col = matrix.getNatTable().getColumnPositionByX(event.x);
49
        int row = matrix.getNatTable().getRowPositionByY(event.y);
50
        return new Point(col, row);
51
    }
52

    
53
    @Override
54
    protected Composite createToolTipContentArea(Event event,
55
            Composite parent) {
56
        parent.setLayout(new GridLayout());
57

    
58
        Chart chart = new Chart(parent, SWT.NONE);
59
        chart.setLayoutData(new GridData(600, 300));
60

    
61
        //get statistics for column
62
        int colPos = matrix.getNatTable().getColumnPositionByX(event.x);
63
        int colIndex = matrix.getNatTable().getColumnIndexByPosition(colPos);
64
        Feature feature = matrix.getIndexToFeatureMap().get(colIndex);
65
        QuantitativeDataStatistics quantitativeDataStatistics = matrix.getFeatureToQuantDataStatisticsMap().get(feature);
66
        List<DescriptiveStatistics> statistics = quantitativeDataStatistics.getStatistics();
67

    
68
        //prepare series
69
        double[] exactMean = statistics.stream().mapToDouble(statistic->statistic.getMean()).toArray();
70
        double[] mins = statistics.stream().mapToDouble(statistic->statistic.getMin()).toArray();
71
        double[] maxs = statistics.stream().mapToDouble(statistic->statistic.getMax()).toArray();
72

    
73
        // set chart title
74
        chart.getTitle().setText(feature.getLabel());
75

    
76
        // create exact/mean series
77
        ILineSeries meanExactSeries = (ILineSeries) chart.getSeriesSet()
78
                .createSeries(SeriesType.LINE, "exact/mean");
79
        meanExactSeries.setYSeries(exactMean);
80
        meanExactSeries.setLineStyle(LineStyle.NONE);
81
        meanExactSeries.setSymbolColor(GUIHelper.COLOR_BLUE);
82

    
83
        // create min series
84
        ILineSeries minSeries = (ILineSeries) chart.getSeriesSet()
85
                .createSeries(SeriesType.LINE, "min");
86
        minSeries.setYSeries(mins);
87
        minSeries.setLineStyle(LineStyle.NONE);
88
        minSeries.setSymbolType(PlotSymbolType.INVERTED_TRIANGLE);
89
        minSeries.setSymbolColor(GUIHelper.COLOR_GREEN);
90

    
91
        // create max series
92
        ILineSeries maxSeries = (ILineSeries) chart.getSeriesSet()
93
                .createSeries(SeriesType.LINE, "max");
94
        maxSeries.setYSeries(maxs);
95
        maxSeries.setLineStyle(LineStyle.NONE);
96
        maxSeries.setSymbolType(PlotSymbolType.TRIANGLE);
97
        maxSeries.setSymbolColor(GUIHelper.COLOR_RED);
98

    
99
        IAxis yAxis = chart.getAxisSet().getYAxis(0);
100
        yAxis.getTitle().setVisible(false);
101
        IAxis xAxis = chart.getAxisSet().getXAxis(0);
102
        xAxis.getTitle().setVisible(false);
103

    
104
        chart.getAxisSet().adjustRange();
105

    
106
        return chart;
107
    }
108

    
109
    @Override
110
    protected boolean shouldCreateToolTip(Event event) {
111
        //Differentiation between "index" and "position" is important here
112
        //"position" is the current visible index
113
        //"index" refers to the underlying data model
114
        int colPos = matrix.getNatTable().getColumnPositionByX(event.x);
115
        int colIndex = matrix.getNatTable().getColumnIndexByPosition(colPos);
116
        int rowPos = matrix.getNatTable().getRowPositionByY(event.y);
117
        Feature feature = matrix.getIndexToFeatureMap().get(colIndex);
118
        QuantitativeDataStatistics dataStatistics = matrix.getFeatureToQuantDataStatisticsMap().get(feature);
119

    
120
        if(matrix.isShowTooltips()
121
                && rowPos==1
122
                && colIndex>=CharacterMatrix.LEADING_COLUMN_COUNT
123
                && dataStatistics!=null) {
124
            return true;
125
        }
126
        return false;
127
    }
128

    
129
}
(17-17/20)