data-plotter 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2019-2021 OHZI Interactive
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # OHZI Data Plotter
2
+
3
+ Data Plotter is a tiny javascript library that allows you to draw live data in a 2D graph.
4
+
5
+
6
+ ### Dependencies
7
+
8
+ Data Plotter has no external dependencies
9
+
10
+ ### Installation
11
+
12
+ ```sh
13
+ $ npm install data-plotter --save
14
+ ```
15
+
16
+
17
+ ### Usage
18
+
19
+ ```javascript
20
+ import { DataPlotter } from 'data-plotter';
21
+
22
+ class AwesomeGraph
23
+ {
24
+ constructor()
25
+ {
26
+ this.graph = new DataPlotter({ name:'Graph of Sine function' });
27
+ }
28
+
29
+ update(sample)
30
+ {
31
+ this.graph.draw_dynamic_live_array(sample, 0, true, '#70b9fc');
32
+ }
33
+ }
34
+
35
+ class RenderLoop
36
+ {
37
+ start()
38
+ {
39
+ this.awesome_graph = new AwesomeGraph();
40
+ this.t = 0;
41
+
42
+ this.update();
43
+ }
44
+
45
+ update()
46
+ {
47
+ this.awesome_graph.update(Math.sin(this.t));
48
+
49
+ this.t += 0.016;
50
+
51
+ requestAnimationFrame(this.update.bind(this));
52
+ }
53
+ }
54
+
55
+ new RenderLoop().start();
56
+ ```
57
+
58
+ ### API
59
+
60
+ - `draw_function(func: any, min_x?: number, max_x?: number, graph_height?: number, color?: string, thick?: number): void;`
61
+
62
+ - `draw_array(array: any, graph_height?: number, baseline?: number, centered_origin?: boolean, color?: string, thick?: number): void;`
63
+
64
+ - `draw_static_live_array(value: any, graph_height?: number, baseline?: number, centered_origin?: boolean, color?: string, thick?: number): void;`
65
+
66
+ - `draw_dynamic_live_array(value: any, baseline?: number, centered_origin?: boolean, color?: string, thick?: number): void;`
67
+
68
+
69
+ ### Running the examples
70
+ You can check out the examples folder, all you need to do is set up an http server for it to work, using something like `http-server` package.
71
+
72
+ ```sh
73
+ npm install -g http-server
74
+ cd examples
75
+ http-server -p 1234
76
+ ```
77
+ This will mount an http server on the examples folder on port 1234
78
+
79
+ License
80
+ ----
81
+
82
+ MIT
@@ -0,0 +1,217 @@
1
+ class DataPlotter
2
+ {
3
+ constructor({ name = '', container_class = undefined, width = 400, height = 100 })
4
+ {
5
+ let graph_container = document.querySelector(`.${container_class}`);
6
+
7
+ if (!graph_container)
8
+ {
9
+ graph_container = document.createElement('div');
10
+ graph_container.classList.add('data-plotter');
11
+ graph_container.style.display = 'flex';
12
+
13
+ graph_container.style['flex-direction'] = 'column';
14
+ graph_container.style['z-index'] = 999;
15
+ graph_container.style['pointer-events'] = 'none';
16
+ graph_container.style['flex-wrap'] = 'wrap';
17
+
18
+ graph_container.style.width = 'fit-content';
19
+ graph_container.style.position = 'relative';
20
+
21
+ document.body.appendChild(graph_container);
22
+ }
23
+
24
+ const name_label = document.createElement('div');
25
+ name_label.textContent = name;
26
+ name_label.style.color = '#000000';
27
+ name_label.style.marginLeft = '5px';
28
+ name_label.style.marginTop = '5px';
29
+ name_label.style.fontFamily = 'Arial';
30
+ graph_container.appendChild(name_label);
31
+
32
+ const canvas = document.createElement('canvas');
33
+ canvas.width = width * window.devicePixelRatio;
34
+ canvas.height = height * window.devicePixelRatio;
35
+
36
+ canvas.style.width = width + 'px';
37
+ canvas.style.height = height + 'px';
38
+ canvas.style.margin = '5px';
39
+ canvas.style.marginTop = '2px';
40
+ graph_container.appendChild(canvas);
41
+
42
+ this.raw_data_label = document.createElement('div');
43
+ this.raw_data_label.style.position = 'absolute';
44
+ this.raw_data_label.style.bottom = '6px';
45
+ this.raw_data_label.style.right = '10px';
46
+ this.raw_data_label.style['font-family'] = 'Arial';
47
+ this.raw_data_label.style['font-size'] = '12px';
48
+ graph_container.appendChild(this.raw_data_label);
49
+
50
+ this.ctx = canvas.getContext('2d');
51
+
52
+ this.live_data_array = [];
53
+
54
+ for (let i = 0; i < width; i++)
55
+ {
56
+ this.live_data_array.push(0);
57
+ }
58
+ }
59
+
60
+ draw_function(func, min_x = 0, max_x = 1, graph_height = 1, color = '#FF0000', thick = 1)
61
+ {
62
+ let ctx = this.ctx;
63
+ let canvas_length = ctx.canvas.width;
64
+
65
+ ctx.fillStyle = '#888888';
66
+ ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
67
+
68
+ this.showAxes();
69
+
70
+ ctx.beginPath();
71
+ ctx.lineWidth = thick;
72
+ ctx.strokeStyle = color;
73
+
74
+ for (let i = 0; i < canvas_length; i++)
75
+ {
76
+ let result = func(this.lerp(min_x, max_x, i / canvas_length)) / graph_height;
77
+
78
+ let y = ctx.canvas.height - result * (ctx.canvas.height - thick);
79
+
80
+ if (i === 0)
81
+ {
82
+ ctx.moveTo(i, y);
83
+ }
84
+ else
85
+ {
86
+ ctx.lineTo(i, y);
87
+ }
88
+ }
89
+
90
+ ctx.stroke();
91
+ }
92
+
93
+ draw_array(array, graph_height = 1, baseline = 0, centered_origin = false, color = '#70b9fc', thick = 1)
94
+ {
95
+ let ctx = this.ctx;
96
+ let canvas_length = ctx.canvas.width;
97
+
98
+ ctx.fillStyle = '#f2f2f2';
99
+ ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
100
+
101
+ ctx.beginPath();
102
+ ctx.lineWidth = thick;
103
+ ctx.strokeStyle = color;
104
+
105
+ let use_center = centered_origin === true ? 0.5 : 1;
106
+
107
+ for (let i = 0; i < canvas_length; i++)
108
+ {
109
+ let array_i = Math.floor((i / canvas_length) * array.length);
110
+ let result = (array[array_i] - baseline) / graph_height;
111
+
112
+ let y = ctx.canvas.height * use_center - result * (ctx.canvas.height * use_center - thick);
113
+
114
+ if (i === 0)
115
+ {
116
+ ctx.moveTo(i, y);
117
+ }
118
+ else
119
+ {
120
+ ctx.lineTo(i, y);
121
+ }
122
+ }
123
+
124
+ ctx.stroke();
125
+
126
+ if (centered_origin)
127
+ {
128
+ this.draw_min_max_labels(graph_height);
129
+ }
130
+ else
131
+ {
132
+ this.draw_max_label(graph_height);
133
+ }
134
+ }
135
+
136
+ draw_static_live_array(value, graph_height = 1, baseline = 0, centered_origin = false, color = '#70b9fc', thick = 1)
137
+ {
138
+ this.raw_data_label.textContent = value.toFixed(2);
139
+
140
+ this.live_data_array.push(value);
141
+ this.live_data_array.shift();
142
+ this.draw_array(this.live_data_array, graph_height, baseline, centered_origin, color, thick);
143
+ if (centered_origin)
144
+ {
145
+ this.draw_min_max_labels(graph_height);
146
+ }
147
+ else
148
+ {
149
+ this.draw_max_label(graph_height);
150
+ }
151
+ }
152
+
153
+ draw_dynamic_live_array(value, baseline = 0, centered_origin = false, color = '#70b9fc', thick = 1)
154
+ {
155
+ this.raw_data_label.textContent = value.toFixed(2);
156
+
157
+ this.live_data_array.push(value);
158
+ this.live_data_array.shift();
159
+
160
+ let max_value = 1;
161
+ for (let i = 0; i < this.live_data_array.length; i++)
162
+ {
163
+ if (Math.abs(this.live_data_array[i]) > max_value)
164
+ {
165
+ max_value = Math.abs(this.live_data_array[i]);
166
+ }
167
+ }
168
+
169
+ this.draw_array(this.live_data_array, max_value, baseline, centered_origin, color, thick);
170
+
171
+ if (centered_origin)
172
+ {
173
+ this.draw_min_max_labels(max_value);
174
+ }
175
+ else
176
+ {
177
+ this.draw_max_label(max_value);
178
+ }
179
+ }
180
+
181
+ draw_min_max_labels(value)
182
+ {
183
+ const top_bottom_padding = 12;
184
+ const left_right_padding = 10;
185
+
186
+ this.ctx.font = '20px Arial';
187
+ this.ctx.fillStyle = '#000000';
188
+ this.ctx.textBaseline = 'alphabetic';
189
+ this.ctx.fillText('-' + value.toFixed(1), left_right_padding, this.ctx.canvas.height - top_bottom_padding);
190
+ this.ctx.textBaseline = 'hanging';
191
+ this.ctx.fillText(value.toFixed(1), left_right_padding, top_bottom_padding);
192
+
193
+ // Center line
194
+ this.ctx.beginPath();
195
+ this.ctx.lineWidth = 1;
196
+ this.ctx.strokeStyle = '#BBBBBB';
197
+ this.ctx.moveTo(0, this.ctx.canvas.height / 2);
198
+ this.ctx.lineTo(this.ctx.canvas.width, this.ctx.canvas.height / 2);
199
+ this.ctx.stroke();
200
+ }
201
+
202
+ draw_max_label(value)
203
+ {
204
+ this.ctx.font = '20px Arial';
205
+ this.ctx.fillStyle = '#000000';
206
+ this.ctx.textBaseline = 'hanging';
207
+ this.ctx.fillText(value.toFixed(1), 5, 8);
208
+ }
209
+
210
+ lerp(a, b, t)
211
+ {
212
+ return (1 - t) * a + b * t;
213
+ }
214
+ }
215
+
216
+ export { DataPlotter };
217
+ //# sourceMappingURL=data-plotter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"data-plotter.js","sources":["../../src/DataPlotter.js"],"sourcesContent":["class DataPlotter\n{\n constructor({ name = '', container_class = undefined, width = 400, height = 100 })\n {\n let graph_container = document.querySelector(`.${container_class}`);\n\n if (!graph_container)\n {\n graph_container = document.createElement('div');\n graph_container.classList.add('data-plotter');\n graph_container.style.display = 'flex';\n\n graph_container.style['flex-direction'] = 'column';\n graph_container.style['z-index'] = 999;\n graph_container.style['pointer-events'] = 'none';\n graph_container.style['flex-wrap'] = 'wrap';\n\n graph_container.style.width = 'fit-content';\n graph_container.style.position = 'relative';\n\n document.body.appendChild(graph_container);\n }\n\n const name_label = document.createElement('div');\n name_label.textContent = name;\n name_label.style.color = '#000000';\n name_label.style.marginLeft = '5px';\n name_label.style.marginTop = '5px';\n name_label.style.fontFamily = 'Arial';\n graph_container.appendChild(name_label);\n\n const canvas = document.createElement('canvas');\n canvas.width = width * window.devicePixelRatio;\n canvas.height = height * window.devicePixelRatio;\n\n canvas.style.width = width + 'px';\n canvas.style.height = height + 'px';\n canvas.style.margin = '5px';\n canvas.style.marginTop = '2px';\n graph_container.appendChild(canvas);\n\n this.raw_data_label = document.createElement('div');\n this.raw_data_label.style.position = 'absolute';\n this.raw_data_label.style.bottom = '6px';\n this.raw_data_label.style.right = '10px';\n this.raw_data_label.style['font-family'] = 'Arial';\n this.raw_data_label.style['font-size'] = '12px';\n graph_container.appendChild(this.raw_data_label);\n\n this.ctx = canvas.getContext('2d');\n\n this.live_data_array = [];\n\n for (let i = 0; i < width; i++)\n {\n this.live_data_array.push(0);\n }\n }\n\n draw_function(func, min_x = 0, max_x = 1, graph_height = 1, color = '#FF0000', thick = 1)\n {\n let ctx = this.ctx;\n let canvas_length = ctx.canvas.width;\n\n ctx.fillStyle = '#888888';\n ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);\n\n this.showAxes();\n\n ctx.beginPath();\n ctx.lineWidth = thick;\n ctx.strokeStyle = color;\n\n for (let i = 0; i < canvas_length; i++)\n {\n let result = func(this.lerp(min_x, max_x, i / canvas_length)) / graph_height;\n\n let y = ctx.canvas.height - result * (ctx.canvas.height - thick);\n\n if (i === 0)\n {\n ctx.moveTo(i, y);\n }\n else\n {\n ctx.lineTo(i, y);\n }\n }\n\n ctx.stroke();\n }\n\n draw_array(array, graph_height = 1, baseline = 0, centered_origin = false, color = '#70b9fc', thick = 1)\n {\n let ctx = this.ctx;\n let canvas_length = ctx.canvas.width;\n\n ctx.fillStyle = '#f2f2f2';\n ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);\n\n ctx.beginPath();\n ctx.lineWidth = thick;\n ctx.strokeStyle = color;\n\n let use_center = centered_origin === true ? 0.5 : 1;\n\n for (let i = 0; i < canvas_length; i++)\n {\n let array_i = Math.floor((i / canvas_length) * array.length);\n let result = (array[array_i] - baseline) / graph_height;\n\n let y = ctx.canvas.height * use_center - result * (ctx.canvas.height * use_center - thick);\n\n if (i === 0)\n {\n ctx.moveTo(i, y);\n }\n else\n {\n ctx.lineTo(i, y);\n }\n }\n\n ctx.stroke();\n\n if (centered_origin)\n {\n this.draw_min_max_labels(graph_height);\n }\n else\n {\n this.draw_max_label(graph_height);\n }\n }\n\n draw_static_live_array(value, graph_height = 1, baseline = 0, centered_origin = false, color = '#70b9fc', thick = 1)\n {\n this.raw_data_label.textContent = value.toFixed(2);\n\n this.live_data_array.push(value);\n this.live_data_array.shift();\n this.draw_array(this.live_data_array, graph_height, baseline, centered_origin, color, thick);\n if (centered_origin)\n {\n this.draw_min_max_labels(graph_height);\n }\n else\n {\n this.draw_max_label(graph_height);\n }\n }\n\n draw_dynamic_live_array(value, baseline = 0, centered_origin = false, color = '#70b9fc', thick = 1)\n {\n this.raw_data_label.textContent = value.toFixed(2);\n\n this.live_data_array.push(value);\n this.live_data_array.shift();\n\n let max_value = 1;\n for (let i = 0; i < this.live_data_array.length; i++)\n {\n if (Math.abs(this.live_data_array[i]) > max_value)\n {\n max_value = Math.abs(this.live_data_array[i]);\n }\n }\n\n this.draw_array(this.live_data_array, max_value, baseline, centered_origin, color, thick);\n\n if (centered_origin)\n {\n this.draw_min_max_labels(max_value);\n }\n else\n {\n this.draw_max_label(max_value);\n }\n }\n\n draw_min_max_labels(value)\n {\n const top_bottom_padding = 12;\n const left_right_padding = 10;\n\n this.ctx.font = '20px Arial';\n this.ctx.fillStyle = '#000000';\n this.ctx.textBaseline = 'alphabetic';\n this.ctx.fillText('-' + value.toFixed(1), left_right_padding, this.ctx.canvas.height - top_bottom_padding);\n this.ctx.textBaseline = 'hanging';\n this.ctx.fillText(value.toFixed(1), left_right_padding, top_bottom_padding);\n\n // Center line\n this.ctx.beginPath();\n this.ctx.lineWidth = 1;\n this.ctx.strokeStyle = '#BBBBBB';\n this.ctx.moveTo(0, this.ctx.canvas.height / 2);\n this.ctx.lineTo(this.ctx.canvas.width, this.ctx.canvas.height / 2);\n this.ctx.stroke();\n }\n\n draw_max_label(value)\n {\n this.ctx.font = '20px Arial';\n this.ctx.fillStyle = '#000000';\n this.ctx.textBaseline = 'hanging';\n this.ctx.fillText(value.toFixed(1), 5, 8);\n }\n\n lerp(a, b, t)\n {\n return (1 - t) * a + b * t;\n }\n}\n\nexport { DataPlotter };\n"],"names":[],"mappings":"AAAA,MAAM,WAAW;AACjB;AACA,EAAE,WAAW,CAAC,EAAE,IAAI,GAAG,EAAE,EAAE,eAAe,GAAG,SAAS,EAAE,KAAK,GAAG,GAAG,EAAE,MAAM,GAAG,GAAG,EAAE;AACnF,EAAE;AACF,IAAI,IAAI,eAAe,GAAG,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,CAAC;AACxE;AACA,IAAI,IAAI,CAAC,eAAe;AACxB,IAAI;AACJ,MAAM,eAAe,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;AACtD,MAAM,eAAe,CAAC,SAAS,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;AACpD,MAAM,eAAe,CAAC,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC;AAC7C;AACA,MAAM,eAAe,CAAC,KAAK,CAAC,gBAAgB,CAAC,GAAG,QAAQ,CAAC;AACzD,MAAM,eAAe,CAAC,KAAK,CAAC,SAAS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IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC;AACd,EAAE;AACF,IAAI,OAAO,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAC/B,GAAG;AACH;;;;"}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "data-plotter",
3
+ "version": "1.0.0",
4
+ "description": "2D Data Plotter",
5
+ "source": "src/index.js",
6
+ "types": "types/index.d.ts",
7
+ "files": [
8
+ "examples/build",
9
+ "LICENSE",
10
+ "src",
11
+ "types",
12
+ "package.json"
13
+ ],
14
+ "scripts": {
15
+ "start": "rollup -w -c",
16
+ "build": "rollup -c",
17
+ "create-tag": "cross-var git tag -a \"v$npm_package_version\" -m \"Release Version $npm_package_version\" && cross-var git push origin \"v$npm_package_version\"",
18
+ "update-version": "cross-var echo \"v$npm_package_version\" > .version",
19
+ "fix-syntax": "standardx --fix",
20
+ "generate-types": "npx -p typescript tsc src/* --declaration --allowJs --emitDeclarationOnly --outDir types"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+ssh://git@github.com/ohzinteractive/data-plotter.git"
25
+ },
26
+ "keywords": [
27
+ "ohzi",
28
+ "2d plotter",
29
+ "data plotter",
30
+ "function plotter"
31
+ ],
32
+ "author": "OHZI Interactive",
33
+ "license": "MIT",
34
+ "bugs": {
35
+ "url": "https://github.com/ohzinteractive/core/issues"
36
+ },
37
+ "homepage": "https://github.com/ohzinteractive/core#readme",
38
+ "devDependencies": {
39
+ "cross-var": "^1.1.0",
40
+ "rollup": "^2.44.0",
41
+ "rollup-plugin-sourcemaps": "^0.6.3",
42
+ "standardx": "^7.0.0",
43
+ "typescript": "^4.4.3"
44
+ }
45
+ }
@@ -0,0 +1,216 @@
1
+ class DataPlotter
2
+ {
3
+ constructor({ name = '', container_class = undefined, width = 400, height = 100 })
4
+ {
5
+ let graph_container = document.querySelector(`.${container_class}`);
6
+
7
+ if (!graph_container)
8
+ {
9
+ graph_container = document.createElement('div');
10
+ graph_container.classList.add('data-plotter');
11
+ graph_container.style.display = 'flex';
12
+
13
+ graph_container.style['flex-direction'] = 'column';
14
+ graph_container.style['z-index'] = 999;
15
+ graph_container.style['pointer-events'] = 'none';
16
+ graph_container.style['flex-wrap'] = 'wrap';
17
+
18
+ graph_container.style.width = 'fit-content';
19
+ graph_container.style.position = 'relative';
20
+
21
+ document.body.appendChild(graph_container);
22
+ }
23
+
24
+ const name_label = document.createElement('div');
25
+ name_label.textContent = name;
26
+ name_label.style.color = '#000000';
27
+ name_label.style.marginLeft = '5px';
28
+ name_label.style.marginTop = '5px';
29
+ name_label.style.fontFamily = 'Arial';
30
+ graph_container.appendChild(name_label);
31
+
32
+ const canvas = document.createElement('canvas');
33
+ canvas.width = width * window.devicePixelRatio;
34
+ canvas.height = height * window.devicePixelRatio;
35
+
36
+ canvas.style.width = width + 'px';
37
+ canvas.style.height = height + 'px';
38
+ canvas.style.margin = '5px';
39
+ canvas.style.marginTop = '2px';
40
+ graph_container.appendChild(canvas);
41
+
42
+ this.raw_data_label = document.createElement('div');
43
+ this.raw_data_label.style.position = 'absolute';
44
+ this.raw_data_label.style.bottom = '6px';
45
+ this.raw_data_label.style.right = '10px';
46
+ this.raw_data_label.style['font-family'] = 'Arial';
47
+ this.raw_data_label.style['font-size'] = '12px';
48
+ graph_container.appendChild(this.raw_data_label);
49
+
50
+ this.ctx = canvas.getContext('2d');
51
+
52
+ this.live_data_array = [];
53
+
54
+ for (let i = 0; i < width; i++)
55
+ {
56
+ this.live_data_array.push(0);
57
+ }
58
+ }
59
+
60
+ draw_function(func, min_x = 0, max_x = 1, graph_height = 1, color = '#FF0000', thick = 1)
61
+ {
62
+ let ctx = this.ctx;
63
+ let canvas_length = ctx.canvas.width;
64
+
65
+ ctx.fillStyle = '#888888';
66
+ ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
67
+
68
+ this.showAxes();
69
+
70
+ ctx.beginPath();
71
+ ctx.lineWidth = thick;
72
+ ctx.strokeStyle = color;
73
+
74
+ for (let i = 0; i < canvas_length; i++)
75
+ {
76
+ let result = func(this.lerp(min_x, max_x, i / canvas_length)) / graph_height;
77
+
78
+ let y = ctx.canvas.height - result * (ctx.canvas.height - thick);
79
+
80
+ if (i === 0)
81
+ {
82
+ ctx.moveTo(i, y);
83
+ }
84
+ else
85
+ {
86
+ ctx.lineTo(i, y);
87
+ }
88
+ }
89
+
90
+ ctx.stroke();
91
+ }
92
+
93
+ draw_array(array, graph_height = 1, baseline = 0, centered_origin = false, color = '#70b9fc', thick = 1)
94
+ {
95
+ let ctx = this.ctx;
96
+ let canvas_length = ctx.canvas.width;
97
+
98
+ ctx.fillStyle = '#f2f2f2';
99
+ ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
100
+
101
+ ctx.beginPath();
102
+ ctx.lineWidth = thick;
103
+ ctx.strokeStyle = color;
104
+
105
+ let use_center = centered_origin === true ? 0.5 : 1;
106
+
107
+ for (let i = 0; i < canvas_length; i++)
108
+ {
109
+ let array_i = Math.floor((i / canvas_length) * array.length);
110
+ let result = (array[array_i] - baseline) / graph_height;
111
+
112
+ let y = ctx.canvas.height * use_center - result * (ctx.canvas.height * use_center - thick);
113
+
114
+ if (i === 0)
115
+ {
116
+ ctx.moveTo(i, y);
117
+ }
118
+ else
119
+ {
120
+ ctx.lineTo(i, y);
121
+ }
122
+ }
123
+
124
+ ctx.stroke();
125
+
126
+ if (centered_origin)
127
+ {
128
+ this.draw_min_max_labels(graph_height);
129
+ }
130
+ else
131
+ {
132
+ this.draw_max_label(graph_height);
133
+ }
134
+ }
135
+
136
+ draw_static_live_array(value, graph_height = 1, baseline = 0, centered_origin = false, color = '#70b9fc', thick = 1)
137
+ {
138
+ this.raw_data_label.textContent = value.toFixed(2);
139
+
140
+ this.live_data_array.push(value);
141
+ this.live_data_array.shift();
142
+ this.draw_array(this.live_data_array, graph_height, baseline, centered_origin, color, thick);
143
+ if (centered_origin)
144
+ {
145
+ this.draw_min_max_labels(graph_height);
146
+ }
147
+ else
148
+ {
149
+ this.draw_max_label(graph_height);
150
+ }
151
+ }
152
+
153
+ draw_dynamic_live_array(value, baseline = 0, centered_origin = false, color = '#70b9fc', thick = 1)
154
+ {
155
+ this.raw_data_label.textContent = value.toFixed(2);
156
+
157
+ this.live_data_array.push(value);
158
+ this.live_data_array.shift();
159
+
160
+ let max_value = 1;
161
+ for (let i = 0; i < this.live_data_array.length; i++)
162
+ {
163
+ if (Math.abs(this.live_data_array[i]) > max_value)
164
+ {
165
+ max_value = Math.abs(this.live_data_array[i]);
166
+ }
167
+ }
168
+
169
+ this.draw_array(this.live_data_array, max_value, baseline, centered_origin, color, thick);
170
+
171
+ if (centered_origin)
172
+ {
173
+ this.draw_min_max_labels(max_value);
174
+ }
175
+ else
176
+ {
177
+ this.draw_max_label(max_value);
178
+ }
179
+ }
180
+
181
+ draw_min_max_labels(value)
182
+ {
183
+ const top_bottom_padding = 12;
184
+ const left_right_padding = 10;
185
+
186
+ this.ctx.font = '20px Arial';
187
+ this.ctx.fillStyle = '#000000';
188
+ this.ctx.textBaseline = 'alphabetic';
189
+ this.ctx.fillText('-' + value.toFixed(1), left_right_padding, this.ctx.canvas.height - top_bottom_padding);
190
+ this.ctx.textBaseline = 'hanging';
191
+ this.ctx.fillText(value.toFixed(1), left_right_padding, top_bottom_padding);
192
+
193
+ // Center line
194
+ this.ctx.beginPath();
195
+ this.ctx.lineWidth = 1;
196
+ this.ctx.strokeStyle = '#BBBBBB';
197
+ this.ctx.moveTo(0, this.ctx.canvas.height / 2);
198
+ this.ctx.lineTo(this.ctx.canvas.width, this.ctx.canvas.height / 2);
199
+ this.ctx.stroke();
200
+ }
201
+
202
+ draw_max_label(value)
203
+ {
204
+ this.ctx.font = '20px Arial';
205
+ this.ctx.fillStyle = '#000000';
206
+ this.ctx.textBaseline = 'hanging';
207
+ this.ctx.fillText(value.toFixed(1), 5, 8);
208
+ }
209
+
210
+ lerp(a, b, t)
211
+ {
212
+ return (1 - t) * a + b * t;
213
+ }
214
+ }
215
+
216
+ export { DataPlotter };
package/src/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import { DataPlotter } from './DataPlotter';
2
+
3
+ export {
4
+ DataPlotter
5
+ };
@@ -0,0 +1,9 @@
1
+ export class DataPlotter {
2
+ constructor(name?: string, width?: number, height?: number);
3
+ ctx: CanvasRenderingContext2D;
4
+ live_data_array: number[];
5
+ draw_function(func: any, min_x?: number, max_x?: number, graph_height?: number, color?: string, thick?: number): void;
6
+ draw_array(array: any, graph_height?: number, baseline?: number, centered_origin?: boolean, color?: string, thick?: number): void;
7
+ draw_static_live_array(value: any, graph_height?: number, baseline?: number, centered_origin?: boolean, color?: string, thick?: number): void;
8
+ draw_dynamic_live_array(value: any, baseline?: number, centered_origin?: boolean, color?: string, thick?: number): void;
9
+ }
@@ -0,0 +1,2 @@
1
+ import { DataPlotter } from './DataPlotter';
2
+ export { DataPlotter };