Skip to content

Instantly share code, notes, and snippets.

View Indy9000's full-sized avatar
🏠
Working from home

Indy M. Indy9000

🏠
Working from home
View GitHub Profile
@Indy9000
Indy9000 / donut-chart-painter.dart
Created May 31, 2021 17:34
animated-donut-chart-07
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) {
@Indy9000
Indy9000 / score.dart
Last active June 13, 2021 15:45
line-chart-with-flutter
class Score {
final double value;
final DateTime time;
Score(this.value, this.time);
}
@Indy9000
Indy9000 / main.dart
Last active June 13, 2021 16:31
line-chart-widget
import 'dart:math';
void main() {
runApp(MyApp());
}
final rng = Random();
const dayCount = 7;
class MyApp extends StatelessWidget {
@Indy9000
Indy9000 / linechart-widget.dart
Last active June 13, 2021 16:41
line-chart-widget
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),
@Indy9000
Indy9000 / linechartpainter.dart
Created June 13, 2021 16:51
line-chart-widget
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;
@Indy9000
Indy9000 / linechartpainter-01.dart
Last active June 13, 2021 18:44
line-chart-widget
@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;
@Indy9000
Indy9000 / linechartpainter-03.dart
Last active June 13, 2021 18:59
line-chart-widget
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) {
@Indy9000
Indy9000 / linechartpainter-02.dart
Last active June 13, 2021 19:00
line-chart-widget
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);
}
@Indy9000
Indy9000 / linechartpainter-05.dart
Created June 13, 2021 19:50
line-chart-widget
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;
@Indy9000
Indy9000 / linechartpainter-04.dart
Created June 13, 2021 19:50
line-chart-widget
@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);