简体   繁体   中英

How do I set the max height on a Flutter Horizontal Bar Chart?

I'm using the horizontal bar chart for flutter that autosizes the bar heights.

https://google.github.io/charts/flutter/example/bar_charts/horizontal_bar_label.html

在此处输入图片说明

When I have a single bar as in this example I want to fix a maxheight (eg 10px) for the bar. How is this configured?

I'm looking at the constructor here but I haven't found a solution https://pub.dev/documentation/charts_flutter/latest/flutter/BarChart-class.html

      child: SizedBox(
        width: 300.0,
        height: 250.0,
        child: charts.BarChart(
          [
            charts.Series<OrdinalSales, String>(
                id: 'Sales',
                domainFn: (OrdinalSales sales, _) => sales.year,
                measureFn: (OrdinalSales sales, _) => sales.sales,
                data: localData,
                colorFn: (_, __) =>
                    charts.MaterialPalette.green.shadeDefault,
                labelAccessorFn: (OrdinalSales sales, _) =>
                    'Category 1: ${sales.sales.toString()}')
          ],
          animate: true,
          vertical: false,
          barRendererDecorator: new charts.BarLabelDecorator<String>(labelPosition: charts.BarLabelPosition.inside
          ),
          domainAxis:
          new charts.OrdinalAxisSpec(renderSpec: new charts.NoneRenderSpec()),
        ),
      ),

There is no possibility to do this for the moment according to this issue .

But I think you can bypass this by creating others items with transparent color or change the Chart library code.

EDIT 2021/03/23

There is still no solution from the google team but Boken's solution seems to be a good one.

Try to do this -

ConstrainedBox(
  constraints: BoxConstraints.expand(height: 50.0), // adjust the height here
  child: Your_chart_here, // place your chart here
),

Workaround

You can try to wrap BarChart in to SizedBox , wrapped with SingleChildScrollView .

In pseudocode

- SingleChildScrollView
       - SizedBox
              - charts.BarChart

Flutter

SingleChildScrollView(
  child: SizedBox(
    height: 2000,
    child: HorizontalBarLabelChart(
      getData(),
    ),
  ),
),

Size calculation

Height of the SizedBox can be calculated base on number of data, eg:

numerOfElements * 50

Before

For 100 elements

在此处输入图片说明

After

For 100 elements (so height is 5000 because 100 * 50 )

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM