|
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Syncfusion Flutter chart'),
),
body: SfCartesianChart(
// For storing the selected data point’s index
onSelectionChanged: (args) {
if (selectedPointIndex == args.overallDataPointIndex) {
selectedPointIndex = -1;
} else {
selectedPointIndex = args.overallDataPointIndex;
}
setState(() {});
},
// Checked for the selected data point’s index and changing the color of the respective
// axis label
onAxisLabelRender: (args) {
TextStyle axisLabelStyle = args.textStyle;
if (args.axisName == 'primaryXAxis') {
if (args.value == selectedPointIndex) {
args.textStyle = axisLabelStyle.copyWith(color: Colors.white);
} else {
args.textStyle = axisLabelStyle.copyWith(color: Colors.grey);
}
}
},
primaryXAxis: CategoryAxis(
name: 'primaryXAxis',
// Set the default color for the axis labels
labelStyle: TextStyle(color: Colors.grey)),
series: <ChartSeries<_SalesData, String>>[
ColumnSeries<_SalesData, String>(
dataSource: chartData,
xValueMapper: (_SalesData sales, _) => sales.year,
yValueMapper: (_SalesData sales, _) => sales.sales,
// Enabling selection for the series
selectionBehavior: SelectionBehavior(
enable: true,
selectedBorderColor: Colors.red,
selectedBorderWidth: 2))
]));
}
} |