]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DynamicVisualisationContributionsNode.java
Add support for gradients in dynamic visualisations
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / nodes / DynamicVisualisationContributionsNode.java
index 53d7862c854115115cb77d2e89f3da8f5a911010..d8d2e1d194a97d9f45c9bdeb8a2e85394392cf2b 100644 (file)
@@ -5,7 +5,6 @@ import java.awt.Color;
 import java.awt.Font;
 import java.awt.Graphics2D;
 import java.awt.geom.AffineTransform;
-import java.awt.geom.Ellipse2D;
 import java.awt.geom.Rectangle2D;
 import java.util.List;
 import java.util.Map;
@@ -119,25 +118,65 @@ public class DynamicVisualisationContributionsNode extends G2DNode {
                 
                 Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9));
                 g2d.setFont(rulerFont);
-                g2d.setColor(Color.BLACK);
-                
-                String str = Double.toString(max);
-                g2d.drawString(str, (int)(colorBarBoxLeft + colorBarBoxWidth - 30), (int)(colorBarBoxTop  + colorBarBoxHeight));
-                
-                str = Double.toString(min);
-                g2d.drawString(str, (int)colorBarBoxLeft + 1, (int)(colorBarBoxTop + colorBarBoxHeight));
                 
+                double interval = (max - min) / intensities.size();
                 
-                for (RGBIntensity intensity : intensities) {
+                for (int j = 0; j < intensities.size(); j++) {
+                    
+                    RGBIntensity intensity = intensities.get(j);
+                    
+                    if (colorBarsOptions.isUseGradients()) {
+                        
+                        RGBIntensity upperLimitIntensity;
+                        if (j + 1 != intensities.size())
+                            upperLimitIntensity = intensities.get(j + 1);
+                        else
+                            upperLimitIntensity = intensity;
+                        
+                        double minRed = intensity.getRed();
+                        double minGreen = intensity.getGreen();
+                        double minBlue = intensity.getBlue();
+                        
+                        double maxRed = upperLimitIntensity.getRed();
+                        double maxGreen = upperLimitIntensity.getGreen();
+                        double maxBlue = upperLimitIntensity.getBlue();
+                        
+                        double redDelta = (maxRed - minRed) / colorVerticalWidth;
+                        double greenDelta = (maxGreen - minGreen) / colorVerticalWidth;
+                        double blueDelta = (maxBlue - minBlue) / colorVerticalWidth;
+                        
+                        for (int k = 0; k < colorVerticalWidth; k++) {
+                            
+                            
+                            g2d.setColor(new Color((float)(minRed + (k * redDelta)), (float)(minGreen + (k * greenDelta)), (float)(minBlue + (k * blueDelta)), 1f));
+                            Rectangle2D colorVertical = new Rectangle2D.Double(colorVerticalLeft + k, colorVerticalTop, 1, colorVerticalHeigth);
+                            g2d.fill(colorVertical);
+                        }
+                        
+                    } else {
+                        g2d.setColor(new Color((float)intensity.getRed(), (float)intensity.getGreen(), (float)intensity.getBlue(), 1f));
+                        Rectangle2D colorVertical = new Rectangle2D.Double(colorVerticalLeft, colorVerticalTop, colorVerticalWidth, colorVerticalHeigth);
+                        g2d.fill(colorVertical);
+                    }
+                    
+
                     
-                    g2d.setColor(new Color((float)intensity.getRed(), (float)intensity.getGreen(), (float)intensity.getBlue(), 1f));
-                    Rectangle2D colorVertical = new Rectangle2D.Double(colorVerticalLeft, colorVerticalTop, colorVerticalWidth, colorVerticalHeigth);
-                    g2d.fill(colorVertical);
+                    double value = min + j * interval;
+                    String str = Double.toString(value);
+                    if (str.length() > 4) {
+                        str = str.substring(0, 3);
+                    }
+                    g2d.setColor(Color.BLACK);
+                    g2d.drawString(str, (float)(colorVerticalLeft - 8), (float)(colorBarBoxTop + colorBarBoxHeight));
                     colorVerticalLeft = colorVerticalLeft + colorVerticalWidth;
                 }
                 g2d.setColor(Color.BLACK);
+                
+                String str = Double.toString(max);
+                g2d.drawString(str, (float)(colorVerticalLeft - 8), (float)(colorBarBoxTop  + colorBarBoxHeight));
+                
                 str = object.getKey() + " - " + label + " [" + unit + "]";
-                g2d.drawString(str, (int)colorBarBoxLeft + 5, (int)colorBarBoxTop + 10);
+                g2d.drawString(str, (float)colorBarBoxLeft + 5, (float)colorBarBoxTop + 10);
             }
         }
     }
