如何在dimple.js中添加计算字段(计算两个或多个数据字段的字段)?
例如:我有两个字段% 1。"Sales Value“2。“销售量”
现在我必须计算一个字段ASP = Sales Value /Sales Volume。
发布于 2016-12-10 02:27:51
我担心酒窝没有一个内置的方法来处理这件事。我假设dimple正在为你聚合数据--这就是问题所在。但在这里,您别无选择,只能预先聚合到数据点的级别,然后自己添加计算字段。例如,如果您的数据具有Brand、SKU和Channel,但您的图表位于Brand、Channel级别,则需要像这样对数据进行预处理:
// var chartData is going to be the aggregated level you use for your chart.
// var added is a dictionary of aggregated data returning a row index
// for each Brand/Channel combination.
var chartData = [],
added = {};
// Aggregate to the Brand/Channel level
data.forEach(function (d) {
var key = d["Brand"] + "|" + d["Channel"],
i = added[key];
// Check the output index
if (i !== undefined) {
// Brand/Channel have been added already so add the measures
chartData[i]["Sales Value"] += parseFloat(d["Sales Value"]);
chartData[i]["Sales Volume"] += parseFloat(d["Sales Volume"]);
} else {
// Get the index for the row we are about to add
added[key] = chartData.length;
// Insert a new output row for the Brand/Channel
chartData.push({
"Brand": d["Brand"],
"Channel": d["Channel"],
"Sales Value": parseFloat(d["Sales Value"]) || 0,
"Sales Volume": parseFloat(d["Sales Volume"]) || 0
});
}
});
// Calculate ASP
chartData.forEach(function (d) {
d["ASP"] = d["Sales Value"] / d["Sales Volume"];
});
// Draw the chart using chartData instead of data
...https://stackoverflow.com/questions/41061644
复制相似问题