|
|
@ -77,47 +77,91 @@ function setTitle(title) {
|
|
|
|
* Draws a graph in a canvas element
|
|
|
|
* Draws a graph in a canvas element
|
|
|
|
* @param element
|
|
|
|
* @param element
|
|
|
|
* @param data
|
|
|
|
* @param data
|
|
|
|
* @param dimensions
|
|
|
|
* @param options
|
|
|
|
* TODO: Draw background grid and x-values and control it via dimensions (rename it to options)
|
|
|
|
* TODO: Seperated Background canvas and graph canvas. Div with span elements for x and y values.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
function drawGraph(element, data, dimensions) {
|
|
|
|
function drawGraph(element, data, options) {
|
|
|
|
let ctx = element.getContext('2d');
|
|
|
|
element.innerHTML = "";
|
|
|
|
let canvWidth = element.width;
|
|
|
|
let cv1 = document.createElement('canvas');
|
|
|
|
let canvHeight = element.height;
|
|
|
|
let cv2 = document.createElement('canvas');
|
|
|
|
|
|
|
|
let spanDiv = document.createElement('div');
|
|
|
|
|
|
|
|
spanDiv.setAttribute('class', 'labels');
|
|
|
|
|
|
|
|
let ctx1 = cv1.getContext('2d');
|
|
|
|
|
|
|
|
let ctx2 = cv2.getContext('2d');
|
|
|
|
|
|
|
|
element.appendChild(cv1);
|
|
|
|
|
|
|
|
element.appendChild(cv2);
|
|
|
|
|
|
|
|
let canvWidth = cv1.width;
|
|
|
|
|
|
|
|
let canvHeight = cv2.height;
|
|
|
|
|
|
|
|
ctx1.clearRect(0, 0, canvWidth, canvHeight);
|
|
|
|
|
|
|
|
ctx2.clearRect(0, 0, canvWidth, canvHeight);
|
|
|
|
let xData = [];
|
|
|
|
let xData = [];
|
|
|
|
let yData = [];
|
|
|
|
let yData = [];
|
|
|
|
if (!dimensions) dimensions = {};
|
|
|
|
if (!options) options = {};
|
|
|
|
|
|
|
|
ctx1.beginPath();
|
|
|
|
ctx.strokeStyle = $(element).css('color');
|
|
|
|
ctx1.strokeStyle = $(element).css('color');
|
|
|
|
|
|
|
|
ctx1.font = "80% Arial";
|
|
|
|
|
|
|
|
|
|
|
|
for (let dt of data) {
|
|
|
|
for (let dt of data) {
|
|
|
|
xData.push(dt[0]);
|
|
|
|
xData.push(dt[0]);
|
|
|
|
yData.push(dt[1]);
|
|
|
|
yData.push(dt[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let xMax = dimensions.xMax || Math.max.apply(Math, xData);
|
|
|
|
let xMax = options.xMax || Math.max.apply(Math, xData);
|
|
|
|
let xMin = dimensions.xMin || Math.min.apply(Math, xData);
|
|
|
|
let xMin = options.xMin || Math.min.apply(Math, xData);
|
|
|
|
let yMax = dimensions.yMax || Math.max.apply(Math, yData);
|
|
|
|
let yMax = options.yMax || Math.max.apply(Math, yData);
|
|
|
|
let yMin = dimensions.yMin || Math.min.apply(Math, yData);
|
|
|
|
let yMin = options.yMin || Math.min.apply(Math, yData);
|
|
|
|
|
|
|
|
let xUnit = options.xUnit || "";
|
|
|
|
|
|
|
|
let yUnit = options.yUnit || "";
|
|
|
|
|
|
|
|
|
|
|
|
let xStep = canvWidth / (xMax - xMin);
|
|
|
|
let xStep = canvWidth / (xMax - xMin);
|
|
|
|
let yStep = canvHeight / (yMax - yMin);
|
|
|
|
let yStep = canvHeight / (yMax - yMin);
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`yMax: ${yMax}; yMin: ${yMin}; ${JSON.stringify(dimensions)}`);
|
|
|
|
let gridCount = canvHeight/yStep;
|
|
|
|
|
|
|
|
while (gridCount > 10) {
|
|
|
|
|
|
|
|
gridCount /= 10;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let gridH = (canvHeight/gridCount);
|
|
|
|
|
|
|
|
for (let i = gridH; i < (canvHeight - gridH/10); i+= gridH) {
|
|
|
|
|
|
|
|
let span = document.createElement('span');
|
|
|
|
|
|
|
|
span.style = `position: absolute; top: ${(i/canvHeight)*100}%; left: 0`;
|
|
|
|
|
|
|
|
span.innerText = Math.round((canvHeight - i)/yStep)+Number(yMin)+" "+yUnit;
|
|
|
|
|
|
|
|
spanDiv.appendChild(span);
|
|
|
|
|
|
|
|
ctx1.moveTo(0, i);
|
|
|
|
|
|
|
|
ctx1.lineTo(canvWidth, i);
|
|
|
|
|
|
|
|
ctx1.stroke();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gridCount = canvWidth/xStep;
|
|
|
|
|
|
|
|
while (gridCount > 10) {
|
|
|
|
|
|
|
|
gridCount /= 2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let gridW = (canvWidth/gridCount);
|
|
|
|
|
|
|
|
for (let i = gridW; i < (canvWidth-gridW/10); i+= gridW) {
|
|
|
|
|
|
|
|
let span = document.createElement('span');
|
|
|
|
|
|
|
|
span.style = `position: absolute; left: ${(i/canvWidth)*100}%; bottom: 0`;
|
|
|
|
|
|
|
|
span.innerText = Math.round(i/xStep)+Number(xMin)+" "+xUnit;
|
|
|
|
|
|
|
|
spanDiv.appendChild(span);
|
|
|
|
|
|
|
|
ctx1.moveTo(i, 0);
|
|
|
|
|
|
|
|
ctx1.lineTo(i, canvHeight);
|
|
|
|
|
|
|
|
ctx1.stroke();
|
|
|
|
|
|
|
|
}
|
|
|
|
let x = 0;
|
|
|
|
let x = 0;
|
|
|
|
let y = canvHeight;
|
|
|
|
let y = canvHeight;
|
|
|
|
|
|
|
|
|
|
|
|
if (data.length > 0) {
|
|
|
|
if (data.length > 0) {
|
|
|
|
x = data[0][0];
|
|
|
|
x = data[0][0];
|
|
|
|
y = data[0][1];
|
|
|
|
y = data[0][1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx2.beginPath();
|
|
|
|
|
|
|
|
ctx2.strokeStyle = $(element).css('outline-color');
|
|
|
|
|
|
|
|
|
|
|
|
for (let dt of data) {
|
|
|
|
for (let dt of data) {
|
|
|
|
ctx.moveTo(x, y);
|
|
|
|
ctx2.moveTo(x, y);
|
|
|
|
x = dt[0] * xStep;
|
|
|
|
x = dt[0] * xStep;
|
|
|
|
y = canvHeight - ((dt[1] - yMin) * yStep);
|
|
|
|
y = canvHeight - ((dt[1] - yMin) * yStep);
|
|
|
|
ctx.lineTo(x, y);
|
|
|
|
ctx2.lineTo(x, y);
|
|
|
|
ctx.stroke();
|
|
|
|
ctx2.stroke();
|
|
|
|
console.log(x, y);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
element.appendChild(spanDiv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -- Events --
|
|
|
|
// -- Events --
|
|
|
|