@@ -209,60 +248,69 @@ public class DynamicVisualisationContributionsNode extends G2DNode {
         
         int i = 0;
         
-        for (Entry<String, DynamicSizeContribution> object : dynamicSizingContributions.entrySet()) {
-            DynamicSizeContribution cc = object.getValue();
-            
-            double min = cc.getDefaultMin();
-            double max = cc.getDefaultMax();
-            String unit = cc.getUnit();
-            String label = cc.getLabel();
-            
-            DynamicSizeMap map = cc.getDefaultSizeMap();
-            
-            List<Double> sizes = map.getSizes();
-            
-            double sizeBarBoxTop = (sizeBarBoxTopInitial + (colorBarBoxHeight * i));
-            i++;
-//                double backgroundBoxPaddingRight = 20;
-//                double backgroundBoxHeight = 50;
-//                double backgroundBoxWidth = 300;
-//                double backgroundBoxRight = bounds.getMaxX() - backgroundBoxPaddingRight;
-//                double backgroundBoxLeft = backgroundBoxRight - backgroundBoxWidth;
-//                double backgroundBoxTop = bounds.getMaxY();// + (initialPosition + (position * i));
-            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
-            g2d.setColor(new Color(0.9f, 0.9f, 0.9f, 0.95f));
-            
-            Rectangle2D vertical = new Rectangle2D.Double(sizeBarBoxLeft, sizeBarBoxTop, colorBarBoxWidth, colorBarBoxHeight);
-            g2d.fill(vertical);
-            
-            double sizeVerticalLeft = sizeBarBoxLeft + 5;
-            double sizeVerticalTop = sizeBarBoxTop + 15;
-            double sizeVerticalHeigth = colorBarBoxHeight - 30;
-
-            double sizeVerticalWidth = (sizeBarBoxWidth - 10) / sizes.size();
-            
-            Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9));
-            g2d.setFont(rulerFont);
-            g2d.setColor(Color.BLACK);
-            
-            String str = Double.toString(max);
-            g2d.drawString(str, (int)(sizeBarBoxLeft + sizeBarBoxWidth - 30), (int)(sizeBarBoxTop + sizeBarBoxHeight));
-            
-            str = Double.toString(min);
-            g2d.drawString(str, (int)sizeBarBoxLeft + 1, (int)(sizeBarBoxTop + sizeBarBoxHeight));
-            
-            
-            g2d.setColor(new Color((float)0, (float)0, (float)0.8, 0.8f));
-            for (Double size: sizes) {
+        if (dynamicSizingContributions != null) {
+            for (Entry<String, DynamicSizeContribution> object : dynamicSizingContributions.entrySet()) {
+                DynamicSizeContribution cc = object.getValue();
+                
+                if (!cc.isUsed())
+                    continue;
+                
+                double min = cc.getDefaultMin();
+                double max = cc.getDefaultMax();
+                String unit = cc.getUnit();
+                String label = cc.getLabel();
+                
+                DynamicSizeMap map = cc.getDefaultSizeMap();
+                
+                List<Double> sizes = map.getSizes();
+                
+                double sizeBarBoxTop = (sizeBarBoxTopInitial + (colorBarBoxHeight * i));
+                i++;
+    
+                g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
+                g2d.setColor(new Color(0.9f, 0.9f, 0.9f, 0.95f));
                 
-                Ellipse2D ellipse = new Ellipse2D.Double(sizeVerticalLeft + sizeVerticalWidth / 2, (sizeVerticalTop), size * sizeVerticalHeigth, size * sizeVerticalHeigth);
-                g2d.fill(ellipse);
+                Rectangle2D vertical = new Rectangle2D.Double(sizeBarBoxLeft, sizeBarBoxTop, colorBarBoxWidth, colorBarBoxHeight);
+                g2d.fill(vertical);
                 
-                sizeVerticalLeft = sizeVerticalLeft + sizeVerticalWidth;
+                double sizeVerticalLeft = sizeBarBoxLeft + 5;
+                double sizeVerticalTop = sizeBarBoxTop + 15;
+                double sizeVerticalHeigth = colorBarBoxHeight - 30;
+    
+                double sizeVerticalWidth = (sizeBarBoxWidth - 10) / sizes.size();
+                
+                Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9));
+                g2d.setFont(rulerFont);
+    
+                double interval = (max - min) / sizes.size();
+                
+                for (int j = 0; j < sizes.size(); j++) {
+                    
+                    Double size = sizes.get(j);
+                    
+                    g2d.setColor(new Color((float)0, (float)0, (float)0.8, 0.8f));
+                    double sizedWidth = (size / 5) * sizeVerticalHeigth;
+                    Rectangle2D rect = new Rectangle2D.Double(sizeVerticalLeft, (sizeVerticalTop), sizedWidth, sizeVerticalHeigth);
+                    g2d.fill(rect);
+                    
+                    double value = min + j * interval;
+                    String str = Double.toString(value);
+                    if (str.length() > 4) {
+                        str = str.substring(0, 3);
+                    }
+                    g2d.setColor(Color.BLACK);
+                    g2d.drawString(str, (float)(sizeVerticalLeft - 8), (float)(sizeBarBoxTop + sizeBarBoxHeight));
+                    
+                    sizeVerticalLeft = sizeVerticalLeft + sizeVerticalWidth;
+                }
+                g2d.setColor(Color.BLACK);
+                
+                String str = Double.toString(max);
+                g2d.drawString(str, (int)(sizeBarBoxLeft + sizeBarBoxWidth - 30), (int)(sizeBarBoxTop + sizeBarBoxHeight));
+                
+                str = object.getKey() + " - " + label + " [" + unit + "]";
+                g2d.drawString(str, (int)sizeBarBoxLeft + 5, (int)sizeBarBoxTop + 10);
             }
-            g2d.setColor(Color.BLACK);
-            str = object.getKey() + " - " + label + " [" + unit + "]";
-            g2d.drawString(str, (int)sizeBarBoxLeft + 5, (int)sizeBarBoxTop + 10);
         }
     }