Range annotations along the measure axis fail due to assuming all annotation start/end values are DateTime.
I/flutter ( 7536): type 'int' is not a subtype of type 'DateTime'
...
I/flutter ( 7536): #0 RangeAnnotation._updateViewData.<anonymous closure> (package:charts_common/src/chart/common/behavior/range_annotation.dart:147:62)
I/flutter ( 7536): #1 List.forEach (dart:core/runtime/libgrowable_array.dart:274:8)
I/flutter ( 7536): #2 RangeAnnotation._updateViewData (package:charts_common/src/chart/common/behavior/range_annotation.dart:128:17)
...
In charts_common/src/chart/common/behavior/range_annotation.dart:147 is a call to _getAnnotationDatum()
which due to the generic's type, is assuming that the startValue
and endValue
are DateTime
. However, on the measure axis in this repo, they are int
-> leading to the exception.
I don't have an immediate recommendation as type changes to that class will likely cascade.
The only lines which cause the exception are 78-79 in the main.dart code below. I copied the code from this repo's example for annotations... changed the start/end values from DateTime
to int
values... changed axis type to charts.RangeAnnotationAxisType.measure
.
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or press Run > Flutter Hot Reload in IntelliJ). Notice that the
// counter didn't reset back to zero; the application is not restarted.
primarySwatch: Colors.blue,
),
home: new TimeSeriesRangeAnnotationChart.withSampleData(),
);
}
}
class TimeSeriesRangeAnnotationChart extends StatelessWidget {
final List<charts.Series> seriesList;
final bool animate;
TimeSeriesRangeAnnotationChart(this.seriesList, {this.animate});
/// Creates a [TimeSeriesChart] with sample data and no transition.
factory TimeSeriesRangeAnnotationChart.withSampleData() {
return new TimeSeriesRangeAnnotationChart(
_createSampleData(),
// Disable animations for image tests.
animate: false,
);
}
// EXCLUDE_FROM_GALLERY_DOCS_START
// This section is excluded from being copied to the gallery.
// It is used for creating random series data to demonstrate animation in
// the example app only.
factory TimeSeriesRangeAnnotationChart.withRandomData() {
return new TimeSeriesRangeAnnotationChart(_createRandomData());
}
/// Create random data.
static List<charts.Series<TimeSeriesSales, DateTime>> _createRandomData() {
final random = new Random();
final data = [
new TimeSeriesSales(new DateTime(2017, 9, 19), random.nextInt(100)),
new TimeSeriesSales(new DateTime(2017, 9, 26), random.nextInt(100)),
new TimeSeriesSales(new DateTime(2017, 10, 3), random.nextInt(100)),
new TimeSeriesSales(new DateTime(2017, 10, 10), random.nextInt(100)),
];
return [
new charts.Series<TimeSeriesSales, DateTime>(
id: 'Sales',
domainFn: (TimeSeriesSales sales, _) => sales.time,
measureFn: (TimeSeriesSales sales, _) => sales.sales,
data: data,
)
];
}
// EXCLUDE_FROM_GALLERY_DOCS_END
@override
Widget build(BuildContext context) {
return new charts.TimeSeriesChart(seriesList, animate: animate, behaviors: [
new charts.RangeAnnotation([
new charts.RangeAnnotationSegment(40,
50, charts.RangeAnnotationAxisType.measure),
]),
]);
}
/// Create one series with sample hard coded data.
static List<charts.Series<TimeSeriesSales, DateTime>> _createSampleData() {
final data = [
new TimeSeriesSales(new DateTime(2017, 9, 19), 5),
new TimeSeriesSales(new DateTime(2017, 9, 26), 25),
new TimeSeriesSales(new DateTime(2017, 10, 3), 100),
new TimeSeriesSales(new DateTime(2017, 10, 10), 75),
];
return [
new charts.Series<TimeSeriesSales, DateTime>(
id: 'Sales',
domainFn: (TimeSeriesSales sales, _) => sales.time,
measureFn: (TimeSeriesSales sales, _) => sales.sales,
data: data,
)
];
}
}
/// Sample time series data type.
class TimeSeriesSales {
final DateTime time;
final int sales;
TimeSeriesSales(this.time, this.sales);
}