When secondary Y-axis label is selected, both primaryYAxis and secondary Y-axis are not showing

I have a custom widget using SfCartesianChart (version syncfusion_flutter_charts: ^24.2.9).

I used the same widget on latest versions (22...) and it worked well, but when I updated to this version (because of flutter's 3.19) I'm getting the following problem.

When the secondary Y-axis label is marked, both of Y-axis disappears. I already tried to control isVisible param, but couldn't solve the problem. Any tips on how do I solve this?

Here are the results I'm getting below.

When the "ESR" (Secondary Y-axis) label is marked:

Image_1810_1709943526111

When the "ESR" (Secondary Y-axis) label is not marked:

Image_2198_1709943472504


Attachment: sfChartexample.txt_5e224c18.zip

1 Reply

PS Preethika Selvam Syncfusion Team March 11, 2024 12:53 PM UTC

Hi Dotted Technology,


We have achieved your requirement, by using the onRendererCreated and onLegendTapped callbacks. We have utilized onRendererCreated callback to update the isVisible property of the series dynamically, based on the state of _chartSeriesController!.isVisible. This approach ensures that tapping on a legend item triggers a UI update via setState() in onLegendTapped callback, facilitating the dynamic updating of the chart. We have shared a code snippet, sample, and output for your reference. You can modify the sample based on your needs.


Code Snippet:


  ChartSeriesController? _chartSeriesController;

  @override

  Widget build(BuildContext context) {

    List<CartesianSeries<TimeSeriesSales, String>> seriesList = [

      ColumnSeries<TimeSeriesSales, String>(

        name: 'Original cost',

        color: widget.data1color,

        borderRadius: BorderRadius.circular(6),

        dataSource: _createData(widget.data1),

        xValueMapper: (TimeSeriesSales sales, _) => sales.time,

        yValueMapper: (TimeSeriesSales sales, _) => sales.sales,

      ),

      ColumnSeries<TimeSeriesSales, String>(

        name: 'Actual cost',

        color: widget.data2color,

        borderRadius: BorderRadius.circular(6),

        dataSource: _createData(widget.data2),

        xValueMapper: (TimeSeriesSales sales, _) => sales.time,

        yValueMapper: (TimeSeriesSales sales, _) => sales.sales,

      ),

      LineSeries<TimeSeriesSales, String>(

          onRendererCreated: (ChartSeriesController controller) {

            _chartSeriesController = controller;

          },

          name: 'ESR',

          color: widget.data3color,

          dataSource: _createData(widget.data3),

          xValueMapper: (TimeSeriesSales sales, _) => sales.time,

          yValueMapper: (TimeSeriesSales sales, _) => sales.sales,

          yAxisName: 'percentage',

          markerSettings: const MarkerSettings(isVisible: true)),

    ];

 

    return Column(

      children: [

        Expanded(

          child: SizedBox(

            width: widget.width,

            height: widget.height,

            child: SfCartesianChart(

              onLegendTapped: (legendTapArgs) {

                setState(() {});

              },

              plotAreaBorderWidth: 0,

              primaryXAxis: CategoryAxis(

                axisLine: const AxisLine(width: 0),

                majorTickLines: const MajorTickLines(size: 0),

                majorGridLines: const MajorGridLines(width: 0),

                labelStyle: const TextStyle(

                  color: Color(0xFFc9c9c9),

                  fontFamily: 'Montserrat',

                  fontSize: 12,

                ),

                edgeLabelPlacement: EdgeLabelPlacement.none,

                plotBands: <PlotBand>[

                  PlotBand(

                    isVisible: true,

                    start: widget.plotBandPosition,

                    end: widget.plotBandPosition,

                    color: Colors.grey.withOpacity(0.5),

                    borderWidth: 4,

                    borderColor: Colors.grey,

                    text: 'Dotted Started',

                    textAngle: 0,

                    horizontalTextPadding: '-2%',

                    verticalTextPadding: '-2%',

                    textStyle: const TextStyle(

                      color: Colors.grey,

                      fontWeight: FontWeight.bold,

                      fontFamily: 'Montserrat',

                      fontSize: 12,

                    ),

                    verticalTextAlignment: TextAnchor.start,

                    horizontalTextAlignment: TextAnchor.end,

                  ),

                ],

              ),

              primaryYAxis: NumericAxis(

                isVisible: true,

                numberFormat:

                    NumberFormat.currency(locale: 'en_US', symbol: '\$'),

                axisLine: const AxisLine(width: 0),

                majorTickLines: const MajorTickLines(size: 0),

                majorGridLines:

                    const MajorGridLines(width: 0, color: Color(0xFFcccccc)),

                labelStyle: const TextStyle(

                  color: Color(0xFFc9c9c9),

                  fontFamily: 'Montserrat',

                  fontSize: 12,

                ),

                edgeLabelPlacement: EdgeLabelPlacement.none,

              ),

              axes: <ChartAxis>[

                NumericAxis(

                  isVisible: _chartSeriesController != null

                      ? _chartSeriesController!.isVisible

                      : true,

                  opposedPosition: true,

                  name: 'percentage',

                  labelFormat: '{value}%',

                  numberFormat: NumberFormat.decimalPattern(),

                  axisLine: const AxisLine(width: 0),

                  majorTickLines: const MajorTickLines(size: 0),

                  majorGridLines:

                      const MajorGridLines(width: 0, color: Color(0xFFcccccc)),

                  labelStyle: const TextStyle(

                    color: Color(0xFFc9c9c9),

                    fontFamily: 'Montserrat',

                    fontSize: 12,

                  ),

                  edgeLabelPlacement: EdgeLabelPlacement.none,

                ),

              ],

              legend: const Legend(

                  isVisible: true,

                  position: LegendPosition.top,

                  alignment: ChartAlignment.far,

                  textStyle: TextStyle(

                    color: Color(0xFF262626),

                    fontFamily: 'Montserrat',

                    fontSize: 14,

                    fontWeight: FontWeight.bold,

                  )),


Please let us know if you need any further assistance.


Regards,

Preethika Selvam.


Attachment: bd566033_bff494f8.zip

Loader.
Up arrow icon