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 all 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
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ PieChart(
),
// gradientList: ---To add gradient colors---
// emptyColorGradient: ---Empty Color gradient---
// textureList: ---To add image textures----
)
```

Expand Down Expand Up @@ -135,6 +136,57 @@ emptyColorGradient: [

<img src="https://raw.githubusercontent.com/apgapg/pie_chart/master/res/s11.png" height = "400" alt="PieChart">

### Image Texures

Pie Chart supports image textures as colors. Just pass `textureList` instead of `colorList` or `gradientList` to add textures to each section.

```dart
Future<ui.Image> _loadImage(String path) async {
final image = await rootBundle.load(path);
return await decodeImageFromList(image.buffer.asUint8List());
}

Future<List<ImageShader>> _loadImageShaders(List<String> paths) async {
List<ImageShader> images = [];
for (String path in paths) {
ui.Image img = await _loadImage(path);
images.add(ImageShader(img, TileMode.mirror, TileMode.mirror, Matrix4.identity().storage));
}
return images;
}

FutureBuilder<List<ImageShader>>(
future: _loadImageShaders([
'textures/tile.jpg',
'textures/tile4.jpg',
'textures/tile2.jpg',
'textures/tile3.jpg',
]),
builder: (context, textures) => textures.hasData ? SizedBox(
height: 300,
width: 300,
child: PieChart(
dataMap: {
"Flutter": 5,
"React": 2,
"Xamarin": 2,
"Ionic": 3,
},
textureList: textures.data,
legendOptions: LegendOptions(
showLegends: false
),
chartValuesOptions: ChartValuesOptions(
showChartValuesInPercentage: true,
),
),
) : Text('loading'),
),
```

<img src="https://raw.githubusercontent.com/apgapg/pie_chart/master/res/s13.jpg" height = "400" alt="PieChart">


### Base Chart Color

Add a base pie-chart color via:
Expand Down
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
Binary file added res/s13.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.