forked from eatvio/eatvio_app
136 lines
4.8 KiB
Dart
136 lines
4.8 KiB
Dart
import 'package:eatvio_app/features/insights/models/insights_payload_model.dart';
|
||
import 'package:eatvio_app/features/insights/presentation/charts/insights_chart_common.dart';
|
||
import 'package:eatvio_app/features/insights/presentation/widgets/insights_colors.dart';
|
||
import 'package:eatvio_app/generated/l10n.dart';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:syncfusion_flutter_charts/charts.dart';
|
||
|
||
/// КБЖУ по дням: калории — площадь на левой оси («ккал»), Б/Ж/У — линии на
|
||
/// правой оси («граммы»). [visibility] — Калории, Белки, Жиры, Углеводы.
|
||
class DailyKbjuChart extends StatelessWidget {
|
||
const DailyKbjuChart({
|
||
super.key,
|
||
required this.model,
|
||
required this.visibility,
|
||
});
|
||
|
||
final DailyChartModel model;
|
||
final List<bool> visibility;
|
||
|
||
static const String _gramsAxisName = 'grams';
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final s = S.of(context);
|
||
final calories = buildPoints(model.labels, model.kcal);
|
||
if (calories.isEmpty) {
|
||
return InsightsChartPlaceholder(text: s.insights_not_enough);
|
||
}
|
||
final proteins = buildPoints(model.labels, model.proteins);
|
||
final fats = buildPoints(model.labels, model.fats);
|
||
final carbs = buildPoints(model.labels, model.carbs);
|
||
|
||
// Локальный диапазон по данным (как на вебе), не ось от нуля.
|
||
// Кривую ниже точек держит insightsSplineType (monotonic).
|
||
final kcalRange = paddedRange(
|
||
calories.map((e) => e.value),
|
||
minPadding: 50,
|
||
clampZero: true,
|
||
);
|
||
final gramValues = [
|
||
...proteins.map((e) => e.value),
|
||
...fats.map((e) => e.value),
|
||
...carbs.map((e) => e.value),
|
||
];
|
||
final gramsRange = paddedRange(
|
||
gramValues,
|
||
minPadding: 5,
|
||
clampZero: true,
|
||
);
|
||
|
||
final grams = [
|
||
(
|
||
name: s.insights_series_proteins,
|
||
color: InsightsColors.proteins,
|
||
points: proteins,
|
||
),
|
||
(name: s.insights_series_fats, color: InsightsColors.fats, points: fats),
|
||
(
|
||
name: s.insights_series_carbs,
|
||
color: InsightsColors.carbs,
|
||
points: carbs,
|
||
),
|
||
];
|
||
|
||
return SizedBox(
|
||
height: insightsChartHeight,
|
||
child: SfCartesianChart(
|
||
key: const Key('insights_daily_chart'),
|
||
margin: insightsChartMargin,
|
||
plotAreaBorderWidth: 0,
|
||
primaryXAxis: buildInsightsIndexAxis(model.labels),
|
||
primaryYAxis: NumericAxis(
|
||
title: AxisTitle(
|
||
text: s.insights_unit_kcal,
|
||
textStyle: insightsAxisLabelStyle,
|
||
),
|
||
labelStyle: insightsAxisLabelStyle,
|
||
minimum: kcalRange.min,
|
||
maximum: kcalRange.max,
|
||
majorTickLines: const MajorTickLines(size: 0),
|
||
axisLine: const AxisLine(width: 0),
|
||
majorGridLines: MajorGridLines(
|
||
color: InsightsColors.border.withValues(alpha: 0.8),
|
||
dashArray: const [4, 4],
|
||
),
|
||
),
|
||
axes: <ChartAxis>[
|
||
NumericAxis(
|
||
name: _gramsAxisName,
|
||
opposedPosition: true,
|
||
minimum: gramsRange.min,
|
||
maximum: gramsRange.max,
|
||
title: AxisTitle(
|
||
text: s.insights_unit_grams,
|
||
textStyle: insightsAxisLabelStyle,
|
||
),
|
||
labelStyle: insightsAxisLabelStyle,
|
||
majorTickLines: const MajorTickLines(size: 0),
|
||
axisLine: const AxisLine(width: 0),
|
||
majorGridLines: const MajorGridLines(width: 0),
|
||
),
|
||
],
|
||
trackballBehavior: buildInsightsTrackball(),
|
||
series: <CartesianSeries<InsightsPoint, num>>[
|
||
if (_isVisible(0))
|
||
SplineAreaSeries<InsightsPoint, num>(
|
||
name: s.insights_series_calories,
|
||
dataSource: calories,
|
||
xValueMapper: (point, _) => point.index,
|
||
yValueMapper: (point, _) => point.value,
|
||
splineType: insightsSplineType,
|
||
borderColor: InsightsColors.calories,
|
||
borderWidth: 3,
|
||
gradient: buildInsightsAreaGradient(InsightsColors.calories),
|
||
),
|
||
for (var i = 0; i < grams.length; i++)
|
||
if (_isVisible(i + 1))
|
||
SplineSeries<InsightsPoint, num>(
|
||
name: grams[i].name,
|
||
yAxisName: _gramsAxisName,
|
||
dataSource: grams[i].points,
|
||
xValueMapper: (point, _) => point.index,
|
||
yValueMapper: (point, _) => point.value,
|
||
splineType: insightsSplineType,
|
||
color: grams[i].color,
|
||
width: 2.5,
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
bool _isVisible(int index) =>
|
||
index < visibility.length ? visibility[index] : true;
|
||
}
|