我需要计算今年相对于去年的增长((TY - LY)/LY)...我可以创建一个计算字段,但是我无法创建一个合适的函数来计算总增长。
总增长值应为 ( 15 - 12 ) / 12 * 100 = 25
我的代码如下:
<html>
<body>
<div id="example-table"></div>
</body>
<script>
//define data array
var tabledata = [{'LOCNAME': 'Location_A',
'SLREVENUE': 6,
'SLREVENUE_LY': 4},
{'LOCNAME': 'Location_B',
'SLREVENUE': 9,
'SLREVENUE_LY': 8}];
//initialize table
var table = new Tabulator("#example-table", {
data:tabledata, //load row data from array
layout:"fitColumns", //fit columns to width of table
columns:[ //define the table columns
{title:"Location", field:"LOCNAME"},
{title:"Revenue", field:"SLREVENUE", topCalc:"sum"},
{title:"Revenue_LY", field:"SLREVENUE_LY", topCalc:"sum"},
{title:"Growth", field:"SLGROWTH", mutator:function(value, data){return (data.SLREVENUE - data.SLREVENUE_LY) / data.SLREVENUE_LY * 100;},
}
],
});
</script>
</html>
请问有什么帮助吗?
我需要一个计算 topCalc 参数中增长的函数
对于标头,您需要为访问
topCalc
的 table.getCalcResults()
属性添加自定义函数。比如:
columns:[
...
{title: 'Growth', ..., topCalc: function(_v,_d,_p) {
let topres = table.getCalcResults().top;
//console.log("TopCalc top results", topres);
return (topres["SLREVENUE"] - topres["SLREVENUE_LY"]) / topres["SLREVENUE_LY"] * 100;
}
}
]
我更新了旧的Codepen我有一个额外的专栏来展示这一点。请参阅
AggregateFn
列的定义以及实现它的 getManipulatedTotal
函数。