From 3116a6d8dbf22c7e4c730db08f9bcaa36cf9d239 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 9 Jun 2015 12:58:09 +0200 Subject: [PATCH] feature #3748: Add derivative for graphs --- src/sunstone/public/app/utils/graphs.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/sunstone/public/app/utils/graphs.js b/src/sunstone/public/app/utils/graphs.js index 024b7edf69..a9eb8f3df8 100644 --- a/src/sunstone/public/app/utils/graphs.js +++ b/src/sunstone/public/app/utils/graphs.js @@ -14,9 +14,9 @@ define(function(require) { CONSTRUCTOR */ - return { + return { 'plot': _plotGraph - } + } /* FUNCTION DEFINITIONS @@ -91,4 +91,21 @@ define(function(require) { $.plot(info.div_graph, series, options); }; } + + function derivative(data) { + for (var i = 0; i < data.length - 1; i++) { + // Each elem is [timestamp, cumulative value] + var first = data[i]; + var second = data[i + 1]; + + // value now - value before / seconds + var speed = (second[1] - first[1]) / (second[0] - first[0]); + + // The first element is replaced with the second one + data[i] = [first[0], speed]; + } + + // The last elem must be removed + data.pop(); + } });