data-plotter 1.1.0 → 1.2.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/README.md +82 -82
- package/examples/build/data-plotter.js +249 -241
- package/examples/build/data-plotter.js.map +1 -1
- package/package.json +47 -46
- package/src/DataPlotter.js +252 -244
- package/types/DataPlotter.d.ts +21 -21
- package/types/index.d.ts +2 -2
package/src/DataPlotter.js
CHANGED
|
@@ -1,244 +1,252 @@
|
|
|
1
|
-
class DataPlotter
|
|
2
|
-
{
|
|
3
|
-
constructor({
|
|
4
|
-
id = '',
|
|
5
|
-
name = '',
|
|
6
|
-
container_selector = undefined,
|
|
7
|
-
width = 400,
|
|
8
|
-
height = 100,
|
|
9
|
-
raw_data_suffix = ''
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
parent
|
|
19
|
-
|
|
20
|
-
parent.style
|
|
21
|
-
|
|
22
|
-
parent.style['
|
|
23
|
-
parent.style['
|
|
24
|
-
|
|
25
|
-
parent.style
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
name_label
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this.
|
|
59
|
-
|
|
60
|
-
this.
|
|
61
|
-
|
|
62
|
-
this.
|
|
63
|
-
|
|
64
|
-
this.raw_data_label
|
|
65
|
-
this.
|
|
66
|
-
|
|
67
|
-
this.
|
|
68
|
-
|
|
69
|
-
this.
|
|
70
|
-
|
|
71
|
-
this.
|
|
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
|
-
ctx.
|
|
99
|
-
|
|
100
|
-
ctx.
|
|
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
|
-
ctx
|
|
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
|
-
this.ctx.
|
|
223
|
-
this.ctx.
|
|
224
|
-
this.ctx.
|
|
225
|
-
this.ctx.
|
|
226
|
-
this.ctx.
|
|
227
|
-
this.ctx.
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
this.ctx.
|
|
233
|
-
this.ctx.
|
|
234
|
-
this.ctx.
|
|
235
|
-
this.ctx.
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
{
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
1
|
+
class DataPlotter
|
|
2
|
+
{
|
|
3
|
+
constructor({
|
|
4
|
+
id = '',
|
|
5
|
+
name = '',
|
|
6
|
+
container_selector = undefined,
|
|
7
|
+
width = 400,
|
|
8
|
+
height = 100,
|
|
9
|
+
raw_data_suffix = '',
|
|
10
|
+
background_color = '#FFFFFF',
|
|
11
|
+
text_color = '#000000',
|
|
12
|
+
center_line_color = '#808080'
|
|
13
|
+
})
|
|
14
|
+
{
|
|
15
|
+
let parent = document.querySelector(container_selector);
|
|
16
|
+
if (!parent)
|
|
17
|
+
{
|
|
18
|
+
parent = document.createElement('div');
|
|
19
|
+
parent.classList.add('data-plotter');
|
|
20
|
+
parent.style.display = 'flex';
|
|
21
|
+
|
|
22
|
+
parent.style['flex-direction'] = 'column';
|
|
23
|
+
parent.style['z-index'] = 999;
|
|
24
|
+
parent.style['pointer-events'] = 'none';
|
|
25
|
+
parent.style['flex-wrap'] = 'wrap';
|
|
26
|
+
|
|
27
|
+
parent.style.width = 'fit-content';
|
|
28
|
+
|
|
29
|
+
document.body.appendChild(parent);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
this.background_color = background_color;
|
|
33
|
+
this.text_color = text_color;
|
|
34
|
+
this.center_line_color = center_line_color;
|
|
35
|
+
|
|
36
|
+
this.container = document.createElement('div');
|
|
37
|
+
|
|
38
|
+
if (id)
|
|
39
|
+
{
|
|
40
|
+
this.container.dataset.id = id;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
this.container.style.position = 'relative';
|
|
44
|
+
this.container.style.margin = '5px';
|
|
45
|
+
this.container.style.marginTop = '2px';
|
|
46
|
+
parent.appendChild(this.container);
|
|
47
|
+
|
|
48
|
+
const name_label = document.createElement('div');
|
|
49
|
+
name_label.textContent = name;
|
|
50
|
+
name_label.style.color = text_color;
|
|
51
|
+
name_label.style.marginLeft = '5px';
|
|
52
|
+
name_label.style.marginTop = '5px';
|
|
53
|
+
name_label.style.marginBottom = '5px';
|
|
54
|
+
name_label.style.fontFamily = 'Arial';
|
|
55
|
+
this.container.appendChild(name_label);
|
|
56
|
+
|
|
57
|
+
this.canvas = document.createElement('canvas');
|
|
58
|
+
this.canvas.style.display = 'flex';
|
|
59
|
+
|
|
60
|
+
this.on_resize(width, height);
|
|
61
|
+
|
|
62
|
+
this.container.appendChild(this.canvas);
|
|
63
|
+
|
|
64
|
+
this.raw_data_label = document.createElement('div');
|
|
65
|
+
this.raw_data_label.classList.add('data-plotter__raw-data');
|
|
66
|
+
this.raw_data_label.style.position = 'absolute';
|
|
67
|
+
this.raw_data_label.style.bottom = '4px';
|
|
68
|
+
this.raw_data_label.style.right = '4px';
|
|
69
|
+
this.raw_data_label.style.color = text_color;
|
|
70
|
+
this.raw_data_label.style['font-family'] = 'Arial';
|
|
71
|
+
this.raw_data_label.style['font-size'] = '12px';
|
|
72
|
+
this.container.appendChild(this.raw_data_label);
|
|
73
|
+
|
|
74
|
+
this.raw_data_suffix = raw_data_suffix;
|
|
75
|
+
|
|
76
|
+
this.ctx = this.canvas.getContext('2d');
|
|
77
|
+
|
|
78
|
+
this.live_data_array = [];
|
|
79
|
+
|
|
80
|
+
for (let i = 0; i < width; i++)
|
|
81
|
+
{
|
|
82
|
+
this.live_data_array.push(0);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
on_resize(width, height)
|
|
87
|
+
{
|
|
88
|
+
this.canvas.width = width * window.devicePixelRatio;
|
|
89
|
+
this.canvas.height = height * window.devicePixelRatio;
|
|
90
|
+
|
|
91
|
+
this.canvas.style.width = width + 'px';
|
|
92
|
+
this.canvas.style.height = height + 'px';
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
draw_function(func, min_x = 0, max_x = 1, graph_height = 1, color = '#FF0000', thick = 1)
|
|
96
|
+
{
|
|
97
|
+
let ctx = this.ctx;
|
|
98
|
+
let canvas_length = ctx.canvas.width;
|
|
99
|
+
|
|
100
|
+
ctx.fillStyle = this.background_color;
|
|
101
|
+
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
|
102
|
+
|
|
103
|
+
this.showAxes();
|
|
104
|
+
|
|
105
|
+
ctx.beginPath();
|
|
106
|
+
ctx.lineWidth = thick;
|
|
107
|
+
ctx.strokeStyle = color;
|
|
108
|
+
|
|
109
|
+
for (let i = 0; i < canvas_length; i++)
|
|
110
|
+
{
|
|
111
|
+
let result = func(this.lerp(min_x, max_x, i / canvas_length)) / graph_height;
|
|
112
|
+
|
|
113
|
+
let y = ctx.canvas.height - result * (ctx.canvas.height - thick);
|
|
114
|
+
|
|
115
|
+
if (i === 0)
|
|
116
|
+
{
|
|
117
|
+
ctx.moveTo(i, y);
|
|
118
|
+
}
|
|
119
|
+
else
|
|
120
|
+
{
|
|
121
|
+
ctx.lineTo(i, y);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
ctx.stroke();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
draw_array(array, graph_height = 1, baseline = 0, centered_origin = false, color = '#70b9fc', thick = 1)
|
|
129
|
+
{
|
|
130
|
+
let ctx = this.ctx;
|
|
131
|
+
let canvas_length = ctx.canvas.width;
|
|
132
|
+
|
|
133
|
+
ctx.fillStyle = this.background_color;
|
|
134
|
+
|
|
135
|
+
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
|
136
|
+
|
|
137
|
+
ctx.beginPath();
|
|
138
|
+
ctx.lineWidth = thick;
|
|
139
|
+
ctx.strokeStyle = color;
|
|
140
|
+
|
|
141
|
+
let use_center = centered_origin === true ? 0.5 : 1;
|
|
142
|
+
|
|
143
|
+
for (let i = 0; i < canvas_length; i++)
|
|
144
|
+
{
|
|
145
|
+
let array_i = Math.floor((i / canvas_length) * array.length);
|
|
146
|
+
let result = (array[array_i] - baseline) / graph_height;
|
|
147
|
+
|
|
148
|
+
let y = ctx.canvas.height * use_center - result * (ctx.canvas.height * use_center - thick);
|
|
149
|
+
|
|
150
|
+
if (i === 0)
|
|
151
|
+
{
|
|
152
|
+
ctx.moveTo(i, y);
|
|
153
|
+
}
|
|
154
|
+
else
|
|
155
|
+
{
|
|
156
|
+
ctx.lineTo(i, y);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
ctx.stroke();
|
|
161
|
+
|
|
162
|
+
if (centered_origin)
|
|
163
|
+
{
|
|
164
|
+
this.draw_min_max_labels(graph_height);
|
|
165
|
+
}
|
|
166
|
+
else
|
|
167
|
+
{
|
|
168
|
+
this.draw_max_label(graph_height);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
draw_static_live_array(value, graph_height = 1, baseline = 0, centered_origin = false, color = '#70b9fc', thick = 1)
|
|
173
|
+
{
|
|
174
|
+
this.raw_data_label.textContent = `${value.toFixed(2)}${this.raw_data_suffix}`;
|
|
175
|
+
|
|
176
|
+
this.live_data_array.push(value);
|
|
177
|
+
this.live_data_array.shift();
|
|
178
|
+
this.draw_array(this.live_data_array, graph_height, baseline, centered_origin, color, thick);
|
|
179
|
+
if (centered_origin)
|
|
180
|
+
{
|
|
181
|
+
this.draw_min_max_labels(graph_height);
|
|
182
|
+
}
|
|
183
|
+
else
|
|
184
|
+
{
|
|
185
|
+
this.draw_max_label(graph_height);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
draw_dynamic_live_array(value, baseline = 0, centered_origin = false, color = '#70b9fc', thick = 1)
|
|
190
|
+
{
|
|
191
|
+
this.raw_data_label.textContent = `${value.toFixed(2)}${this.raw_data_suffix}`;
|
|
192
|
+
|
|
193
|
+
this.live_data_array.push(value);
|
|
194
|
+
this.live_data_array.shift();
|
|
195
|
+
|
|
196
|
+
let max_value = 1;
|
|
197
|
+
for (let i = 0; i < this.live_data_array.length; i++)
|
|
198
|
+
{
|
|
199
|
+
if (Math.abs(this.live_data_array[i]) > max_value)
|
|
200
|
+
{
|
|
201
|
+
max_value = Math.abs(this.live_data_array[i]);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
this.draw_array(this.live_data_array, max_value, baseline, centered_origin, color, thick);
|
|
206
|
+
|
|
207
|
+
if (centered_origin)
|
|
208
|
+
{
|
|
209
|
+
this.draw_min_max_labels(max_value);
|
|
210
|
+
}
|
|
211
|
+
else
|
|
212
|
+
{
|
|
213
|
+
this.draw_max_label(max_value);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
draw_min_max_labels(value)
|
|
218
|
+
{
|
|
219
|
+
const top_bottom_padding = 12;
|
|
220
|
+
const left_right_padding = 10;
|
|
221
|
+
|
|
222
|
+
this.ctx.font = '20px Arial';
|
|
223
|
+
this.ctx.fillStyle = this.text_color;
|
|
224
|
+
this.ctx.textBaseline = 'alphabetic';
|
|
225
|
+
this.ctx.fillText('-' + value.toFixed(1), left_right_padding, this.ctx.canvas.height - top_bottom_padding);
|
|
226
|
+
this.ctx.textBaseline = 'hanging';
|
|
227
|
+
this.ctx.fillText(value.toFixed(1), left_right_padding, top_bottom_padding);
|
|
228
|
+
|
|
229
|
+
// Center line
|
|
230
|
+
this.ctx.beginPath();
|
|
231
|
+
this.ctx.lineWidth = 1;
|
|
232
|
+
this.ctx.strokeStyle = this.center_line_color;
|
|
233
|
+
this.ctx.moveTo(0, this.ctx.canvas.height / 2);
|
|
234
|
+
this.ctx.lineTo(this.ctx.canvas.width, this.ctx.canvas.height / 2);
|
|
235
|
+
this.ctx.stroke();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
draw_max_label(value)
|
|
239
|
+
{
|
|
240
|
+
this.ctx.font = '20px Arial';
|
|
241
|
+
this.ctx.fillStyle = this.text_color;
|
|
242
|
+
this.ctx.textBaseline = 'hanging';
|
|
243
|
+
this.ctx.fillText(value.toFixed(1), 5, 8);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
lerp(a, b, t)
|
|
247
|
+
{
|
|
248
|
+
return (1 - t) * a + b * t;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export { DataPlotter };
|
package/types/DataPlotter.d.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
export class DataPlotter {
|
|
2
|
-
constructor({ id, name, container_selector, width, height, raw_data_suffix }: {
|
|
3
|
-
id?: string;
|
|
4
|
-
name?: string;
|
|
5
|
-
container_selector?: any;
|
|
6
|
-
width?: number;
|
|
7
|
-
height?: number;
|
|
8
|
-
raw_data_suffix?: string;
|
|
9
|
-
});
|
|
10
|
-
container: HTMLDivElement;
|
|
11
|
-
canvas: HTMLCanvasElement;
|
|
12
|
-
raw_data_label: HTMLDivElement;
|
|
13
|
-
raw_data_suffix: string;
|
|
14
|
-
ctx: CanvasRenderingContext2D;
|
|
15
|
-
live_data_array: number[];
|
|
16
|
-
on_resize(width: any, height: any): void;
|
|
17
|
-
draw_function(func: any, min_x?: number, max_x?: number, graph_height?: number, color?: string, thick?: number): void;
|
|
18
|
-
draw_array(array: any, graph_height?: number, baseline?: number, centered_origin?: boolean, color?: string, thick?: number): void;
|
|
19
|
-
draw_static_live_array(value: any, graph_height?: number, baseline?: number, centered_origin?: boolean, color?: string, thick?: number): void;
|
|
20
|
-
draw_dynamic_live_array(value: any, baseline?: number, centered_origin?: boolean, color?: string, thick?: number): void;
|
|
21
|
-
}
|
|
1
|
+
export class DataPlotter {
|
|
2
|
+
constructor({ id, name, container_selector, width, height, raw_data_suffix }: {
|
|
3
|
+
id?: string;
|
|
4
|
+
name?: string;
|
|
5
|
+
container_selector?: any;
|
|
6
|
+
width?: number;
|
|
7
|
+
height?: number;
|
|
8
|
+
raw_data_suffix?: string;
|
|
9
|
+
});
|
|
10
|
+
container: HTMLDivElement;
|
|
11
|
+
canvas: HTMLCanvasElement;
|
|
12
|
+
raw_data_label: HTMLDivElement;
|
|
13
|
+
raw_data_suffix: string;
|
|
14
|
+
ctx: CanvasRenderingContext2D;
|
|
15
|
+
live_data_array: number[];
|
|
16
|
+
on_resize(width: any, height: any): void;
|
|
17
|
+
draw_function(func: any, min_x?: number, max_x?: number, graph_height?: number, color?: string, thick?: number): void;
|
|
18
|
+
draw_array(array: any, graph_height?: number, baseline?: number, centered_origin?: boolean, color?: string, thick?: number): void;
|
|
19
|
+
draw_static_live_array(value: any, graph_height?: number, baseline?: number, centered_origin?: boolean, color?: string, thick?: number): void;
|
|
20
|
+
draw_dynamic_live_array(value: any, baseline?: number, centered_origin?: boolean, color?: string, thick?: number): void;
|
|
21
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { DataPlotter } from './DataPlotter';
|
|
2
|
-
export { DataPlotter };
|
|
1
|
+
import { DataPlotter } from './DataPlotter';
|
|
2
|
+
export { DataPlotter };
|