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.eclipse.jface.window.ToolTip;
|
14
|
import org.eclipse.swt.SWT;
|
15
|
import org.eclipse.swt.graphics.Point;
|
16
|
import org.eclipse.swt.layout.GridData;
|
17
|
import org.eclipse.swt.layout.GridLayout;
|
18
|
import org.eclipse.swt.widgets.Composite;
|
19
|
import org.eclipse.swt.widgets.Event;
|
20
|
import org.swtchart.Chart;
|
21
|
import org.swtchart.IAxis;
|
22
|
import org.swtchart.IBarSeries;
|
23
|
import org.swtchart.ISeries.SeriesType;
|
24
|
import org.swtchart.ISeriesLabel;
|
25
|
|
26
|
import eu.etaxonomy.cdm.model.description.Feature;
|
27
|
import eu.etaxonomy.cdm.model.description.State;
|
28
|
|
29
|
/**
|
30
|
* @author pplitzner
|
31
|
* @since Jul 6, 2018
|
32
|
*
|
33
|
*/
|
34
|
public class CategoricalChartTooltip extends ToolTip {
|
35
|
|
36
|
private CharacterMatrix matrix;
|
37
|
|
38
|
public CategoricalChartTooltip(CharacterMatrix matrix) {
|
39
|
super(matrix.getNatTable(), ToolTip.NO_RECREATE, false);
|
40
|
this.matrix = matrix;
|
41
|
}
|
42
|
|
43
|
@Override
|
44
|
protected Object getToolTipArea(Event event) {
|
45
|
int col = matrix.getNatTable().getColumnPositionByX(event.x);
|
46
|
int row = matrix.getNatTable().getRowPositionByY(event.y);
|
47
|
return new Point(col, row);
|
48
|
}
|
49
|
|
50
|
@Override
|
51
|
protected Composite createToolTipContentArea(Event event,
|
52
|
Composite parent) {
|
53
|
parent.setLayout(new GridLayout());
|
54
|
|
55
|
Chart chart = new Chart(parent, SWT.NONE);
|
56
|
chart.setLayoutData(new GridData(600, 300));
|
57
|
|
58
|
//get histogram for column
|
59
|
int colPos = matrix.getNatTable().getColumnPositionByX(event.x);
|
60
|
int colIndex = matrix.getNatTable().getColumnIndexByPosition(colPos);
|
61
|
Feature feature = matrix.getIndexToFeatureMap().get(colIndex);
|
62
|
CategoricalDataHistogram histogram = matrix.getFeatureToHistogramMap().get(feature);
|
63
|
|
64
|
// set chart title
|
65
|
chart.getTitle().setText(feature.getLabel());
|
66
|
IAxis xAxis = chart.getAxisSet().getXAxis(0);
|
67
|
|
68
|
//initalize x and y axis values
|
69
|
List<State> statesList = histogram.getStates();
|
70
|
String[] states = new String[statesList.size()];
|
71
|
double[] counts = new double[statesList.size()];
|
72
|
|
73
|
int index = 0;
|
74
|
for (State state : statesList) {
|
75
|
states[index] = state.getLabel();
|
76
|
counts[index] = histogram.getCount(state);
|
77
|
index++;
|
78
|
}
|
79
|
xAxis.setCategorySeries(states);
|
80
|
xAxis.enableCategory(true);
|
81
|
xAxis.getTitle().setVisible(false);
|
82
|
xAxis.getTick().setTickLabelAngle(25);
|
83
|
|
84
|
IAxis yAxis = chart.getAxisSet().getYAxis(0);
|
85
|
yAxis.getTitle().setVisible(false);
|
86
|
|
87
|
chart.getLegend().setVisible(false);
|
88
|
|
89
|
IBarSeries barSeries = (IBarSeries) chart.getSeriesSet()
|
90
|
.createSeries(SeriesType.BAR, "state counts");
|
91
|
ISeriesLabel seriesLabel = barSeries.getLabel();
|
92
|
seriesLabel.setVisible(true);
|
93
|
barSeries.setYSeries(counts);
|
94
|
|
95
|
chart.getAxisSet().adjustRange();
|
96
|
|
97
|
return chart;
|
98
|
}
|
99
|
|
100
|
@Override
|
101
|
protected boolean shouldCreateToolTip(Event event) {
|
102
|
//Differentiation between "index" and "position" is important here
|
103
|
//"position" is the current visible index
|
104
|
//"index" refers to the underlying data model
|
105
|
int colPos = matrix.getNatTable().getColumnPositionByX(event.x);
|
106
|
int colIndex = matrix.getNatTable().getColumnIndexByPosition(colPos);
|
107
|
int rowPos = matrix.getNatTable().getRowPositionByY(event.y);
|
108
|
Feature feature = matrix.getIndexToFeatureMap().get(colIndex);
|
109
|
CategoricalDataHistogram histogram = matrix.getFeatureToHistogramMap().get(feature);
|
110
|
|
111
|
if(matrix.isShowTooltips()
|
112
|
&& rowPos==1
|
113
|
&& colIndex>=CharacterMatrix.LEADING_COLUMN_COUNT
|
114
|
&& histogram!=null
|
115
|
&& !histogram.getStates().isEmpty()) {
|
116
|
return true;
|
117
|
}
|
118
|
return false;
|
119
|
}
|
120
|
|
121
|
}
|