Set maximum value on primary measure axis
Asked Answered
S

1

6

This is a question for Flutter development using charts_flutter. Is there a parameter to fix the maximum value on the measure axis? I'm currently using desiredTickCount as a hack but I ideally only want 3 tickers (0,5,10) for a range of 0-10 on the measure axis.

Code snippet:
  Widget _createChart() {
        return new charts.BarChart(
          _createSampleData(),
          animate: true,
          vertical: false,


          primaryMeasureAxis: new charts.NumericAxisSpec(
            tickProviderSpec: new charts.BasicNumericTickProviderSpec(
              desiredTickCount: 11,
            ),
          ),
          barRendererDecorator: new charts.BarLabelDecorator(),
          // Hide domain axis.
          domainAxis:
              new charts.OrdinalAxisSpec(renderSpec: new charts.NoneRenderSpec()),
        );
      }

      static List<charts.Series<OrdinalSales, String>> _createSampleData() {
        final data = [
          new OrdinalSales('Liver', 8),
          new OrdinalSales('Heart', 4),
          new OrdinalSales('Spleen', 5),
          new OrdinalSales('Lung', 1),
          new OrdinalSales('Kidney', 2),
        ];

        return [
          new charts.Series<OrdinalSales, String>(
            id: 'Sales',
            domainFn: (OrdinalSales sales, _) => sales.year,
            measureFn: (OrdinalSales sales, _) => sales.sales,
            data: data,
            // Set a label accessor to control the text of the bar label.
            labelAccessorFn: (OrdinalSales sales, _) => '${sales.year}',
          ),
        ];
      }
    }

    class OrdinalSales {
      final String year;
      final int sales;

      OrdinalSales(this.year, this.sales);
    }
Secondbest answered 29/8, 2018 at 10:7 Comment(2)
Do you mean using charts_flutter? google_charts is for web. Please post the snippet where you currently use desiredTickCount.Research
Yes, I meant using charts_flutter. Apologies for the confusion.Secondbest
R
7

If you really just want 0, 5 and 10 try using a StaticNumericTickProviderSpec

    primaryMeasureAxis: new charts.NumericAxisSpec(
      tickProviderSpec: new charts.StaticNumericTickProviderSpec(
        <charts.TickSpec<num>>[
          charts.TickSpec<num>(0),
          charts.TickSpec<num>(5),
          charts.TickSpec<num>(10),
        ],
      ),
    ),
Research answered 30/8, 2018 at 1:13 Comment(5)
Unfortunately, I didn't manage to get it to work. Only the 0 ticker showed on the vertical axis.Secondbest
It works for me, but note that not all your ticks may be shown if the data doesn't require them. For example, I just tested with a time series chart with data ranging between 5-25, with static ticks at 0, 10, 20, 30 and 40. Only the 10 and 20 ticks are shown. Using the same data but with static ticks at 12, 14, 16 and 18 all four are shown.Research
How do we set the max a min, say if data goes from -1000 to +1000?Folia
@OliverDixon Try adding the viewport parameter to the NumericAxisSpecResearch
I figured it out, just had to put negatives into the TickSpec, so like charts.TickSpec<num>(-1000), charts.TickSpec<num>(1000)Folia

© 2022 - 2024 — McMap. All rights reserved.