gpt4 book ai didi

javascript - 使用 JavaScript 从表创建多系列 FusionCharts 的数据集

转载 作者:太空宇宙 更新时间:2023-11-04 15:38:02 24 4
gpt4 key购买 nike

我有来自表格的数据:

var my_data = [
{"series":"group1", "type":"device1", "value":15},
{"series":"group1", "type":"device3", "value":12},
{"series":"group2", "type":"device1", "value":16},
{"series":"group2", "type":"device1", "value":11},
{"series":"group2", "type":"device2", "value":17},
{"series":"group2", "type":"device2", "value":18},
{"series":"group2", "type":"device3", "value":10},
{"series":"group2", "type":"device4", "value":14},
{"series":"group3", "type":"device1", "value":13},
{"series":"group3", "type":"device3", "value":19},
{"series":"group3", "type":"device4", "value":11},
{"series":"group3", "type":"device4", "value":15}
];

我正在努力编写代码,将其转换为 FusionCharts 多系列图表所需的类别和数据集...结果应该是:

var my_categories = [{"category": [{"label": "device1"}, {"label": "device2"}, {"label": "device3"}, {"label": "device4"}]}];

var my_dataset = [
{"seriesname": "group1", "data": [{"value": 15}, {"value": 0}, {"value": 12}, {"value": 0}]},
{"seriesname": "group2", "data": [{"value": 27}, {"value": 35}, {"value": 10}, {"value": 14}]},
{"seriesname": "group3", "data": [{"value": 13}, {"value": 0}, {"value": 19}, {"value": 26}]}
];

挑战在于数据集顺序必须与类别相匹配,并且数据值需要汇总到每个类别的单个设备。

如何转换这些数据?我假设第一步是使用 for 循环获取类别。但在那之后,我就陷入了困境。

这是工作图表,但手动转换了数据: http://jsfiddle.net/yqqbqqe9/

最佳答案

您可以创建一些辅助对象,用于类别并作为对同一系列的引用。然后为新系列构建一个具有全零值的值数组的对象。

至少将值添加到相应的对象中。

此提案适用于未排序的数据,但首次出现的顺序构建了系列的顺序。如有必要,可以稍后对此进行排序。

var data = [{ series: "group1", type: "device1", value: 15 }, { series: "group1", type: "device3", value: 12 }, { series: "group2", type: "device1", value: 16 }, { series: "group2", type: "device1", value: 11 }, { series: "group2", type: "device2", value: 17 }, { series: "group2", type: "device2", value: 18 }, { series: "group2", type: "device3", value: 10 }, { series: "group2", type: "device4", value: 14 }, { series: "group3", type: "device1", value: 13 }, { series: "group3", type: "device3", value: 19 }, { series: "group3", type: "device4", value: 11 }, { series: "group3", type: "device4", value: 15 }],
categories = [{ category: [{ label: "device1" }, { label: "device2" }, { label: "device3" }, { label: "device4" }] }],
result = data.reduce(function (categories) {
var catIndex = categories.reduce(function (r, o, i) {
r[o.label] = i;
return r;
}, {}),
series = Object.create(null);

return function (r, o) {
if (!series[o.series]) {
series[o.series] = {
seriesname: o.series,
data: categories.map(function () { return { value: 0 } })
};
r.push(series[o.series]);
}
series[o.series].data[catIndex[o.type]].value += o.value;
return r;
};
}(categories[0].category), []);


console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 使用 JavaScript 从表创建多系列 FusionCharts 的数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44136695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com