Trackball RangeError when dataSource's length is less than the number of Series in chart

I'm trying to show a graph with only two data points and when I added the trackball is throwing an error:

RangeError (index): Invalid value: Not in inclusive range 0..1: 2

However, when I add a third (or more) data point the error doesn't happen.

Any help is appreciated :)

I'm using version 19.4.54 and here's the code to reproduce it:

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:lib/styles/colors.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import 'dart:ui' as ui;

class MyData {
final DateTime date;
final double value;

MyData(this.date, this.value);
}

class GraphsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Graphs'),
),
body: Center(
child: TheGraph(),
),
);
}
}

class TheGraph extends StatefulWidget {
@override
State<TheGraph> createState() => _TheGraphState();
}

class _TheGraphState extends State<TheGraph> {
final List<MyData> dummyData2 = [
MyData(DateTime(2021, 6, 13), 9.3),
MyData(DateTime(2021, 8, 1), 10.1),
];

TrackballBehavior _trackballBehavior = TrackballBehavior(
enable: true,
shouldAlwaysShow: true,
activationMode: ActivationMode.singleTap,
markerSettings: TrackballMarkerSettings(
// color: BaseColors.red,
shape: DataMarkerType.circle,
markerVisibility: TrackballVisibilityMode.visible,
),
tooltipDisplayMode: TrackballDisplayMode.none,
lineColor: BaseColors.red,
);

@override
void initState() {
super.initState();
Future.delayed(Duration(milliseconds: 100), () {
_trackballBehavior.showByIndex(dummyData2.length - 1);
});
}

@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(horizontal: 32),
height: 180,
width: double.infinity,
child: SfCartesianChart(
palette: [BaseColors.red],
plotAreaBorderWidth: 0,
onTrackballPositionChanging: (trackballArgs) {
final i = trackballArgs.chartPointInfo.dataPointIndex;
final value = dummyData2[i!].value;
print(value);
},
trackballBehavior: _trackballBehavior,
primaryXAxis: DateTimeAxis(
interval: 1,
intervalType: DateTimeIntervalType.months,
dateFormat: DateFormat('MMM'),
majorGridLines: MajorGridLines(width: 0),
majorTickLines: MajorTickLines(width: 0),
),
primaryYAxis: NumericAxis(
axisLine: AxisLine(width: 0),
minimum: 0,
maximum: 15,
maximumLabelWidth: 0,
majorGridLines: MajorGridLines(
width: .5,
),
majorTickLines: MajorTickLines(width: 0),
),
series: <ChartSeries>[
SplineAreaSeries<MyData, DateTime>(
animationDuration: 0,
onCreateShader: (ShaderDetails details) {
return ui.Gradient.linear(
details.rect.topCenter,
details.rect.bottomCenter,
[
BaseColors.red.withOpacity(.3),
BaseColors.red.withOpacity(0),
],
[0, .6],
);
},
dataSource: dummyData2,
xValueMapper: (MyData data, _) => data.date,
yValueMapper: (MyData data, _) => data.value,
),
SplineSeries<MyData, DateTime>(
animationDuration: 0,
dataSource: dummyData2,
xValueMapper: (MyData data, _) => data.date,
yValueMapper: (MyData data, _) => data.value,
),
LineSeries<MyData, DateTime>(
width: 0,
animationDuration: 0,
dataSource: dummyData2,
xValueMapper: (MyData data, _) => data.date,
yValueMapper: (MyData data, _) => 0,
),
],
),
);
}
}


3 Replies

JA Jaime March 8, 2022 01:56 PM UTC

Hello

I have been looking deeper into the error and found some things

- This error happens if there's more Series than datapoints in the SfCartesianChart

- In /src/chart/user_interaction/trackball_painter.dart line 480 there's a "trackballRenderingDetails.renderTrackballMarker(....)" call that passes an index related to the series to iterate through datapoints. I think this 4th argument (index) should be different.


Thank you



YG Yuvaraj Gajaraj Syncfusion Team March 9, 2022 05:38 PM UTC

Hi Jaime, 
 
The reported issue is already fixed, and it will be roll out in our next weekly patch release expected on 15th Mar 2022. We appreciate your patience until then. 
 
Regards, 
Yuvaraj. 



YG Yuvaraj Gajaraj Syncfusion Team March 15, 2022 01:49 PM UTC

Hi Jaime, 
 
The reported issue regarding range error exception when having a data point count less than the series count has been fixed and rolled out in our weekly patch release. To avoid this kindly upgrade your chart package to the latest version below. 
 
 
Regards, 
Yuvaraj.

Loader.
Up arrow icon