Flutter googele LineChart with String domainFn
1,076
You can't use a string value for the X axis of a line chart. That value must be numeric - int or double.
Are your strings actually dates? In which case you should use a time series chart. If they are just discrete points, you should use a bar chart.
A line chart will draw the X axis points at the appropriate points along a linear axis. If the values are not evenly spaced the data points won't be evenly spaced. (The ticks
will be, of course.)
Author by
DolDurma
Updated on December 13, 2022Comments
-
DolDurma over 1 year
for using google
LineChart
i can't set String fordomainFn
parameter_generateData() { var data = [ Task('05/10',20), Task('05/20',24), Task('05/30',25), Task('06/05',40), ]; _seriesLineData.add(charts.Series( data: data, domainFn: (Task task, _) => task.task, measureFn: (Task task, _) => task.taskValue, colorFn: (Task task, _) => charts.ColorUtil.fromDartColor(Color(0xff990099)), id: 'weights', labelAccessorFn: (Task row, _) => '${row.taskValue}', )); }
when i try this i get error:
type 'List<Series<Task, String>>' is not a subtype of type 'List<Series<dynamic, num>>'
and i can't find any good reference to resolve that
my code:
class FragmentAddNewWeight extends StatefulWidget{ @override State<StatefulWidget> createState() => FragmentAddNewWeightState(); } class FragmentAddNewWeightState extends State<FragmentAddNewWeight> { List<charts.Series<Task, String>> _seriesLineData; _generateData() { var data = [ Task('05/10',20), Task('05/20',24), Task('05/30',25), Task('06/05',40), ]; _seriesLineData.add(charts.Series( data: data, domainFn: (Task task, _) => task.task, measureFn: (Task task, _) => task.taskValue, colorFn: (Task task, _) => charts.ColorUtil.fromDartColor(Color(0xff990099)), id: 'weights', labelAccessorFn: (Task row, _) => '${row.taskValue}', )); } @override void initState() { super.initState(); _seriesLineData = List<charts.Series<Task, String>>(); _generateData(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.transparent, body: Container( child: Column(children: <Widget>[ Directionality( textDirection: TextDirection.ltr, child: Column( children: <Widget>[ Card( clipBehavior: Clip.antiAlias, margin:EdgeInsets.all(10.0), color: Colors.grey[200], child: Container( height:200.0, padding:EdgeInsets.all(10.0), child: charts.LineChart( _seriesLineData, defaultRenderer: charts.LineRendererConfig(includeArea: true, stacked: true), animate: true, animationDuration: Duration(milliseconds: 500), behaviors: [new charts.PanAndZoomBehavior()], ), ), ), ], ), ), ]), ), ); } } class Task { String task; int taskValue; Task(this.task, this.taskValue); }
-
CopsOnRoad over 4 yearsReplace
List<Series<Task, String>>
withList<Series<dynamic, num>>
-
DolDurma over 4 years@CopsOnRoad its not correct, because i get
error: The return type 'String' isn't a 'int', as defined by anonymous closure
error ondomainFn
parameter now
-