@eox/chart 2.0.0-alpha2 → 2.0.0-alpha4

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,17 @@
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
+ *{font-family: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"}
8
+ label{
9
+ font-size: 0.87em;
10
+ display: inline-block;
11
+ vertical-align: middle;
12
+ }
13
+ input {
14
+ vertical-align: middle;
15
+ }
7
16
  html,
8
17
  body {
9
18
  margin: 0;
@@ -61,7 +70,7 @@
61
70
  padding-top: 30px;
62
71
  }
63
72
  </style>
64
- <script type="module" crossorigin src="./assets/main-7d9c1cfa.js"></script>
73
+ <script type="module" crossorigin src="./assets/main-998f4938.js"></script>
65
74
  </head>
66
75
  <body>
67
76
  <div id="controls" class="controls" data-cy="controls"></div>
package/index.html CHANGED
@@ -2,8 +2,17 @@
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
+ *{font-family: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"}
8
+ label{
9
+ font-size: 0.87em;
10
+ display: inline-block;
11
+ vertical-align: middle;
12
+ }
13
+ input {
14
+ vertical-align: middle;
15
+ }
7
16
  html,
8
17
  body {
9
18
  margin: 0;
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@eox/chart",
3
- "version": "2.0.0-alpha2",
3
+ "version": "2.0.0-alpha4",
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",
@@ -1,36 +1,77 @@
1
1
  import { DateTime } from "luxon";
2
- import SignalsDataManager from "./signalsDataManager";
2
+ import SignalsDataManager, { SDMOptions } from "./signalsDataManager";
3
3
 
4
4
  export interface ChartControlOptions {}
5
5
 
6
6
  class ChartControls {
7
7
  element: HTMLElement;
8
8
  sdm: SignalsDataManager;
9
+ sdmOptions: SDMOptions;
9
10
  options?: ChartControlOptions;
10
11
 
11
12
  constructor(
12
13
  element: HTMLElement,
13
14
  sdm: SignalsDataManager,
15
+ sdmOptions: SDMOptions,
14
16
  options?: ChartControlOptions
15
17
  ) {
16
18
  this.element = element;
17
19
  this.sdm = sdm;
18
20
  this.options = options;
21
+ this.sdmOptions = sdmOptions;
19
22
  this.generateTimeSelectionOptions();
20
23
  this.generateTimeAggregationOptions();
24
+ this.addNormalizeCheckbox();
25
+ this.addShowMinMax();
21
26
  const spinner = document.createElement("span");
22
27
  spinner.id = "loadingIndicator";
23
28
  spinner.className = "loader hidden";
24
29
  this.element.appendChild(spinner);
25
30
  }
26
31
 
32
+ private addNormalizeCheckbox() {
33
+ var checkbox = document.createElement("input");
34
+ checkbox.type = "checkbox";
35
+ checkbox.id = "normalize";
36
+ checkbox.value = "normalize";
37
+ checkbox.checked = this.sdmOptions.normalize;
38
+
39
+ var label = document.createElement("label");
40
+ label.htmlFor = "normalize";
41
+ label.appendChild(document.createTextNode("normalize"));
42
+
43
+ this.element.appendChild(label);
44
+ this.element.appendChild(checkbox);
45
+ checkbox.addEventListener("change", () => {
46
+ this.sdm.setNormalize(checkbox.checked);
47
+ });
48
+ }
49
+
50
+ private addShowMinMax() {
51
+ var checkbox = document.createElement("input");
52
+ checkbox.type = "checkbox";
53
+ checkbox.id = "showMinMax";
54
+ checkbox.value = "showMinMax";
55
+ checkbox.checked = this.sdmOptions.showMinMax;
56
+
57
+ var label = document.createElement("label");
58
+ label.htmlFor = "showMinMax";
59
+ label.appendChild(document.createTextNode("show min/max"));
60
+
61
+ this.element.appendChild(label);
62
+ this.element.appendChild(checkbox);
63
+ checkbox.addEventListener("change", () => {
64
+ this.sdm.setShowMinMax(checkbox.checked);
65
+ });
66
+ }
67
+
27
68
  private generateTimeSelectionOptions() {
28
69
  const options = [
29
- { text: "3 months back", value: "months3" },
30
- { text: "6 months back", value: "months6" },
31
- { text: "1 year back", value: "year1" },
32
- { text: "2 years back", value: "year2" },
33
- { text: "3 years back", value: "year3" },
70
+ { text: "3 months back", value: "months-3" },
71
+ { text: "6 months back", value: "months-6" },
72
+ { text: "1 year back", value: "year-1" },
73
+ { text: "2 years back", value: "year-2" },
74
+ { text: "3 years back", value: "year-3" },
34
75
  ];
35
76
  const selectEl = document.createElement("select");
36
77
  options.forEach((entry) => {
@@ -42,21 +83,19 @@ class ChartControls {
42
83
  this.element.appendChild(selectEl);
43
84
  selectEl.addEventListener("change", (evt) => {
44
85
  const currDate = DateTime.now();
45
- switch ((evt.target as HTMLSelectElement).value) {
46
- case "months3":
47
- this.sdm.setTimeInterval(currDate.minus({ months: 3 }), currDate);
48
- break;
49
- case "months6":
50
- this.sdm.setTimeInterval(currDate.minus({ months: 6 }), currDate);
51
- break;
52
- case "year1":
53
- this.sdm.setTimeInterval(currDate.minus({ years: 1 }), currDate);
54
- break;
55
- case "year2":
56
- this.sdm.setTimeInterval(currDate.minus({ years: 2 }), currDate);
86
+ const [type, val] = (evt.target as HTMLSelectElement).value.split("-");
87
+ switch (type) {
88
+ case "month":
89
+ this.sdm.setTimeInterval(
90
+ currDate.minus({ month: Number(val) }),
91
+ currDate
92
+ );
57
93
  break;
58
- case "year3":
59
- this.sdm.setTimeInterval(currDate.minus({ years: 3 }), currDate);
94
+ case "year":
95
+ this.sdm.setTimeInterval(
96
+ currDate.minus({ year: Number(val) }),
97
+ currDate
98
+ );
60
99
  break;
61
100
  }
62
101
  });
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
@@ -77,7 +77,7 @@ const setSignalsEndpoint = (options: {
77
77
  endTime?: string;
78
78
  }) => {
79
79
  sdmInstance = new SignalsDataManager(eoxchart, options);
80
- new ChartControls(document.getElementById("controls"), sdmInstance);
80
+ new ChartControls(document.getElementById("controls"), sdmInstance, options);
81
81
  sdmInstance.setActiveFields(options.active);
82
82
  };
83
83
 
@@ -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 {
@@ -48,6 +50,10 @@ export interface SDMOptions {
48
50
  startTime?: string;
49
51
  endTime?: string;
50
52
  timeAggregation?: object;
53
+ retries?: number;
54
+ normalize?: boolean;
55
+ showMinMax?: boolean;
56
+ colors?: string[];
51
57
  }
52
58
 
53
59
  class SignalsDataManager {
@@ -74,6 +80,13 @@ class SignalsDataManager {
74
80
  if (this.options.timeAggregation) {
75
81
  this.timeAggregation = this.options.timeAggregation;
76
82
  }
83
+ this.options.retries = this.options.retries ? this.options.retries : 5;
84
+ this.options.normalize = this.options.normalize
85
+ ? this.options.normalize
86
+ : false;
87
+ this.options.showMinMax = this.options.showMinMax
88
+ ? this.options.showMinMax
89
+ : false;
77
90
  this.dataStorage = {};
78
91
  // Initialize data storage
79
92
  this.options.features.forEach((_, idx) => (this.dataStorage[idx] = {}));
@@ -89,18 +102,13 @@ class SignalsDataManager {
89
102
  maxRotation: 0,
90
103
  },
91
104
  },
92
- /*y: {
93
- min: 0,
94
- max: 0.5,
95
- },*/
96
105
  },
97
106
  plugins: {
98
107
  legend: {
99
108
  labels: {
100
- /*
101
109
  usePointStyle: true,
102
- pointStyle: "line",
103
- */
110
+ pointStyle: "rect",
111
+ padding: 5,
104
112
  sort: (a, b) => a.text.localeCompare(b.text),
105
113
  },
106
114
  position: "right",
@@ -166,7 +174,7 @@ class SignalsDataManager {
166
174
  `Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`
167
175
  );
168
176
  },
169
- retries: 1,
177
+ retries: this.options.retries,
170
178
  }).then(function (res) {
171
179
  return res.json();
172
180
  });
@@ -177,7 +185,7 @@ class SignalsDataManager {
177
185
  this.checkLoadingStatus();
178
186
  this.chart.options = this.chartOptions;
179
187
  const datasets: ChartDataset[] = [];
180
- this.options.features.flat().forEach((key) => {
188
+ this.options.features.flat().forEach((key, dsIndex) => {
181
189
  // Find group
182
190
  let groupIndex: number = -1;
183
191
  this.options.features.forEach((group, idx) => {
@@ -205,22 +213,33 @@ class SignalsDataManager {
205
213
  });
206
214
 
207
215
  let actualDataAdded = false;
216
+ let max = Number.MIN_VALUE;
217
+ let min = Number.MAX_VALUE;
208
218
  let signalData = <any>data.map((datapoint) => {
209
219
  let ds = {};
210
220
  if (datapoint && datapoint.date !== "missing") {
221
+ const stats = datapoint.basicStats;
222
+ if (this.options.showMinMax) {
223
+ min = stats.min < min ? stats.min : min;
224
+ max = stats.max > max ? stats.max : max;
225
+ } else {
226
+ min = stats.mean < min ? stats.mean : min;
227
+ max = stats.mean > max ? stats.mean : max;
228
+ }
211
229
  ds = {
212
230
  x: DateTime.fromISO(datapoint.date).setZone("UTC"),
213
231
  y: datapoint.basicStats.mean,
214
- yMin: [datapoint.basicStats.min],
215
- yMax: [datapoint.basicStats.max],
232
+ yMin: datapoint.basicStats.min,
233
+ yMax: datapoint.basicStats.max,
216
234
  };
217
235
  actualDataAdded = true;
218
236
  }
219
237
  return ds;
220
238
  });
221
239
  if (actualDataAdded && this.timeAggregation) {
222
- console.log("applying aggregation");
223
- console.log(signalData);
240
+ // If we aggregate data we recalculate min max so we reset it
241
+ max = Number.MIN_VALUE;
242
+ min = Number.MAX_VALUE;
224
243
  const aggrData = [];
225
244
  let currDataIdx = 0;
226
245
  let currDate = DateTime.fromObject({
@@ -230,6 +249,8 @@ class SignalsDataManager {
230
249
  });
231
250
  while (currDate < this.endTime) {
232
251
  let dataSum = 0;
252
+ let minSum = 0;
253
+ let maxSum = 0;
233
254
  while (
234
255
  currDataIdx < signalData.length &&
235
256
  signalData[currDataIdx].x < currDate
@@ -241,17 +262,32 @@ class SignalsDataManager {
241
262
  currDataIdx < signalData.length &&
242
263
  signalData[currDataIdx].x < currDate.plus(this.timeAggregation)
243
264
  ) {
244
- dataSum += signalData[currDataIdx].y;
265
+ const sd = signalData[currDataIdx];
266
+ dataSum += sd.y;
267
+ minSum += sd.yMin;
268
+ maxSum += sd.yMax;
245
269
  currDataIdx++;
246
270
  }
247
271
  const x2 = currDataIdx;
248
272
  const count = x2 - x1;
249
273
  // If datapoints are within the interval we push the aggregation to the aggregated data
250
274
  if (count > 0) {
275
+ const yMean = dataSum / count;
276
+ const yMinMean = minSum / count;
277
+ const yMaxMean = maxSum / count;
278
+ if (this.options.showMinMax) {
279
+ min = yMinMean < min ? yMinMean : min;
280
+ max = yMaxMean > max ? yMaxMean : max;
281
+ } else {
282
+ min = yMean < min ? yMean : min;
283
+ max = yMean > max ? yMean : max;
284
+ }
251
285
  // TODO: can we save a time interval here?
252
286
  aggrData.push({
253
287
  x: currDate,
254
- y: dataSum / count,
288
+ y: yMean,
289
+ yMin: yMinMean,
290
+ yMax: yMaxMean,
255
291
  });
256
292
  }
257
293
  currDate = currDate.plus(this.timeAggregation);
@@ -259,14 +295,54 @@ class SignalsDataManager {
259
295
  signalData = aggrData;
260
296
  }
261
297
 
262
- datasets.push({
263
- // type: "lineWithErrorBars",
298
+ if (actualDataAdded && this.options.normalize) {
299
+ signalData = signalData.map(
300
+ (dp: { x: DateTime; y: number; yMin: number; yMax: number }) => {
301
+ dp.y = (dp.y - min) / (max - min);
302
+ dp.yMin = (dp.yMin - min) / (max - min);
303
+ dp.yMax = (dp.yMax - min) / (max - min);
304
+ return dp;
305
+ }
306
+ );
307
+ }
308
+
309
+ let ds: ChartDataset = {
264
310
  type: "line",
265
311
  label: key,
266
- // casting to any because an array of object should also be possible
267
312
  data: signalData,
268
313
  hidden: !this.activeFields.includes(key),
269
- });
314
+ };
315
+ if (this.options.showMinMax) {
316
+ ds = {
317
+ type: "lineWithErrorBars",
318
+ label: key,
319
+ data: signalData,
320
+ hidden: !this.activeFields.includes(key),
321
+ errorBarWhiskerLineWidth: 2,
322
+ errorBarColor: "#00000000",
323
+ };
324
+ }
325
+ if (this.options.colors && this.options.colors.length >= dsIndex) {
326
+ const color = this.options.colors[dsIndex];
327
+ ds.backgroundColor = color;
328
+ ds.borderColor = color;
329
+
330
+ if (this.options.showMinMax) {
331
+ ds = {
332
+ type: "lineWithErrorBars",
333
+ label: key,
334
+ data: signalData,
335
+ hidden: !this.activeFields.includes(key),
336
+ errorBarWhiskerLineWidth: 2,
337
+ errorBarColor: "#00000000",
338
+ errorBarWhiskerColor: color,
339
+ errorBarWhiskerSize: 10,
340
+ backgroundColor: color,
341
+ borderColor: color,
342
+ };
343
+ }
344
+ }
345
+ datasets.push(ds);
270
346
  }
271
347
  });
272
348
 
@@ -360,6 +436,16 @@ class SignalsDataManager {
360
436
  this.updateChart();
361
437
  }
362
438
 
439
+ setNormalize(normalize: boolean) {
440
+ this.options.normalize = normalize;
441
+ this.updateChart();
442
+ }
443
+
444
+ setShowMinMax(showMinMax: boolean) {
445
+ this.options.showMinMax = showMinMax;
446
+ this.updateChart();
447
+ }
448
+
363
449
  setTimeAggregation(
364
450
  aggregation: {
365
451
  day?: number;