1+ import 'package:flutter_web.examples.github_dataviz/constants.dart' ;
12import 'package:flutter_web.examples.github_dataviz/data/contribution_data.dart' ;
3+ import 'package:flutter_web.examples.github_dataviz/data/week_label.dart' ;
24import 'package:flutter_web/material.dart' ;
35
46class Timeline extends StatefulWidget {
57 int numWeeks;
68 double animationValue;
7-
8- Timeline (this .numWeeks, this .animationValue);
9+ List <WeekLabel > weekLabels;
10+
11+ Timeline (this .numWeeks, this .animationValue, this .weekLabels);
912
1013 @override
1114 State <StatefulWidget > createState () {
@@ -17,39 +20,49 @@ class TimelineState extends State<Timeline> {
1720 @override
1821 Widget build (BuildContext context) {
1922 return new Container (
20- child: new CustomPaint (foregroundPainter: new TimelinePainter (widget.numWeeks, widget.animationValue), child: new Container (height: 200 ,)));
23+ child: new CustomPaint (
24+ foregroundPainter: new TimelinePainter (widget.numWeeks, widget.animationValue, widget.weekLabels),
25+ child: new Container (height: 200 ,))
26+ );
2127 }
2228}
2329
2430class TimelinePainter extends CustomPainter {
2531
2632 Paint mainLinePaint;
33+ Paint milestoneLinePaint;
2734
2835 Color lineColor = Colors .white;
2936
3037 int numWeeks;
3138 double animationValue;
3239 int weekYearOffset = 9 ; // Week 0 in our data is 9 weeks before the year boundary (i.e. week 43)
3340
41+ List <WeekLabel > weekLabels;
42+
3443 int yearNumber = 2015 ;
3544
36- TimelinePainter (this .numWeeks, this .animationValue) {
45+ TimelinePainter (this .numWeeks, this .animationValue, this .weekLabels ) {
3746 mainLinePaint = new Paint ();
3847 mainLinePaint.style = PaintingStyle .stroke;
39- mainLinePaint.color = lineColor;
48+ mainLinePaint.color = Constants .timelineLineColor;
49+ milestoneLinePaint = new Paint ();
50+ milestoneLinePaint.style = PaintingStyle .stroke;
51+ milestoneLinePaint.color = Constants .milestoneColor;
4052 }
4153
4254 @override
4355 void paint (Canvas canvas, Size size) {
4456
45- double yearHeight = 20 ;
57+ double labelHeight = 20 ;
58+ double labelHeightDoubled = labelHeight* 2 ;
4659
4760 double mainLineY = size.height/ 2 ;
4861 canvas.drawLine (new Offset (0 , mainLineY), new Offset (size.width, mainLineY), mainLinePaint);
4962
5063 {
5164 double currWeekX = size.width * animationValue;
52- canvas.drawLine (new Offset (currWeekX, yearHeight ), new Offset (currWeekX, size.height - yearHeight ), mainLinePaint);
65+ canvas.drawLine (new Offset (currWeekX, labelHeightDoubled ), new Offset (currWeekX, size.height - labelHeightDoubled ), mainLinePaint);
5366 }
5467
5568 {
@@ -60,7 +73,7 @@ class TimelinePainter extends CustomPainter {
6073 // Year
6174 isYear = true ;
6275 lineHeight = size.height/ 2 ;
63- } else if (week % 4 == 0 ) {
76+ } else if (( week - 1 ) % 4 == 0 ) {
6477 // Month
6578 lineHeight = size.height/ 8 ;
6679 }
@@ -75,11 +88,27 @@ class TimelinePainter extends CustomPainter {
7588 TextSpan span = new TextSpan (style: new TextStyle (color: Color .fromARGB (255 , 255 , 255 , 255 ), fontSize: 12 ), text: "${yearNumber }" );
7689 TextPainter tp = new TextPainter (text: span, textAlign: TextAlign .left, textDirection: TextDirection .ltr);
7790 tp.layout ();
78- tp.paint (canvas, new Offset (currX, size.height - yearHeight ));
91+ tp.paint (canvas, new Offset (currX, size.height - labelHeight ));
7992 yearNumber++ ;
8093 }
8194 }
8295 }
96+
97+ {
98+ for (int i = 0 ; i< weekLabels.length; i++ ) {
99+ WeekLabel weekLabel = weekLabels[i];
100+ double currX = (weekLabel.weekNum / numWeeks.toDouble ()) * size.width;
101+ double lineHeight = size.height/ 2 ;
102+ double margin = (size.height - lineHeight) / 2 ;
103+ canvas.drawLine (new Offset (currX, margin), new Offset (currX, size.height - margin), milestoneLinePaint);
104+
105+ TextSpan span = new TextSpan (style: new TextStyle (color: Constants .milestoneColor, fontSize: 12 ), text: weekLabel.label);
106+ TextPainter tp = new TextPainter (text: span, textAlign: TextAlign .left, textDirection: TextDirection .ltr);
107+ tp.layout ();
108+ tp.paint (canvas, new Offset (currX, size.height - labelHeightDoubled));
109+
110+ }
111+ }
83112 }
84113
85114 @override
0 commit comments