Sync Fusion Charts Time Interval Problem

Using time intervals to change view of data currently have this week, last week, last 30 days, last 90 days, and last 12 months. Everything works except for last week as it displays same data as this week, see picture.

Here is the code for the custom widget:

// Automatic FlutterFlow imports

import '/backend/backend.dart';

import '/backend/schema/structs/index.dart';

import '/flutter_flow/flutter_flow_theme.dart';

import '/flutter_flow/flutter_flow_util.dart';

import '/custom_code/widgets/index.dart'; // Imports other custom widgets

import '/flutter_flow/custom_functions.dart'; // Imports custom functions

import 'package:flutter/material.dart';

// Begin custom widget code

// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'package:intl/intl.dart';

import 'package:syncfusion_flutter_charts/charts.dart';

class StripeFinancialsChart extends StatefulWidget {

  const StripeFinancialsChart({

    Key? key,

    this.width,

    this.height,

    this.stripeData,

    this.range,

    this.intervalType,

  }) : super(key: key);

  final double? width;

  final double? height;

  final List<StripeDataRecord>? stripeData;

  final String? intervalType;

  final int? range;

  @override

  StripeFinancialsChartState createState() => StripeFinancialsChartState();

}

class _StripeFinancialsChartState extends State<StripeFinancialsChart> {

  UniqueKey _chartKey = UniqueKey();

  @override

  void didUpdateWidget(covariant StripeFinancialsChart oldWidget) {

    super.didUpdateWidget(oldWidget);

    if (widget.range != oldWidget.range ||

        widget.intervalType != oldWidget.intervalType) {

      _chartKey = UniqueKey();

      WidgetsBinding.instance.addPostFrameCallback((_) {

        setState(() {});

      });

    }

  }

  DateTime _getFirstDayOfWeek(DateTime dateTime) {

    int dayOffset = dateTime.weekday - DateTime.monday;

    return dateTime.subtract(Duration(days: dayOffset));

  }

  DateTime _determineChartStartDate() {

    DateTime now = DateTime.now();

    print('Current date/time: $now');

    DateTime startOfLastWeek;

    switch (widget.intervalType) {

      case 'This week':

        return _getFirstDayOfWeek(now);

      case 'Last week':

        DateTime previousSunday = now.subtract(Duration(days: now.weekday));

        startOfLastWeek =

            _getFirstDayOfWeek(previousSunday).subtract(Duration(days: 7));

        print('Start of last week: $startOfLastWeek');

        return startOfLastWeek;

      case 'Last 12 Months':

        return DateTime(now.year - 1, now.month);

      case 'Last 90 Days':

        return now.subtract(Duration(days: 90));

      case 'Last 30 Days':

        return now.subtract(Duration(days: 30));

      default:

        return _getFirstDayOfWeek(now); // Default to 'This week'

    }

  }

  DateTime _determineChartEndDate(DateTime startDate) {

    switch (widget.intervalType) {

      case 'This week':

      case 'Last week':

        return startDate.add(Duration(days: 7));

      case 'Last 12 Months':

        return DateTime(startDate.year + 1, startDate.month);

      case 'Last 90 Days':

        return startDate.add(Duration(days: 90));

      case 'Last 30 Days':

        return startDate.add(Duration(days: 30));

      default:

        return startDate.add(Duration(days: 6)); // Default to 'This week'

    }

  }

  List<StripeDataRecord> _filterDataForInterval(List<StripeDataRecord>? data) {

    DateTime startDate = _determineChartStartDate();

    DateTime endDate = _determineChartEndDate(startDate);

    return data

            ?.where((record) =>

                record.timestamp != null &&

                record.timestamp!.isAfter(startDate) &&

                record.timestamp!.isBefore(endDate))

            .toList() ??

        [];

  }

  @override

  Widget build(BuildContext context) {

    DateTime visibleMinimum = _determineChartStartDate();

    DateTime visibleMaximum = _determineChartEndDate(visibleMinimum);

    return ClipRect(

      child: SfCartesianChart(

        key: _chartKey,

        plotAreaBorderWidth: 0,

        primaryXAxis: DateTimeAxis(

          intervalType: DateTimeIntervalType.days,

          interval: 1,

          dateFormat: DateFormat('MM/dd'),

          edgeLabelPlacement: EdgeLabelPlacement.shift,

          minimum: visibleMinimum, // Set the minimum range for the x-axis

          maximum: visibleMaximum, // Set the maximum range for the x-axis

        ),

        primaryYAxis: NumericAxis(

          majorTickLines: MajorTickLines(color: Colors.transparent),

          axisLine: AxisLine(width: 0),

        ),

        series: _getLineSeries(),

        trackballBehavior: TrackballBehavior(

          enable: true,

          lineType: TrackballLineType.vertical,

          activationMode: ActivationMode.singleTap,

          tooltipSettings: InteractiveTooltip(enable: true),

          tooltipDisplayMode: TrackballDisplayMode.groupAllPoints,

          shouldAlwaysShow: true,

        ),

      ),

    );

  }

  List<ChartSeries<StripeDataRecord, DateTime>> _getLineSeries() {

    List<StripeDataRecord> filteredData =

        _filterDataForInterval(widget.stripeData);

    return <ChartSeries<StripeDataRecord, DateTime>>[

      SplineSeries<StripeDataRecord, DateTime>(

        dataSource: filteredData,

        xValueMapper: (StripeDataRecord data, _) => data.timestamp != null

            ? DateTime(data.timestamp!.year, data.timestamp!.month,

                data.timestamp!.day)

            : null,

        yValueMapper: (StripeDataRecord data, _) => data.balanceAmount,

        name: 'Balance Amount',

        animationDuration: 2500,

        splineType: SplineType.monotonic,

      ),

      SplineSeries<StripeDataRecord, DateTime>(

        dataSource: filteredData,

        xValueMapper: (StripeDataRecord data, _) => data.timestamp != null

            ? DateTime(data.timestamp!.year, data.timestamp!.month,

                data.timestamp!.day)

            : null,

        yValueMapper: (StripeDataRecord data, _) => data.pendingAmount,

        name: 'Pending Amount',

        animationDuration: 2500,

        splineType: SplineType.monotonic,

      ),

    ];

  }

}

3
1 reply