Skip to content

Instantly share code, notes, and snippets.

@kururu-abdo
Created September 9, 2023 13:44
Show Gist options
  • Save kururu-abdo/a57a5de3bd1bd5d92dcad696cf754f6f to your computer and use it in GitHub Desktop.
Save kururu-abdo/a57a5de3bd1bd5d92dcad696cf754f6f to your computer and use it in GitHub Desktop.
implement line chart with flutter
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:ui' as ui;
class ChartPage extends StatefulWidget {
const ChartPage({super.key});
@override
State<ChartPage> createState() => _ChartPageState();
}
class _ChartPageState extends State<ChartPage> {
@override
Widget build(BuildContext context) {
return
Scaffold(
body: SizedBox.expand(
child: Center(
child: AspectRatio(
aspectRatio: 3/2,
child: CustomPaint(
foregroundPainter: MyPainter(),
child: Container(
color: Colors.grey,
),
),
),
),
),
);
}
}
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
..color =Colors.purple
..strokeWidth= 1
..style = PaintingStyle.stroke
;
canvas.drawRect(
Rect.fromCenter(center:
Offset(size.width/2, size.height/2)
,
width: size.width, height: size.height)
// Offset(0, 0)
,
paint);
var horizontalLines = 5;
var horizontalSize= size.height / (horizontalLines);
var linePainter = Paint()
..color=Colors.purple
..style=PaintingStyle.fill
..strokeWidth=1
;
for (var i = 0; i < horizontalLines; i++) {
var startY = horizontalSize*(0+i);
canvas.drawLine(Offset(0, startY),
Offset(size.width, startY), linePainter);
}
var verticalLines = 5;
var verticalSize= size.width / (verticalLines);
for (var i = 0; i < verticalLines; i++) {
var startX = verticalSize*(0+i);
canvas.drawLine(Offset(startX, 0),
Offset(startX , size.height), linePainter);
}
var pathPaint = Paint()
..color = Colors.green
..style =PaintingStyle.stroke
..strokeWidth=5;
var path = drawPath(true, size);
canvas.drawPath(path, pathPaint);
var innerPath = Path()
..addPath(path, Offset.zero)
..lineTo(size.width, size.height)
..lineTo(0, size.height)
..close();
var innerPaint = Paint()
..shader = ui.Gradient.linear(
Offset.zero,
Offset(size.width , size.height),
[
Colors.green.withAlpha(240),
Colors.transparent,
],
)
..style =PaintingStyle.fill
;
canvas.drawPath(innerPath, innerPaint);
}
Path drawPath(bool closePath , Size size ) {
final width = size.width;
final height = size.height;
final path = Path();
final segmentWidth = width / 3 / 2;
path.moveTo(0, height);
path.lineTo(size.width*.20, size.height*.80);
path.lineTo(size.width*.35, size.height*.70);
path.lineTo(size.width*.50, size.height*.70);
path.lineTo(size.width*.70, size.height*.40);
path.lineTo(size.width*.80, size.height*.90);
path.lineTo(size.width*.90, size.height*.80);
path.lineTo(size.width, size.height);
// path.cubicTo(segmentWidth, height, 2 * segmentWidth, 0, 3 * segmentWidth, 0);
// path.cubicTo(4 * segmentWidth, 0, 5 * segmentWidth, height, 6 * segmentWidth, height);
return path;
}
@override
bool shouldRepaint(MyPainter oldDelegate) => false;
@override
bool shouldRebuildSemantics(MyPainter oldDelegate) => false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment