From 903da795ab3f7a5399a33cf221ea70d8b19880b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 3 Apr 2017 16:53:28 +0200 Subject: [PATCH] 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 --- kfet/static/kfet/js/statistic.js | 37 ++++++++++++++++---------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/kfet/static/kfet/js/statistic.js b/kfet/static/kfet/js/statistic.js index f6d21237..9593c9f7 100644 --- a/kfet/static/kfet/js/statistic.js +++ b/kfet/static/kfet/js/statistic.js @@ -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 = $("
"); @@ -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 = $(""); + var canvas = $(""); 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 = $("
",