Skip to content

Instantly share code, notes, and snippets.

@Abdullamhd
Created May 2, 2020 11:07
Show Gist options
  • Save Abdullamhd/8f1b9998295c4cd32dd7a6f9cde2f49a to your computer and use it in GitHub Desktop.
Save Abdullamhd/8f1b9998295c4cd32dd7a6f9cde2f49a to your computer and use it in GitHub Desktop.
this is pure pi chart in dart programming language
// chart data
List expenses = [
{"name":"Groceries","amount":500.0},
{"name":"Online Shopping","amount":100.0},
{"name":"Eating out","amount":80.0},
{"name":"Bills","amount":50.0},
{"name":"Subscriptions","amount":100.0},
{"name":"Fees","amount":30.0},
];
List pieColors = [
Colors.indigo[400],
Colors.blue,
Colors.green,
Colors.amber,
Colors.deepOrange,
Colors.brown,
];
// custom painter class for this data
class PiChartPainter extends CustomPainter{
@override
void paint(Canvas canvas, Size size) {
Offset center = Offset(size.width /2 , size.height /2);
double radius = min(size.width /2 , size.height /2);
var paint = new Paint()
..style = PaintingStyle.stroke
..strokeWidth = 100 ;
double total = 0 ;
expenses.forEach((element) {
total += element['amount'];
});
var start = -pi / 4;
for(int i = 0 ; i < expenses.length ; i ++){
var currentExpense = expenses[i];
var sweepRadius = (currentExpense['amount'] / total) * 2 * pi ;
paint.color = pieColors[i];
canvas.drawArc(Rect.fromCircle(center: center,radius: radius), start, sweepRadius, false, paint);
start += sweepRadius ;
}
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
// TODO: implement shouldRepaint
return true ;
}
}
// implementation :-
/*
CustomPaint(
foregroundPainter: PiChartPainter(),
child: Container(),
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment