athlete_power_ratio_chart.dart 2.85 KB
Newer Older
1 2
import 'dart:math';

Administrator's avatar
Administrator committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import 'package:charts_flutter/flutter.dart';
import 'package:flutter/material.dart';
import 'package:encrateia/models/activity.dart';
import 'package:encrateia/models/activity_list.dart';
import 'package:encrateia/utils/enums.dart';

class AthletePowerRatioChart extends StatelessWidget {
  final List<Activity> activities;

  AthletePowerRatioChart({@required this.activities});

  @override
  Widget build(BuildContext context) {
    int xAxesDays = 60;

18
    ActivityList(activities: activities).enrichGlidingAverage(
Administrator's avatar
Administrator committed
19 20 21 22
      quantity: ActivityAttr.avgPowerRatio,
      fullDecay: 30,
    );

23
    var recentActivities = activities
Administrator's avatar
Administrator committed
24 25 26 27
        .where((activity) =>
            DateTime.now().difference(activity.db.timeCreated).inDays <
            xAxesDays)
        .toList();
28 29 30 31
    if (recentActivities.length < 40) {
      int amount = min(activities.length, 40);
      recentActivities = activities.sublist(0, amount);
    }
Administrator's avatar
Administrator committed
32 33 34 35 36 37 38 39 40 41

    var data = [
      Series<Activity, DateTime>(
        id: 'Average power ratio',
        colorFn: (_, __) => MaterialPalette.blue.shadeDefault,
        domainFn: (Activity activity, _) => activity.db.timeCreated,
        measureFn: (Activity activity, _) =>
            100 *
            (activity.db.avgPower - activity.db.avgFormPower) /
            activity.db.avgPower,
42
        data: recentActivities,
Administrator's avatar
Administrator committed
43 44 45 46 47 48
      ),
      Series<Activity, DateTime>(
        id: 'Gliding average power ratio',
        colorFn: (_, __) => MaterialPalette.green.shadeDefault,
        domainFn: (Activity activity, _) => activity.db.timeCreated,
        measureFn: (Activity activity, _) => activity.glidingAvgPowerRatio,
49
        data: recentActivities,
Administrator's avatar
Administrator committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
      )..setAttribute(rendererIdKey, 'glidingAverageRenderer'),
    ];

    return new Container(
      height: 300,
      child: TimeSeriesChart(
        data,
        animate: false,
        defaultRenderer: LineRendererConfig(
          includePoints: true,
          includeLine: false,
        ),
        customSeriesRenderers: [
          LineRendererConfig(
            customRendererId: 'glidingAverageRenderer',
            dashPattern: [1, 2],
          ),
        ],
        primaryMeasureAxis: NumericAxisSpec(
          tickProviderSpec: BasicNumericTickProviderSpec(
            zeroBound: false,
            dataIsInWholeNumbers: false,
            desiredTickCount: 6,
          ),
        ),
        behaviors: [
          ChartTitle(
            'Power Ratio (%)',
            titleStyleSpec: TextStyleSpec(fontSize: 13),
            behaviorPosition: BehaviorPosition.start,
            titleOutsideJustification: OutsideJustification.end,
          ),
          ChartTitle(
            'Date',
            titleStyleSpec: TextStyleSpec(fontSize: 13),
            behaviorPosition: BehaviorPosition.bottom,
            titleOutsideJustification: OutsideJustification.end,
          ),
        ],
      ),
    );
  }
}