[英]Sum of Hours,MInutes in PivotUI JS
pivotUI js I am having issue with pivotUI js (chart creation JS). ivotUI js我遇到了ivotUI js (图表创建JS)问题。
i want to calculate duration field's total value as hours:min format. 我想将小时数字段的总值计算为小时:分钟格式。 but pivotUI js default functions are not helpful for my requirements.
但是ivotUI js默认功能对我的要求没有帮助。
i want to create custom function for calculate that duration to hours and minutes format. 我想创建自定义函数来计算持续时间到小时和分钟的格式。
any suggestions ? 有什么建议么 ?
my code is like below. 我的代码如下。
var renderers = $.extend($.pivotUtilities.renderers, $.pivotUtilities.export_renderers, $.pivotUtilities.gchart_renderers, $.pivotUtilities.derivers);
var tpl = $.pivotUtilities.aggregatorTemplates;
$("#output").pivotUI(data.data, {
renderers: renderers,
rows: ["fieldOne", "fieldTwo", "fieldThree", "Duration"],
cols: [],
//hiddenAttributes: ["Total"],
aggregators: {
"Total Time": function () {
return tpl.sum()(["Duration"]);
}
},
aggregatorName: "Total Time"
});
after debugging at very deep level, finally i got solution by adding custom function for calculating duration on pivot.js file. 经过非常深入的调试后,最终我通过在pivot.js文件上添加用于计算持续时间的自定义函数获得了解决方案。
step 1: firstly define your custom variables on js, like wise other variables used in pivot.js (hourMinuteFormat,hourMinuteInt are custom variables). 第1步:首先在js上定义您的自定义变量,就像在ivot.js中使用的其他其他变量一样(hourMinuteFormat,hourMinuteInt是自定义变量)。 eg:
例如:
var PivotData, addSeparators, aggregatorTemplates, aggregators, dayNamesEn, derivers, getSort, locales, mthNamesEn, naturalSort, numberFormat, pivotTableRenderer, renderers, sortAs, usFmt, usFmtInt, usFmtPct, zeroPad, hourMinuteFormat, hourMinuteInt;
Step 2: create function which get duration and calculate total hours and total mins by logic as below. 第2步:创建函数,获取持续时间并通过以下逻辑计算总时数和总分钟数。
hourMinuteFormat = function (opts) {
var defaults;
defaults = {
separator: ":"
};
opts = $.extend({}, defaults, opts);
return function (x) {
var totalh = Math.floor(x / 60);
if (totalh < 10)
{
totalh = "0" + totalh;
}
var totalm = x % 60;
if (totalm < 10)
{
totalm = "0" + totalm;
}
var result = totalh + opts.separator + totalm;
return result;
};
};
hourMinuteInt = hourMinuteFormat();
Step 3: After that need to create one more function which accept duration as "hh:mm" format and do further process to split it using ":" and store on different variables like hours,minutes. 步骤3:之后,需要再创建一个函数,以“ hh:mm”格式接受持续时间,并执行进一步的过程以使用“:”将其拆分,并存储在不同的变量(例如小时,分钟)上。
HoursMinutes: function (formatter) {
if (formatter == null) {
formatter = hourMinuteInt;
}
return function (arg) {
var attr;
attr = arg[0];
return function (data, rowKey, colKey) {
return {
sum: 0,
push: function (record) {
if (record[attr]) {
var minute = parseInt((record[attr]).split(':')[1]);
var hour = parseInt((record[attr]).split(':')[0]) * 60;
return this.sum += minute + hour;
}
},
value: function () {
return this.sum;
},
format: formatter,
numInputs: attr != null ? 0 : 1
};
};
};
},
Step 4: find $.pivotUtilities
array where other default functions are defined there define your custom function Like below: 步骤4:找到
$.pivotUtilities
数组,其中定义了其他默认函数,在其中定义您的自定义函数,如下所示:
$.pivotUtilities = {
aggregatorTemplates: aggregatorTemplates,
aggregators: aggregators,
renderers: renderers,
derivers: derivers,
locales: locales,
naturalSort: naturalSort,
numberFormat: numberFormat,
sortAs: sortAs,
PivotData: PivotData,
hourMinuteFormat: hourMinuteFormat //custom function
};
Step 5: after that in your file where you want to calculate duration, you need to use function named HoursMinutes() as below. 步骤5:之后,在要计算持续时间的文件中,需要使用名为HoursMinutes()的函数,如下所示。
var renderers = $.extend($.pivotUtilities.renderers, $.pivotUtilities.export_renderers, $.pivotUtilities.gchart_renderers, $.pivotUtilities.derivers);
var tpl = $.pivotUtilities.aggregatorTemplates;
$("#output").pivotUI(data.data, {
renderers: renderers,
rows: ["fieldOne", "fieldTwo", "fieldThree", "Duration"],
cols: [],
aggregators: {
"Total Time": function () {
return tpl.HoursMinutes()(["Duration"]);
}
},
aggregatorName: "Total Time"
});
i hope its understandable, if any one having query, then can ask me. 我希望它可以理解,如果有人有疑问,可以问我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.