clean kfet statistic.js

- no longer dictToArray where it isn't necessary (because already an
  array)
- fix chart height:
  - previous charts were causing bugs
  - height is fixed (even with window resizing)
- clean whitespaces
This commit is contained in:
Aurélien Delobelle 2017-04-03 16:53:28 +02:00
parent 87bc90ec8b
commit 903da795ab

View file

@ -1,10 +1,10 @@
(function($){ (function($){
window.StatsGroup = function (url, target) { window.StatsGroup = function (url, target) {
// a class to properly display statictics // a class to properly display statictics
// url : points to an ObjectResumeStat that lists the options through JSON // url : points to an ObjectResumeStat that lists the options through JSON
// target : element of the DOM where to put the stats // target : element of the DOM where to put the stats
var self = this; var self = this;
var element = $(target); var element = $(target);
var content = $("<div>"); var content = $("<div>");
@ -22,21 +22,21 @@
return array; return array;
} }
function handleTimeChart (dict) { function handleTimeChart (data) {
// reads the balance data and put it into chartjs formatting // reads the balance data and put it into chartjs formatting
var data = dictToArray(dict, 0); chart_data = new Array();
for (var i = 0; i < data.length; i++) { for (var i = 0; i < data.length; i++) {
var source = data[i]; var source = data[i];
data[i] = { x: new Date(source.at), chart_data[i] = { x: new Date(source.at),
y: source.balance, y: source.balance,
label: source.label } label: source.label }
} }
return data; return chart_data;
} }
function showStats () { function showStats () {
// CALLBACK : called when a button is selected // CALLBACK : called when a button is selected
// shows the focus on the correct button // shows the focus on the correct button
buttons.find(".focus").removeClass("focus"); buttons.find(".focus").removeClass("focus");
$(this).addClass("focus"); $(this).addClass("focus");
@ -50,14 +50,14 @@
var chart_datasets = []; var chart_datasets = [];
var charts = dictToArray(data.charts); var charts = dictToArray(data.charts);
// are the points indexed by timestamps? // are the points indexed by timestamps?
var is_time_chart = data.is_time_chart || false; var is_time_chart = data.is_time_chart || false;
// reads the charts data // reads the charts data
for (var i = 0; i < charts.length; i++) { for (var i = 0; i < charts.length; i++) {
var chart = charts[i]; var chart = charts[i];
// format the data // format the data
var chart_data = is_time_chart ? handleTimeChart(chart.values) : dictToArray(chart.values, 1); var chart_data = is_time_chart ? handleTimeChart(chart.values) : dictToArray(chart.values, 1);
@ -77,6 +77,7 @@
var chart_options = var chart_options =
{ {
responsive: true, responsive: true,
maintainAspectRatio: false,
tooltips: { tooltips: {
mode: 'index', mode: 'index',
intersect: false, intersect: false,
@ -129,25 +130,25 @@
type: 'line', type: 'line',
options: chart_options, options: chart_options,
data: { data: {
labels: dictToArray(data.labels, 1), labels: (data.labels || []).slice(1),
datasets: chart_datasets, datasets: chart_datasets,
} }
}; };
// saves the previous charts to be destroyed // saves the previous charts to be destroyed
var prev_chart = content.children(); var prev_chart = content.children();
// clean
prev_chart.remove();
// creates a blank canvas element and attach it to the DOM // creates a blank canvas element and attach it to the DOM
var canvas = $("<canvas height='70'>"); var canvas = $("<canvas height='250'>");
content.append(canvas); content.append(canvas);
// create the chart // create the chart
var chart = new Chart(canvas, chart_model); var chart = new Chart(canvas, chart_model);
// clean
prev_chart.remove();
} }
// initialize the interface // initialize the interface
function initialize (data) { function initialize (data) {
// creates the bar with the buttons // creates the bar with the buttons
@ -158,7 +159,7 @@
var to_click; var to_click;
var context = data.stats; var context = data.stats;
for (var i = 0; i < context.length; i++) { for (var i = 0; i < context.length; i++) {
// creates the button // creates the button
var btn_wrapper = $("<div>", var btn_wrapper = $("<div>",