Commit 3db2c2ee authored by Michael Zinsmaier's avatar Michael Zinsmaier

API break refactored the code to prepare it for a new algorithmic core

parent 3fa41abe
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
THE SOFTWARE. THE SOFTWARE.
*/ */
/* /*
____________________________________________________ ____________________________________________________
...@@ -30,17 +30,13 @@ ...@@ -30,17 +30,13 @@
____________________________________________________ ____________________________________________________
curvedLines is a plugin for flot, that tries to display lines in a smoother way. curvedLines is a plugin for flot, that tries to display lines in a smoother way.
The plugin is based on nergal.dev's work https://code.google.com/p/flot/issues/detail?id=226 This is achieved through adding of more data points. The plugin is a data processor and can thus be used
and further extended with a mode that forces the min/max points of the curves to be on the in combination with standard line / point rendering options.
points. Both modes are achieved through adding of more data points
=> 1) with large data sets you may get trouble => 1) with large data sets you may get trouble
=> 2) if you want to display the points too, you have to plot them as 2nd data series over the lines => 2) if you want to display the points too, you have to plot them as 2nd data series over the lines
&& 3) consecutive x data points are not allowed to have the same value && 3) consecutive x data points are not allowed to have the same value
This is version 0.5 of curvedLines so it will probably not work in every case. However
the basic form of use descirbed next works (:
Feel free to further improve the code Feel free to further improve the code
____________________________________________________ ____________________________________________________
...@@ -61,18 +57,21 @@ ...@@ -61,18 +57,21 @@
active: bool true => plugin can be used active: bool true => plugin can be used
apply: bool true => series will be drawn as curved line apply: bool true => series will be drawn as curved line
deprecated options from flot prior to 1.0.0:
------------------------------------------------
legacyOverride bool true => use old default OR
legacyOverride optionArray
{
fit: bool true => forces the max,mins of the curve to be on the datapoints fit: bool true => forces the max,mins of the curve to be on the datapoints
curvePointFactor int defines how many "virtual" points are used per "real" data point to curvePointFactor int defines how many "virtual" points are used per "real" data point to
emulate the curvedLines (points total = real points * curvePointFactor) emulate the curvedLines (points total = real points * curvePointFactor)
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. } to enforce the min max condition.
+ line options (since v0.5 curved lines use flots line implementation for drawing
=> line options like fill, show ... are supported out of the box)
*/ */
/* /*
* v0.1 initial commit * v0.1 initial commit
* v0.15 negative values should work now (outcommented a negative -> 0 hook hope it does no harm) * v0.15 negative values should work now (outcommented a negative -> 0 hook hope it does no harm)
* v0.2 added fill option (thanks to monemihir) and multi axis support (thanks to soewono effendi) * v0.2 added fill option (thanks to monemihir) and multi axis support (thanks to soewono effendi)
...@@ -82,19 +81,21 @@ ...@@ -82,19 +81,21 @@
* This change breakes existing code however CurvedLines are now just many tiny straight lines to flot and therefore all flot lines options (like gradient fill, * This change breakes existing code however CurvedLines are now just many tiny straight lines to flot and therefore all flot lines options (like gradient fill,
* shadow) are now supported out of the box * shadow) are now supported out of the box
* v0.6 flot 0.8 compatibility and some bug fixes * v0.6 flot 0.8 compatibility and some bug fixes
* v0.6.0 changed versioning schema * v0.6.x changed versioning schema
*
* v1.0.0 API Break marked existing implementation/options as deprecated
*/ */
(function($) { (function($) {
var options = { var options = {
series : { series : {
curvedLines : { curvedLines : {
active : false, active : false,
apply: false, apply : false,
fit : false, monotonicFit : false,
curvePointFactor : 20, tension : 0.0,
fitPointDist : undefined legacyOverride : undefined
} }
} }
}; };
...@@ -113,13 +114,14 @@ ...@@ -113,13 +114,14 @@
//only if the plugin is active //only if the plugin is active
function processDatapoints(plot, series, datapoints) { function processDatapoints(plot, series, datapoints) {
var nrPoints = datapoints.points.length / datapoints.pointsize; var nrPoints = datapoints.points.length / datapoints.pointsize;
var EPSILON = 0.5; //pretty large epsilon but save var EPSILON = 0.005;
if (series.curvedLines.apply == true && series.originSeries === undefined && nrPoints > (1 + EPSILON)) { if (series.curvedLines.apply == true && series.originSeries === undefined && nrPoints > (1 + EPSILON)) {
if (series.lines.fill) { if (series.lines.fill) {
var pointsTop = calculateCurvePoints(datapoints, series.curvedLines, 1) var pointsTop = calculateCurvePoints(datapoints, series.curvedLines, 1);
,pointsBottom = calculateCurvePoints(datapoints, series.curvedLines, 2); //flot makes sure for us that we've got a second y point if fill is true ! var pointsBottom = calculateCurvePoints(datapoints, series.curvedLines, 2);
//flot makes sure for us that we've got a second y point if fill is true !
//Merge top and bottom curve //Merge top and bottom curve
datapoints.pointsize = 3; datapoints.pointsize = 3;
...@@ -139,11 +141,11 @@ ...@@ -139,11 +141,11 @@
} else if (pointsTop[i] < pointsBottom[j]) { } else if (pointsTop[i] < pointsBottom[j]) {
datapoints.points[k] = pointsTop[i]; datapoints.points[k] = pointsTop[i];
datapoints.points[k + 1] = pointsTop[i + 1]; datapoints.points[k + 1] = pointsTop[i + 1];
datapoints.points[k + 2] = k > 0 ? datapoints.points[k-1] : null; datapoints.points[k + 2] = k > 0 ? datapoints.points[k - 1] : null;
i += ps; i += ps;
} else { } else {
datapoints.points[k] = pointsBottom[j]; datapoints.points[k] = pointsBottom[j];
datapoints.points[k + 1] = k > 1 ? datapoints.points[k-2] : null; datapoints.points[k + 1] = k > 1 ? datapoints.points[k - 2] : null;
datapoints.points[k + 2] = pointsBottom[j + 1]; datapoints.points[k + 2] = pointsBottom[j + 1];
j += ps; j += ps;
} }
...@@ -156,9 +158,24 @@ ...@@ -156,9 +158,24 @@
} }
} }
function calculateCurvePoints(datapoints, curvedLinesOptions, yPos) {
if (typeof curvedLinesOptions.legacyOverride != 'undefined' && curvedLinesOptions.legacyOverride != false) {
var defaultOptions = {
fit : false,
curvePointFactor : 20,
fitPointDist : undefined
};
var legacyOptions = jQuery.extend(defaultOptions, curvedLinesOptions.legacyOverride);
return calculateLegacyCurvePoints(datapoints, legacyOptions, yPos);
}
return datapoints.points;
}
//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(datapoints, curvedLinesOptions, yPos) { function calculateLegacyCurvePoints(datapoints, curvedLinesOptions, yPos) {
var points = datapoints.points, ps = datapoints.pointsize; var points = datapoints.points, ps = datapoints.pointsize;
var num = curvedLinesOptions.curvePointFactor * (points.length / ps); var num = curvedLinesOptions.curvePointFactor * (points.length / ps);
...@@ -175,11 +192,12 @@ ...@@ -175,11 +192,12 @@
//to have a max,min at the data point. //to have a max,min at the data point.
var fpDist; var fpDist;
if(typeof curvedLinesOptions.fitPointDist == 'undefined') { if ( typeof curvedLinesOptions.fitPointDist == 'undefined') {
//estimate it //estimate it
var minX = points[0]; var minX = points[0];
var maxX = points[points.length-ps]; var maxX = points[points.length - ps];
fpDist = (maxX - minX) / (500 * 100); //x range / (estimated pixel length of placeholder * factor) fpDist = (maxX - minX) / (500 * 100);
//x range / (estimated pixel length of placeholder * factor)
} else { } else {
//use user defined value //use user defined value
fpDist = curvedLinesOptions.fitPointDist; fpDist = curvedLinesOptions.fitPointDist;
...@@ -306,12 +324,13 @@ ...@@ -306,12 +324,13 @@
}//end init }//end init
$.plot.plugins.push({ $.plot.plugins.push({
init : init, init : init,
options : options, options : options,
name : 'curvedLines', name : 'curvedLines',
version : '0.6.4' version : '1.0.0'
}); });
})(jQuery); })(jQuery);
...@@ -121,10 +121,12 @@ ...@@ -121,10 +121,12 @@
}, },
curvedLines: { curvedLines: {
apply: true, apply: true,
legacyOverride: {
/*play with parameters (lines)*/ /*play with parameters (lines)*/
//fitPointDist: 86400, //fitPointDist: 86400,
//curvePointFactor: 200, //curvePointFactor: 200,
fit: true fit: true
}
}, },
clickable: false, clickable: false,
hoverable: false, hoverable: false,
...@@ -138,10 +140,12 @@ ...@@ -138,10 +140,12 @@
}, },
curvedLines: { curvedLines: {
apply: true, apply: true,
legacyOverride: {
/*play with parameters (points)*/ /*play with parameters (points)*/
//fitPointDist: 86400, //fitPointDist: 86400,
//curvePointFactor: 200, //curvePointFactor: 200,
fit: true fit: true
}
}, },
color:'#FF0000' color:'#FF0000'
}; };
......
...@@ -47,7 +47,11 @@ $(function() { ...@@ -47,7 +47,11 @@ $(function() {
], ],
{ {
series: { series: {
curvedLines: { active: true, fit: true} curvedLines: {
active: true,
apply: true,
legacyOverride : { fit: true }
}
} }
} }
); );
......
...@@ -50,6 +50,7 @@ ...@@ -50,6 +50,7 @@
}, },
curvedLines : { curvedLines : {
apply : true, apply : true,
legacyOverride: true
} }
}, { }, {
data : d1, data : d1,
...@@ -87,6 +88,7 @@ ...@@ -87,6 +88,7 @@
}, },
curvedLines : { curvedLines : {
apply : true, apply : true,
legacyOverride: true
} }
}, { }, {
data : d1, data : d1,
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
yaxis: { min:10, max: 90} yaxis: { min:10, max: 90}
}; };
$.plot($("#flotOrig"), [{data: d1, lines: { show: true, lineWidth: 3}, curvedLines: {apply: true}}, {data: d1, points: { show: true }}], options); $.plot($("#flotOrig"), [{data: d1, lines: { show: true, lineWidth: 3}, curvedLines: {apply: true, legacyOverride: true}}, {data: d1, points: { show: true }}], options);
}); });
</script> </script>
</body> </body>
......
...@@ -26,8 +26,8 @@ ...@@ -26,8 +26,8 @@
$.plot($("#fillAndMultiAxis"), $.plot($("#fillAndMultiAxis"),
[ [
{data: d1, lines: { show: true, fill: true, fillColor: "rgba(195, 195, 195, 0.4)", lineWidth: 3}, curvedLines: {apply: true}}, {data: d1, points: { show: true }}, {data: d1, lines: { show: true, fill: true, fillColor: "rgba(195, 195, 195, 0.4)", lineWidth: 3}, curvedLines: {apply: true, legacyOverride: true}}, {data: d1, points: { show: true }},
{data: d2, lines: { show: true, lineWidth: 3}, curvedLines: {apply: true}, yaxis:2}, {data: d2, points: { show: true }, yaxis:2} {data: d2, lines: { show: true, lineWidth: 3}, curvedLines: {apply: true, legacyOverride: true}, yaxis:2}, {data: d2, points: { show: true }, yaxis:2}
], options); ], options);
}); });
</script> </script>
......
...@@ -36,9 +36,13 @@ ...@@ -36,9 +36,13 @@
show : true, show : true,
lineWidth : 3 lineWidth : 3
}, },
curvedLines: {apply: true, curvedLines:
{
apply: true,
legacyOverride: {
fit : true, fit : true,
} }
}
}, { }, {
data : d1, data : d1,
points : { points : {
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
yaxis: { min:10, max: 45} yaxis: { min:10, max: 45}
}; };
$.plot($("#flotOrig"), [{data: d1, lines: { show: true, lineWidth: 3}, curvedLines: {apply: true}}, {data: d1, points: { show: true }}], options); $.plot($("#flotOrig"), [{data: d1, lines: { show: true, lineWidth: 3}, curvedLines: {apply: true, legacyOverride: true}}, {data: d1, points: { show: true }}], options);
}); });
</script> </script>
</body> </body>
......
...@@ -26,7 +26,8 @@ ...@@ -26,7 +26,8 @@
show : true show : true
}, },
curvedLines : { curvedLines : {
apply : true apply : true,
legacyOverride: true
} }
}, { }, {
data : d1, data : d1,
......
...@@ -41,7 +41,8 @@ ...@@ -41,7 +41,8 @@
lineWidth : 3 lineWidth : 3
}, },
curvedLines : { curvedLines : {
apply : true apply : true,
legacyOverride: true
} }
}, { }, {
data : d1, data : d1,
......
...@@ -50,7 +50,8 @@ $(function () { ...@@ -50,7 +50,8 @@ $(function () {
//general options //general options
var options = { var options = {
series: {curvedLines : { series: {curvedLines : {
active : true active : true,
legacyOverride: true
}, },
lines: { lineWidth: 5}, lines: { lineWidth: 5},
points: { radius: 4 } points: { radius: 4 }
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
yaxis: { min:0, max: 10} yaxis: { min:0, max: 10}
}; };
$.plot($("#flotOrig"), [{data: d1, lines: { show: true, lineWidth: 3}, curvedLines: {apply: true}}, {data: d1, points: { show: true }}], options); $.plot($("#flotOrig"), [{data: d1, lines: { show: true, lineWidth: 3}, curvedLines: {apply: true, legacyOverride: true}}, {data: d1, points: { show: true }}], options);
}); });
</script> </script>
</body> </body>
......
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