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

Feature legend position #17

Merged
merged 2 commits into from
Nov 10, 2019
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ PieChart(
chartValueBackgroundColor: Colors.grey[200],
colorList: colorList,
showLegends: true,
legendPosition: LegendPosition.right,
decimalPlaces: 1,
showChartValueLabel: true,
initialAngle: 0,
Expand Down
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class _HomePageState extends State<HomePage> {
chartValueBackgroundColor: Colors.grey[200],
colorList: colorList,
showLegends: true,
legendPosition: LegendPosition.right,
decimalPlaces: 1,
showChartValueLabel: true,
initialAngle: 0,
Expand Down
164 changes: 113 additions & 51 deletions lib/src/pie_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import 'chart_painter.dart';
import 'legend.dart';
import 'utils.dart';

enum LegendPosition { top, bottom, left, right }

class PieChart extends StatefulWidget {
PieChart({
@required this.dataMap,
this.showChartValueLabel = false,
this.chartValueStyle = defaultChartValueStyle,
this.legendPosition = LegendPosition.right,
this.chartValueBackgroundColor = Colors.grey,
this.legendStyle = defaultLegendStyle,
this.chartRadius,
Expand All @@ -34,6 +37,8 @@ class PieChart extends StatefulWidget {
//Legend styling
final TextStyle legendStyle;

final LegendPosition legendPosition;

final double chartRadius;
final Duration animationDuration;
final double chartLegendSpacing;
Expand Down Expand Up @@ -121,62 +126,119 @@ class _PieChartState extends State<PieChart>
super.dispose();
}

_getChart() {
return Flexible(
child: LayoutBuilder(
builder: (_, c) => Container(
height: widget.chartRadius != null
? c.maxWidth < widget.chartRadius
? c.maxWidth
: widget.chartRadius
: null,
child: CustomPaint(
painter: PieChartPainter(
_fraction,
widget.showChartValuesOutside,
widget.colorList,
chartValueStyle: widget.chartValueStyle,
chartValueBackgroundColor: widget.chartValueBackgroundColor,
values: legendValues,
initialAngle: widget.initialAngle,
showValuesInPercentage: widget.showChartValuesInPercentage,
decimalPlaces: widget.decimalPlaces,
showChartValueLabel: widget.showChartValueLabel,
),
child: AspectRatio(aspectRatio: 1),
),
),
),
);
}

_getLegend(EdgeInsets legendSpacing) {
if (widget.showLegends) {
return Flexible(
fit: FlexFit.loose,
child: Padding(
padding: legendSpacing,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: legendTitles
.map((item) => Legend(
title: item,
color: getColor(
widget.colorList,
legendTitles.indexOf(item),
),
style: widget.legendStyle,
))
.toList(),
),
),
);
}
}

_getPieChart() {
switch (widget.legendPosition) {
case LegendPosition.top:
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
_getLegend(
EdgeInsets.only(
bottom: widget.chartLegendSpacing,
),
),
_getChart(),
],
);
case LegendPosition.bottom:
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
_getChart(),
_getLegend(
EdgeInsets.only(
top: widget.chartLegendSpacing,
),
),
],
);
case LegendPosition.left:
return Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
_getLegend(
EdgeInsets.only(
right: widget.chartLegendSpacing,
),
),
_getChart(),
],
);
case LegendPosition.right:
return Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
_getChart(),
_getLegend(
EdgeInsets.only(
left: widget.chartLegendSpacing,
),
),
],
);
}
}

@override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.center,
padding: EdgeInsets.all(8.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Flexible(
child: LayoutBuilder(
builder: (_, c) => Container(
height: widget.chartRadius != null
? c.maxWidth < widget.chartRadius
? c.maxWidth
: widget.chartRadius
: null,
child: CustomPaint(
painter: PieChartPainter(
_fraction,
widget.showChartValuesOutside,
widget.colorList,
chartValueStyle: widget.chartValueStyle,
chartValueBackgroundColor: widget.chartValueBackgroundColor,
values: legendValues,
initialAngle: widget.initialAngle,
showValuesInPercentage: widget.showChartValuesInPercentage,
decimalPlaces: widget.decimalPlaces,
showChartValueLabel: widget.showChartValueLabel,
),
child: AspectRatio(aspectRatio: 1),
),
),
),
),
if (widget.showLegends) ...[
SizedBox(width: widget.chartLegendSpacing ?? 16.0),
Flexible(
fit: FlexFit.loose,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: legendTitles
.map((item) => Legend(
title: item,
color: getColor(
widget.colorList,
legendTitles.indexOf(item),
),
style: widget.legendStyle,
))
.toList(),
),
),
]
],
),
child: _getPieChart(),
);
}
}