Merge branch 'develop' into taxonDescription
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / descriptiveDataSet / matrix / QuantitativeChartTooltip.java
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.FillLayout;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Event;
24 import org.eclipse.swt.widgets.Label;
25 import org.swtchart.Chart;
26 import org.swtchart.IAxis;
27 import org.swtchart.ILineSeries;
28 import org.swtchart.ILineSeries.PlotSymbolType;
29 import org.swtchart.ISeries.SeriesType;
30 import org.swtchart.LineStyle;
31
32 import eu.etaxonomy.cdm.model.description.Feature;
33
34 /**
35 * @author pplitzner
36 * @since Jul 6, 2018
37 *
38 */
39 public class QuantitativeChartTooltip extends DefaultToolTip {
40
41 private CharacterMatrix matrix;
42
43 public QuantitativeChartTooltip(CharacterMatrix matrix) {
44 super(matrix.getNatTable(), ToolTip.NO_RECREATE, false);
45 this.matrix = matrix;
46 }
47
48 @Override
49 protected Object getToolTipArea(Event event) {
50 int col = matrix.getNatTable().getColumnPositionByX(event.x);
51 int row = matrix.getNatTable().getRowPositionByY(event.y);
52 return new Point(col, row);
53 }
54
55 @Override
56 protected Composite createToolTipContentArea(Event event,
57 Composite parent) {
58
59 int colPos = matrix.getNatTable().getColumnPositionByX(event.x);
60 int colIndex = matrix.getNatTable().getColumnIndexByPosition(colPos);
61 Feature feature = matrix.getIndexToFeatureMap().get(colIndex);
62 QuantitativeDataStatistics dataStatistics = matrix.getFeatureToQuantDataStatisticsMap().get(feature);
63
64 Composite tooltip;
65 if(dataStatistics!=null){
66 parent.setLayout(new GridLayout());
67 tooltip = createTooltip(event, parent);
68 }
69 else{
70 tooltip = new Composite(parent, SWT.NONE);
71 tooltip.setLayout(new FillLayout());
72 Label label = new Label(tooltip, SWT.NONE);
73 label.setText(CategoricalChartTooltip.TOOLTIP_NOT_AVAILABLE);
74 }
75 return tooltip;
76
77 }
78
79 private Composite createTooltip(Event event, Composite parent){
80 Chart chart = new Chart(parent, SWT.NONE);
81 chart.setLayoutData(new GridData(600, 300));
82
83 //get statistics for column
84 int colPos = matrix.getNatTable().getColumnPositionByX(event.x);
85 int colIndex = matrix.getNatTable().getColumnIndexByPosition(colPos);
86 Feature feature = matrix.getIndexToFeatureMap().get(colIndex);
87 QuantitativeDataStatistics quantitativeDataStatistics = matrix.getFeatureToQuantDataStatisticsMap().get(feature);
88 List<DescriptiveStatistics> statistics = quantitativeDataStatistics.getStatistics();
89
90 //prepare series
91 double[] exactMean = statistics.stream().mapToDouble(statistic->statistic.getMean()).toArray();
92 double[] mins = statistics.stream().mapToDouble(statistic->statistic.getMin()).toArray();
93 double[] maxs = statistics.stream().mapToDouble(statistic->statistic.getMax()).toArray();
94
95 // set chart title
96 chart.getTitle().setText(feature.getLabel());
97
98 // create exact/mean series
99 ILineSeries meanExactSeries = (ILineSeries) chart.getSeriesSet()
100 .createSeries(SeriesType.LINE, "exact/mean");
101 meanExactSeries.setYSeries(exactMean);
102 meanExactSeries.setLineStyle(LineStyle.NONE);
103 meanExactSeries.setSymbolColor(GUIHelper.COLOR_BLUE);
104
105 // create min series
106 ILineSeries minSeries = (ILineSeries) chart.getSeriesSet()
107 .createSeries(SeriesType.LINE, "min");
108 minSeries.setYSeries(mins);
109 minSeries.setLineStyle(LineStyle.NONE);
110 minSeries.setSymbolType(PlotSymbolType.INVERTED_TRIANGLE);
111 minSeries.setSymbolColor(GUIHelper.COLOR_GREEN);
112
113 // create max series
114 ILineSeries maxSeries = (ILineSeries) chart.getSeriesSet()
115 .createSeries(SeriesType.LINE, "max");
116 maxSeries.setYSeries(maxs);
117 maxSeries.setLineStyle(LineStyle.NONE);
118 maxSeries.setSymbolType(PlotSymbolType.TRIANGLE);
119 maxSeries.setSymbolColor(GUIHelper.COLOR_RED);
120
121 IAxis yAxis = chart.getAxisSet().getYAxis(0);
122 yAxis.getTitle().setVisible(false);
123 IAxis xAxis = chart.getAxisSet().getXAxis(0);
124 xAxis.getTitle().setVisible(false);
125
126 chart.getAxisSet().adjustRange();
127
128 return chart;
129 }
130
131 @Override
132 protected boolean shouldCreateToolTip(Event event) {
133 //Differentiation between "index" and "position" is important here
134 //"position" is the current visible index
135 //"index" refers to the underlying data model
136 int colPos = matrix.getNatTable().getColumnPositionByX(event.x);
137 int colIndex = matrix.getNatTable().getColumnIndexByPosition(colPos);
138 int rowPos = matrix.getNatTable().getRowPositionByY(event.y);
139 Feature feature = matrix.getIndexToFeatureMap().get(colIndex);
140
141 if(matrix.isShowTooltips()
142 && rowPos==1
143 && feature!=null
144 && feature.isSupportsQuantitativeData()
145 && colIndex>=CharacterMatrix.LEADING_COLUMN_COUNT) {
146 return true;
147 }
148 return false;
149 }
150
151 }