首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在dimple.js中创建计算字段

如何在dimple.js中创建计算字段
EN

Stack Overflow用户
提问于 2016-12-09 21:31:50
回答 1查看 50关注 0票数 0

如何在dimple.js中添加计算字段(计算两个或多个数据字段的字段)?

例如:我有两个字段% 1。"Sales Value“2。“销售量”

现在我必须计算一个字段ASP = Sales Value /Sales Volume。

EN

回答 1

Stack Overflow用户

发布于 2016-12-10 02:27:51

我担心酒窝没有一个内置的方法来处理这件事。我假设dimple正在为你聚合数据--这就是问题所在。但在这里,您别无选择,只能预先聚合到数据点的级别,然后自己添加计算字段。例如,如果您的数据具有Brand、SKU和Channel,但您的图表位于Brand、Channel级别,则需要像这样对数据进行预处理:

代码语言:javascript
复制
// 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
...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41061644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档