简体   繁体   English

如何在 Flutter 水平条形图上设置最大高度?

[英]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 https://google.github.io/charts/flutter/example/bar_charts/horizo​​ntal_bar_label.html

在此处输入图片说明

When I have a single bar as in this example I want to fix a maxheight (eg 10px) for the bar.当我在这个例子中只有一个栏时,我想为栏固定一个最大高度(例如 10px)。 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我在这里查看构造函数,但我还没有找到解决方案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编辑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 .您可以尝试将BarChart包装到SizedBox ,并用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: SizedBox高度可以根据数据的数量计算,例如:

numerOfElements * 50

Before

For 100 elements对于 100 个元素

在此处输入图片说明

After

For 100 elements (so height is 5000 because 100 * 50 )对于 100 个元素(所以高度是5000因为100 * 50

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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