Commit ea38c499 authored by Wolfgang Fellger's avatar Wolfgang Fellger

Make graph drawing more robust

Now renders sets with a single data point and useful scales for ranges larger than 20kg
parent 4e45bf6b
......@@ -7,7 +7,7 @@ WeightGraph = {
}
// Adjust diagram size to data
var xMin = data[0] ? data[0][0] : Date.now() - 3600 * 1000 * 24 * 5;
var xMin = data[0] ? data[0][0] : Date.now();
var xMax = data[data.length-1] ? data[data.length-1][0] : Date.now();
var yMin = 250;
var yMax = 0;
......@@ -15,17 +15,17 @@ WeightGraph = {
if (data[x][1] < yMin)
yMin = Math.max(Math.floor(data[x][1] / 5) * 5, 20);
if (data[x][1] > yMax)
yMax = Math.min(Math.ceil(data[x][1] / 5) * 5, 200);
yMax = Math.min(Math.ceil(data[x][1] / 5) * 5, 300);
}
yMax += 0.5;
yMin -= 0.5;
var xLength = xMax - xMin;
var xLength = Math.max(xMax - xMin, 3600 * 1000 * 24 * 3);
var yLength = yMax - yMin;
var xScale = diagWidth / xLength;
var yScale = diagHeight / yLength;
ctx.save();
ctx.translate(50, 0);
ctx.translate(50, 4);
// Draw diagram scales and guides
ctx.moveTo(-0.5,0);
......@@ -44,16 +44,19 @@ WeightGraph = {
ctx.strokeText(text, x, diagHeight + 15, 100);
}
var stepping = Math.ceil(25 / (translate(0, 0).y - translate(0, 1).y))
for (var y = Math.round(yMin); y < Math.round(yMax); y++) {
var translated = Math.round(translate(0, y).y - 0.5) + 0.5;
ctx.beginPath();
ctx.strokeStyle = '#ccc';
ctx.moveTo(-5, translated);
ctx.lineTo(diagWidth, translated);
ctx.stroke();
if (y % 2 == 0) {
if (stepping <= 5 || y % stepping == 0) {
ctx.beginPath();
ctx.strokeStyle = '#ccc';
ctx.moveTo(-5, translated);
ctx.lineTo(diagWidth, translated);
ctx.stroke();
}
if (y % stepping == 0) {
ctx.strokeStyle = '#000';
ctx.strokeText(y + ' kg', -40, translated + 5, 50);
ctx.strokeText(y + ' kg', -40, translated + 4, 50);
}
}
ctx.restore();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment