Flutter d_chart:如何在图表中将两个或多个重复的域合并为一个?

huangapple go评论75阅读模式
英文:

Flutter d_chart : How to combine two or more duplicated domains as one in the chart?

问题

有人之前了解过图表吗,我正在使用d_chart.dart?

如果有重复的域名,我如何将它们添加并合并为一个?

我的预期输出是从2021年:4和2021年:6中的2021年:10

```dart
DChartBar(
    data: [
        {
            'id': 'Bar',
            'data': [
                {'domain': '2020', 'measure': 3},
                {'domain': '2021', 'measure': 4},
                {'domain': '2021', 'measure': 6},
                {'domain': '2023', 'measure': 0.3},
            ],
        },
    ],
    domainLabelPaddingToAxisLine: 16,
    axisLineTick: 2,
    axisLinePointTick: 2,
    axisLinePointWidth: 10,
    axisLineColor: Colors.green,
    measureLabelPaddingToAxisLine: 16,
    barColor: (barData, index, id) => Colors.green,
    showBarValue: true,
),
英文:

Has anyone known of chart before, I'm using d_chart.dart?

If there are duplicated domains, how do I add and combine them as one?

My expected output is 2021: 10 from 2021: 4 and 2021: 6

DChartBar(
    data: [
        {
            'id': 'Bar',
            'data': [
                {'domain': '2020', 'measure': 3},
                {'domain': '2021', 'measure': 4},
                {'domain': '2021', 'measure': 6},
                {'domain': '2023', 'measure': 0.3},
            ],
        },
    ],
    domainLabelPaddingToAxisLine: 16,
    axisLineTick: 2,
    axisLinePointTick: 2,
    axisLinePointWidth: 10,
    axisLineColor: Colors.green,
    measureLabelPaddingToAxisLine: 16,
    barColor: (barData, index, id) => Colors.green,
    showBarValue: true,
),

答案1

得分: 1

你需要预处理你的数据。

List<Map<String, dynamic>> data = [
  {
    'id': 'Bar',
    'data': [
      {'domain': '2020', 'measure': 3},
      {'domain': '2021', 'measure': 4},
      {'domain': '2021', 'measure': 6},
      {'domain': '2023', 'measure': 0.3},
    ],
  },
];

// 合并重复的域。
data.forEach((element) {
  element['data'].forEach((bar) {
    if (element['data'].where((b) => b['domain'] == bar['domain']).length > 1) {
      bar['measure'] += element['data'].where((b) => b['domain'] == bar['domain']).map((b) => b['measure']).reduce((a, b) => a + b);
    }
  });
});

然后稍后使用这些数据:

DChartBar(
  data: data,
  domainLabelPaddingToAxisLine: 16,
  axisLineTick: 2,
  axisLinePointTick: 2,
  axisLinePointWidth: 10,
  axisLineColor: Colors.green,
  measureLabelPaddingToAxisLine: 16,
  barColor: (barData, index, id) => Colors.green,
  showBarValue: true,
);
英文:

You need to preprocess your data.

 List&lt;Map&lt;String, dynamic&gt;&gt; data = [
    {
      &#39;id&#39;: &#39;Bar&#39;,
      &#39;data&#39;: [
        {&#39;domain&#39;: &#39;2020&#39;, &#39;measure&#39;: 3},
        {&#39;domain&#39;: &#39;2021&#39;, &#39;measure&#39;: 4},
        {&#39;domain&#39;: &#39;2021&#39;, &#39;measure&#39;: 6},
        {&#39;domain&#39;: &#39;2023&#39;, &#39;measure&#39;: 0.3},
      ],
    },
  ];

  // Combine duplicate domains.
  data.forEach((element) {
    element[&#39;data&#39;].forEach((bar) {
      if (element[&#39;data&#39;].where((b) =&gt; b[&#39;domain&#39;] == bar[&#39;domain&#39;]).length &gt; 1) {
        bar[&#39;measure&#39;] += element[&#39;data&#39;].where((b) =&gt; b[&#39;domain&#39;] == bar[&#39;domain&#39;]).map((b) =&gt; b[&#39;measure&#39;]).reduce((a, b) =&gt; a + b);
      }
    });
  });

And later use the data

 DChartBar(
    data: data,
    domainLabelPaddingToAxisLine: 16,
    axisLineTick: 2,
    axisLinePointTick: 2,
    axisLinePointWidth: 10,
    axisLineColor: Colors.green,
    measureLabelPaddingToAxisLine: 16,
    barColor: (barData, index, id) =&gt; Colors.green,
    showBarValue: true,
  );

答案2

得分: 1

你可以将数据分离到新的变量中,然后进行编辑。你可以在dartpad.dev中查看这段代码。

Console output:
在此处查看图像描述

最后设置到图表小部件:

DChartBar(
    data: realData,
    domainLabelPaddingToAxisLine: 16,
    axisLineTick: 2,
    axisLinePointTick: 2,
    axisLinePointWidth: 10,
    axisLineColor: Colors.green,
    measureLabelPaddingToAxisLine: 16,
    barColor: (barData, index, id) => Colors.green,
    showBarValue: true,
),
英文:

you can separate data to new variable, then edit it. you can check this code in dartpad.dev

List realData = [
  {
    &#39;id&#39;: &#39;Bar&#39;,
    &#39;data&#39;: [
      {&#39;domain&#39;: &#39;2020&#39;, &#39;measure&#39;: 3},
      {&#39;domain&#39;: &#39;2021&#39;, &#39;measure&#39;: 4},
      {&#39;domain&#39;: &#39;2021&#39;, &#39;measure&#39;: 6},
      {&#39;domain&#39;: &#39;2023&#39;, &#39;measure&#39;: 0.3},
    ],
  },
];

// cek console
print(&#39;--------------&#39;);
realData[0][&#39;data&#39;].forEach((e){
  print(e[&#39;domain&#39;] + &#39; : &#39;+e[&#39;measure&#39;].toString());
});

// start proses...
List newData = [];
for (var item in realData[0][&#39;data&#39;]){
  int existIndex = newData.indexWhere((e) =&gt; e[&#39;domain&#39;] == item[&#39;domain&#39;]);
  if(existIndex == -1) {
    // not found / new domain
    newData.add(item);
  }else{
    // update measure
    newData[existIndex][&#39;measure&#39;] += item[&#39;measure&#39;];
  }    
}

realData[0][&#39;data&#39;] = newData;
// end proses...

// cek console
print(&#39;--------------&#39;);
realData[0][&#39;data&#39;].forEach((e){
  print(e[&#39;domain&#39;] + &#39; : &#39;+e[&#39;measure&#39;].toString());
});

Console output:
enter image description here

and finally set to widget chart:

DChartBar(
    data: realData,
    domainLabelPaddingToAxisLine: 16,
    axisLineTick: 2,
    axisLinePointTick: 2,
    axisLinePointWidth: 10,
    axisLineColor: Colors.green,
    measureLabelPaddingToAxisLine: 16,
    barColor: (barData, index, id) =&gt; Colors.green,
    showBarValue: true,
),

huangapple
  • 本文由 发表于 2023年6月26日 17:44:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定