From 1edf4352423e285a24f04bff7ca39c5436c31be7 Mon Sep 17 00:00:00 2001
From: tpylak <tpylak>
Date: Mon, 15 Nov 2010 14:09:20 +0000
Subject: [PATCH] LMS-1838 heatmap basics

SVN: 18692
---
 .../detailviewers/HeatmapUtil.java            |  83 +-----------
 .../heatmaps/IHeatmapRenderer.java            |  34 +++++
 .../heatmaps/StringHeatmapRenderer.java       | 123 ++++++++++++++++++
 .../detailviewers/heatmaps/dto/Color.java     |  35 +++++
 .../heatmaps/dto/HeatmapScaleRange.java       |  43 ++++++
 5 files changed, 239 insertions(+), 79 deletions(-)
 create mode 100644 screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/heatmaps/IHeatmapRenderer.java
 create mode 100644 screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/heatmaps/StringHeatmapRenderer.java
 create mode 100644 screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/heatmaps/dto/Color.java
 create mode 100644 screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/heatmaps/dto/HeatmapScaleRange.java

diff --git a/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/HeatmapUtil.java b/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/HeatmapUtil.java
index 0e32a388f6c..738145b4a8f 100644
--- a/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/HeatmapUtil.java
+++ b/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/HeatmapUtil.java
@@ -16,13 +16,14 @@
 
 package ch.systemsx.cisd.openbis.plugin.screening.client.web.client.application.detailviewers;
 
-import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.List;
-import java.util.Set;
 
 import com.google.gwt.user.client.ui.Widget;
 
+import ch.systemsx.cisd.openbis.plugin.screening.client.web.client.application.detailviewers.heatmaps.IHeatmapRenderer;
+import ch.systemsx.cisd.openbis.plugin.screening.client.web.client.application.detailviewers.heatmaps.dto.Color;
+import ch.systemsx.cisd.openbis.plugin.screening.client.web.client.application.detailviewers.heatmaps.dto.HeatmapScaleRange;
+
 /**
  * @author Tomasz Pylak
  */
