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

adding textures to each segment #98

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 22 additions & 1 deletion lib/src/chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PieChartPainter extends CustomPainter {
final Color? emptyColor;
final List<List<Color>>? gradientList;
final List<Color>? emptyColorGradient;
final List<ImageShader>? textureList;
final DegreeOptions degreeOptions;
final Color baseChartColor;
final double? totalValue;
Expand Down Expand Up @@ -54,6 +55,7 @@ class PieChartPainter extends CustomPainter {
this.emptyColor,
this.gradientList,
this.emptyColorGradient,
this.textureList,
this.degreeOptions = const DegreeOptions(),
required this.baseChartColor,
this.totalValue,
Expand All @@ -65,14 +67,16 @@ class PieChartPainter extends CustomPainter {
_total = totalValue!;
}

if (gradientList?.isEmpty ?? true) {
if ((gradientList?.isEmpty ?? true) && (textureList?.isEmpty ?? true)) {
for (int i = 0; i < values.length; i++) {
final paint = Paint()..color = getColor(colorList, i);
setPaintProps(paint);
_paintList.add(paint);
}
}



_totalAngle = angleFactor * doublePi * drawPercentage;
_subParts = values;
_prevAngle = degreeToRadian(degreeOptions.initialAngle);
Expand Down Expand Up @@ -136,6 +140,7 @@ class PieChartPainter extends CustomPainter {
final isGradientPresent = gradientList?.isNotEmpty ?? false;
final isNonGradientElementPresent =
(_subParts.length - (gradientList?.length ?? 0)) > 0;
final isTexturePresent = textureList?.isNotEmpty ?? false;

for (int i = 0; i < _subParts.length; i++) {
if (isGradientPresent) {
Expand All @@ -152,6 +157,22 @@ class PieChartPainter extends CustomPainter {
emptyColorGradient: emptyColorGradient!),
);
paint.shader = gradient.createShader(boundingSquare);
if (chartType == ChartType.ring) {
paint.style = PaintingStyle.stroke;
paint.strokeWidth = strokeWidth!;
paint.strokeCap = StrokeCap.butt;
}
canvas.drawArc(
boundingSquare,
_prevAngle,
endAngle,
useCenter,
paint,
);
} else if (isTexturePresent) {
final endAngle = (((_totalAngle) / _total) * _subParts[i]);
final paint = Paint()..shader = textureList![i];

if (chartType == ChartType.ring) {
paint.style = PaintingStyle.stroke;
paint.strokeWidth = strokeWidth!;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/pie_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class PieChart extends StatefulWidget {
this.emptyColor = Colors.grey,
this.gradientList,
this.emptyColorGradient = const [Colors.black26, Colors.black54],
this.textureList,
this.legendLabels = const {},
Key? key,
this.degreeOptions = const DegreeOptions(),
Expand All @@ -41,6 +42,7 @@ class PieChart extends StatefulWidget {
final double chartLegendSpacing;
final List<Color> colorList;
final List<List<Color>>? gradientList;
final List<ImageShader>? textureList;
@Deprecated('use degreeOptions. instead')
final double? initialAngleInDegree;
final Function? formatChartValues;
Expand Down Expand Up @@ -146,6 +148,7 @@ class _PieChartState extends State<PieChart>
emptyColor: widget.emptyColor,
gradientList: widget.gradientList,
emptyColorGradient: widget.emptyColorGradient,
textureList: widget.textureList,
degreeOptions: widget.degreeOptions.copyWith(
// because we've deprecated initialAngleInDegree,
// we want the old value to be used if it's not null
Expand Down