From 5e90444f6391cc5aa48765e177310af359d6e64a Mon Sep 17 00:00:00 2001 From: mubaroqiqbal Date: Tue, 10 Nov 2020 13:43:41 +0700 Subject: [PATCH 1/2] add textStyle and fix strokeWidth --- lib/src/chart_painter.dart | 64 +++++++++++++++++++++------------- lib/src/pie_chart.dart | 70 ++++++++++++++++++++------------------ lib/src/utils.dart | 6 ++++ 3 files changed, 84 insertions(+), 56 deletions(-) diff --git a/lib/src/chart_painter.dart b/lib/src/chart_painter.dart index cdd5281..f3bb7e0 100644 --- a/lib/src/chart_painter.dart +++ b/lib/src/chart_painter.dart @@ -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 colorList, { - this.chartValueStyle, - this.chartValueBackgroundColor, - List values, - List 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 colorList, { + this.chartValueStyle, + this.chartValueBackgroundColor, + List values, + List 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); } @@ -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), @@ -99,8 +103,8 @@ 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); } @@ -123,6 +127,20 @@ class PieChartPainter extends CustomPainter { style: chartValueStyle, text: name, ); + + if(centerTextStyle!=null){ + if(centerTextStyle.fontSize>(chartRadius/7)){ + span = TextSpan( + style: defaultCenterTextStyle, + text: name, + ); + }else{ + span = TextSpan( + style: centerTextStyle, + text: name, + ); + } + } TextPainter tp = TextPainter( text: span, textAlign: TextAlign.center, diff --git a/lib/src/pie_chart.dart b/lib/src/pie_chart.dart index fea7308..fdbdcef 100644 --- a/lib/src/pie_chart.dart +++ b/lib/src/pie_chart.dart @@ -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); @@ -41,6 +42,7 @@ class PieChart extends StatefulWidget { final LegendOptions legendOptions; final ChartValuesOptions chartValuesOptions; final Color emptyColor; + final TextStyle centerTextStyle; @override _PieChartState createState() => _PieChartState(); @@ -65,8 +67,8 @@ class _PieChartState extends State 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(); @@ -99,31 +101,33 @@ class _PieChartState extends State 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), ), @@ -211,15 +215,15 @@ class _PieChartState extends State 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(), ), ); diff --git a/lib/src/utils.dart b/lib/src/utils.dart index 8eebb07..238a63e 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -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, From 0d5a2b951eefb0ef3d0876c19e8238be177bd474 Mon Sep 17 00:00:00 2001 From: mubaroqiqbal Date: Tue, 10 Nov 2020 14:01:30 +0700 Subject: [PATCH 2/2] done --- example/lib/main.dart | 4 +--- lib/src/chart_painter.dart | 12 ++++++------ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 54b27dd..93d0ec4 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -81,9 +81,7 @@ class _HomePageState extends State { 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, diff --git a/lib/src/chart_painter.dart b/lib/src/chart_painter.dart index f3bb7e0..a249ad6 100644 --- a/lib/src/chart_painter.dart +++ b/lib/src/chart_painter.dart @@ -106,7 +106,7 @@ class PieChartPainter extends CustomPainter { .toStringAsFixed(this.decimalPlaces) + '%') : value; - _drawName(canvas, name, x, y, side); + _drawName(canvas, name, x, y, side, chartValueStyle); } } _prevAngle = _prevAngle + (((_totalAngle) / _total) * _subParts[i]); @@ -119,24 +119,24 @@ 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/7)){ + if(centerTextStyle.fontSize>(chartRadius/6.5)){ span = TextSpan( style: defaultCenterTextStyle, text: name, ); }else{ span = TextSpan( - style: centerTextStyle, + style: style, text: name, ); }