Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Indy M. Indy9000

🏠
Working from home
View GitHub Profile
View linechartpainter-01.dart
@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;
View linechartpainter.dart
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;
View linechart-widget.dart
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 Jun 13, 2021
line-chart-widget
View main.dart
import 'dart:math';
void main() {
runApp(MyApp());
}
final rng = Random();
const dayCount = 7;
class MyApp extends StatelessWidget {
@Indy9000
Indy9000 / score.dart
Last active Jun 13, 2021
line-chart-with-flutter
View score.dart
class Score {
final double value;
final DateTime time;
Score(this.value, this.time);
}
View donut-chart-painter.dart
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) {
View donut-chart-painter.dart
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));
View donut-chart-painter.dart
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;
}
View donut-chart-painter.dart
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);
}
View donut-chart-painter.dart
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;