53 function getControlPoints(x0, y0, x1, y1, x2, y2, tension) {
57 d01, d12, fa, fb, p1x, p1y, p2x, p2y;
60 d01 = sqrt(pow(x1 - x0, 2) + pow(y1 - y0, 2));
61 d12 = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
63 fa = tension * d01 / (d01 + d12);
66 p1x = x1 + fa * (x0 - x2);
67 p1y = y1 + fa * (y0 - y2);
69 p2x = x1 - fb * (x0 - x2);
70 p2y = y1 - fb * (y0 - y2);
72 return [p1x, p1y, p2x, p2y];
77 function drawLine(points, ctx, height, fill, seriesColor) {
78 var c = $.color.parse(seriesColor);
80 c.a = typeof fill ==
"number" ? fill : .3;
85 ctx.moveTo(points[0][0], points[0][1]);
87 var plength = points.length;
89 for (var i = 0; i < plength; i++) {
90 ctx[points[i][3]].apply(ctx, points[i][2]);
96 ctx.lineTo(points[plength - 1][0], height);
97 ctx.lineTo(points[0][0], height);
101 if (fill !==
false) {
115 function queue(ctx, type, points, cpoints) {
116 if (type ===
void 0 || (type !==
'bezier' && type !==
'quadratic')) {
119 type = type +
'CurveTo';
121 if (line.length == 0) line.push([points[0], points[1], cpoints.concat(points.slice(2)), type]);
122 else if (type ==
"quadraticCurveTo" && points.length == 2) {
123 cpoints = cpoints.slice(0, 2).concat(points);
125 line.push([points[0], points[1], cpoints, type]);
127 else line.push([points[2], points[3], cpoints.concat(points.slice(2)), type]);
138 function drawSpline(plot, ctx, series) {
140 if (series.splines.show !==
true) {
146 tension = series.splines.tension || 0.5,
147 idx, x, y, points = series.datapoints.points,
148 ps = series.datapoints.pointsize,
149 plotOffset = plot.getPlotOffset(),
157 $.extend(series.lines, series.splines);
161 for (idx = 0; idx < len; idx += ps) {
164 if (x == null || x < series.xaxis.min || x > series.xaxis.max || y < series.yaxis.min || y > series.yaxis.max) {
168 pts.push(series.xaxis.p2c(x) + plotOffset.left, series.yaxis.p2c(y) + plotOffset.top);
174 for (idx = 0; idx < len - 2; idx += 2) {
175 cp = cp.concat(getControlPoints.apply(
this, pts.slice(idx, idx + 6).concat([tension])));
179 ctx.strokeStyle = series.color;
180 ctx.lineWidth = series.splines.lineWidth;
182 queue(ctx,
'quadratic', pts.slice(0, 4), cp.slice(0, 2));
184 for (idx = 2; idx < len - 3; idx += 2) {
185 queue(ctx,
'bezier', pts.slice(idx, idx + 4), cp.slice(2 * idx - 2, 2 * idx + 2));
188 queue(ctx,
'quadratic', pts.slice(len - 2, len), [cp[2 * len - 10], cp[2 * len - 9], pts[len - 4], pts[len - 3]]);
190 drawLine(line, ctx, plot.height() + 10, series.splines.fill, series.color);
195 $.plot.plugins.push({
196 init:
function(plot) {
197 plot.hooks.drawSeries.push(drawSpline);