2016-12-09 28 views
0

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

例如。我有两个字段 1.“销售价值” 2.“销售量”

现在我必须计算字段ASP =销售价值/销售量。

回答

1

我害怕酒窝没有内置的方式来处理这个。我假设酒窝正在汇总你的数据 - 因此困难。但是在这里你没有选择,只能预先聚合到一个数据点的水平,并自己添加计算的字段。例如,如果您的数据包含品牌,SKU和渠道,但您的图表处于品牌,渠道级别,则需要预先处理如下数据:

// 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 
... 
+0

非常感谢,解决方案正在运行:) – Karthik

相关问题