@eox/chart 2.0.0-alpha3 → 2.0.0-alpha5

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/dist/index.html CHANGED
@@ -2,8 +2,15 @@
2
2
  <html lang="en">
3
3
  <head>
4
4
  <meta charset="UTF-8" />
5
- <title>Simple Chart</title>
5
+ <title>EOxElements Chart</title>
6
6
  <style>
7
+ *{
8
+ font-family: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif";
9
+ font-size: 0.95em;
10
+ }
11
+ label{
12
+ margin-top: 2.5px;
13
+ }
7
14
  html,
8
15
  body {
9
16
  margin: 0;
@@ -55,13 +62,18 @@
55
62
  #controls {
56
63
  position: absolute;
57
64
  width: 100%;
58
- height: 30px;
65
+ display: flex;
66
+ gap: 5px;
67
+ flex-direction: row;
68
+ }
69
+ .parsingError {
70
+ background-color : #ff6d6d;
59
71
  }
60
72
  #chart {
61
73
  padding-top: 30px;
62
74
  }
63
75
  </style>
64
- <script type="module" crossorigin src="./assets/main-d9b1cb92.js"></script>
76
+ <script type="module" crossorigin src="./assets/main-3b62be28.js"></script>
65
77
  </head>
66
78
  <body>
67
79
  <div id="controls" class="controls" data-cy="controls"></div>
package/index.html CHANGED
@@ -2,8 +2,15 @@
2
2
  <html lang="en">
3
3
  <head>
4
4
  <meta charset="UTF-8" />
5
- <title>Simple Chart</title>
5
+ <title>EOxElements Chart</title>
6
6
  <style>
7
+ *{
8
+ font-family: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif";
9
+ font-size: 0.95em;
10
+ }
11
+ label{
12
+ margin-top: 2.5px;
13
+ }
7
14
  html,
8
15
  body {
9
16
  margin: 0;
@@ -55,7 +62,12 @@
55
62
  #controls {
56
63
  position: absolute;
57
64
  width: 100%;
58
- height: 30px;
65
+ display: flex;
66
+ gap: 5px;
67
+ flex-direction: row;
68
+ }
69
+ .parsingError {
70
+ background-color : #ff6d6d;
59
71
  }
60
72
  #chart {
61
73
  padding-top: 30px;
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@eox/chart",
3
- "version": "2.0.0-alpha3",
3
+ "version": "2.0.0-alpha5",
4
4
  "dependencies": {
5
5
  "chart.js": "^4.2.1",
6
6
  "chartjs-adapter-luxon": "^1.3.1",
7
- "chartjs-chart-error-bars": "^4.1.1"
7
+ "chartjs-chart-error-bars": "^4.1.2"
8
8
  },
