jquery.flot.spline.js
6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* Flot plugin that provides spline interpolation for line graphs
* author: Alex Bardas < alex.bardas@gmail.com >
* modified by: Avi Kohn https://github.com/AMKohn
* based on the spline interpolation described at:
* http://scaledinnovation.com/analytics/splines/aboutSplines.html
*
* Example usage: (add in plot options series object)
* for linespline:
* series: {
* ...
* lines: {
* show: false
* },
* splines: {
* show: true,
* tension: x, (float between 0 and 1, defaults to 0.5),
* lineWidth: y (number, defaults to 2),
* fill: z (float between 0 .. 1 or false, as in flot documentation)
* },
* ...
* }
* areaspline:
* series: {
* ...
* lines: {
* show: true,
* lineWidth: 0, (line drawing will not execute)
* fill: x, (float between 0 .. 1, as in flot documentation)
* ...
* },
* splines: {
* show: true,
* tension: 0.5 (float between 0 and 1)
* },
* ...
* }
*
*/
(function($) {
'use strict'
/**
* @param {Number} x0, y0, x1, y1: coordinates of the end (knot) points of the segment
* @param {Number} x2, y2: the next knot (not connected, but needed to calculate p2)
* @param {Number} tension: control how far the control points spread
* @return {Array}: p1 -> control point, from x1 back toward x0
* p2 -> the next control point, returned to become the next segment's p1
*
* @api private
*/
function getControlPoints(x0, y0, x1, y1, x2, y2, tension) {
var pow = Math.pow,
sqrt = Math.sqrt,
d01, d12, fa, fb, p1x, p1y, p2x, p2y;
// Scaling factors: distances from this knot to the previous and following knots.
d01 = sqrt(pow(x1 - x0, 2) + pow(y1 - y0, 2));
d12 = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
fa = tension * d01 / (d01 + d12);
fb = tension - fa;
p1x = x1 + fa * (x0 - x2);
p1y = y1 + fa * (y0 - y2);
p2x = x1 - fb * (x0 - x2);
p2y = y1 - fb * (y0 - y2);
return [p1x, p1y, p2x, p2y];
}
var line = [];
function drawLine(points, ctx, height, fill, seriesColor) {
var c = $.color.parse(seriesColor);
c.a = typeof fill == "number" ? fill : .3;
c.normalize();
c = c.toString();
ctx.beginPath();
ctx.moveTo(points[0][0], points[0][1]);
var plength = points.length;
for (var i = 0; i < plength; i++) {
ctx[points[i][3]].apply(ctx, points[i][2]);
}
ctx.stroke();
ctx.lineWidth = 0;
ctx.lineTo(points[plength - 1][0], height);
ctx.lineTo(points[0][0], height);
ctx.closePath();
if (fill !== false) {
ctx.fillStyle = c;
ctx.fill();
}
}
/**
* @param {Object} ctx: canvas context
* @param {String} type: accepted strings: 'bezier' or 'quadratic' (defaults to quadratic)
* @param {Array} points: 2 points for which to draw the interpolation
* @param {Array} cpoints: control points for those segment points
*
* @api private
*/
function queue(ctx, type, points, cpoints) {
if (type === void 0 || (type !== 'bezier' && type !== 'quadratic')) {
type = 'quadratic';
}
type = type + 'CurveTo';
if (line.length == 0) line.push([points[0], points[1], cpoints.concat(points.slice(2)), type]);
else if (type == "quadraticCurveTo" && points.length == 2) {
cpoints = cpoints.slice(0, 2).concat(points);
line.push([points[0], points[1], cpoints, type]);
}
else line.push([points[2], points[3], cpoints.concat(points.slice(2)), type]);
}
/**
* @param {Object} plot
* @param {Object} ctx: canvas context
* @param {Object} series
*
* @api private
*/
function drawSpline(plot, ctx, series) {
// Not interested if spline is not requested
if (series.splines.show !== true) {
return;
}
var cp = [],
// array of control points
tension = series.splines.tension || 0.5,
idx, x, y, points = series.datapoints.points,
ps = series.datapoints.pointsize,
plotOffset = plot.getPlotOffset(),
len = points.length,
pts = [];
line = [];
// Cannot display a linespline/areaspline if there are less than 3 points
if (len / ps < 4) {
$.extend(series.lines, series.splines);
return;
}
for (idx = 0; idx < len; idx += ps) {
x = points[idx];
y = points[idx + 1];
if (x == null || x < series.xaxis.min || x > series.xaxis.max || y < series.yaxis.min || y > series.yaxis.max) {
continue;
}
pts.push(series.xaxis.p2c(x) + plotOffset.left, series.yaxis.p2c(y) + plotOffset.top);
}
len = pts.length;
// Draw an open curve, not connected at the ends
for (idx = 0; idx < len - 2; idx += 2) {
cp = cp.concat(getControlPoints.apply(this, pts.slice(idx, idx + 6).concat([tension])));
}
ctx.save();
ctx.strokeStyle = series.color;
ctx.lineWidth = series.splines.lineWidth;
queue(ctx, 'quadratic', pts.slice(0, 4), cp.slice(0, 2));
for (idx = 2; idx < len - 3; idx += 2) {
queue(ctx, 'bezier', pts.slice(idx, idx + 4), cp.slice(2 * idx - 2, 2 * idx + 2));
}
queue(ctx, 'quadratic', pts.slice(len - 2, len), [cp[2 * len - 10], cp[2 * len - 9], pts[len - 4], pts[len - 3]]);
drawLine(line, ctx, plot.height() + 10, series.splines.fill, series.color);
ctx.restore();
}
$.plot.plugins.push({
init: function(plot) {
plot.hooks.drawSeries.push(drawSpline);
},
options: {
series: {
splines: {
show: false,
lineWidth: 2,
tension: 0.5,
fill: false
}
}
},
name: 'spline',
version: '0.8.2'
});
})(jQuery);