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
package timeflow.vis.timeline;
 
import java.awt.*;
import java.util.*;
 
import timeflow.data.time.Interval;
import timeflow.data.time.TimeUtils;
import timeflow.model.*;
import timeflow.vis.Mouseover;
import timeflow.vis.TimeScale;
 
public class AxisRenderer {
   
   TimelineVisuals visuals;
   
   public AxisRenderer(TimelineVisuals visuals)
   {
       this.visuals=visuals;
   }
   
   public void render(Graphics2D g, Collection<Mouseover> objectLocations)
   {        
       TFModel model=visuals.getModel();
       g.setColor(model.getDisplay().getColor("chart.background"));
       Rectangle bounds=visuals.getBounds();
       
       TimeScale scale=visuals.getTimeScale();
       java.util.List<AxisTicMarks> t=AxisTicMarks.allRelevant(scale.getInterval());
       
       int dateLabelH=model.getDisplay().getInt("timeline.datelabel.height");
       int y=bounds.y+bounds.height-dateLabelH;    
       
       // draw in reverse order so bigger granularity at top.
       int n=t.size();
       for (int i=0; i<n; i++)
       {
           render(t.get(i), g, bounds.x, y, dateLabelH-1, bounds.y, i==0, objectLocations);
           y-=dateLabelH;
       }
   }
   
   void render(AxisTicMarks t, Graphics2D g, int x, int y, int h, int top, boolean full, Collection<Mouseover> objectLocations)
   {
       TFModel model=visuals.getModel();
 
       int n=t.tics.size();
       for (int i=0; i<n-1; i++)
       {
           
           long start=t.tics.get(i);
           long end=t.tics.get(i+1);
           
           int x0=Math.max(x,visuals.getTimeScale().toInt(start));            
           int x1=visuals.getTimeScale().toInt(end);
           
           int dayOfWeek=TimeUtils.cal(start).get(Calendar.DAY_OF_WEEK);
           
           g.setColor(t.unit.isDayOrLess() && (dayOfWeek==1 || dayOfWeek==7) ? 
                   new Color(245,245,245) : new Color(240,240,240));
 
           g.fillRect(x0, y, x1-x0-1, h);
           g.setColor(Color.white);
           g.drawLine(x1-1, y, x1-1, y+h);
           g.drawLine(x0,y+h,x1,y+h);
           objectLocations.add(new Mouseover(new Interval(start,end), x0, y, x1-x0-1, h));
           
           g.setFont(model.getDisplay().timeLabel());
           String label=full? t.unit.formatFull(start) : t.unit.format(new Date(start));
           int tx=x0+3;
           int ty=y+h-5;
           g.setColor(full ? Color.darkGray : Color.gray);
           int sw=model.getDisplay().timeLabelFontMetrics().stringWidth(label);
           if (sw<x1-tx-3)
               g.drawString(label, tx,ty);
           else
           {
               int c=label.indexOf(':');
               if (c>0)
               {
                   label=label.substring(0,c);
                   sw=model.getDisplay().timeLabelFontMetrics().stringWidth(label);
                   if (sw<x1-tx-3)
                       g.drawString(label, tx,ty);
               }
           }
       }
   }
}