@eox/chart 2.0.0-alpha1 → 2.0.0-alpha11

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.
@@ -1,31 +1,129 @@
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;
19
- this.generateSelectionOptions();
21
+ this.sdmOptions = sdmOptions;
22
+ this.generateTimeAggregationOptions();
23
+ this.addNormalizeCheckbox();
24
+ this.addShowMinMax();
25
+ this.addCSVDownload();
26
+ this.generateTimeSelectionOptions();
27
+ const spinner = document.createElement("span");
28
+ spinner.id = "loadingIndicator";
29
+ spinner.className = "loader hidden";
30
+ this.element.appendChild(spinner);
31
+ }
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
+
72
+ private addNormalizeCheckbox() {
73
+ var checkbox = document.createElement("input");
74
+ checkbox.type = "checkbox";
75
+ checkbox.id = "normalize";
76
+ checkbox.value = "normalize";
77
+ checkbox.checked = this.sdmOptions.normalize;
78
+
79
+ var label = document.createElement("label");
80
+ label.htmlFor = "normalize";
81
+ label.appendChild(document.createTextNode("normalize"));
82
+
83
+ this.element.appendChild(label);
84
+ this.element.appendChild(checkbox);
85
+ checkbox.addEventListener("change", () => {
86
+ this.sdm.setNormalize(checkbox.checked);
87
+ });
88
+ }
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
+ });
20
117
  }
21
118
 
