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 @@ -