Simplify statistic.js

On supprime des fonctions inutiles, on lint, et on simplifie 2-3 options
inutilisées.
This commit is contained in:
Ludovic Stephan 2020-03-09 16:19:06 +01:00
parent 48ad5cd1c7
commit c66fb7eb6f

View file

@ -1,28 +1,15 @@
(function($){
(function ($) {
window.StatsGroup = function (url, target) {
// a class to properly display statictics
// url : points to an ObjectResumeStat that lists the options through JSON
// target : element of the DOM where to put the stats
var self = this;
var element = $(target);
var content = $("<div class='full'>");
var buttons;
function dictToArray (dict, start) {
// converts the dicts returned by JSONResponse to Arrays
// necessary because for..in does not guarantee the order
if (start === undefined) start = 0;
var array = new Array();
for (var k in dict) {
array[k] = dict[k];
}
array.splice(0, start);
return array;
}
function handleTimeChart (data) {
function handleTimeChart(data) {
// reads the balance data and put it into chartjs formatting
chart_data = new Array();
for (var i = 0; i < data.length; i++) {
@ -36,7 +23,7 @@
return chart_data;
}
function showStats () {
function showStats() {
// CALLBACK : called when a button is selected
// shows the focus on the correct button
@ -44,24 +31,20 @@
$(this).addClass("focus");
// loads data and shows it
$.getJSON(this.stats_target_url, {format: 'json'}, displayStats);
$.getJSON(this.stats_target_url, displayStats);
}
function displayStats (data) {
function displayStats(data) {
// reads the json data and updates the chart display
var chart_datasets = [];
var charts = dictToArray(data.charts);
// are the points indexed by timestamps?
var is_time_chart = data.is_time_chart || false;
// reads the charts data
for (var i = 0; i < charts.length; i++) {
var chart = charts[i];
for (let chart of data.charts) {
// format the data
var chart_data = is_time_chart ? handleTimeChart(chart.values) : dictToArray(chart.values, 0);
var chart_data = is_time_chart ? handleTimeChart(chart.values) : chart.values;
chart_datasets.push(
{
@ -94,11 +77,6 @@
chart_options['scales'] = {
xAxes: [{
type: "time",
display: true,
scaleLabel: {
display: false,
labelString: 'Date'
},
time: {
tooltipFormat: 'll HH:mm',
displayFormats: {
@ -115,13 +93,6 @@
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: false,
labelString: 'value'
}
}]
};
}
@ -131,7 +102,7 @@
type: 'line',
options: chart_options,
data: {
labels: data.labels || [],
labels: data.labels || [],
datasets: chart_datasets,
}
};
@ -151,23 +122,26 @@
}
// initialize the interface
function initialize (data) {
function initialize(data) {
// creates the bar with the buttons
buttons = $("<ul>",
{class: "nav stat-nav",
"aria-label": "select-period"});
{
class: "nav stat-nav",
"aria-label": "select-period"
});
var to_click;
var context = data.stats;
for (var i = 0; i < context.length; i++) {
for (let stat of data.stats) {
// creates the button
var btn_wrapper = $("<li>", {role:"presentation"});
var btn_wrapper = $("<li>", { role: "presentation" });
var btn = $("<a>",
{class: "btn btn-nav",
type: "button"})
.text(context[i].label)
.prop("stats_target_url", context[i].url)
{
class: "btn btn-nav",
type: "button"
})
.text(stat.label)
.prop("stats_target_url", stat.url)
.on("click", showStats);
// saves the default option to select
@ -189,7 +163,7 @@
// constructor
(function () {
$.getJSON(url, {format: 'json'}, initialize);
$.getJSON(url, initialize);
})();
};
})(jQuery);