![](/img/trans.png)
[英]How can I convert the sum of minutes, hours in the 'timelite.js' or 'moment.js'?
[英]Sum of Hours,MInutes in PivotUI JS
ivotUI js我遇到了ivotUI js (图表创建JS)问题。
我想将小时数字段的总值计算为小时:分钟格式。 但是ivotUI js默认功能对我的要求没有帮助。
我想创建自定义函数来计算持续时间到小时和分钟的格式。
有什么建议么 ?
我的代码如下。
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"
});
经过非常深入的调试后,最终我通过在pivot.js文件上添加用于计算持续时间的自定义函数获得了解决方案。
第1步:首先在js上定义您的自定义变量,就像在ivot.js中使用的其他其他变量一样(hourMinuteFormat,hourMinuteInt是自定义变量)。 例如:
var PivotData, addSeparators, aggregatorTemplates, aggregators, dayNamesEn, derivers, getSort, locales, mthNamesEn, naturalSort, numberFormat, pivotTableRenderer, renderers, sortAs, usFmt, usFmtInt, usFmtPct, zeroPad, hourMinuteFormat, hourMinuteInt;
第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();
步骤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
};
};
};
},
步骤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
};
步骤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"
});
我希望它可以理解,如果有人有疑问,可以问我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.