Highcharts Stacked Column chart from JSON not charting correct values

拟墨画扇 提交于 2020-06-28 09:54:39

问题


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

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

Why is that happening?

My code:

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
}); 

Here is my fiddle: 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?


回答1:


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));
}



回答2:


Instead of using push, use splice to set the data at particular category index.

 for (a in get_grouped) {
  cat.forEach((t, index) => {
    if (!get_grouped[a].some(v => v.type == t)) {
      const obj = {
        "date": get_grouped[a][0].date,
        "metric": 0,
        "type": t
      }
      get_grouped[a].splice(index, 0, obj);
    }
  })
}

Updated Fiddle https://jsfiddle.net/technochamp/wj82bk60/1/



来源:https://stackoverflow.com/questions/61650530/highcharts-stacked-column-chart-from-json-not-charting-correct-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!