This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class _DonutChartWidgetState extends State<DonutChartWidget> { | |
late Timer timer; | |
double fullAngle = 0.0; | |
double secondsToComplete = 3.0; | |
@override | |
void initState() { | |
super.initState(); | |
timer = Timer.periodic(Duration(milliseconds: 1000 ~/ 60), (timer) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Score { | |
final double value; | |
final DateTime time; | |
Score(this.value, this.time); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
void main() { | |
runApp(MyApp()); | |
} | |
final rng = Random(); | |
const dayCount = 7; | |
class MyApp extends StatelessWidget { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LineChartWidget extends StatelessWidget { | |
final List<Score> scores; | |
const LineChartWidget(this.scores, {Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
child: CustomPaint( | |
child: Container(), | |
painter: LineChartPainter(scores), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const WeekDays = ["", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; | |
class LineChartPainter extends CustomPainter { | |
final List<Score> scores; | |
late double _min, _max; | |
late List<double> _Y; | |
late List<String> _X; | |
LineChartPainter(this.scores) { | |
var min = double.maxFinite; | |
var max = -double.maxFinite; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@override | |
void paint(Canvas canvas, Size size) { | |
// compute the drawable chart width and height | |
final drawableHeight = size.height - 2.0 * border; | |
final drawableWidth = size.width - 2.0 * border; | |
final hu = drawableHeight / 5.0; // height unit | |
final wu = drawableWidth / _X.length.toDouble(); // width unit | |
// compute box dimensions | |
final boxH = hu * 3.0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Offset> _computePoints(Offset lt, double boxW, double boxH, double hr) { | |
return _Y.map((yp) { | |
final yy = boxH - (yp - _min) * hr; | |
final dp = lt + Offset(0, yy); | |
lt += Offset(boxW, 0); | |
return dp; | |
}).toList(); | |
} | |
Path _computePath(List<Offset> points) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void paint(Canvas canvas, Size size) { | |
... | |
final hr = boxH / (_max - _min); // height per unit value | |
final lt = Offset(border + boxW / 2.0, border); | |
final points = _computePoints(lt, boxW, boxH, hr); | |
final path = _computePath(points); | |
canvas.drawPath(path, linePaint); | |
_drawDataPoints(canvas, points, dotPaintFill); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<String> _computeLabels() { | |
return _Y.map((yp) => "${yp.toStringAsFixed(1)}").toList(); | |
} | |
void _drawYLabels(Canvas canvas, List<String> labels, List<Offset> points, | |
double boxW, double top) { | |
var i = 0; | |
labels.forEach((label) { | |
final dp = points[i]; | |
final dy = (dp.dy - 15.0) < top ? 15.0 : -15.0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@override | |
void paint(Canvas canvas, Size size) { | |
... | |
final labels = _computeLabels(); | |
_drawYLabels(canvas, labels, points, boxW, border); | |
// draw x labels | |
final lt1 = Offset(border + boxW / 2.0, border + 4.5 * hu); | |
_drawXLabels(canvas, lt1, boxW); |