9
9
  "devDependencies": {
10
10
  "@eox/eslint-config": "^1.0.0",
@@ -19,15 +19,56 @@ class ChartControls {
19
19
  this.sdm = sdm;
20
20
  this.options = options;
21
21
  this.sdmOptions = sdmOptions;
22
- this.generateTimeSelectionOptions();
23
22
  this.generateTimeAggregationOptions();
24
23
  this.addNormalizeCheckbox();
24
+ this.addShowMinMax();
25
+ this.addCSVDownload();
26
+ this.generateTimeSelectionOptions();
25
27
  const spinner = document.createElement("span");
26
28
  spinner.id = "loadingIndicator";
27
29
  spinner.className = "loader hidden";
28
30
  this.element.appendChild(spinner);
29
31
  }
30
32
 
33
+ private removeStartEndInputs() {
34
+ const start = document.getElementById("startInput");
35
+ const end = document.getElementById("endInput");
36
+ const button = document.getElementById("setTime");
37
+ if (start && end && button) {
38
+ start.remove();
39
+ end.remove();
40
+ button.remove();
41
+ }
42
+ }
43
+ private addStartEndInputs() {
44
+ const startEl = document.createElement("input");
45
+ startEl.id = "startInput";
46
+ startEl.size = 8;
47
+ startEl.value = this.sdm.startTime.toISODate();
48
+ const endEl = document.createElement("input");
49
+ endEl.id = "endInput";
50
+ endEl.size = 8;
51
+ endEl.value = this.sdm.endTime.toISODate();
52
+ this.element.appendChild(startEl);
53
+ this.element.appendChild(endEl);
54
+ var button = document.createElement("button");
55
+ button.id = "setTime";
56
+ button.textContent = "ok";
57
+
58
+ this.element.appendChild(button);
59
+ button.addEventListener("click", () => {
60
+ const startDate = DateTime.fromISO(startEl.value);
61
+ const endDate = DateTime.fromISO(endEl.value);
62
+ if (startDate.isValid && endDate.isValid) {
63
+ this.sdm.setTimeInterval(startDate, endDate);
64
+ this.removeStartEndInputs();
65
+ } else {
66
+ startEl.className = startDate.isValid ? "" : "parsingError";
67
+ endEl.className = endDate.isValid ? "" : "parsingError";
68
+ }
69
+ });
70
+ }
71
+
31
72
  private addNormalizeCheckbox() {
32
73
  var checkbox = document.createElement("input");
33
74
  checkbox.type = "checkbox";
@@ -37,7 +78,7 @@ class ChartControls {
37
78
 
38
79
  var label = document.createElement("label");
39
80
  label.htmlFor = "normalize";
40
- label.appendChild(document.createTextNode("Normalize"));
81
+ label.appendChild(document.createTextNode("normalize"));
41
82
 
42
83
  this.element.appendChild(label);
43
84
  this.element.appendChild(checkbox);
@@ -46,13 +87,43 @@ class ChartControls {
46
87
  });
47
88
  }
48
89
 
90
+ private addShowMinMax() {
91
+ var checkbox = document.createElement("input");
92
+ checkbox.type = "checkbox";
93
+ checkbox.id = "showMinMax";
94
+ checkbox.value = "showMinMax";
95
+ checkbox.checked = this.sdmOptions.showMinMax;
96
+
97
+ var label = document.createElement("label");
98
+ label.htmlFor = "showMinMax";
99
+ label.appendChild(document.createTextNode("show min/max"));
100
+
101
+ this.element.appendChild(label);
102
+ this.element.appendChild(checkbox);
103
+ checkbox.addEventListener("change", () => {
104
+ this.sdm.setShowMinMax(checkbox.checked);
105
+ });
106
+ }
107
+
108
+ private addCSVDownload() {
109
+ var button = document.createElement("button");
110
+ button.id = "csvDownload";
111
+ button.textContent = "download csv";
112
+
113
+ this.element.appendChild(button);
114
+ button.addEventListener("click", () => {
115
+ this.sdm.triggerCSVDownload();
116
+ });
117
+ }
118
+
49
119
  private generateTimeSelectionOptions() {
50
120
  const options = [
51
- { text: "3 months back", value: "months-3" },
52
- { text: "6 months back", value: "months-6" },
121
+ { text: "3 months back", value: "month-3" },
122
+ { text: "6 months back", value: "month-6" },
53
123
  { text: "1 year back", value: "year-1" },
54
124
  { text: "2 years back", value: "year-2" },
55
125
  { text: "3 years back", value: "year-3" },
126
+ { text: "custom", value: "custom" },
56
127
  ];
57
128
  const selectEl = document.createElement("select");
58
129
  options.forEach((entry) => {
@@ -65,6 +136,7 @@ class ChartControls {
65
136
  selectEl.addEventListener("change", (evt) => {
66
137
  const currDate = DateTime.now();
67
138
  const [type, val] = (evt.target as HTMLSelectElement).value.split("-");
139
+ this.removeStartEndInputs();
68
140
  switch (type) {
69
141
  case "month":
70
142
  this.sdm.setTimeInterval(
@@ -78,6 +150,9 @@ class ChartControls {
78
150
  currDate
79
151
  );
80
152
  break;
153
+ case "custom":
154
+ this.addStartEndInputs();
155
+ break;
81
156
  }
82
157
  });
83
158
  }
package/src/interface.ts CHANGED
@@ -21,6 +21,7 @@ class EOxChart {
21
21
  timeInterval: object;
22
22
  startTime?: string;
23
23
  endTime?: string;
24
+ colors?: string[];
24
25
  }) {
25
26
  port1.postMessage({ type: "setSignalsEndpoint", body: { options } });
26
27
  }
package/src/main.ts CHANGED
@@ -10,6 +10,27 @@ const eoxchart = new Chart(
10
10
  labels: [],
11
11
  datasets: [],
12
12
  },
13
+ plugins: [
14
+ {
15
+ id: "selectionLineHighlight",
16
+ afterDraw: (chart) => {
17
+ const active = chart.tooltip.getActiveElements();
18
+ if (active?.length) {
19
+ let x = active[0].element.x;
20
+ let yAxis = chart.scales.y;
21
+ let ctx = chart.ctx;
22
+ ctx.save();
23
+ ctx.beginPath();
24
+ ctx.moveTo(x, yAxis.top);
25
+ ctx.lineTo(x, yAxis.bottom);
26
+ ctx.lineWidth = 1;
27
+ ctx.strokeStyle = "rgba(0, 0, 255, 0.4)";
28
+ ctx.stroke();
29
+ ctx.restore();
30
+ }
31
+ },
32
+ },
33
+ ],
13
34
  }
14
35
  );
15
36
 
@@ -1,10 +1,12 @@
1
1
  import Chart, {
2
- ChartDataset /*, LinearScale, CategoryScale */,
2
+ ChartDataset,
3
+ LinearScale,
4
+ CategoryScale,
3
5
  ChartOptions,
4
6
  } from "chart.js/auto";
5
7
  import pRetry, { AbortError } from "p-retry";
6
8
  import { DateTime } from "luxon";
7
- /*
9
+
8
10
  import {
9
11
  LineWithErrorBarsController,
10
12
  LineWithErrorBarsChart,
@@ -21,7 +23,7 @@ Chart.register(
21
23
  ScatterWithErrorBarsController,
22
24
  LinearScale,
23
25
  CategoryScale
24
- );*/
26
+ );
25
27
 
26
28
  type status = "ready" | "loading" | "error";
27
29
  interface DSDict {
@@ -50,6 +52,8 @@ export interface SDMOptions {
50
52
  timeAggregation?: object;
51
53
  retries?: number;
52
54
  normalize?: boolean;
55
+ showMinMax?: boolean;
56
+ colors?: string[];
53
57
  }
54
58
 
55
59
  class SignalsDataManager {
@@ -80,6 +84,9 @@ class SignalsDataManager {
80
84
  this.options.normalize = this.options.normalize
81
85
  ? this.options.normalize
82
86
  : false;
87
+ this.options.showMinMax = this.options.showMinMax
88
+ ? this.options.showMinMax
89
+ : false;
83
90
  this.dataStorage = {};
84
91
  // Initialize data storage
85
92
  this.options.features.forEach((_, idx) => (this.dataStorage[idx] = {}));
@@ -95,18 +102,18 @@ class SignalsDataManager {
95
102
  maxRotation: 0,
96
103
  },
97
104
  },
98
- /*y: {
99
- min: 0,
100
- max: 0.5,
101
- },*/
105
+ },
106
+ interaction: {
107
+ axis: "x",
108
+ mode: "nearest",
109
+ intersect: false,
102
110
  },
103
111
  plugins: {
104
112
  legend: {
105
113
  labels: {
106
- /*
107
114
  usePointStyle: true,
108
- pointStyle: "line",
109
- */
115
+ pointStyle: "rect",
116
+ padding: 5,
110
117
  sort: (a, b) => a.text.localeCompare(b.text),
111
118
  },
112
119
  position: "right",
@@ -121,6 +128,20 @@ class SignalsDataManager {
121
128
  }
122
129
  },
123
130
  },
131
+ tooltip: {
132
+ callbacks: {
133
+ title: (tooltipItem) => {
134
+ const raw: any = tooltipItem[0]?.raw;
135
+ return raw.x.toISODate();
136
+ },
137
+ label: (tooltipItem) => {
138
+ const raw: any = tooltipItem.raw;
139
+ return `${tooltipItem.dataset.label}: ↔ ${raw.y.toPrecision(
140
+ 3
141
+ )}; ↓ ${raw.yMin.toPrecision(3)}, ↑ ${raw.yMax.toPrecision(3)}`;
142
+ },
143
+ },
144
+ },
124
145
  },
125
146
  };
