pubspec.yaml파일에 해당 코드를 추가한다.
charts_flutter: ^버전
dependencies:
charts_flutter: ^0.9.0
커맨드 창에서
$ flutter pub get
명령어를 입력한다.
import 'package:charts_flutter/flutter.dart' as charts;
class _AutoAssetChartState extends State<AutoAssetChart> {
List<charts.Series<AssetPortfolio, String>> _seriesPieData;
class _AutoAssetChartState extends State<AutoAssetChart> {
List<charts.Series<AssetPortfolio, String>> _seriesPieData;
List<AssetPortfolio> mydata;
_generateData(mydata) {
_seriesPieData = List<charts.Series<AssetPortfolio, String>>();
_seriesPieData = [];
_seriesPieData.add(
charts.Series(
id: 'assetportfolios',
domainFn: (AssetPortfolio portfolio, _) => portfolio.kind, // 도메인 이름
measureFn: (AssetPortfolio portfolio, _) => portfolio.money, //실제 그려지는 데이터의 크기
data: mydata,
// Set a label accessor to control the text of the arc label.
labelAccessorFn: (AssetPortfolio row, _) => //그래프에 라벨 표시
'${row.percent.toStringAsFixed(1)}%',
),
);
}
@override
Widget build(BuildContext context) {
_generateData(data); //원하는 데이터 입력
}
@override
Widget build(BuildContext context) {
_generateData(data);
return new charts.PieChart(_seriesPieData,
animate: false,
animationDuration: Duration.zero,
behaviors: [
new charts.DatumLegend( //데이터 범례 등록
outsideJustification: charts.OutsideJustification.middleDrawArea,
horizontalFirst: false,
desiredMaxRows: 4,
cellPadding: new EdgeInsets.only(right: 4.0, bottom: 4.0),
entryTextStyle: charts.TextStyleSpec(
color: charts.MaterialPalette.purple.shadeDefault,
fontFamily: 'Georgia',
fontSize: 11))
],
defaultRenderer: new charts.ArcRendererConfig(
arcWidth: 100,
arcRendererDecorators: [
new charts.ArcLabelDecorator(
labelPosition: charts.ArcLabelPosition.inside, //라벨 위치 지정
)
],
));
}