Skip to content
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
4 changes: 1 addition & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ class _HomePageState extends State<HomePage> {
dataMap: dataMap,
animationDuration: Duration(milliseconds: 800),
chartLegendSpacing: _chartLegendSpacing,
chartRadius: MediaQuery.of(context).size.width / 3.2 > 300
? 300
: MediaQuery.of(context).size.width / 3.2,
chartRadius: MediaQuery.of(context).size.width / 2,
colorList: colorList,
initialAngleInDegree: 0,
chartType: _chartType,
Expand Down
72 changes: 45 additions & 27 deletions lib/src/chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,40 @@ class PieChartPainter extends CustomPainter {
final ChartType chartType;
final String centerText;
final Function formatChartValues;
final double strokeWidth;
final double ringStrokeWidth;
final Color emptyColor;
final TextStyle centerTextStyle;
final double chartRadius;

double _prevAngle = 0;

PieChartPainter(
double angleFactor,
this.showChartValues,
this.showChartValuesOutside,
List<Color> colorList, {
this.chartValueStyle,
this.chartValueBackgroundColor,
List<double> values,
List<String> titles,
this.initialAngle,
this.showValuesInPercentage,
this.decimalPlaces,
this.showChartValueLabel,
this.chartType,
this.centerText,
this.formatChartValues,
this.strokeWidth,
this.emptyColor,
}) {
double angleFactor,
this.showChartValues,
this.showChartValuesOutside,
List<Color> colorList, {
this.chartValueStyle,
this.chartValueBackgroundColor,
List<double> values,
List<String> titles,
this.initialAngle,
this.showValuesInPercentage,
this.decimalPlaces,
this.showChartValueLabel,
this.chartType,
this.centerText,
this.formatChartValues,
this.ringStrokeWidth,
this.emptyColor,
this.centerTextStyle,
this.chartRadius
}) {
_total = values.fold(0, (v1, v2) => v1 + v2);
for (int i = 0; i < values.length; i++) {
final paint = Paint()..color = getColor(colorList, i);
if (chartType == ChartType.ring) {
paint.style = PaintingStyle.stroke;
paint.strokeWidth = strokeWidth;
paint.strokeWidth = ringStrokeWidth;
}
_paintList.add(paint);
}
Expand All @@ -65,7 +69,7 @@ class PieChartPainter extends CustomPainter {
final paint = Paint()..color = emptyColor;
if (chartType == ChartType.ring) {
paint.style = PaintingStyle.stroke;
paint.strokeWidth = strokeWidth;
paint.strokeWidth = ringStrokeWidth;
}
canvas.drawArc(
new Rect.fromLTWH(0.0, 0.0, side, size.height),
Expand Down Expand Up @@ -99,10 +103,10 @@ class PieChartPainter extends CustomPainter {
if (showChartValues) {
final name = showValuesInPercentage
? (((_subParts.elementAt(i) / _total) * 100)
.toStringAsFixed(this.decimalPlaces) +
'%')
.toStringAsFixed(this.decimalPlaces) +
'%')
: value;
_drawName(canvas, name, x, y, side);
_drawName(canvas, name, x, y, side, chartValueStyle);
}
}
_prevAngle = _prevAngle + (((_totalAngle) / _total) * _subParts[i]);
Expand All @@ -115,14 +119,28 @@ class PieChartPainter extends CustomPainter {
}

void _drawCenterText(Canvas canvas, double side) {
_drawName(canvas, centerText, 0, 0, side);
_drawName(canvas, centerText, 0, 0, side, centerTextStyle);
}

void _drawName(Canvas canvas, String name, double x, double y, double side) {
void _drawName(Canvas canvas, String name, double x, double y, double side, TextStyle style){
TextSpan span = TextSpan(
style: chartValueStyle,
style: style,
text: name,
);

if(centerTextStyle!=null){
if(centerTextStyle.fontSize>(chartRadius/6.5)){
span = TextSpan(
style: defaultCenterTextStyle,
text: name,
);
}else{
span = TextSpan(
style: style,
text: name,
);
}
}
TextPainter tp = TextPainter(
text: span,
textAlign: TextAlign.center,
Expand Down
70 changes: 37 additions & 33 deletions lib/src/pie_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class PieChart extends StatefulWidget {
this.legendOptions = const LegendOptions(),
this.chartValuesOptions = const ChartValuesOptions(),
this.emptyColor = Colors.grey,
this.centerTextStyle = defaultCenterTextStyle,
Key key,
}) : super(key: key);

Expand All @@ -41,6 +42,7 @@ class PieChart extends StatefulWidget {
final LegendOptions legendOptions;
final ChartValuesOptions chartValuesOptions;
final Color emptyColor;
final TextStyle centerTextStyle;

@override
_PieChartState createState() => _PieChartState();
Expand All @@ -65,8 +67,8 @@ class _PieChartState extends State<PieChart>

void initData() {
assert(
widget.dataMap != null && widget.dataMap.isNotEmpty,
"dataMap passed to pie chart cant be null or empty",
widget.dataMap != null && widget.dataMap.isNotEmpty,
"dataMap passed to pie chart cant be null or empty",
);
initLegends();
initValues();
Expand Down Expand Up @@ -99,31 +101,33 @@ class _PieChartState extends State<PieChart>
builder: (_, c) => Container(
height: widget.chartRadius != null
? c.maxWidth < widget.chartRadius
? c.maxWidth
: widget.chartRadius
? c.maxWidth
: widget.chartRadius
: null,
child: CustomPaint(
painter: PieChartPainter(
_animFraction,
widget.chartValuesOptions.showChartValues,
widget.chartValuesOptions.showChartValuesOutside,
widget.colorList,
chartValueStyle: widget.chartValuesOptions.chartValueStyle,
chartValueBackgroundColor:
widget.chartValuesOptions.chartValueBackgroundColor,
values: legendValues,
titles: legendTitles,
initialAngle: widget.initialAngleInDegree,
showValuesInPercentage:
widget.chartValuesOptions.showChartValuesInPercentage,
decimalPlaces: widget.chartValuesOptions.decimalPlaces,
showChartValueLabel:
widget.chartValuesOptions.showChartValueBackground,
chartType: widget.chartType,
centerText: widget.centerText,
formatChartValues: widget.formatChartValues,
strokeWidth: widget.ringStrokeWidth,
emptyColor: widget.emptyColor,
_animFraction,
widget.chartValuesOptions.showChartValues,
widget.chartValuesOptions.showChartValuesOutside,
widget.colorList,
chartValueStyle: widget.chartValuesOptions.chartValueStyle,
chartValueBackgroundColor:
widget.chartValuesOptions.chartValueBackgroundColor,
values: legendValues,
titles: legendTitles,
initialAngle: widget.initialAngleInDegree,
showValuesInPercentage:
widget.chartValuesOptions.showChartValuesInPercentage,
decimalPlaces: widget.chartValuesOptions.decimalPlaces,
showChartValueLabel:
widget.chartValuesOptions.showChartValueBackground,
chartType: widget.chartType,
centerText: widget.centerText,
formatChartValues: widget.formatChartValues,
ringStrokeWidth: widget.ringStrokeWidth,
emptyColor: widget.emptyColor,
centerTextStyle: widget.centerTextStyle,
chartRadius: widget.chartRadius
),
child: AspectRatio(aspectRatio: 1),
),
Expand Down Expand Up @@ -211,15 +215,15 @@ class _PieChartState extends State<PieChart>
children: legendTitles
.map(
(item) => Legend(
title: item,
color: getColor(
widget.colorList,
legendTitles.indexOf(item),
),
style: widget.legendOptions.legendTextStyle,
legendShape: widget.legendOptions.legendShape,
),
)
title: item,
color: getColor(
widget.colorList,
legendTitles.indexOf(item),
),
style: widget.legendOptions.legendTextStyle,
legendShape: widget.legendOptions.legendShape,
),
)
.toList(),
),
);
Expand Down
6 changes: 6 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ const defaultChartValueStyle = TextStyle(
color: Colors.black,
);

const defaultCenterTextStyle = TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
color: Colors.black,
);

const defaultLegendStyle = TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
Expand Down