126
147
  }
@@ -183,7 +204,7 @@ class SignalsDataManager {
183
204
  this.checkLoadingStatus();
184
205
  this.chart.options = this.chartOptions;
185
206
  const datasets: ChartDataset[] = [];
186
- this.options.features.flat().forEach((key) => {
207
+ this.options.features.flat().forEach((key, dsIndex) => {
187
208
  // Find group
188
209
  let groupIndex: number = -1;
189
210
  this.options.features.forEach((group, idx) => {
@@ -216,23 +237,28 @@ class SignalsDataManager {
216
237
  let signalData = <any>data.map((datapoint) => {
217
238
  let ds = {};
218
239
  if (datapoint && datapoint.date !== "missing") {
219
- if (datapoint.basicStats.mean < min) {
220
- min = datapoint.basicStats.mean;
221
- }
222
- if (datapoint.basicStats.mean > max) {
223
- max = datapoint.basicStats.mean;
240
+ const stats = datapoint.basicStats;
241
+ if (this.options.showMinMax) {
242
+ min = stats.min < min ? stats.min : min;
243
+ max = stats.max > max ? stats.max : max;
244
+ } else {
245
+ min = stats.mean < min ? stats.mean : min;
246
+ max = stats.mean > max ? stats.mean : max;
224
247
  }
225
248
  ds = {
226
249
  x: DateTime.fromISO(datapoint.date).setZone("UTC"),
227
250
  y: datapoint.basicStats.mean,
228
- yMin: [datapoint.basicStats.min],
229
- yMax: [datapoint.basicStats.max],
251
+ yMin: datapoint.basicStats.min,
252
+ yMax: datapoint.basicStats.max,
230
253
  };
231
254
  actualDataAdded = true;
232
255
  }
233
256
  return ds;
234
257
  });
235
258
  if (actualDataAdded && this.timeAggregation) {
259
+ // If we aggregate data we recalculate min max so we reset it
260
+ max = Number.MIN_VALUE;
261
+ min = Number.MAX_VALUE;
236
262
  const aggrData = [];
237
263
  let currDataIdx = 0;
238
264
  let currDate = DateTime.fromObject({
@@ -242,28 +268,47 @@ class SignalsDataManager {
242
268
  });
243
269
  while (currDate < this.endTime) {
244
270
  let dataSum = 0;
271
+ let minSum = 0;
272
+ let maxSum = 0;
245
273
  while (
246
274
  currDataIdx < signalData.length &&
247
- signalData[currDataIdx].x < currDate
275
+ (Object.keys(signalData[currDataIdx]).length === 0 ||
276
+ signalData[currDataIdx].x < currDate)
248
277
  ) {
249
278
  currDataIdx++;
250
279
  }
251
280
  const x1 = currDataIdx;
252
281
  while (
253
282
  currDataIdx < signalData.length &&
254
- signalData[currDataIdx].x < currDate.plus(this.timeAggregation)
283
+ (Object.keys(signalData[currDataIdx]).length === 0 ||
284
+ signalData[currDataIdx].x < currDate.plus(this.timeAggregation))
255
285
  ) {
256
- dataSum += signalData[currDataIdx].y;
286
+ const sd = signalData[currDataIdx];
287
+ dataSum += sd.y;
288
+ minSum += sd.yMin;
289
+ maxSum += sd.yMax;
257
290
  currDataIdx++;
258
291
  }
259
292
  const x2 = currDataIdx;
260
293
  const count = x2 - x1;
261
294
  // If datapoints are within the interval we push the aggregation to the aggregated data
262
295
  if (count > 0) {
296
+ const yMean = dataSum / count;
297
+ const yMinMean = minSum / count;
298
+ const yMaxMean = maxSum / count;
299
+ if (this.options.showMinMax) {
300
+ min = yMinMean < min ? yMinMean : min;
301
+ max = yMaxMean > max ? yMaxMean : max;
302
+ } else {
303
+ min = yMean < min ? yMean : min;
304
+ max = yMean > max ? yMean : max;
305
+ }
263
306
  // TODO: can we save a time interval here?
264
307
  aggrData.push({
265
308
  x: currDate,
266
- y: dataSum / count,
309
+ y: yMean,
310
+ yMin: yMinMean,
311
+ yMax: yMaxMean,
267
312
  });
268
313
  }
269
314
  currDate = currDate.plus(this.timeAggregation);
@@ -273,26 +318,52 @@ class SignalsDataManager {
273
318
 
274
319
  if (actualDataAdded && this.options.normalize) {
275
320
  signalData = signalData.map(
276
- (dp: {
277
- x: DateTime;
278
- y: number;
279
- yMin: number[];
280
- yMax: number[];
281
- }) => {
321
+ (dp: { x: DateTime; y: number; yMin: number; yMax: number }) => {
282
322
  dp.y = (dp.y - min) / (max - min);
323
+ dp.yMin = (dp.yMin - min) / (max - min);
324
+ dp.yMax = (dp.yMax - min) / (max - min);
283
325
  return dp;
284
326
  }
285
327
  );
286
328
  }
287
329
 
288
- datasets.push({
289
- // type: "lineWithErrorBars",
330
+ let ds: ChartDataset = {
290
331
  type: "line",
291
332
  label: key,
292
- // casting to any because an array of object should also be possible
293
333
  data: signalData,
294
334
  hidden: !this.activeFields.includes(key),
295
- });
335
+ };
336
+ if (this.options.showMinMax) {
337
+ ds = {
338
+ type: "lineWithErrorBars",
339
+ label: key,
340
+ data: signalData,
341
+ hidden: !this.activeFields.includes(key),
342
+ errorBarWhiskerLineWidth: 2,
343
+ errorBarColor: "#00000000",
344
+ };
345
+ }
346
+ if (this.options.colors && this.options.colors.length >= dsIndex) {
347
+ const color = this.options.colors[dsIndex];
348
+ ds.backgroundColor = color;
349
+ ds.borderColor = color;
350
+
351
+ if (this.options.showMinMax) {
352
+ ds = {
353
+ type: "lineWithErrorBars",
354
+ label: key,
355
+ data: signalData,
356
+ hidden: !this.activeFields.includes(key),
357
+ errorBarWhiskerLineWidth: 2,
358
+ errorBarColor: "#00000000",
359
+ errorBarWhiskerColor: color,
360
+ errorBarWhiskerSize: 10,
361
+ backgroundColor: color,
362
+ borderColor: color,
363
+ };
364
+ }
365
+ }
366
+ datasets.push(ds);
296
367
  }
297
368
  });
298
369
 
@@ -363,6 +434,91 @@ class SignalsDataManager {
363
434
  }
364
435
  }
365
436
 
437
+ triggerCSVDownload() {
438
+ const completeData: {
439
+ date: string;
440
+ basicStats?: { mean: number; min: number; max: number };
441
+ }[][] = [];
442
+ const headers: string[] = [];
443
+ const allSignals = this.activeFields;
444
+ allSignals.forEach((key, signalIdx) => {
445
+ // Find group
446
+ let groupIndex: number = -1;
447
+ this.options.features.forEach((group, idx) => {
448
+ if (group.includes(key)) {
449
+ groupIndex = idx;
450
+ }
451
+ });
452
+ if (groupIndex !== -1) {
453
+ let data: {
454
+ date: string;
455
+ basicStats?: { mean: number; min: number; max: number };
456
+ }[] = [];
457
+ Object.keys(this.dataStorage[groupIndex]).forEach((timekey) => {
458
+ const dsEntry = this.dataStorage[groupIndex][timekey];
459
+ if (dsEntry.status === "finished") {
460
+ if (
461
+ Object.keys(dsEntry.data).length !== 0 &&
462
+ Object.keys(dsEntry.data).includes(key)
463
+ ) {
464
+ data.push(...dsEntry.data[key]);
465
+ }
466
+ }
467
+ });
468
+ if (data.length > 0) {
469
+ headers.push(allSignals[signalIdx]);
470
+ completeData.push(data);
471
+ }
472
+ }
473
+ });
474
+ // We need to try and bring the different timed datasets into "same timegrid" lets do some binning
475
+ const binnedData: {
476
+ [key: string]: {
477
+ [key: string]: {
478
+ mean: number;
479
+ max: number;
480
+ min: number;
481
+ };
482
+ };
483
+ } = {};
484
+ headers.forEach((datakey, idx) => {
485
+ completeData[idx].forEach((dp) => {
486
+ if (binnedData[dp.date]) {
487
+ binnedData[dp.date][datakey] = dp.basicStats;
488
+ } else {
489
+ binnedData[dp.date] = {};
490
+ binnedData[dp.date][datakey] = dp.basicStats;
491
+ }
492
+ });
493
+ });
494
+
495
+ // Now we try to construct the csv adding empty values when timestamp not available for a dataset
496
+ let csv = "time,";
497
+ const allHeaders = headers.map((val) => [val, `${val}_max`, `${val}_min`]);
498
+ csv += allHeaders.flat().join(",");
499
+ csv += "\n";
500
+ Object.keys(binnedData).forEach((dateKey) => {
501
+ const row: (string | number)[] = [dateKey];
502
+ headers.forEach((signalKey) => {
503
+ const stats = binnedData[dateKey][signalKey]
504
+ ? binnedData[dateKey][signalKey]
505
+ : null;
506
+ if (stats != null) {
507
+ row.push(...[stats.mean, stats.max, stats.min]);
508
+ } else {
509
+ row.push(...["", "", ""]);
510
+ }
511
+ });
512
+ csv += row.join(",");
513
+ csv += "\n";
514
+ });
515
+ var hiddenElement = document.createElement("a");
516
+ hiddenElement.href = "data:text/csv;charset=utf-8," + encodeURI(csv);
517
+ hiddenElement.target = "_blank";
518
+ hiddenElement.download = "chart_data.csv";
519
+ hiddenElement.click();
520
+ }
521
+
366
522
  setGeometry(geometry: object) {
367
523
  // Clear all currently saved data
368
524
  this.dataStorage = {};
@@ -391,6 +547,11 @@ class SignalsDataManager {
391
547
  this.updateChart();
392
548
  }
393
549
 
550
+ setShowMinMax(showMinMax: boolean) {
551
+ this.options.showMinMax = showMinMax;
552
+ this.updateChart();
553
+ }
554
+
394
555
  setTimeAggregation(
395
556
  aggregation: {
396
557
  day?: number;