Commit 46ab621f authored by MichaelZinsmaier's avatar MichaelZinsmaier

formatting

parent 64c5b94e
/* The MIT License /* The MIT License
Copyright (c) 2011 by Michael Zinsmaier and nergal.dev Copyright (c) 2011 by Michael Zinsmaier and nergal.dev
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
*/ */
/* /*
____________________________________________________ ____________________________________________________
what it is:
____________________________________________________
curvedLines is a plugin for flot, that tries to display lines in a smoother way. what it is:
The plugin is based on nergal.dev's work https://code.google.com/p/flot/issues/detail?id=226 ____________________________________________________
and further extended with a mode that forces the min/max points of the curves to be on the
points. Both modes are achieved through adding of more data points
=> 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
This is version 0.1 of curvedLines so it will probably not work in every case. However curvedLines is a plugin for flot, that tries to display lines in a smoother way.
the basic form of use descirbed next works (: The plugin is based on nergal.dev's work https://code.google.com/p/flot/issues/detail?id=226
and further extended with a mode that forces the min/max points of the curves to be on the
points. Both modes are achieved through adding of more data points
=> 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
Feel free to further improve the code This is version 0.1 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
how to use it: ____________________________________________________
____________________________________________________
how to use it:
____________________________________________________
var d1 = [[5,5],[7,3],[9,12]]; var d1 = [[5,5],[7,3],[9,12]];
var options = { series: { curvedLines: { active: true }}}; var options = { series: { curvedLines: { active: true }}};
$.plot($("#placeholder"), [{data = d1, curvedLines: { show: true}}], options); $.plot($("#placeholder"), [{data = d1, curvedLines: { show: true}}], options);
_____________________________________________________ _____________________________________________________
options: options:
_____________________________________________________ _____________________________________________________
fill: bool true => lines get filled fill: bool true => lines get filled
fillColor: null or the color that should be used for filling fillColor: null or the color that should be used for filling
...@@ -71,8 +68,7 @@ _____________________________________________________ ...@@ -71,8 +68,7 @@ _____________________________________________________
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)
*/
*/
/* /*
* v0.1 initial commit * v0.1 initial commit
...@@ -82,33 +78,34 @@ _____________________________________________________ ...@@ -82,33 +78,34 @@ _____________________________________________________
* *
*/ */
(function ($) { (function($) {
var options = {
series : {
var options = { series: { curvedLines: { active: false, curvedLines : {
show: false, active : false,
fit: false, show : false,
fill: false, fit : false,
fillColor: null, fill : false,
lineWidth: 2, fillColor : null,
curvePointFactor: 20, lineWidth : 2,
fitPointDist: 0.0001 curvePointFactor : 20,
}}}; fitPointDist : 0.0001
}
}
};
function init(plot) { function init(plot) {
plot.hooks.processOptions.push(processOptions); plot.hooks.processOptions.push(processOptions);
//if the plugin is active register draw method //if the plugin is active register draw method
function processOptions(plot,options) { function processOptions(plot, options) {
if (options.series.curvedLines.active) { if (options.series.curvedLines.active) {
plot.hooks.draw.push(draw); plot.hooks.draw.push(draw);
} }
} }
//select the data sets that should be drawn with curved lines and draws them //select the data sets that should be drawn with curved lines and draws them
function draw(plot, ctx) { function draw(plot, ctx) {
var series; var series;
...@@ -126,7 +123,7 @@ _____________________________________________________ ...@@ -126,7 +123,7 @@ _____________________________________________________
ctx.translate(offset.left, offset.top); ctx.translate(offset.left, offset.top);
ctx.lineJoin = "round"; ctx.lineJoin = "round";
ctx.strokeStyle = series.color; ctx.strokeStyle = series.color;
if(series.curvedLines.fill) { if (series.curvedLines.fill) {
var fillColor = series.curvedLines.fillColor == null ? series.color : series.curvedLines.fillColor; var fillColor = series.curvedLines.fillColor == null ? series.color : series.curvedLines.fillColor;
var c = $.color.parse(fillColor); var c = $.color.parse(fillColor);
c.a = typeof fill == "number" ? fill : 0.4; c.a = typeof fill == "number" ? fill : 0.4;
...@@ -142,7 +139,6 @@ _____________________________________________________ ...@@ -142,7 +139,6 @@ _____________________________________________________
} }
} }
//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) {
...@@ -164,12 +160,12 @@ _____________________________________________________ ...@@ -164,12 +160,12 @@ _____________________________________________________
// clip with ymin // clip with ymin
if (y1 <= y2 && y1 < axisy.min) { if (y1 <= y2 && y1 < axisy.min) {
if (y2 < axisy.min) if (y2 < axisy.min)
continue; // line segment is outside continue;
// line segment is outside
// compute new intersection point // compute new intersection point
x1 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1; x1 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
y1 = axisy.min; y1 = axisy.min;
} } else if (y2 <= y1 && y2 < axisy.min) {
else if (y2 <= y1 && y2 < axisy.min) {
if (y1 < axisy.min) if (y1 < axisy.min)
continue; continue;
x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1; x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
...@@ -182,8 +178,7 @@ _____________________________________________________ ...@@ -182,8 +178,7 @@ _____________________________________________________
continue; continue;
x1 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1; x1 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
y1 = axisy.max; y1 = axisy.max;
} } else if (y2 >= y1 && y2 > axisy.max) {
else if (y2 >= y1 && y2 > axisy.max) {
if (y1 > axisy.max) if (y1 > axisy.max)
continue; continue;
x2 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1; x2 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
...@@ -196,8 +191,7 @@ _____________________________________________________ ...@@ -196,8 +191,7 @@ _____________________________________________________
continue; continue;
y1 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1; y1 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
x1 = axisx.min; x1 = axisx.min;
} } else if (x2 <= x1 && x2 < axisx.min) {
else if (x2 <= x1 && x2 < axisx.min) {
if (x1 < axisx.min) if (x1 < axisx.min)
continue; continue;
y2 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1; y2 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
...@@ -210,8 +204,7 @@ _____________________________________________________ ...@@ -210,8 +204,7 @@ _____________________________________________________
continue; continue;
y1 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1; y1 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
x1 = axisx.max; x1 = axisx.max;
} } else if (x2 >= x1 && x2 > axisx.max) {
else if (x2 >= x1 && x2 > axisx.max) {
if (x1 > axisx.max) if (x1 > axisx.max)
continue; continue;
y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1; y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
...@@ -237,7 +230,6 @@ _____________________________________________________ ...@@ -237,7 +230,6 @@ _____________________________________________________
ctx.stroke(); ctx.stroke();
} }
//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
//I don't understand what nergal computes here and to be honest I didn't even try //I don't understand what nergal computes here and to be honest I didn't even try
function calculateCurvePoints(data, curvedLinesOptions) { function calculateCurvePoints(data, curvedLinesOptions) {
...@@ -250,30 +242,28 @@ _____________________________________________________ ...@@ -250,30 +242,28 @@ _____________________________________________________
//insert a point before and after the "real" data point to force the line //insert a point before and after the "real" data point to force the line
//to have a max,min at the data point //to have a max,min at the data point
var neigh = curvedLinesOptions.fitPointDist; var neigh = curvedLinesOptions.fitPointDist;
var j=0; var j = 0;
for(var i = 0; i < data.length; i++) { for (var i = 0; i < data.length; i++) {
//smooth front //smooth front
xdata[j] = data[i][0]-0.1; xdata[j] = data[i][0] - 0.1;
if (i > 0) { if (i > 0) {
ydata[j] = data[i-1][1]*neigh + data[i][1] * (1 - neigh); ydata[j] = data[i-1][1] * neigh + data[i][1] * (1 - neigh);
} else { } else {
ydata[j] = data[i][1]; ydata[j] = data[i][1];
} }
j++; j++;
xdata[j] = data[i][0]; xdata[j] = data[i][0];
ydata[j] = data[i][1]; ydata[j] = data[i][1];
j++; j++;
//smooth back //smooth back
xdata[j] = data[i][0]+0.1; xdata[j] = data[i][0] + 0.1;
if ((i+1) < data.length) { if ((i + 1) < data.length) {
ydata[j] = data[i+1][1]*neigh + data[i][1] * (1 - neigh); ydata[j] = data[i+1][1] * neigh + data[i][1] * (1 - neigh);
} else { } else {
ydata[j] = data[i][1]; ydata[j] = data[i][1];
} }
...@@ -282,7 +272,7 @@ _____________________________________________________ ...@@ -282,7 +272,7 @@ _____________________________________________________
} }
} else { } else {
//just use the datapoints //just use the datapoints
for(var i = 0; i < data.length; i++) { for (var i = 0; i < data.length; i++) {
xdata[i] = data[i][0]; xdata[i] = data[i][0];
ydata[i] = data[i][1]; ydata[i] = data[i][1];
} }
...@@ -296,9 +286,9 @@ _____________________________________________________ ...@@ -296,9 +286,9 @@ _____________________________________________________
y2[n - 1] = 0; y2[n - 1] = 0;
delta[0] = 0; delta[0] = 0;
for(var i = 1; i < n - 1; ++i) { for (var i = 1; i < n - 1; ++i) {
var d = (xdata[i + 1] - xdata[i - 1]); var d = (xdata[i + 1] - xdata[i - 1]);
if(d == 0) { if (d == 0) {
return null; return null;
} }
...@@ -309,7 +299,7 @@ _____________________________________________________ ...@@ -309,7 +299,7 @@ _____________________________________________________
delta[i] = (6 * delta[i] / (xdata[i + 1] - xdata[i - 1]) - s * delta[i - 1]) / p; delta[i] = (6 * delta[i] / (xdata[i + 1] - xdata[i - 1]) - s * delta[i - 1]) / p;
} }
for(var j = n - 2; j >= 0; --j) { for (var j = n - 2; j >= 0; --j) {
y2[j] = y2[j] * y2[j + 1] + delta[j]; y2[j] = y2[j] * y2[j + 1] + delta[j];
} }
...@@ -322,16 +312,15 @@ _____________________________________________________ ...@@ -322,16 +312,15 @@ _____________________________________________________
xnew[0] = xdata[0]; xnew[0] = xdata[0];
ynew[0] = ydata[0]; ynew[0] = ydata[0];
for ( j = 1; j < num; ++j) {
for(j = 1; j < num; ++j) {
xnew[j] = xnew[0] + j * step; xnew[j] = xnew[0] + j * step;
var max = n - 1; var max = n - 1;
var min = 0; var min = 0;
while(max - min > 1) { while (max - min > 1) {
var k = Math.round((max + min) / 2); var k = Math.round((max + min) / 2);
if(xdata[k] > xnew[j]) { if (xdata[k] > xnew[j]) {
max = k; max = k;
} else { } else {
min = k; min = k;
...@@ -340,7 +329,7 @@ _____________________________________________________ ...@@ -340,7 +329,7 @@ _____________________________________________________
var h = (xdata[max] - xdata[min]); var h = (xdata[max] - xdata[min]);
if(h == 0) { if (h == 0) {
return null; return null;
} }
...@@ -348,9 +337,9 @@ _____________________________________________________ ...@@ -348,9 +337,9 @@ _____________________________________________________
var b = (xnew[j] - xdata[min]) / h; var b = (xnew[j] - xdata[min]) / h;
ynew[j] = a * ydata[min] + b * ydata[max] + ((a * a * a - a) * y2[min] + (b * b * b - b) * y2[max]) * (h * h) / 6; ynew[j] = a * ydata[min] + b * ydata[max] + ((a * a * a - a) * y2[min] + (b * b * b - b) * y2[max]) * (h * h) / 6;
// if (ynew[j] < 0.01){ // if (ynew[j] < 0.01){
// ynew[j] = 0; // ynew[j] = 0;
// } // }
result.push(xnew[j]); result.push(xnew[j]);
result.push(ynew[j]); result.push(ynew[j]);
} }
...@@ -358,20 +347,15 @@ _____________________________________________________ ...@@ -358,20 +347,15 @@ _____________________________________________________
return result; return result;
} }
}//end init }//end init
$.plot.plugins.push({ $.plot.plugins.push({
init: init, init : init,
options: options, options : options,
name: 'curvedLines', name : 'curvedLines',
version: '0.2' version : '0.2'
}); });
})(jQuery); })(jQuery);
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