ref #7549 Add min and max data series to quant data chart
authorPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 18 Jul 2018 12:09:25 +0000 (14:09 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 18 Jul 2018 12:09:25 +0000 (14:09 +0200)
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/matrix/CharacterMatrix.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/matrix/CharacterMatrixBottomToolbar.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/matrix/QuantitativeChartTooltip.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/matrix/QuantitativeDataStatistics.java [new file with mode: 0644]

index 451bddaeeedda49645fd4e54ffa1ee4059a2f492..9b2cc1d15771dade351d8cbfc6cd9ade74d714dc 100644 (file)
@@ -22,7 +22,6 @@ import java.util.stream.Collectors;
 import javax.inject.Inject;
 
 import org.apache.commons.collections4.map.LinkedMap;
-import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
 import org.eclipse.core.runtime.ICoreRunnable;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
@@ -139,7 +138,7 @@ public class CharacterMatrix extends Composite {
 
     private Map<Feature, CategoricalDataHistogram> featureToHistogramMap = new HashMap<>();
 
-    private Map<Feature, DescriptiveStatistics> featureToQuantStatisticsMap = new HashMap<>();
+    private Map<Feature, QuantitativeDataStatistics> featureToQuantDataStatisticsMap = new HashMap<>();
 
     private ListDataProvider<Object> bodyDataProvider;
 
@@ -633,8 +632,8 @@ public class CharacterMatrix extends Composite {
         return featureToHistogramMap;
     }
 
-    public Map<Feature, DescriptiveStatistics> getFeatureToQuantStatisticsMap() {
-        return featureToQuantStatisticsMap;
+    public Map<Feature, QuantitativeDataStatistics> getFeatureToQuantDataStatisticsMap() {
+        return featureToQuantDataStatisticsMap;
     }
 
     public void toogleIsShowTooltips() {
index c3e76bc53d602cab37c84578749d3eb93e9caa6a..759f136f6e1fb1a132446b96487c025c52aac421 100644 (file)
@@ -18,7 +18,6 @@ import java.util.Set;
 import java.util.UUID;
 import java.util.stream.Collectors;
 
-import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
 import org.eclipse.jface.layout.GridDataFactory;
 import org.eclipse.jface.window.Window;
 import org.eclipse.swt.SWT;
@@ -39,7 +38,6 @@ import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
 import eu.etaxonomy.cdm.model.description.Feature;
 import eu.etaxonomy.cdm.model.description.QuantitativeData;
 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
-import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
@@ -160,7 +158,7 @@ public class CharacterMatrixBottomToolbar extends Composite{
                 matrix.setDirty();
 
                 aggregateCategorcialHistogram(matrix.getFeatureToHistogramMap());
-                aggregateQuantitativeSummary(matrix.getFeatureToQuantStatisticsMap());
+                aggregateQuantitativeSummary(matrix.getFeatureToQuantDataStatisticsMap());
             }
 
         });
@@ -185,21 +183,19 @@ public class CharacterMatrixBottomToolbar extends Composite{
     }
 
     @SuppressWarnings("unchecked")
-    private void aggregateQuantitativeSummary(Map<Feature, DescriptiveStatistics> featureToQuantStatisticsMap) {
-        featureToQuantStatisticsMap.clear();
+    private void aggregateQuantitativeSummary(Map<Feature, QuantitativeDataStatistics> featureToQuantDataStatisticsMap) {
+        featureToQuantDataStatisticsMap.clear();
         matrix.getDescriptions()
         .forEach(o -> ((RowWrapperDTO) o).getSpecimenDescription().getElements().stream()
                 .filter(descriptionElement -> descriptionElement instanceof QuantitativeData)
                 .forEach(quantData -> {
                     Feature feature = ((QuantitativeData) quantData).getFeature();
-                    DescriptiveStatistics statistics = featureToQuantStatisticsMap.get(feature);
-                    if(statistics==null){
-                        statistics = new DescriptiveStatistics();
+                    QuantitativeDataStatistics dataStatistics = featureToQuantDataStatisticsMap.get(feature);
+                    if(dataStatistics==null){
+                        dataStatistics = new QuantitativeDataStatistics();
                     }
-                    featureToQuantStatisticsMap.put(feature, statistics);
-                    ((QuantitativeData) quantData).getStatisticalValues().stream()
-                    .filter(value -> value.getType().equals(StatisticalMeasure.EXACT_VALUE()))
-                    .forEach(value -> featureToQuantStatisticsMap.get(feature).addValue(value.getValue()));
+                    featureToQuantDataStatisticsMap.put(feature, dataStatistics);
+                    dataStatistics.addQuantitativeData((QuantitativeData) quantData);
                 }));
     }
 
index baf77eb92cb3106ba790f7aa21e6321f93f8d9a7..93bad1ba5815f16d405cf08b505f952cadcf9a02 100644 (file)
@@ -8,7 +8,7 @@
 */
 package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix;
 
-import java.util.Arrays;
+import java.util.List;
 
 import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
 import org.eclipse.jface.window.DefaultToolTip;
@@ -23,6 +23,7 @@ import org.eclipse.swt.widgets.Event;
 import org.swtchart.Chart;
 import org.swtchart.IAxis;
 import org.swtchart.ILineSeries;
+import org.swtchart.ILineSeries.PlotSymbolType;
 import org.swtchart.ISeries.SeriesType;
 import org.swtchart.LineStyle;
 
@@ -61,9 +62,13 @@ public class QuantitativeChartTooltip extends DefaultToolTip {
         int colPos = matrix.getNatTable().getColumnPositionByX(event.x);
         int colIndex = matrix.getNatTable().getColumnIndexByPosition(colPos);
         Feature feature = matrix.getIndexToFeatureMap().get(colIndex);
-        DescriptiveStatistics statistics = matrix.getFeatureToQuantStatisticsMap().get(feature);
-        double[] ySeries = statistics.getValues();
-        Arrays.sort(ySeries);
+        QuantitativeDataStatistics quantitativeDataStatistics = matrix.getFeatureToQuantDataStatisticsMap().get(feature);
+        List<DescriptiveStatistics> statistics = quantitativeDataStatistics.getStatistics();
+
+        //prepare series
+        double[] exactMean = statistics.stream().mapToDouble(statistic->statistic.getMean()).toArray();
+        double[] mins = statistics.stream().mapToDouble(statistic->statistic.getMin()).toArray();
+        double[] maxs = statistics.stream().mapToDouble(statistic->statistic.getMax()).toArray();
 
         // set chart title
         chart.getTitle().setText(feature.getLabel());
@@ -71,28 +76,25 @@ public class QuantitativeChartTooltip extends DefaultToolTip {
         // create exact/mean series
         ILineSeries meanExactSeries = (ILineSeries) chart.getSeriesSet()
                 .createSeries(SeriesType.LINE, "exact/mean");
-        meanExactSeries.setYSeries(ySeries);
+        meanExactSeries.setYSeries(exactMean);
         meanExactSeries.setLineStyle(LineStyle.NONE);
         meanExactSeries.setSymbolColor(GUIHelper.COLOR_BLUE);
 
-        //TODO get min and max values
         // create min series
-//        ILineSeries minSeries = (ILineSeries) chart.getSeriesSet()
-//                .createSeries(SeriesType.LINE, "min");
-//        double[] mins = { 0.3, 0.4, 0.3, 0.9, 1.1, 2.5, 2.6, 2.6};
-//        minSeries.setYSeries(mins);
-//        minSeries.setLineStyle(LineStyle.NONE);
-//        minSeries.setSymbolType(PlotSymbolType.INVERTED_TRIANGLE);
-//        minSeries.setSymbolColor(GUIHelper.COLOR_GREEN);
-//
-//        // create max series
-//        ILineSeries maxSeries = (ILineSeries) chart.getSeriesSet()
-//                .createSeries(SeriesType.LINE, "max");
-//        double[] maxs = { 1.3, 2.4, 2.5, 2.6, 2.8, 3.6, 3.6, 3.8 };
-//        maxSeries.setYSeries(maxs);
-//        maxSeries.setLineStyle(LineStyle.NONE);
-//        maxSeries.setSymbolType(PlotSymbolType.TRIANGLE);
-//        maxSeries.setSymbolColor(GUIHelper.COLOR_RED);
+        ILineSeries minSeries = (ILineSeries) chart.getSeriesSet()
+                .createSeries(SeriesType.LINE, "min");
+        minSeries.setYSeries(mins);
+        minSeries.setLineStyle(LineStyle.NONE);
+        minSeries.setSymbolType(PlotSymbolType.INVERTED_TRIANGLE);
+        minSeries.setSymbolColor(GUIHelper.COLOR_GREEN);
+
+        // create max series
+        ILineSeries maxSeries = (ILineSeries) chart.getSeriesSet()
+                .createSeries(SeriesType.LINE, "max");
+        maxSeries.setYSeries(maxs);
+        maxSeries.setLineStyle(LineStyle.NONE);
+        maxSeries.setSymbolType(PlotSymbolType.TRIANGLE);
+        maxSeries.setSymbolColor(GUIHelper.COLOR_RED);
 
         IAxis yAxis = chart.getAxisSet().getYAxis(0);
         yAxis.getTitle().setVisible(false);
@@ -113,12 +115,12 @@ public class QuantitativeChartTooltip extends DefaultToolTip {
         int colIndex = matrix.getNatTable().getColumnIndexByPosition(colPos);
         int rowPos = matrix.getNatTable().getRowPositionByY(event.y);
         Feature feature = matrix.getIndexToFeatureMap().get(colIndex);
-        DescriptiveStatistics statistics = matrix.getFeatureToQuantStatisticsMap().get(feature);
+        QuantitativeDataStatistics dataStatistics = matrix.getFeatureToQuantDataStatisticsMap().get(feature);
 
         if(matrix.isShowTooltips()
                 && rowPos==1
                 && colIndex>=CharacterMatrix.LEADING_COLUMN_COUNT
-                && statistics!=null) {
+                && dataStatistics!=null) {
             return true;
         }
         return false;
diff --git a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/matrix/QuantitativeDataStatistics.java b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/matrix/QuantitativeDataStatistics.java
new file mode 100644 (file)
index 0000000..d010a50
--- /dev/null
@@ -0,0 +1,55 @@
+/**
+* Copyright (C) 2018 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
+
+import eu.etaxonomy.cdm.model.description.QuantitativeData;
+import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
+
+/**
+ * @author pplitzner
+ * @since Jul 18, 2018
+ *
+ */
+public class QuantitativeDataStatistics {
+
+    private List<DescriptiveStatistics> statistics = new ArrayList<>();
+
+    public void addQuantitativeData(QuantitativeData quantitativeData){
+        double[] exactValues = quantitativeData.getStatisticalValues().stream()
+                .filter(value -> value.getType().equals(StatisticalMeasure.EXACT_VALUE()))
+                .mapToDouble(value -> value.getValue()).toArray();
+        //if there are exact values we use those
+        if (exactValues.length > 0) {
+            statistics.add(new DescriptiveStatistics(exactValues));
+        }
+        //otherwise we use the aggregated values (min, max, etc.)
+        else{
+            DescriptiveStatistics aggregatedStatistics = new DescriptiveStatistics();
+            if(quantitativeData.getMin()!=null){
+                aggregatedStatistics.addValue(quantitativeData.getMin().doubleValue());
+            }
+            if(quantitativeData.getMax()!=null){
+                aggregatedStatistics.addValue(quantitativeData.getMax().doubleValue());
+            }
+            statistics.add(aggregatedStatistics);
+        }
+        Collections.sort(statistics, (s1, s2)->Double.compare(s1.getMean(), s2.getMean()));
+    }
+
+    public List<DescriptiveStatistics> getStatistics() {
+        return statistics;
+    }
+
+}