How to render Flutter time series chart using the charts widget (SfCartesianChart) ?
In this article, we explaine how to render date time values in the cartesian chart with interval in hours.
Our Flutter Cartesian chart provides DateTimeAxis support to render the date-time values. By default interval type will be calculated based on the data. If you wish to calculate the interval on specific type say for example month, hours, seconds, etc, then intervalType property can be used and if you wish to show the date time values in a specific format means, you can use the dateFormat property.
Refer the following instructions, to render date time values in the cartesian chart with interval in hours.
Step 1: Initialize the data source with required x (date-time) and y values.
List<ChartSampleData> chartData = <ChartSampleData>[ ChartSampleData(x: DateTime(2015, 1, 1, 1), yValue: 1.13), ChartSampleData(x: DateTime(2015, 1, 2, 2), yValue: 1.12), ChartSampleData(x: DateTime(2015, 1, 3, 3), yValue: 1.08), ChartSampleData(x: DateTime(2015, 1, 4, 4), yValue: 1.12), ChartSampleData(x: DateTime(2015, 1, 5, 5), yValue: 1.1), ChartSampleData(x: DateTime(2015, 1, 6, 6), yValue: 1.12), ChartSampleData(x: DateTime(2015, 1, 7, 7), yValue: 1.1), ChartSampleData(x: DateTime(2015, 1, 8, 8), yValue: 1.12), ChartSampleData(x: DateTime(2015, 1, 9, 9), yValue: 1.16), ChartSampleData(x: DateTime(2015, 1, 10, 10), yValue: 1.1), ];
Step 2: Initialize the SfCartesianChart widget with the required properties along with x-axis as DateTimeAxis.
SfCartesianChart( primaryXAxis: DateTimeAxis(), series: <LineSeries<ChartSampleData, DateTime>>[ LineSeries<ChartSampleData, DateTime>( dataSource: chartData, xValueMapper: (ChartSampleData sales, _) => sales.x, yValueMapper: (ChartSampleData sales, _) => sales.yValue ) ] )
Step 3: Specify the interval type as hours to display interval in hours and render the chart.
primaryXAxis: DateTimeAxis( //Specified date time interval type in hours intervalType: DateTimeIntervalType.hours ),
To know more about date time axis: https://help.syncfusion.com/flutter/cartesian-charts/axis-types#date-time-axis
Screenshot
Conclusion
I hope you enjoyed learning about how to render Flutter time series chart using the charts widget (SfCartesianChart).
You can refer to our Flutter CartesianChart feature tour page to know about its other groundbreaking feature representations. You can also explore our Flutter CartesianChart documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!