From 88dd8b7936f534717b14be2b4a84dc0fca3eaafd Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 22 Oct 2018 22:06:12 +0200 Subject: [PATCH] Added Elements - Added primitive graph element - Changed data --- glob/script.js | 47 ++++++++++++++++++++++++++++++++++++++++ glob/style/elements.sass | 9 +++++++- res/html/data.htm | 20 ++++++++--------- res/scripts/data.js | 32 ++++----------------------- 4 files changed, 69 insertions(+), 39 deletions(-) diff --git a/glob/script.js b/glob/script.js index 31154a3..9749153 100644 --- a/glob/script.js +++ b/glob/script.js @@ -73,6 +73,53 @@ function setTitle(title) { $('.navigationBar .title').html("" + title + ""); } +/** + * Draws a graph in a canvas element + * @param element + * @param data + * @param dimensions + * TODO: Draw background grid and x-values and control it via dimensions (rename it to options) + */ +function drawGraph(element, data, dimensions) { + let ctx = element.getContext('2d'); + let canvWidth = element.width; + let canvHeight = element.height; + let xData = []; + let yData = []; + if (!dimensions) dimensions = {}; + + ctx.strokeStyle = $(element).css('color'); + + for (let dt of data) { + xData.push(dt[0]); + yData.push(dt[1]); + } + let xMax = dimensions.xMax || Math.max.apply(Math, xData); + let xMin = dimensions.xMin || Math.min.apply(Math, xData); + let yMax = dimensions.yMax || Math.max.apply(Math, yData); + let yMin = dimensions.yMin || Math.min.apply(Math, yData); + + let xStep = canvWidth / (xMax - xMin); + let yStep = canvHeight / (yMax - yMin); + + console.log(`yMax: ${yMax}; yMin: ${yMin}; ${JSON.stringify(dimensions)}`); + + let x = 0; + let y = canvHeight; + if (data.length > 0) { + x = data[0][0]; + y = data[0][1]; + } + for (let dt of data) { + ctx.moveTo(x, y); + x = dt[0] * xStep; + y = canvHeight - ((dt[1] - yMin) * yStep); + ctx.lineTo(x, y); + ctx.stroke(); + console.log(x, y); + } +} + // -- Events -- // Define what happens onLoad $(document).ready(function(){ diff --git a/glob/style/elements.sass b/glob/style/elements.sass index a211776..a1c2cb6 100644 --- a/glob/style/elements.sass +++ b/glob/style/elements.sass @@ -143,4 +143,11 @@ top: 50% height: calc(50% - 37.5px) left: 0 - overflow: auto \ No newline at end of file + overflow: auto + .graph + color: $cOnPrimary + position: absolute + left: $sPadding + top: 20% + height: calc(80% - 12.5px) + width: calc(100% - 25px) \ No newline at end of file diff --git a/res/html/data.htm b/res/html/data.htm index 3c26706..97a6ec7 100644 --- a/res/html/data.htm +++ b/res/html/data.htm @@ -1,15 +1,15 @@ -

Data

-
-
- - - - +
+
+ + Title + + + +
- + \ No newline at end of file diff --git a/res/scripts/data.js b/res/scripts/data.js index 1dc423d..c620c79 100644 --- a/res/scripts/data.js +++ b/res/scripts/data.js @@ -1,28 +1,4 @@ -function getFormattedData(dataArray) { - let arrData = ["0,100"]; - for (let d of dataArray) { - arrData.push(d[0]+","+(100-d[1])); - } - arrData.push("100,100"); - return arrData.join(" "); -} - -new Vue({ - el: '#app', - data: { - temperatureData: [1,2,3,4], - percentData: [2,3,4,5], - labels: ["1", "2", "3", "4", "5"], - chartOptions: { - responsive: true, - maintainAspectRatio: false - } - }, - mounted() { - let self = this; - $.getJSON('cpu.json', d => { - self.temperatureData = getFormattedData(d['100temp']), - self.percentData = getFormattedData(d['100perc']) - }) - } -}) +$.getJSON('cpu.json', data =>{ + let c = document.querySelector('.graph'); + drawGraph(c, data['100temp'], {yMax: 50, yMin: "0"}); +}); \ No newline at end of file