Prashant Verma
Prashant Verma

Reputation: 21

How to enable/disable gridlines in mp_chart for flutter once chart is drawn

I am making a chart in flutter using mp_chart package (v 0.1.9). Chart type is performance line chart.

In the controller definition, I have enabled grid as

xAxisSettingFunction: (xAxis, controller) {
    xAxis.enabled = true;

    xAxis
      ..drawGridLines = (true)
      ..setLabelCount1(120)
      ..drawLabels = (false)
      ..drawAxisLine = (false)
      ..granularityEnabled = (true)
      ..setGranularity(5.0);
  },

Accordingly I am getting the grid drawn. Now I want to toggle the grid (hide and unhide) So I suppose something like this should work

myController.xAxis.drawGridLines = false;
setState(() {});

Resetting the grid line enable parameter and calling set state. But its not working. How can I achieve this?

Upvotes: 1

Views: 787

Answers (1)

Prashant Verma
Prashant Verma

Reputation: 21

Got it. After changing the property like

myController.xAxis.drawGridLines = false;

when the setState() is called. It overrides the value defined in the controller definition. so to achieve the desired result, we can use a flag (bool) in controller definition.

xAxisSettingFunction: (xAxis, controller) {
    xAxis.enabled = true;

    if (flag) {
            xAxis.drawGridLines = true;
          } else {
            xAxis.drawGridLines = true;
          }
  },

then change flag as required and call setState()

Upvotes: 1

Related Questions