@eox/chart 2.0.0-alpha3 → 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-d9b1cb92.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-alpha3",
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",
@@ -22,6 +22,7 @@ class ChartControls {
22
22
  this.generateTimeSelectionOptions();
23
23
  this.generateTimeAggregationOptions();
24
24
  this.addNormalizeCheckbox();
25
+ this.addShowMinMax();
25
26
  const spinner = document.createElement("span");
26
27
  spinner.id = "loadingIndicator";
27
28
  spinner.className = "loader hidden";
@@ -37,7 +38,7 @@ class ChartControls {
37
38
 
38
39
  var label = document.createElement("label");
39
40
  label.htmlFor = "normalize";
40
- label.appendChild(document.createTextNode("Normalize"));
41
+ label.appendChild(document.createTextNode("normalize"));
41
42
 
42
43
  this.element.appendChild(label);
43
44
  this.element.appendChild(checkbox);
@@ -46,6 +47,24 @@ class ChartControls {
46
47
  });
47
48
  }
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
+
49
68
  private generateTimeSelectionOptions() {
50
69
  const options = [
51
70
  { text: "3 months back", value: "months-3" },
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
  }
@@ -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,13 @@ class SignalsDataManager {
95
102
  maxRotation: 0,
96
103
  },
97
104
  },
98
- /*y: {
99
- min: 0,
100
- max: 0.5,
101
- },*/
102
105
  },
103
106
  plugins: {
104
107
  legend: {
105
108
  labels: {
106
- /*
107
109
  usePointStyle: true,
108
- pointStyle: "line",
109
- */
110
+ pointStyle: "rect",
111
+ padding: 5,
110
112
  sort: (a, b) => a.text.localeCompare(b.text),
111
113
  },
112
114
  position: "right",
@@ -183,7 +185,7 @@ class SignalsDataManager {
183
185
  this.checkLoadingStatus();
184
186
  this.chart.options = this.chartOptions;
185
187
  const datasets: ChartDataset[] = [];
186
- this.options.features.flat().forEach((key) => {
188
+ this.options.features.flat().forEach((key, dsIndex) => {
187
189
  // Find group
188
190
  let groupIndex: number = -1;
189
191
  this.options.features.forEach((group, idx) => {
@@ -216,23 +218,28 @@ class SignalsDataManager {
216
218
  let signalData = <any>data.map((datapoint) => {
217
219
  let ds = {};
218
220
  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;
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;
224
228
  }
225
229
  ds = {
226
230
  x: DateTime.fromISO(datapoint.date).setZone("UTC"),
227
231
  y: datapoint.basicStats.mean,
228
- yMin: [datapoint.basicStats.min],
229
- yMax: [datapoint.basicStats.max],
232
+ yMin: datapoint.basicStats.min,
233
+ yMax: datapoint.basicStats.max,
230
234
  };
231
235
  actualDataAdded = true;
232
236
  }
233
237
  return ds;
234
238
  });
235
239
  if (actualDataAdded && this.timeAggregation) {
240
+ // If we aggregate data we recalculate min max so we reset it
241
+ max = Number.MIN_VALUE;
242
+ min = Number.MAX_VALUE;
236
243
  const aggrData = [];
237
244
  let currDataIdx = 0;
238
245
  let currDate = DateTime.fromObject({
@@ -242,6 +249,8 @@ class SignalsDataManager {
242
249
  });
243
250
  while (currDate < this.endTime) {
244
251
  let dataSum = 0;
252
+ let minSum = 0;
253
+ let maxSum = 0;
245
254
  while (
246
255
  currDataIdx < signalData.length &&
247
256
  signalData[currDataIdx].x < currDate
@@ -253,17 +262,32 @@ class SignalsDataManager {
253
262
  currDataIdx < signalData.length &&
254
263
  signalData[currDataIdx].x < currDate.plus(this.timeAggregation)
255
264
  ) {
256
- dataSum += signalData[currDataIdx].y;
265
+ const sd = signalData[currDataIdx];
266
+ dataSum += sd.y;
267
+ minSum += sd.yMin;
268
+ maxSum += sd.yMax;
257
269
  currDataIdx++;
258
270
  }
259
271
  const x2 = currDataIdx;
260
272
  const count = x2 - x1;
261
273
  // If datapoints are within the interval we push the aggregation to the aggregated data
262
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
+ }
263
285
  // TODO: can we save a time interval here?
264
286
  aggrData.push({
265
287
  x: currDate,
266
- y: dataSum / count,
288
+ y: yMean,
289
+ yMin: yMinMean,
290
+ yMax: yMaxMean,
267
291
  });
268
292
  }
269
293
  currDate = currDate.plus(this.timeAggregation);
@@ -273,26 +297,52 @@ class SignalsDataManager {
273
297
 
274
298
  if (actualDataAdded && this.options.normalize) {
275
299
  signalData = signalData.map(
276
- (dp: {
277
- x: DateTime;
278
- y: number;
279
- yMin: number[];
280
- yMax: number[];
281
- }) => {
300
+ (dp: { x: DateTime; y: number; yMin: number; yMax: number }) => {
282
301
  dp.y = (dp.y - min) / (max - min);
302
+ dp.yMin = (dp.yMin - min) / (max - min);
303
+ dp.yMax = (dp.yMax - min) / (max - min);
283
304
  return dp;
284
305
  }
285
306
  );
286
307
  }
287
308
 
288
- datasets.push({
289
- // type: "lineWithErrorBars",
309
+ let ds: ChartDataset = {
290
310
  type: "line",
291
311
  label: key,
292
- // casting to any because an array of object should also be possible
293
312
  data: signalData,
294
313
  hidden: !this.activeFields.includes(key),
295
- });
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);
296
346
  }
297
347
  });
298
348
 
@@ -391,6 +441,11 @@ class SignalsDataManager {
391
441
  this.updateChart();
392
442
  }
393
443
 
444
+ setShowMinMax(showMinMax: boolean) {
445
+ this.options.showMinMax = showMinMax;
446
+ this.updateChart();
447
+ }
448
+
394
449
  setTimeAggregation(
395
450
  aggregation: {
396
451
  day?: number;