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
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
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
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 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
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
void drawLabels(Canvas canvas, Offset c, double radius, double startAngle, | |
double sweepAngle, String label) { | |
final r = radius * 0.4; | |
final dx = r * cos(startAngle + sweepAngle / 2.0); | |
final dy = r * sin(startAngle + sweepAngle / 2.0); | |
final position = c + Offset(dx, dy); | |
drawTextCentered(canvas, position, label, labelStyle, 100.0, (Size sz) { | |
final rect = Rect.fromCenter( | |
center: position, width: sz.width + 5, height: sz.height + 5); | |
final rrect = RRect.fromRectAndRadius(rect, Radius.circular(5)); |
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
TextPainter measureText( | |
String s, TextStyle style, double maxWidth, TextAlign align) { | |
final span = TextSpan(text: s, style: style); | |
final tp = TextPainter( | |
text: span, textAlign: align, textDirection: TextDirection.ltr); | |
// ellipsis: "..."); | |
tp.layout(minWidth: 0, maxWidth: maxWidth); | |
return tp; | |
} |
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 drawSector(Canvas canvas, DataItem di, Rect rect, double startAngle, | |
double sweepAngle) { | |
final paint = Paint() | |
..style = PaintingStyle.fill | |
..color = di.color; | |
canvas.drawArc(rect, startAngle, sweepAngle, true, paint); | |
} |
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 DonutChartPainter extends CustomPainter { | |
final List<DataItem> dataset; | |
DonutChartPainter(this.dataset); | |
@override | |
void paint(Canvas canvas, Size size) { | |
final c = Offset(size.width / 2.0, size.height / 2.0); | |
final radius = size.width * 0.9; | |
final rect = Rect.fromCenter(center: c, width: radius, height: radius); | |
final fullAngle = 360.0; |