forked from ubermajestix/flot-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.flot.bubble.js
415 lines (347 loc) · 14.6 KB
/
jquery.flot.bubble.js
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
Flot plugin for bubble charting
To activate bubbles:
plot = $.plot($("#placeholder"), [
// data needs to be in the form [x,y,z] where z is the radius of the point
{ data: [[1,2,10],[4,6,8],[3,1,4]], color: '#A1FDFF', label: 'boom' }
],
{
series: {
bubble: true, // set this to true to activate bubbles, otherwise your data will be plotted as points
points: { show: true }, // bubbles will only work with point charts, the plugin will NOT attempt to bubble a bar chart
lines: { show: true } // the bubble plugin supports connecting bubbles in a series with lines
},
}
);
*/
(function ($) {
var options = {
series: {bubble: null}
};
function init(plot) {
function log(){
if(window.console) {
console.log.apply(console, arguments);
}
}
function drawSeriesBubblePoints(series, ctx) {
function plotPoints(datapoints, radius, fillStyle, offset, circumference, axisx, axisy, rawData) {
var points = datapoints.points, ps = datapoints.pointsize;
for (var i = 0; i < points.length; i += ps) {
var x = points[i], y = points[i + 1];
if (x == null || x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max)
continue;
var radius_index = i/2,
radius = rawData[radius_index][2]
ctx.beginPath();
var options = plot.getOptions();
if (options.series.bubble_options && options.series.bubble_options.occupancy){
ctx.arc(axisx.p2c(x), axisy.p2c(y) + (options.series.bubble_options.radius_offset - radius), radius, 0, circumference, false);
} else {
ctx.arc(axisx.p2c(x), axisy.p2c(y) + offset, radius, 0, circumference, false);
}
if (fillStyle) {
ctx.fillStyle = fillStyle;
ctx.fill();
}
ctx.stroke();
}
}
ctx.save();
var plotOffset = plot.getPlotOffset()
ctx.translate(plotOffset.left, plotOffset.top);
var lw = series.lines.lineWidth,
sw = series.shadowSize,
radius = series.points.radius;
if (lw > 0 && sw > 0) {
// draw shadow in two steps
var w = sw / 2;
ctx.lineWidth = w;
ctx.strokeStyle = "rgba(0,0,0,0.1)";
plotPoints(series.datapoints, radius, null, w + w/2, Math.PI,
series.xaxis, series.yaxis, series.data);
ctx.strokeStyle = "rgba(0,0,0,0.2)";
plotPoints(series.datapoints, radius, null, w/2, Math.PI,
series.xaxis, series.yaxis, series.data);
}
ctx.lineWidth = lw;
ctx.strokeStyle = series.color;
plotPoints(series.datapoints, radius,
getFillStyle(series.points, series.color), 0, 2 * Math.PI,
series.xaxis, series.yaxis, series.data);
ctx.restore();
}
//duped function from flot
function getFillStyle(filloptions, seriesColor, bottom, top) {
var fill = filloptions.fill;
if (!fill)
return null;
if (filloptions.fillColor)
//fill color is same as series
// TODO allow to specify fillColor?
return getColorOrGradient(seriesColor, bottom, top, seriesColor);
var c = $.color.parse(seriesColor);
c.a = typeof fill == "number" ? fill : 0.4;
c.normalize();
return c.toString();
}
//duped function from flot
function getColorOrGradient(spec, bottom, top, defaultColor) {
if (typeof spec == "string")
return spec;
else {
// assume this is a gradient spec; IE currently only
// supports a simple vertical gradient properly, so that's
// what we support too
var gradient = ctx.createLinearGradient(0, top, 0, bottom);
for (var i = 0, l = spec.colors.length; i < l; ++i) {
var c = spec.colors[i];
if (typeof c != "string") {
var co = $.color.parse(defaultColor);
if (c.brightness != null)
co = co.scale('rgb', c.brightness)
if (c.opacity != null)
co.a *= c.opacity;
c = co.toString();
}
gradient.addColorStop(i / (l - 1), c);
}
return gradient;
}
}
var highlights = []
function onMouseMove(e) {
triggerClickHoverEvent("plothover", e,
function (s) { return s["hoverable"] != false; });
}
function onClick(e) {
triggerClickHoverEvent("plotclick", e,
function (s) { return s["clickable"] != false; });
}
function triggerClickHoverEvent(eventname, event, seriesFilter) {
var offset = plot.bubble.eventHolder.offset(),
canvasX = event.pageX - offset.left - plot.getPlotOffset().left,
canvasY = event.pageY - offset.top - plot.getPlotOffset().top,
pos = plot.c2p({ left: canvasX, top: canvasY });
pos.pageX = event.pageX;
pos.pageY = event.pageY;
var item = findNearbyItem(canvasX, canvasY, seriesFilter);
if (item) {
// fill in mouse pos for any listeners out there
item.pageX = parseInt(item.series.xaxis.p2c(item.datapoint[0]) + offset.left + plot.getPlotOffset().left);
item.pageY = parseInt(item.series.yaxis.p2c(item.datapoint[1]) + offset.top + plot.getPlotOffset().top);
}
if (plot.getOptions().grid.autoHighlight) {
// clear auto-highlights
for (var i = 0; i < highlights.length; ++i) {
var h = highlights[i];
if (h.auto == eventname &&
!(item && h.series == item.series && h.point == item.datapoint))
unhighlight(h.series, h.point);
}
if (item)
highlight(item.series, item.datapoint, eventname);
}
plot.getPlaceholder().trigger(eventname, [ pos, item ]);
}
// returns the data item the mouse is over, or null if none is found
function findNearbyItem(mouseX, mouseY, seriesFilter) {
var maxDistance = plot.getOptions().grid.mouseActiveRadius,
smallestDistance = maxDistance * maxDistance + 1,
item = null, foundPoint = false, i, j;
// log("nearby. smallDis: ", smallestDistance)
var series = plot.getData();
for (i = series.length - 1; i >= 0; --i) {
if (!seriesFilter(series[i]))
continue;
var s = series[i],
axisx = s.xaxis,
axisy = s.yaxis,
points = s.datapoints.points,
ps = s.datapoints.pointsize,
mx = axisx.c2p(mouseX), // precompute some stuff to make the loop faster
my = axisy.c2p(mouseY);
// maxx = maxDistance / axisx.scale,
//maxy = maxDistance / axisy.scale;
if(s.lines.show || s.points.show) {
for (j = 0; j < points.length; j += ps) {
var x = points[j], y = points[j + 1];
if (x == null)
continue;
// TODO this is really slow b/c its doing array traversal to get the radius
// then sets the distances for every point on every hover
var newmaxDistance = radiusAtPoint(s, [x,y]),
// log("new max", newmaxDistance * newmaxDistance + 1)
newSmallDist = newmaxDistance * newmaxDistance + 1,
// For points and lines, the cursor must be within a
// certain distance to the data point
maxx = newmaxDistance / axisx.scale,
maxy = newmaxDistance / axisy.scale;
if (x - mx > maxx || x - mx < -maxx ||
y - my > maxy || y - my < -maxy)
continue;
// We have to calculate distances in pixels, not in
// data units, because the scales of the axes may be different
var dx = Math.abs(axisx.p2c(x) - mouseX),
dy = Math.abs(axisy.p2c(y) - mouseY),
dist = dx * dx + dy * dy; // we save the sqrt
// use <= to ensure last point takes precedence
// (last generally means on top of)
// log('dist: ', dist < newSmallDist)
// if (dist < smallestDistance) {
// smallestDistance = dist;
if (dist < newSmallDist) {
newSmallDist = dist;
item = [i, j / ps];
}
}
}
if (s.bars.show && !item) { // no other point can be nearby
var barLeft = s.bars.align == "left" ? 0 : -s.bars.barWidth/2,
barRight = barLeft + s.bars.barWidth;
for (j = 0; j < points.length; j += ps) {
var x = points[j], y = points[j + 1], b = points[j + 2];
if (x == null)
continue;
// for a bar graph, the cursor must be inside the bar
if (series[i].bars.horizontal ?
(mx <= Math.max(b, x) && mx >= Math.min(b, x) &&
my >= y + barLeft && my <= y + barRight) :
(mx >= x + barLeft && mx <= x + barRight &&
my >= Math.min(b, y) && my <= Math.max(b, y)))
item = [i, j / ps];
}
}
}
if (item) {
i = item[0];
j = item[1];
var ps = series[i].datapoints.pointsize;
return { datapoint: series[i].datapoints.points.slice(j * ps, (j + 1) * ps),
dataIndex: j,
series: series[i],
seriesIndex: i };
}
return null;
}
function highlight(s, point, auto) {
if (typeof s == "number")
s = series[s];
if (typeof point == "number")
point = s.data[point];
var i = indexOfHighlight(s, point);
if (i == -1) {
highlights.push({ series: s, point: point, auto: auto });
plot.triggerRedrawOverlay();
}
else if (!auto)
highlights[i].auto = false;
}
function unhighlight(s, point) {
if (s == null && point == null) {
highlights = [];
triggerRedrawOverlay();
}
if (typeof s == "number")
s = series[s];
if (typeof point == "number")
point = s.data[point];
var i = indexOfHighlight(s, point);
if (i != -1) {
highlights.splice(i, 1);
plot.triggerRedrawOverlay();
}
}
function indexOfHighlight(s, p) {
for (var i = 0; i < highlights.length; ++i) {
var h = highlights[i];
if (h.series == s && h.point[0] == p[0]
&& h.point[1] == p[1])
return i;
}
return -1;
}
function drawPointHighlight(series, point) {
var x = point[0], y = point[1], axisx = series.xaxis, axisy = series.yaxis;
if (x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max)
return;
var octx = plot.bubble.octx,
bubble_radius = radiusAtPoint(series, point),
radius = bubble_radius + series.points.lineWidth / 2,
plotOffset = plot.getPlotOffset();
octx.lineWidth = series.points.lineWidth *5.5 ;
octx.strokeStyle = $.color.parse(series.color).scale('a', 0.4).toString();
octx.beginPath();
octx.arc(axisx.p2c(x) + plotOffset.left, axisy.p2c(y) + plotOffset.top, radius, 0, 2 * Math.PI, false)
octx.stroke();
}
function axisSpecToRealAxis(obj, attr) {
var a = obj[attr];
if (!a || a == 1)
return axes[attr];
if (typeof a == "number")
return axes[attr.charAt(0) + a + attr.slice(1)];
return a; // assume it's OK
}
//given a series and a point returns the radius defined in the dataset
function radiusAtPoint(series, point){
var points = series.datapoints.points, ps = series.datapoints.pointsize;
for (var i = points.length; i > 1; i -= ps) { // walk back to return the last (top) hit
var x = points[i - 2], y = points[i - 1];
// if(i > 0)
// radius_index = i/2
if(point[0] == x && point[1] == y) {
var radius_index = (i - 2) / 2;
return series.data[radius_index][2];
}
// log(radius_array, radius_array[radius_index])
// log(i,[x,y], radius_index)
}
// log("point: ", point, " radius: ", bubble_radius)
return 0;
}
// bind hoverable events
function bindEvents(plot, eventHolder)
{
plot.bubble.eventHolder = eventHolder;
var options = plot.getOptions();
if (weShouldBubble() && options.grid.hoverable)
eventHolder.unbind('mousemove').mousemove(onMouseMove);
if (options.series.bubble && options.grid.clickable)
eventHolder.unbind('click').click(onClick);
}
function weShouldBubble() {
// log('should we bubble? ', !!plot.getOptions().series.bubble && !!plot.getOptions().series.points.show)
return !! plot.getOptions().series.bubble && !!plot.getOptions().series.points.show
}
function blowBubbles(plot, ctx) {
if (!weShouldBubble())
return;
var series = plot.getData()
$.each(series, function(index, s) {
drawSeriesBubblePoints(s, ctx)
})
}
function blowHighlights(plot, octx) {
plot.bubble.octx = octx
if (!weShouldBubble())
return;
$.each(highlights, function(index, hi){
drawPointHighlight(hi.series, hi.point);
});
}
plot.bubble = {
eventHolder: null,
octx: null
}
plot.hooks.bindEvents.push(bindEvents);
plot.hooks.draw.push(blowBubbles);
plot.hooks.drawOverlay.push(blowHighlights);
}
$.plot.plugins.push({
init: init,
options: options,
name: 'bubble',
version: '0.1'
});
})(jQuery);