@@ -30,37 +31,6 @@ public class HeatmapUtil
 {
     // --- DTOs
 
-    static class Color
-    {
-        private final String hexColor;
-
-        public Color(String hexColor)
-        {
-            this.hexColor = hexColor;
-        }
-
-        public String getHexColor()
-        {
-            return hexColor;
-        }
-    }
-
-    static class HeatmapScaleRange
-    {
-        String label;
-
-        Color color;
-    }
-
-    // ---
-
-    static interface IHeatmapRenderer<T>
-    {
-        Color getColor(T value);
-
-        List<HeatmapScaleRange> calculateScale();
-    }
-
     static class NumberHeatmapRenderer implements IHeatmapRenderer<Float>
     {
 
@@ -83,51 +53,6 @@ public class HeatmapUtil
 
     }
 
-    @SuppressWarnings("unused")
-    static class StringHeatmapRenderer implements IHeatmapRenderer<String>
-    {
-        private static final List<String> DEFAULT_COLORS = Arrays.asList("#67001F", "#B2182B",
-                "#D6604D", "#F4A582", "#FDDBC7", "#F7F7F7", "#D1E5F0", "#92C5DE", "#4393C3",
-                "#2166AC", "#053061");
-
-        private final Set<String> values;
-
-        private final List<Color> scaleColors;
-
-        public StringHeatmapRenderer(Set<String> values)
-        {
-            this(values, asColors(DEFAULT_COLORS));
-        }
-
-        private static List<Color> asColors(List<String> defaultColors)
-        {
-            List<Color> colors = new ArrayList<Color>();
-            for (String color : DEFAULT_COLORS)
-            {
-                colors.add(new Color(color));
-            }
-            return colors;
-        }
-
-        public StringHeatmapRenderer(Set<String> values, List<Color> scaleColors)
-        {
-            this.values = values;
-            this.scaleColors = scaleColors;
-        }
-
-        public Color getColor(String value)
-        {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        public List<HeatmapScaleRange> calculateScale()
-        {
-            // TODO Auto-generated method stub
-            return null;
-        }
-    }
-
     static class ScaleDrawer
     {
         Widget draw(List<HeatmapScaleRange> ranges, int height)
diff --git a/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/heatmaps/IHeatmapRenderer.java b/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/heatmaps/IHeatmapRenderer.java
new file mode 100644
index 00000000000..f5a44697d53
--- /dev/null
+++ b/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/heatmaps/IHeatmapRenderer.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.plugin.screening.client.web.client.application.detailviewers.heatmaps;
+
+import java.util.List;
+
+import ch.systemsx.cisd.openbis.plugin.screening.client.web.client.application.detailviewers.heatmaps.dto.Color;
+import ch.systemsx.cisd.openbis.plugin.screening.client.web.client.application.detailviewers.heatmaps.dto.HeatmapScaleRange;
+
+/**
+ * @author Tomasz Pylak
+ */
+public interface IHeatmapRenderer<T>
+{
+    /** @return color on the heatmap for the specified value */
+    Color getColor(T value);
+
+    /** @return heatmap scale description, order is relevant. */
+    List<HeatmapScaleRange> calculateScale();
+}
\ No newline at end of file
diff --git a/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/heatmaps/StringHeatmapRenderer.java b/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/heatmaps/StringHeatmapRenderer.java
new file mode 100644
index 00000000000..060add3e9dc
--- /dev/null
+++ b/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/heatmaps/StringHeatmapRenderer.java
@@ -0,0 +1,123 @@
+package ch.systemsx.cisd.openbis.plugin.screening.client.web.client.application.detailviewers.heatmaps;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import ch.systemsx.cisd.openbis.plugin.screening.client.web.client.application.detailviewers.heatmaps.dto.Color;
+import ch.systemsx.cisd.openbis.plugin.screening.client.web.client.application.detailviewers.heatmaps.dto.HeatmapScaleRange;
+
+/**
+ * Assigns colors to string labels. Colors will be used to create a heatmap.
+ * 
+ * @author Tomasz Pylak
+ */
+public class StringHeatmapRenderer implements IHeatmapRenderer<String>
+{
+    private static final Color CATEGORY_OTHERS_COLOR = new Color("#00FF00");
+
+    private static final List<String> LONG_DEFAULT_COLORS = Arrays.asList("#67001F", "#B2182B",
+            "#D6604D", "#F4A582", "#FDDBC7", "#F7F7F7", "#D1E5F0", "#92C5DE", "#4393C3", "#2166AC",
+            "#053061");
+
+    private static final List<String> SHORT_DEFAULT_COLORS = Arrays.asList("#5E3C99", "$B2ABD2",
+            "#F7F7F7", "#FDB863", "#E66101");
+
+    private static final List<String> DOUBLE_DEFAULT_COLORS = Arrays.asList("#5E3C99", "#F7F7F7");
+
+    private static final List<String> SINGLE_DEFAULT_COLORS = Arrays.asList("#F7F7F7");
+
+    private static final String CATEGORY_OTHERS_LABEL = "Others";
+
+    private final Map<String, Color> colorsMap;
+
+    private final boolean moreLabelsThanColors;
+
+    /**
+     * Assigns colors to string labels from a fixed default set of colors. If there are more values
+     * than colors, the "overhead values" are marked as belonging to one "Others" group.
+     */
+    public StringHeatmapRenderer(List<String> values)
+    {
+        this(values, asColors(getDefaultColors(values.size())));
+    }
+
+    private static List<String> getDefaultColors(int size)
+    {
+        if (size == 1)
+        {
+            return SINGLE_DEFAULT_COLORS;
+        } else if (size == 2)
+        {
+            return DOUBLE_DEFAULT_COLORS;
+        } else if (size <= SHORT_DEFAULT_COLORS.size())
+        {
+            return SHORT_DEFAULT_COLORS;
+        } else
+        {
+            return LONG_DEFAULT_COLORS;
+        }
+    }
+
+    /**
+     * Assigns specified colors to string labels using colors in the specified order. If there are
+     * more values than colors, the "overhead values" are marked as belonging to one "Others" group.
+     */
+    public StringHeatmapRenderer(List<String> uniqueValues, List<Color> scaleColors)
+    {
+        this.colorsMap = calculateColorMap(uniqueValues, scaleColors);
+        this.moreLabelsThanColors = (uniqueValues.size() > scaleColors.size());
+    }
+
+    private static List<Color> asColors(List<String> defaultColors)
+    {
+        List<Color> colors = new ArrayList<Color>();
+        for (String color : LONG_DEFAULT_COLORS)
+        {
+            colors.add(new Color(color));
+        }
+        return colors;
+    }
+
+    private static Map<String, Color> calculateColorMap(List<String> uniqueValues,
+            List<Color> scaleColors)
+    {
+        Map<String, Color> colorsMap = new HashMap<String, Color>();
+        Iterator<Color> colorsIter = scaleColors.iterator();
+        Iterator<String> valuesIter = uniqueValues.iterator();
+        while (colorsIter.hasNext() && valuesIter.hasNext())
+        {
+            colorsMap.put(valuesIter.next(), colorsIter.next());
+        }
+        return colorsMap;
+    }
+
+    public Color getColor(String value)
+    {
+        Color color = colorsMap.get(value);
+        assert color != null || moreLabelsThanColors : "Unexpected value " + value;
+
+        if (color == null)
+        {
+            return CATEGORY_OTHERS_COLOR;
+        } else
+        {
+            return color;
+        }
+    }
+
+    public List<HeatmapScaleRange> calculateScale()
+    {
+        List<HeatmapScaleRange> scale = new ArrayList<HeatmapScaleRange>();
+        for (Entry<String, Color> entry : colorsMap.entrySet())
+        {
+            scale.add(new HeatmapScaleRange(entry.getKey(), entry.getValue()));
+        }
+        scale.add(new HeatmapScaleRange(CATEGORY_OTHERS_LABEL, CATEGORY_OTHERS_COLOR));
+        return scale;
+    }
+}
\ No newline at end of file
diff --git a/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/heatmaps/dto/Color.java b/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/heatmaps/dto/Color.java
new file mode 100644
index 00000000000..87fd509ffdd
--- /dev/null
+++ b/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/heatmaps/dto/Color.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.plugin.screening.client.web.client.application.detailviewers.heatmaps.dto;
+
+/**
+ * @author Tomasz Pylak
+ */
+public class Color
+{
+    private final String hexColor;
+
+    public Color(String hexColor)
+    {
+        this.hexColor = hexColor;
+    }
+
+    public String getHexColor()
+    {
+        return hexColor;
+    }
+}
\ No newline at end of file
diff --git a/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/heatmaps/dto/HeatmapScaleRange.java b/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/heatmaps/dto/HeatmapScaleRange.java
new file mode 100644
index 00000000000..9e5b1cb3441
--- /dev/null
+++ b/screening/source/java/ch/systemsx/cisd/openbis/plugin/screening/client/web/client/application/detailviewers/heatmaps/dto/HeatmapScaleRange.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.plugin.screening.client.web.client.application.detailviewers.heatmaps.dto;
+
+/**
+ * @author Tomasz Pylak
+ */
+public class HeatmapScaleRange
+{
+    private final String label;
+
+    private final Color color;
+
+    public HeatmapScaleRange(String label, Color color)
+    {
+        this.label = label;
+        this.color = color;
+    }
+
+    public String getLabel()
+    {
+        return label;
+    }
+
+    public Color getColor()
+    {
+        return color;
+    }
+}
\ No newline at end of file
-- 
GitLab