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 / 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.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 / 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 / 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 / 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 / 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 / donut-chart-painter.dart
Created May 31, 2021 17:21
animated-donut-chart-06
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));
@Indy9000
Indy9000 / donut-chart-painter.dart
Created May 31, 2021 16:55
animated-donut-chart-05
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;
}
@Indy9000
Indy9000 / donut-chart-painter.dart
Created May 31, 2021 16:41
animated-donut-chart-04
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);
}
@Indy9000
Indy9000 / donut-chart-painter.dart
Created May 31, 2021 16:20
animated-donut-chart-03
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;