Normand Briere
2018-07-07 e416acb9b012b17d1efe49ad2199ea7132d874d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package timeflow.app.ui;
 
import timeflow.model.*;
import timeflow.data.db.*;
import timeflow.data.time.*;
 
import timeflow.util.*;
 
import java.awt.*;
import java.awt.geom.AffineTransform;
 
public class SizeLegendPanel extends ModelPanel {
   Field sizeField;
   double min, max;
 
   public SizeLegendPanel(TFModel model)
   {
       super(model);
       setBackground(Color.white);
   }
   
   @Override
   public void note(TFEvent e) {
       Field size=getModel().getDB().getField(VirtualField.SIZE);
       
       if (size!=null && (size!=sizeField || e.affectsData()))
       {
           double[] minmax=DBUtils.minmax(getModel().getActs(), size);
           min=minmax[0];
           max=minmax[1];
       }
       sizeField=size;        
       repaint();
   }
   
   public Dimension getPreferredSize()
   {
       return new Dimension(200,40);
   }
   
   public void paintComponent(Graphics g1)
   {
       Graphics2D g=(Graphics2D)g1;
       g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
       int w=getSize().width;
       int h=getSize().height;
       TFModel model=getModel();
       Display display=model.getDisplay();
       g.setColor(getBackground());
       g.setFont(display.plain());
       g.fillRect(0,0,w,h);
       g.setColor(Color.gray);
       
       if (sizeField==null)
       {
           return;
       }
       else if (Double.isNaN(min))
       {
           g.drawString("All values missing.",3,20);
           return;
       }
       else
       {
           AffineTransform old=g.getTransform();
           g.setTransform(AffineTransform.getTranslateInstance(20, 0));
           if (min==max)
           {
               g.setColor(Color.gray);
               g.fillOval(3,h/2-3,6,6);
               g.setColor(Color.black);
               g.setFont(display.tiny());
               g.drawString(format(min),12,h/2+5);                
           }
           else
           {
               String leftLabel=format(min);
               String rightLabel=format(max);
               g.setFont(display.tiny());
               int lw=display.tinyFontMetrics().stringWidth(leftLabel);
               int rw=display.tinyFontMetrics().stringWidth(rightLabel);
               g.setColor(Color.black);
               int ty=h/2+5;;
               g.drawString(leftLabel,2,ty);
               g.setColor(Color.lightGray);
               double maxAbs=Math.max(Math.abs(min), Math.abs(max));
               int dx=8+lw;
               for (int i=0; i<5; i++)
               {
                   double z=(i*max+(4-i)*min)/4;
                   int r=(int)(Math.sqrt(Math.abs(z/maxAbs))*Display.MAX_DOT_SIZE);
                   if (r<1)
                       r=1;
                   if (z>0)
                       g.fillOval(dx,h/2-r,2*r,2*r);
                   else
                       g.drawOval(dx,h/2-r,2*r,2*r);
                   dx+=5+2*r;
               }
               g.setColor(Color.black);
               g.drawString(rightLabel,dx+4,ty);
           }
           g.setTransform(old);
       }
   }
   
   String format(double x)
   {
       if (Math.abs(x)>10 && (max-min)>10)
           return Display.format(Math.round(x));
       return Display.format(x);
   }
}