| 1 |
/* |
| 2 |
Flot plugin that adds some extra symbols for plotting points. |
| 3 |
|
| 4 |
The symbols are accessed as strings through the standard symbol |
| 5 |
choice: |
| 6 |
|
| 7 |
series: { |
| 8 |
points: { |
| 9 |
symbol: "square" // or "diamond", "triangle", "cross" |
| 10 |
} |
| 11 |
} |
| 12 |
|
| 13 |
*/ |
| 14 |
|
| 15 |
(function ($) { |
| 16 |
function processRawData(plot, series, datapoints) { |
| 17 |
// we normalize the area of each symbol so it is approximately the |
| 18 |
// same as a circle of the given radius |
| 19 |
|
| 20 |
var handlers = { |
| 21 |
square: function (ctx, x, y, radius, shadow) { |
| 22 |
// pi * r^2 = (2s)^2 => s = r * sqrt(pi)/2 |
| 23 |
var size = radius * Math.sqrt(Math.PI) / 2; |
| 24 |
ctx.rect(x - size, y - size, size + size, size + size); |
| 25 |
}, |
| 26 |
diamond: function (ctx, x, y, radius, shadow) { |
| 27 |
// pi * r^2 = 2s^2 => s = r * sqrt(pi/2) |
| 28 |
var size = radius * Math.sqrt(Math.PI / 2); |
| 29 |
ctx.moveTo(x - size, y); |
| 30 |
ctx.lineTo(x, y - size); |
| 31 |
ctx.lineTo(x + size, y); |
| 32 |
ctx.lineTo(x, y + size); |
| 33 |
ctx.lineTo(x - size, y); |
| 34 |
}, |
| 35 |
triangle: function (ctx, x, y, radius, shadow) { |
| 36 |
// pi * r^2 = 1/2 * s^2 * sin (pi / 3) => s = r * sqrt(2 * pi / sin(pi / 3)) |
| 37 |
var size = radius * Math.sqrt(2 * Math.PI / Math.sin(Math.PI / 3)); |
| 38 |
var height = size * Math.sin(Math.PI / 3); |
| 39 |
ctx.moveTo(x - size/2, y + height/2); |
| 40 |
ctx.lineTo(x + size/2, y + height/2); |
| 41 |
if (!shadow) { |
| 42 |
ctx.lineTo(x, y - height/2); |
| 43 |
ctx.lineTo(x - size/2, y + height/2); |
| 44 |
} |
| 45 |
}, |
| 46 |
cross: function (ctx, x, y, radius, shadow) { |
| 47 |
// pi * r^2 = (2s)^2 => s = r * sqrt(pi)/2 |
| 48 |
var size = radius * Math.sqrt(Math.PI) / 2; |
| 49 |
ctx.moveTo(x - size, y - size); |
| 50 |
ctx.lineTo(x + size, y + size); |
| 51 |
ctx.moveTo(x - size, y + size); |
| 52 |
ctx.lineTo(x + size, y - size); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
var s = series.points.symbol; |
| 57 |
if (handlers[s]) |
| 58 |
series.points.symbol = handlers[s]; |
| 59 |
} |
| 60 |
|
| 61 |
function init(plot) { |
| 62 |
plot.hooks.processDatapoints.push(processRawData); |
| 63 |
} |
| 64 |
|
| 65 |
$.plot.plugins.push({ |
| 66 |
init: init, |
| 67 |
name: 'symbols', |
| 68 |
version: '1.0' |
| 69 |
}); |
| 70 |
})(jQuery); |
| 71 |
|