UNPKG

3.4 kBJavaScriptView Raw
1/* Flot plugin that adds some extra symbols for plotting points.
2
3Copyright (c) 2007-2014 IOLA and Ole Laursen.
4Licensed under the MIT license.
5
6The symbols are accessed as strings through the standard symbol options:
7
8 series: {
9 points: {
10 symbol: "square" // or "diamond", "triangle", "cross", "plus", "ellipse", "rectangle"
11 }
12 }
13
14*/
15
16(function ($) {
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 square = function (ctx, x, y, radius, shadow) {
21 // pi * r^2 = (2s)^2 => s = r * sqrt(pi)/2
22 var size = radius * Math.sqrt(Math.PI) / 2;
23 ctx.rect(x - size, y - size, size + size, size + size);
24 },
25 rectangle = function (ctx, x, y, radius, shadow) {
26 // pi * r^2 = (2s)^2 => s = r * sqrt(pi)/2
27 var size = radius * Math.sqrt(Math.PI) / 2;
28 ctx.rect(x - size, y - size, size + size, size + size);
29 },
30 diamond = function (ctx, x, y, radius, shadow) {
31 // pi * r^2 = 2s^2 => s = r * sqrt(pi/2)
32 var size = radius * Math.sqrt(Math.PI / 2);
33 ctx.moveTo(x - size, y);
34 ctx.lineTo(x, y - size);
35 ctx.lineTo(x + size, y);
36 ctx.lineTo(x, y + size);
37 ctx.lineTo(x - size, y);
38 ctx.lineTo(x, y - size);
39 },
40 triangle = function (ctx, x, y, radius, shadow) {
41 // pi * r^2 = 1/2 * s^2 * sin (pi / 3) => s = r * sqrt(2 * pi / sin(pi / 3))
42 var size = radius * Math.sqrt(2 * Math.PI / Math.sin(Math.PI / 3));
43 var height = size * Math.sin(Math.PI / 3);
44 ctx.moveTo(x - size / 2, y + height / 2);
45 ctx.lineTo(x + size / 2, y + height / 2);
46 if (!shadow) {
47 ctx.lineTo(x, y - height / 2);
48 ctx.lineTo(x - size / 2, y + height / 2);
49 ctx.lineTo(x + size / 2, y + height / 2);
50 }
51 },
52 cross = function (ctx, x, y, radius, shadow) {
53 // pi * r^2 = (2s)^2 => s = r * sqrt(pi)/2
54 var size = radius * Math.sqrt(Math.PI) / 2;
55 ctx.moveTo(x - size, y - size);
56 ctx.lineTo(x + size, y + size);
57 ctx.moveTo(x - size, y + size);
58 ctx.lineTo(x + size, y - size);
59 },
60 ellipse = function(ctx, x, y, radius, shadow, fill) {
61 if (!shadow) {
62 ctx.moveTo(x + radius, y);
63 ctx.arc(x, y, radius, 0, Math.PI * 2, false);
64 }
65 },
66 plus = function (ctx, x, y, radius, shadow) {
67 var size = radius * Math.sqrt(Math.PI / 2);
68 ctx.moveTo(x - size, y);
69 ctx.lineTo(x + size, y);
70 ctx.moveTo(x, y + size);
71 ctx.lineTo(x, y - size);
72 },
73 handlers = {
74 square: square,
75 rectangle: rectangle,
76 diamond: diamond,
77 triangle: triangle,
78 cross: cross,
79 ellipse: ellipse,
80 plus: plus
81 };
82
83 square.fill = true;
84 rectangle.fill = true;
85 diamond.fill = true;
86 triangle.fill = true;
87 ellipse.fill = true;
88
89 function init(plot) {
90 plot.drawSymbol = handlers;
91 }
92
93 $.plot.plugins.push({
94 init: init,
95 name: 'symbols',
96 version: '1.0'
97 });
98})(jQuery);