Created
June 28, 2020 15:34
-
-
Save gitaaron/75c67363a042073bb492978d3eecc138 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
/// Timeseries chart example | |
import 'package:charts_flutter/flutter.dart' as charts; | |
import 'package:charts_flutter/flutter.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/scheduler.dart'; | |
const String FIRST_SEL = 'FIRST'; | |
final data_a = [ | |
new TimeSeriesSales(new DateTime(2017, 9, 1), 5), | |
new TimeSeriesSales(new DateTime(2017, 9, 4), 7), | |
new TimeSeriesSales(new DateTime(2017, 9, 6), 9), | |
new TimeSeriesSales(new DateTime(2017, 9, 8), 10), | |
]; | |
class SimpleTimeSeriesChart extends StatefulWidget { | |
final List<charts.Series> seriesList; | |
SimpleTimeSeriesChart(this.seriesList, {this.animate}); | |
final bool animate; | |
/// Creates a [TimeSeriesChart] with sample data and no transition. | |
factory SimpleTimeSeriesChart.withSampleData() { | |
return new SimpleTimeSeriesChart( | |
_createSampleData(), | |
// Disable animations for image tests. | |
animate: false, | |
); | |
} | |
/// Create one series with sample hard coded data. | |
static List<charts.Series<TimeSeriesSales, int>> _createSampleData() { | |
return [ | |
charts.Series<TimeSeriesSales, int>( | |
id: 'sales_a', | |
domainFn: (TimeSeriesSales sales, _) => sales.time, | |
measureFn: (TimeSeriesSales sales, _) => sales.sales, | |
data: data_a, | |
), | |
]; | |
} | |
SimpleTimeSeriesChartState createState() => SimpleTimeSeriesChartState(); | |
} | |
class SimpleTimeSeriesChartState extends State<SimpleTimeSeriesChart> { | |
_onSliderChange(Point<int> point, dynamic domain, String roleId, | |
charts.SliderListenerDragState dragState) { | |
void rebuild(_) { | |
// @NOTE commenting out the following line makes it work | |
setState(() { }); | |
} | |
SchedulerBinding.instance.addPostFrameCallback(rebuild); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Padding( | |
padding:EdgeInsets.all(10.0), | |
child: SizedBox( | |
height:300.0, | |
child: charts.LineChart( | |
widget.seriesList, | |
animate: widget.animate, | |
defaultInteractions: false, | |
// Optionally pass in a [DateTimeFactory] used by the chart. The factory | |
// should create the same type of [DateTime] as the data provided. If none | |
// specified, the default creates local date time. | |
// @NOTE commenting out the following line makes it work | |
defaultRenderer: charts.LineRendererConfig(includePoints: true), | |
behaviors: [ | |
charts.Slider( | |
initialDomainValue:data_a.first.time, | |
onChangeCallback: _onSliderChange, | |
snapToDatum: true, | |
style:SliderStyle(handleSize:const Rectangle(0,0,8,25)), | |
roleId:FIRST_SEL | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
/// Sample time series data type. | |
class TimeSeriesSales { | |
final DateTime _time; | |
final int sales; | |
int get time { | |
return _time.day; | |
} | |
TimeSeriesSales(this._time, this.sales); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment