From 1e287502f88c094b30f29df2765225dd12a6d938 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Thu, 16 Mar 2017 13:31:49 +0100 Subject: [PATCH] Fix messy graph bug due to unsorted x axis --- static/slaveDisplay.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/static/slaveDisplay.js b/static/slaveDisplay.js index 6173227a..9da60283 100644 --- a/static/slaveDisplay.js +++ b/static/slaveDisplay.js @@ -55,7 +55,7 @@ function makeGraph(slaveID, selector) { chartData[chartData.length] = generateLineChartArray(data, itemNames[o]); } } - console.log(chartData) + //console.log(chartData) drawChart(selector, chartData) } // callback(data); @@ -96,6 +96,7 @@ function generateLineChartArray(data, nameKey) { } } } + chartData = sortByKey(chartData, "x") let xyz = {}; xyz.name = nameKey; xyz.type = "line"; @@ -128,7 +129,7 @@ function drawChart(selector, chartData) { e.dataSeries.visible = false; } else { e.dataSeries.visible = true; - } + } e.chart.render(); } }, @@ -137,4 +138,12 @@ function drawChart(selector, chartData) { chart = chartsByID[selector]; // console.log(chart) chart.render(); +} + +// function to sort arrays of objects after a keys value +function sortByKey(array, key) { + return array.sort(function(a, b) { + var x = Number(a[key]); var y = Number(b[key]); + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + }); } \ No newline at end of file