How to Render Multi-Colored Line Chart Using Flutter Cartesian chart?
In this article, we explain how to a render multi-colored line chart using Flutter Cartesian chart.
In SfCartesianChart widget using the pointColorMapper property, you can map the color from the data source to each point. Follow the below steps, to accomplish this.
Step 1: Initialize the chartData variable which holds the data source for the chart. Here provides the color to each point.
late List<_ChartData> chartData; @override void initState() { chartData = <_ChartData>[ _ChartData(DateTime(1925), 415, const Color.fromRGBO(248, 184, 131, 1)), _ChartData(DateTime(1926), 408, const Color.fromRGBO(248, 184, 131, 1)), _ChartData(DateTime(1927), 415, const Color.fromRGBO(248, 184, 131, 1)), _ChartData(DateTime(1928), 350, const Color.fromRGBO(248, 184, 131, 1)), _ChartData(DateTime(1929), 375, const Color.fromRGBO(248, 184, 131, 1)), _ChartData(DateTime(1930), 500, const Color.fromRGBO(248, 184, 131, 1)), _ChartData(DateTime(1931), 390, const Color.fromRGBO(229, 101, 144, 1)), _ChartData(DateTime(1932), 450, const Color.fromRGBO(229, 101, 144, 1)), _ChartData(DateTime(1933), 440, const Color.fromRGBO(229, 101, 144, 1)), _ChartData(DateTime(1934), 350, const Color.fromRGBO(229, 101, 144, 1)), _ChartData(DateTime(1935), 400, const Color.fromRGBO(229, 101, 144, 1)), _ChartData(DateTime(1936), 365, const Color.fromRGBO(53, 124, 210, 1)), _ChartData(DateTime(1937), 490, const Color.fromRGBO(53, 124, 210, 1)), _ChartData(DateTime(1938), 400, const Color.fromRGBO(53, 124, 210, 1)), _ChartData(DateTime(1939), 520, const Color.fromRGBO(53, 124, 210, 1)), _ChartData(DateTime(1940), 510, const Color.fromRGBO(53, 124, 210, 1)), _ChartData(DateTime(1941), 395, const Color.fromRGBO(0, 189, 174, 1)), _ChartData(DateTime(1942), 380, const Color.fromRGBO(0, 189, 174, 1)), _ChartData(DateTime(1943), 404, const Color.fromRGBO(0, 189, 174, 1)), _ChartData(DateTime(1944), 400, const Color.fromRGBO(0, 189, 174, 1)), _ChartData(DateTime(1945), 500, const Color.fromRGBO(0, 189, 174, 1)) ]; super.initState(); } class _ChartData { _ChartData(this.x, this.y, [this.lineColor]); final DateTime x; final double y; final Color? lineColor; }
Step 2: Now create the SfCartesianChart widget with the LineSeries and assign the chartData to the dataSource property and map the x, y values to xValueMapper, yValueMapper properties respectively.
SfCartesianChart( primaryXAxis: DateTimeAxis(), primaryYAxis: NumericAxis(), series: <LineSeries<_ChartData, DateTime>>[ LineSeries<_ChartData, DateTime>( dataSource: chartData, xValueMapper: (_ChartData sales, _) => sales.x, yValueMapper: (_ChartData sales, _) => sales.y, width: 2) ], )
Step 3: Then map the color values in the data source to the pointColorMapper property. It will map the respective color to the respective points.
SfCartesianChart( series: <LineSeries<_ChartData, DateTime>>[ LineSeries<_ChartData, DateTime>( // The property used to apply the color to each data. pointColorMapper: (_ChartData sales, _) => sales.lineColor, // Other required properties. ) ], )
Thus, the line series with the multi-colored is achieved with the help of pointColorMapper property in the LineSeries.
Conclusion
I hope you enjoyed learning about how to render multi-colored line chart using Flutter Cartesian chart.
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!