commento/frontend/js/dashboard-statistics.js

111 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2018-06-04 00:06:17 +08:00
(function (global, document) {
"use strict";
(document);
2018-06-04 00:06:17 +08:00
global.numberify = function(x) {
if (x === 0) {
2018-06-04 00:06:17 +08:00
return {"zeros": "000", "num": "", "units": ""}
}
2018-06-04 00:06:17 +08:00
if (x < 10) {
2018-06-04 00:06:17 +08:00
return {"zeros": "00", "num": x, "units": ""}
}
2018-06-04 00:06:17 +08:00
if (x < 100) {
2018-06-04 00:06:17 +08:00
return {"zeros": "0", "num": x, "units": ""}
}
2018-06-04 00:06:17 +08:00
if (x < 1000) {
2018-06-04 00:06:17 +08:00
return {"zeros": "", "num": x, "units": ""}
}
2018-06-04 00:06:17 +08:00
var res;
if (x < 1000000) {
res = global.numberify((x/1000).toFixed(0))
2018-06-04 00:06:17 +08:00
res.units = "K"
} else if (x < 1000000000) {
res = global.numberify((x/1000000).toFixed(0))
2018-06-04 00:06:17 +08:00
res.units = "M"
} else if (x < 1000000000000) {
res = global.numberify((x/1000000000).toFixed(0))
2018-06-04 00:06:17 +08:00
res.units = "B"
}
if (res.num*10 % 10 === 0) {
2018-06-04 00:06:17 +08:00
res.num = Math.ceil(res.num);
}
2018-06-04 00:06:17 +08:00
return res;
}
global.statisticsOpen = function() {
var data = global.dashboard.$data;
var json = {
"ownerToken": global.cookieGet("commentoOwnerToken"),
"domain": data.domains[data.cd].domain,
2018-06-04 00:06:17 +08:00
}
$(".view").hide();
global.post(global.origin + "/api/domain/statistics", json, function(resp) {
2018-06-04 00:06:17 +08:00
$("#statistics-view").show();
if (!resp.success) {
global.globalErrorShow(resp.message);
2018-06-04 00:06:17 +08:00
return;
}
var options = {
showPoint: false,
axisY: {
onlyInteger: true,
showGrid: false,
},
axisX: {
showGrid: false,
},
showArea: true,
};
var views;
var comments;
views = resp.viewsLast30Days;
// views = [0, 1, 4, 16, 14, 12, 10, 25, 13, 5, 20, 25, 12, 57, 46, 64, 4, 36, 7, 80, 43, 86, 121, 6, 74, 94, 83, 73, 140, 89, 25];
comments = resp.commentsLast30Days;
// comments = [0, 0, 1, 2, 3, 3, 4, 5, 7, 8, 5, 9, 9, 5, 6, 7, 8, 3, 1, 16, 3, 10, 8, 5, 12, 5, 4, 8, 4, 23, 19];
var labels = new Array();
for (var i = 0; i < views.length; i++) {
if ((views.length-i) % 7 === 0) {
2018-06-04 00:06:17 +08:00
var x = (views.length-i)/7;
labels.push(x + " week" + (x > 1 ? "s" : "") + " ago");
} else {
2018-06-04 00:06:17 +08:00
labels.push("");
}
2018-06-04 00:06:17 +08:00
}
new Chartist.Line("#views-graph", {
labels: labels,
series: [views],
}, options);
new Chartist.Line("#comments-graph", {
labels: labels,
series: [comments],
}, options);
data.domains[data.cd].viewsLast30Days = global.numberify(views.reduce(function(a, b) {
return a + b;
}, 0));
data.domains[data.cd].commentsLast30Days = global.numberify(comments.reduce(function(a, b) {
return a + b;
}, 0));
2018-06-04 00:06:17 +08:00
});
}
} (window.commento, document));