JSON的Highcharts堆积柱形图未绘制正确的值 [英] Highcharts Stacked Column chart from JSON not charting correct values

查看:65
本文介绍了JSON的Highcharts堆积柱形图未绘制正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制堆积的柱状图,但是我得到的结果与JSON数据不匹配.

I am trying to chart a stacked column chart but I am getting the result I am getting is not matching the JSON data.

我有3个类别,但排在最后一个类别的图表似乎并未出现.

I have 3 categories but the charting does not seem to happen in the last category.

为什么会这样?

我的代码:

let cat = [];
vals = json.map(val => {
            return val.type;
        });
console.log(vals);
vals.forEach(v=>{
if (cat.indexOf(v) === -1) cat.push(v);
});
console.log(cat);

const group_data = (array, key) => {
    return array.reduce((a, c) => {
        (a[c[key]] = a[c[key]] || []).push(c);
        return a;
    }, {});
}

const get_grouped = group_data(json, 'date');
console.log(get_grouped);

for (a in get_grouped) {
            cat.forEach(t => {
                if (!get_grouped[a].some(v => v.type == t)) {
                    get_grouped[a].push({
        "date": get_grouped[a][0].date,
        "metric": 0,
        "type": t
      });
    }
  })
}
console.log(get_grouped);
        let series = Object.entries(get_grouped).map(([key, group]) => ({
            ['name']: key,
            ['data']: group.map(entry => entry['metric']),
            ['marker']: {
                symbol: 'circle'
            }
        }));
console.log(series);

Highcharts.chart('container', {
    chart: {
      type: 'column'
    },
    title: {
      text: 'Stacked column chart'
    },
    xAxis: {
      categories: cat
    },
    legend: {
      align: 'right',
      x: -30,
      verticalAlign: 'top',
      y: 25,
      floating: true,
      backgroundColor:
      Highcharts.defaultOptions.legend.backgroundColor || 'white',
      borderColor: '#CCC',
      borderWidth: 1,
      shadow: false
    },
    tooltip: {
      headerFormat: '<b>{point.x}</b><br/>',
      pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}'
    },
    plotOptions: {
      column: {
        stacking: 'normal'
      }
    },
    series: series
}); 

这是我的小提琴: https://jsfiddle.net/noob0605/1y58t3fn/22/

据我所知,这是因为订单类别和数据不匹配.我该如何解决?

From what I can tell, it is because of the order categories and the data that is not matching. How do I fix that?

推荐答案

是的,问题是类别的顺序与get_grouped对象中数据的顺序不匹配.您可以通过在for (a in get_grouped)循环中添加排序(使每个数组中的对象的顺序与cat中的顺序匹配)来解决此问题:

You're right, the issue is that the order of categories does not match the order of the data in the get_grouped object. You can fix that by adding a sort (to make the order of objects in each array match the order in cat) to the for (a in get_grouped) loop:

for (a in get_grouped) {
            cat.forEach(t => {
                if (!get_grouped[a].some(v => v.type == t)) {
                    get_grouped[a].push({
        "date": get_grouped[a][0].date,
        "metric": 0,
        "type": t
      });
    }
  });
  get_grouped[a].sort((a, b) => cat.indexOf(a.type)-cat.indexOf(b.type));
}

这篇关于JSON的Highcharts堆积柱形图未绘制正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