flutter_application_1/lib/widgets/chart_widget.dart

63 lines
1.7 KiB
Dart
Raw Normal View History

2024-05-06 17:28:10 +02:00
import 'package:flutter/cupertino.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
class StackedColumnChart extends StatelessWidget {
final List<double> lowerValues;
final List<double> upperValues;
const StackedColumnChart({
super.key,
required this.lowerValues,
required this.upperValues,
});
@override
Widget build(BuildContext context) {
return SfCartesianChart(
primaryXAxis: const CategoryAxis(
title: AxisTitle(text: 'Jahr'),
),
primaryYAxis: const NumericAxis(
title: AxisTitle(text: 'Euro'),
),
series: <CartesianSeries>[
StackedColumnSeries<SalesData, int>(
dataSource: _getLowerChartData(),
xValueMapper: (SalesData sales, _) => sales.year,
yValueMapper: (SalesData sales, _) => sales.value,
color: CupertinoColors.systemRed.highContrastColor,
),
StackedColumnSeries<SalesData, int>(
dataSource: _getUpperChartData(),
xValueMapper: (SalesData sales, _) => sales.year,
yValueMapper: (SalesData sales, _) => sales.value,
color: CupertinoColors.systemBlue.highContrastColor,
),
],
);
}
List<SalesData> _getLowerChartData() {
List<SalesData> chartData = [];
for (int i = 0; i < lowerValues.length; i++) {
chartData.add(SalesData(i + 1, lowerValues[i]));
}
return chartData;
}
List<SalesData> _getUpperChartData() {
List<SalesData> chartData = [];
for (int i = 0; i < upperValues.length; i++) {
chartData.add(SalesData(i + 1, upperValues[i]));
}
return chartData;
}
}
class SalesData {
final int year;
final double value;
SalesData(this.year, this.value);
}