![](/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.