22
- private generateSelectionOptions() {
119
+ private generateTimeSelectionOptions() {
23
120
  const options = [
24
- { text: "3 months back", value: "months3" },
25
- { text: "6 months back", value: "months6" },
26
- { text: "1 year back", value: "year1" },
27
- { text: "2 years back", value: "year2" },
28
- { text: "3 years back", value: "year3" },
121
+ { text: "3 months back", value: "month-3" },
122
+ { text: "6 months back", value: "month-6" },
123
+ { text: "1 year back", value: "year-1" },
124
+ { text: "2 years back", value: "year-2" },
125
+ { text: "3 years back", value: "year-3" },
126
+ { text: "custom", value: "custom" },
29
127
  ];
30
128
  const selectEl = document.createElement("select");
31
129
  options.forEach((entry) => {
@@ -37,25 +135,64 @@ class ChartControls {
37
135
  this.element.appendChild(selectEl);
38
136
  selectEl.addEventListener("change", (evt) => {
39
137
  const currDate = DateTime.now();
40
- switch ((evt.target as HTMLSelectElement).value) {
41
- case "months3":
42
- this.sdm.setTimeInterval(currDate.minus({ months: 3 }), currDate);
138
+ const [type, val] = (evt.target as HTMLSelectElement).value.split("-");
139
+ this.removeStartEndInputs();
140
+ switch (type) {
141
+ case "month":
142
+ this.sdm.setTimeInterval(
143
+ currDate.minus({ month: Number(val) }),
144
+ currDate
145
+ );
146
+ break;
147
+ case "year":
148
+ this.sdm.setTimeInterval(
149
+ currDate.minus({ year: Number(val) }),
150
+ currDate
151
+ );
152
+ break;
153
+ case "custom":
154
+ this.addStartEndInputs();
43
155
  break;
44
- case "months6":
45
- this.sdm.setTimeInterval(currDate.minus({ months: 6 }), currDate);
156
+ }
157
+ });
158
+ }
159
+
160
+ private generateTimeAggregationOptions() {
161
+ const options = [
162
+ { text: "no aggregation", value: "none" },
163
+ { text: "1 week", value: "week-1" },
164
+ { text: "2 weeks", value: "week-2" },
165
+ { text: "1 month", value: "month-1" },
166
+ { text: "3 months", value: "month-3" },
167
+ { text: "6 months", value: "month-6" },
168
+ ];
169
+ const selectEl = document.createElement("select");
170
+ options.forEach((entry) => {
171
+ const option = document.createElement("option");
172
+ option.text = entry.text;
173
+ option.value = entry.value;
174
+ selectEl.appendChild(option);
175
+ });
176
+ this.element.appendChild(selectEl);
177
+ selectEl.addEventListener("change", (evt) => {
178
+ const [type, val] = (evt.target as HTMLSelectElement).value.split("-");
179
+ switch (type) {
180
+ case "none":
181
+ this.sdm.setTimeAggregation(null);
46
182
  break;
47
- case "year1":
48
- this.sdm.setTimeInterval(currDate.minus({ years: 1 }), currDate);
183
+ case "week":
184
+ this.sdm.setTimeAggregation({ week: Number(val) });
49
185
  break;
50
- case "year2":
51
- this.sdm.setTimeInterval(currDate.minus({ years: 2 }), currDate);
186
+ case "month":
187
+ this.sdm.setTimeAggregation({ month: Number(val) });
52
188
  break;
53
- case "year3":
54
- this.sdm.setTimeInterval(currDate.minus({ years: 3 }), currDate);
189
+ case "year":
190
+ this.sdm.setTimeAggregation({ year: Number(val) });
55
191
  break;
56
192
  }
57
193
  });
58
194
  const spinner = document.createElement("span");
195
+ spinner.id = "loadingIndicator";
59
196
  spinner.className = "loader hidden";
60
197
  this.element.appendChild(spinner);
61
198
  }
package/src/interface.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import pkg from "../package.json";
2
+
1
3
  const channel = new MessageChannel();
2
4
  const port1 = channel.port1;
3
5
 
@@ -21,9 +23,13 @@ class EOxChart {
21
23
  timeInterval: object;
22
24
  startTime?: string;
23
25
  endTime?: string;
26
+ colors?: string[];
24
27
  }) {
25
28
  port1.postMessage({ type: "setSignalsEndpoint", body: { options } });
26
29
  }
30
+ setSignalsGeometry(geometry: object) {
31
+ port1.postMessage({ type: "setSignalsGeometry", body: { geometry } });
32
+ }
27
33
  setOptions(options: object) {
28
34
  port1.postMessage({ type: "setOptions", body: { options } });
29
35
  }
@@ -53,10 +59,23 @@ const createChart = (div: HTMLElement | null) => {
53
59
  "src",
54
60
  import.meta?.url?.includes("localhost")
55
61
  ? "http://localhost:5173/index.html"
56
- : "https://www.unpkg.com/@eox/chart/dist/index.html"
62
+ : "about:blank"
57
63
  );
58
64
  iframe.setAttribute("id", "EOxChart");
59
65
  div?.appendChild(iframe);
66
+ if (!import.meta?.url?.includes("localhost")) {
67
+ const hostUrl = `https://www.unpkg.com/@eox/chart@${pkg.version}/dist`;
68
+ fetch(`${hostUrl}/index.html`)
69
+ .then((response) => {
70
+ return response.text();
71
+ })
72
+ .then((text) => {
73
+ const html = text.replace("./assets/", `${hostUrl}/assets/`);
74
+ iframe.contentDocument?.open();
75
+ iframe.contentDocument?.write(html);
76
+ iframe.contentDocument?.close();
77
+ });
78
+ }
60
79
  let iframeLoaded = false;
61
80
  iframe.onload = () => {
62
81
  // store if the iFrame has already loaded, since in Cypress hovering over the test triggers
package/src/main.ts CHANGED
@@ -10,9 +10,34 @@ const eoxchart = new Chart(
10
10
  labels: [],
11
11
  datasets: [],
12
12
  },
13
+ plugins: [
14
+ {
15
+ id: "selectionLineHighlight",
16
+ afterDraw: (chart) => {
17
+ if (chart && chart.tooltip) {
18
+ const active = chart.tooltip.getActiveElements();
19
+ if (active?.length) {
20
+ let x = active[0].element.x;
21
+ let yAxis = chart.scales.y;
22
+ let ctx = chart.ctx;
23
+ ctx.save();
24
+ ctx.beginPath();
25
+ ctx.moveTo(x, yAxis.top);
26
+ ctx.lineTo(x, yAxis.bottom);
27
+ ctx.lineWidth = 1;
28
+ ctx.strokeStyle = "rgba(0, 0, 255, 0.4)";
29
+ ctx.stroke();
30
+ ctx.restore();
31
+ }
32
+ }
33
+ },
34
+ },
35
+ ],
13
36
  }
14
37
  );
15
38
 
39
+ let sdmInstance: SignalsDataManager | null = null;
40
+
16
41
  let application: MessagePort;
17
42
 
18
43
  window.addEventListener("message", (event) => {
@@ -39,6 +64,9 @@ const onMessage = (event: MessageEvent) => {
39
64
  case "setSignalsEndpoint":
40
65
  setSignalsEndpoint(event.data.body.options);
41
66
  break;
67
+ case "setSignalsGeometry":
68
+ setSignalsGeometry(event.data.body.geometry);
69
+ break;
42
70
  case "setSignalsData":
43
71
  eoxchart.data = {
44
72
  datasets: Object.entries(event.data.body.data).map(
@@ -71,7 +99,13 @@ const setSignalsEndpoint = (options: {
71
99
  startTime?: string;
72
100
  endTime?: string;
73
101
  }) => {
74
- const sdm = new SignalsDataManager(eoxchart, options);
75
- new ChartControls(document.getElementById("controls"), sdm);
76
- sdm.setActiveFields(options.active);
102
+ sdmInstance = new SignalsDataManager(eoxchart, options);
103
+ new ChartControls(document.getElementById("controls"), sdmInstance, options);
104
+ sdmInstance.setActiveFields(options.active);
105
+ };
106
+
107
+ const setSignalsGeometry = (geometry: object) => {
108
+ if (sdmInstance) {
109
+ sdmInstance.setGeometry(geometry);
110
+ }
77
111
  };