Commit b95eb528 authored by MichaelZinsmaier's avatar MichaelZinsmaier

attemps to handle the "Smoothing a graph when there's a drop to zero" issue

thinking about it this option does not make much sense...
parent 89483ad4
...@@ -65,6 +65,7 @@ ...@@ -65,6 +65,7 @@
fitPointDist: int defines the x axis distance of the additional two points that are used fitPointDist: int defines the x axis distance of the additional two points that are used
to enforce the min max condition. (you will get curvePointFactor * 3 * |datapoints| to enforce the min max condition. (you will get curvePointFactor * 3 * |datapoints|
"virtual" points if fit is true) "virtual" points if fit is true)
smoothZero: bool true => smooth joining of the curve with the x-axis when displaying values below zero with a y-axis min of zero
*/ */
/* /*
...@@ -87,7 +88,8 @@ ...@@ -87,7 +88,8 @@
fillColor : null, fillColor : null,
lineWidth : 2, lineWidth : 2,
curvePointFactor : 20, curvePointFactor : 20,
fitPointDist : 0.0001 fitPointDist : 0.0001,
smoothZero: false
} }
} }
}; };
...@@ -139,7 +141,14 @@ ...@@ -139,7 +141,14 @@
data = series.data; data = series.data;
} }
var points = calculateCurvePoints(data, series.curvedLines);
//optional smooth out zero passes
if (series.curvedLines.smoothZero) {
points = calculateZeroSmoothedCurvePoints(data, series.curvedLines);
} else {
points = calculateCurvePoints(data, series.curvedLines);
}
plotLine(ctx, points, axisx, axisy, series.curvedLines.fill); plotLine(ctx, points, axisx, axisy, series.curvedLines.fill);
ctx.restore(); ctx.restore();
} }
...@@ -167,6 +176,7 @@ ...@@ -167,6 +176,7 @@
return ret; return ret;
} }
//nearly the same as in the core library //nearly the same as in the core library
//only ps is adjusted to 2 //only ps is adjusted to 2
function plotLine(ctx, points, axisx, axisy, fill) { function plotLine(ctx, points, axisx, axisy, fill) {
...@@ -258,6 +268,48 @@ ...@@ -258,6 +268,48 @@
ctx.stroke(); ctx.stroke();
} }
function calculateZeroSmoothedCurvePoints(data, curvedLinesOptions) {
var data2 = new Array(new Array, new Array);
var Y = 1
var X = 0
var j = 0;
for (var i = 0; i < data.length; i++) {
if (data[i][Y] < 0) {
if (i > 0 && data[i-1][Y] > 0) {
//point before exists and is over zero
var x1 = data[i-1][X];
var x2 = data[i][X];
var y1 = data[i-1][Y];
var y2 = data[i][Y];
var newX = ((-y1) / ((y2-y1)/(x2-x1))) + x1;
data2[j] = new Array(newX,0);
j++;
}
data2[j] = new Array(data[i][X],0);
j++;
if (i < (data.length-1) && data[i+1][Y] > 0) {
//point before exists and is over zero
var x1 = data[i][X];
var x2 = data[i+1][X];
var y1 = data[i][Y];
var y2 = data[i+1][Y];
var newX = ((-y1) / ((y2-y1)/(x2-x1))) + x1;
data2[j] = new Array(newX,0);
j++;
}
} else {
data2[j] = data[i];
j++;
}
}
return calculateCurvePoints(data2, curvedLinesOptions);
}
//no real idea whats going on here code mainly from https://code.google.com/p/flot/issues/detail?id=226 //no real idea whats going on here code mainly from https://code.google.com/p/flot/issues/detail?id=226
//if fit option is selected additional datapoints get inserted before the curve calculations in nergal.dev s code. //if fit option is selected additional datapoints get inserted before the curve calculations in nergal.dev s code.
function calculateCurvePoints(data, curvedLinesOptions) { function calculateCurvePoints(data, curvedLinesOptions) {
......
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