如何自定义颤振中条形图每个条的颜色?

xen*_*123 3 charts dart flutter

我是 flutter 的新手,并尝试构建具有不同颜色的条形图。我使用谷歌提供的图表依赖项。

https://google.github.io/charts/flutter/gallery.html

但是,我发现我只能更改所有条形的颜色。无论如何我可以自定义一个特定酒吧的颜色吗?我在网上搜索过,没有发布此类信息。

col*_*ars 6

扩展 Hemanth 所指的colorFn是一个属性,它采用一个返回颜色的函数。

因此,如果您执行以下操作:

colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault
Run Code Online (Sandbox Code Playgroud)

您将为系列中的每个片段返回相同的颜色。

要为系列中的各个段设置不同的颜色,您可以执行以下操作:

class ExampleSegment {
  final String segment;
  final int size;

  ExampleSegment(this.segment, this.size);
}

static List<charts.Series<ExampleSegment, String>> _createSampleData() {
  final blue = charts.MaterialPalette.blue.makeShades(2);
  final red = charts.MaterialPalette.red.makeShades(2);
  final green = charts.MaterialPalette.green.makeShades(2);

  final data = [
    new ExampleSegment('First', 100),
    new ExampleSegment('Second', 100),
    new ExampleSegment('Third', 100),
    new ExampleSegment('Fourth', 100),
  ];

  return [
    new charts.Series<ExampleSegment, String>(
        id: 'Segments',
        domainFn: (ExampleSegment segment, _) => segment.segment,
        measureFn: (ExampleSegment segment, _) => segment.size,
        data: data,
        colorFn: (ExampleSegment segment, _) {
          switch (segment.segment) {
            case "First":
              {
                return blue[1];
              }

            case "Second":
              {
                return red[1];
              }

            case "Third":
              {
                return green[1];
              }

            case "Fourth":
              {
                return blue[0];
              }

            default:
              {
                return red[0];
              }
          }
        }
      ),
  ];
}

Run Code Online (Sandbox Code Playgroud)