Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gradient for PieChart #1524

Merged
merged 3 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/src/chart/pie_chart/pie_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class PieChartSectionData {
PieChartSectionData({
double? value,
Color? color,
this.gradient,
double? radius,
bool? showTitle,
this.titleStyle,
Expand Down Expand Up @@ -178,6 +179,9 @@ class PieChartSectionData {
/// Defines the color of section.
final Color color;

/// Defines the gradient of section. If specified, overrides the color setting.
final Gradient? gradient;

/// Defines the radius of section.
final double radius;

Expand Down Expand Up @@ -218,6 +222,7 @@ class PieChartSectionData {
PieChartSectionData copyWith({
double? value,
Color? color,
Gradient? gradient,
double? radius,
bool? showTitle,
TextStyle? titleStyle,
Expand All @@ -230,6 +235,7 @@ class PieChartSectionData {
return PieChartSectionData(
value: value ?? this.value,
color: color ?? this.color,
gradient: gradient ?? this.gradient,
radius: radius ?? this.radius,
showTitle: showTitle ?? this.showTitle,
titleStyle: titleStyle ?? this.titleStyle,
Expand All @@ -252,6 +258,7 @@ class PieChartSectionData {
return PieChartSectionData(
value: lerpDouble(a.value, b.value, t),
color: Color.lerp(a.color, b.color, t),
gradient: Gradient.lerp(a.gradient, b.gradient, t),
radius: lerpDouble(a.radius, b.radius, t),
showTitle: b.showTitle,
titleStyle: TextStyle.lerp(a.titleStyle, b.titleStyle, t),
Expand Down
17 changes: 15 additions & 2 deletions lib/src/chart/pie_chart/pie_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:fl_chart/fl_chart.dart';
import 'package:fl_chart/src/chart/base/base_chart/base_chart_painter.dart';
import 'package:fl_chart/src/chart/base/line.dart';
import 'package:fl_chart/src/chart/pie_chart/pie_chart_data.dart';
import 'package:fl_chart/src/extensions/paint_extension.dart';
import 'package:fl_chart/src/utils/canvas_wrapper.dart';
import 'package:fl_chart/src/utils/utils.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -102,9 +103,17 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {
final sectionDegree = sectionsAngle[i];

if (sectionDegree == 360) {
final radius = centerRadius + section.radius / 2;
final rect = Rect.fromCircle(center: center, radius: radius);
_sectionPaint
..color = section.color
..setColorOrGradient(
section.color,
section.gradient,
rect,
)
..strokeWidth = section.radius
..style = PaintingStyle.fill;

final bounds = Rect.fromCircle(
center: center,
radius: centerRadius + section.radius,
Expand Down Expand Up @@ -293,7 +302,11 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {
CanvasWrapper canvasWrapper,
) {
_sectionPaint
..color = section.color
..setColorOrGradient(
section.color,
section.gradient,
sectionPath.getBounds(),
)
..style = PaintingStyle.fill;
canvasWrapper.drawPath(sectionPath, _sectionPaint);
}
Expand Down
1 change: 1 addition & 0 deletions repo_files/documentations/pie_chart.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ When you change the chart's state, it animates to the new state internally (usin
|:---------------|:---------------|:-------|
|value| value is the weight of each section, for example if all values is 25, and we have 4 section, then the sum is 100 and each section takes 1/4 of the whole circle (360/4) degree|10|
|color| colors the section| Colors.red
|gradient| You can use any [Gradient](https://api.flutter.dev/flutter/dart-ui/Gradient-class.html) here. such as [LinearGradient](https://api.flutter.dev/flutter/painting/LinearGradient-class.html) or [RadialGradient](https://api.flutter.dev/flutter/painting/RadialGradient-class.html)|null|
raldhafiri marked this conversation as resolved.
Show resolved Hide resolved
|radius| the width radius of each section|40|
|showTitle| determines to show or hide the titles on each section|true|
|titleStyle| TextStyle of the titles| TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold)|
Expand Down
14 changes: 1 addition & 13 deletions test/chart/pie_chart/pie_chart_painter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,26 +172,14 @@ void main() {
final result2 = verify(
mockCanvasWrapper.drawCircle(
const Offset(100, 100),
10 + 30 - (3 / 2),
10 + (3 / 2),
captureAny,
),
);
expect(result2.callCount, 1);
expect((result2.captured.single as Paint).color, MockData.color3);
expect((result2.captured.single as Paint).strokeWidth, 3);
expect((result2.captured.single as Paint).style, PaintingStyle.stroke);

final result3 = verify(
mockCanvasWrapper.drawCircle(
const Offset(100, 100),
10 + (3 / 2),
captureAny,
),
);
expect(result3.callCount, 1);
expect((result3.captured.single as Paint).color, MockData.color3);
expect((result3.captured.single as Paint).strokeWidth, 3);
expect((result3.captured.single as Paint).style, PaintingStyle.stroke);
});

test('test 2', () {
Expand Down