Skip to content

Instantly share code, notes, and snippets.

@jtmcdole
Created December 7, 2023 04:29
Show Gist options
  • Save jtmcdole/6c34b94a3f3aaadffcc8954e99a8ca97 to your computer and use it in GitHub Desktop.
Save jtmcdole/6c34b94a3f3aaadffcc8954e99a8ca97 to your computer and use it in GitHub Desktop.
bard flutter pie chart https://g.co/bard/share/bb4ed89ed37b
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
import 'dart:math' as math;
void main() => runApp(MyApp());
class PieChart extends CustomPainter {
final List<double> data; // percentages for each slice
final List<Color> colors; // colors for each slice
PieChart({required this.data, required this.colors});
@override
void paint(Canvas canvas, Size size) {
// calculate center and radius
final center = Offset(size.width / 2, size.height / 2);
final radius = math.min(size.width / 2, size.height / 2);
// draw background circle
final paint = Paint()
..color = Colors.grey.shade300
..style = PaintingStyle.fill;
canvas.drawCircle(center, radius, paint);
// calculate starting angle for each slice
double startAngle = 0;
// draw pie slices
for (int i = 0; i < data.length; i++) {
final sweepAngle = 2 * math.pi * data[i] / 100;
final arcPaint = Paint()
..color = colors[i]
..style = PaintingStyle.fill;
canvas.drawArc(
Rect.fromCircle(center: center, radius: radius), startAngle, sweepAngle, true, arcPaint);
startAngle += sweepAngle;
}
}
@override
bool shouldRepaint(PieChart oldDelegate) =>
data != oldDelegate.data || colors != oldDelegate.colors;
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
List<double> data = [30, 20, 50];
List<Color> colors = [Colors.blue, Colors.green, Colors.orange];
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
CustomPaint(
painter:
PieChart(
data: data,
colors: colors,),
size: const Size(100,100))
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment