-
Notifications
You must be signed in to change notification settings - Fork 17
/
demo.html
138 lines (113 loc) · 3.9 KB
/
demo.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Canvas Sparkline demo</title>
</head>
<body>
<h1>Canvas Sparklines</h1>
<h2>Example One</h2>
<dl>
<dt>Dataset:</dt>
<dd>5, 12, 14, 6, 8, 4, 0, 4, 8, 12, 1, 0, 0, 14, 15</dd>
<dt>Canvas Dimensions:</dt>
<dd>100 x 15</dd>
<dt>Enpoint:</dt>
<dd>true</dd>
</dl>
<canvas id="example1" width=100 height=15></canvas>
<h2>Example Two</h2>
<dl>
<dt>Dataset:</dt>
<dd>113, 78, 89, 34, 12, 45, 67, 89, 112, 134, 123, 102, 78, 34, 23, 21, 45, 63, 54, 89</dd>
<dt>Canvas Dimensions:</dt>
<dd>75 x 20</dd>
<dt>Enpoint:</dt>
<dd>false</dd>
</dl>
<canvas id="example2" width=75 height=20></canvas>
<h2>Example Three</h2>
<dl>
<dt>Dataset:</dt>
<dd>1345, 1267, 1178, 891, 1349, 1567, 1891, 1921, 2045, 2102, 2391, 2197, 1899, 1456, 1209, 781, 567, 344</dd>
<dt>Canvas Dimensions:</dt>
<dd>100 x 15</dd>
<dt>Enpoint:</dt>
<dd>true</dd>
</dl>
<canvas id="example3" width=100 height=15></canvas>
<h2>Example Four</h2>
<dl>
<dt>Dataset:</dt>
<dd>9, 37, 23, 28, 44, 26, 43, 43, 24, 33, 17, 18, 35, 31, 30, 49, 32, 25, 48, 37, 45, 20, 23, 34, 37, 26, 34, 38, 37, 34, 23, 26, 34, 37, 18, 39, 33, 28, 27, 24, 22, 35, 24, 38, 50, 45, 41, 18, 34, 19, 9</dd>
<dt>Canvas Dimensions:</dt>
<dd>100 x 25</dd>
<dt>Enpoint:</dt>
<dd>true</dd>
</dl>
<canvas id="example4" width=100 height=25></canvas>
<h2>Example Five</h2>
<dl>
<dt>Dataset:</dt>
<dd>9, 37, 23, 28, 44, 26, 43, 43, 24, 33, 17, 18, 35, 31, 30, 49, 32, 25, 48, 37, 45, 20, 23, 34, 37, 26, 34, 38, 37, 34, 23, 26, 34, 37, 18, 39, 33, 28, 27, 24, 22, 35, 24, 38, 50, 45, 41, 18, 34, 19, 9</dd>
<dt>Canvas Dimensions:</dt>
<dd>150 x 25</dd>
<dt>Enpoint:</dt>
<dd>true</dd>
<dt>Color:</dt>
<dd>rgba(0,0,255,.5)</dd>
<dt>Style:</dt>
<dd>bar</dd>
</dl>
<canvas id="example5" width=150 height=25></canvas>
<script>
var sparkline = function(canvas_id, data, endpoint, color, style) {
if (window.HTMLCanvasElement) {
var c = document.getElementById(canvas_id),
ctx = c.getContext('2d'),
color = (color ? color : 'rgba(0,0,0,0.5)'),
style = (style == 'bar' ? 'bar' : 'line'),
height = c.height - 3,
width = c.width,
total = data.length,
max = Math.max.apply(Math, data),
xstep = width/total,
ystep = max/height,
x = 0,
y = height - data[0]/ystep,
i;
if (window.devicePixelRatio) {
c.width = c.width * window.devicePixelRatio;
c.height = c.height * window.devicePixelRatio;
c.style.width = (c.width / window.devicePixelRatio) + 'px';
c.style.height = (c.height / window.devicePixelRatio) + 'px';
c.style.display = 'inline-block';
ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
}
ctx.clearRect(0, 0, width, height);
ctx.beginPath();
ctx.strokeStyle = color;
ctx.moveTo(x, y);
for (i = 1; i < total; i = i + 1) {
x = x + xstep;
y = height - data[i]/ystep + 2;
if (style == 'bar') { ctx.moveTo(x,height); }
ctx.lineTo(x, y);
}
ctx.stroke();
if (endpoint && style == 'line') {
ctx.beginPath();
ctx.fillStyle = 'rgba(255,0,0,0.5)';
ctx.arc(x, y, 1.5, 0, Math.PI*2);
ctx.fill();
}
}
};
sparkline('example1', [5, 12, 14, 6, 8, 4, 0, 4, 8, 12, 1, 0, 0, 14, 15], true);
sparkline('example2', [113, 78, 89, 34, 12, 45, 67, 89, 112, 134, 123, 102, 78, 34, 23, 21, 45, 63, 54, 89]);
sparkline('example3', [1345, 1267, 1178, 891, 1349, 1567, 1891, 1921, 2045, 2102, 2391, 2197, 1899, 1456, 1209, 781, 567, 344], true);
sparkline('example4', [9, 37, 23, 28, 44, 26, 43, 43, 24, 33, 17, 18, 35, 31, 30, 49, 32, 25, 48, 37, 45, 20, 23, 34, 37, 26, 34, 38, 37, 34, 23, 26, 34, 37, 18, 39, 33, 28, 27, 24, 22, 35, 24, 38, 50, 45, 41, 18, 34, 19, 9], true);
sparkline('example5', [9, 37, 23, 28, 44, 26, 43, 43, 24, 33, 17, 18, 35, 31, 30, 49, 32, 25, 48, 37, 45, 20, 23, 34, 37, 26, 34, 38, 37, 34, 23, 26, 34, 37, 18, 39, 33, 28, 27, 24, 22, 35, 24, 38, 50, 45, 41, 18, 34, 19, 9], true, 'rgba(0,0,255,.5)', 'bar');
</script>
</body>
</html>