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