119 lines
3.8 KiB
Dart
119 lines
3.8 KiB
Dart
|
|
/// Simple pie chart example.
|
||
|
|
import 'package:charts_flutter/flutter.dart' as charts;
|
||
|
|
import 'package:flutter/material.dart';
|
||
|
|
import 'package:intl/intl.dart';
|
||
|
|
import 'package:fcs/vo/buyer.dart';
|
||
|
|
import 'package:fcs/vo/product.dart';
|
||
|
|
import 'package:fcs/widget/localization/app_translations.dart';
|
||
|
|
|
||
|
|
class QuotaChart extends StatelessWidget {
|
||
|
|
static final numberFormatter = new NumberFormat("#,###");
|
||
|
|
|
||
|
|
final List<charts.Series> quotaSeries;
|
||
|
|
final String title;
|
||
|
|
|
||
|
|
QuotaChart(this.quotaSeries, this.title);
|
||
|
|
|
||
|
|
factory QuotaChart.dailyQuota(
|
||
|
|
BuildContext context, Buyer buyer, List<Product> products) {
|
||
|
|
List<Quota> data = [];
|
||
|
|
products.sort((p1, p2) => p1.displayOrder.compareTo(p2.displayOrder));
|
||
|
|
products.forEach((p) {
|
||
|
|
if (buyer.dailyQuotaUsedProducts.containsKey(p.id)) {
|
||
|
|
int value = buyer.dailyQuotaUsedProducts[p.id];
|
||
|
|
data.add(Quota(p.name, value, p.color));
|
||
|
|
}
|
||
|
|
});
|
||
|
|
data.add(
|
||
|
|
new Quota(AppTranslations.of(context).text("chart.remaining"),
|
||
|
|
buyer.dailyQuota - buyer.dailyQuotaUsed, Colors.purple.value),
|
||
|
|
);
|
||
|
|
|
||
|
|
return new QuotaChart(
|
||
|
|
_createData(data),
|
||
|
|
AppTranslations.of(context).text("chart.daily.title",
|
||
|
|
translationVariables: [numberFormatter.format(buyer.dailyQuota)]));
|
||
|
|
}
|
||
|
|
|
||
|
|
factory QuotaChart.maxQuota(
|
||
|
|
BuildContext context, Buyer buyer, List<Product> products) {
|
||
|
|
List<Quota> data = [];
|
||
|
|
products.sort((p1, p2) => p1.displayOrder.compareTo(p2.displayOrder));
|
||
|
|
products.forEach((p) {
|
||
|
|
if (buyer.maxQuotaUsedProducts.containsKey(p.id)) {
|
||
|
|
int value = buyer.maxQuotaUsedProducts[p.id];
|
||
|
|
data.add(Quota(p.name, value, p.color));
|
||
|
|
}
|
||
|
|
});
|
||
|
|
data.add(
|
||
|
|
new Quota(AppTranslations.of(context).text("chart.remaining"),
|
||
|
|
buyer.maxQuota - buyer.maxQuotaUsed, Colors.purple.value),
|
||
|
|
);
|
||
|
|
return new QuotaChart(
|
||
|
|
_createData(data),
|
||
|
|
AppTranslations.of(context).text("chart.max.title",
|
||
|
|
translationVariables: [numberFormatter.format(buyer.maxQuota)]));
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Widget build(BuildContext context) {
|
||
|
|
return Column(
|
||
|
|
children: <Widget>[
|
||
|
|
Text(title),
|
||
|
|
Padding(
|
||
|
|
padding: const EdgeInsets.all(8.0),
|
||
|
|
child: Container(
|
||
|
|
height: 200,
|
||
|
|
child: charts.PieChart(
|
||
|
|
quotaSeries,
|
||
|
|
animate: false,
|
||
|
|
behaviors: [
|
||
|
|
new charts.DatumLegend(
|
||
|
|
position: charts.BehaviorPosition.end,
|
||
|
|
horizontalFirst: false,
|
||
|
|
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: 60,
|
||
|
|
arcRendererDecorators: [
|
||
|
|
new charts.ArcLabelDecorator(
|
||
|
|
labelPosition: charts.ArcLabelPosition.auto,
|
||
|
|
labelPadding: 0)
|
||
|
|
],
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
],
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
static List<charts.Series<Quota, String>> _createData(List<Quota> data) {
|
||
|
|
return [
|
||
|
|
new charts.Series<Quota, String>(
|
||
|
|
id: 'Daily Quota',
|
||
|
|
domainFn: (Quota quota, _) => "${quota.label}\n",
|
||
|
|
measureFn: (Quota quota, _) => quota.quota,
|
||
|
|
data: data,
|
||
|
|
colorFn: (Quota quota, i) =>
|
||
|
|
charts.ColorUtil.fromDartColor(Color(quota.color)),
|
||
|
|
labelAccessorFn: (Quota row, _) =>
|
||
|
|
'${numberFormatter.format(row.quota)}',
|
||
|
|
)
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
class Quota {
|
||
|
|
final String label;
|
||
|
|
final int quota;
|
||
|
|
final int color;
|
||
|
|
|
||
|
|
Quota(this.label, this.quota, this.color);
|
||
|
|
}
|