@eox/chart 2.0.0-alpha11 → 2.0.0-alpha13

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/src/interface.ts DELETED
@@ -1,93 +0,0 @@
1
- import pkg from "../package.json";
2
-
3
- const channel = new MessageChannel();
4
- const port1 = channel.port1;
5
-
6
- class EOxChart {
7
- iframe: HTMLIFrameElement;
8
- constructor(frame: HTMLIFrameElement) {
9
- this.iframe = frame;
10
- }
11
- setData(data: object) {
12
- port1.postMessage({ type: "setData", body: { data } });
13
- }
14
- setSignalsData(data: object) {
15
- port1.postMessage({ type: "setSignalsData", body: { data } });
16
- }
17
- setSignalsEndpoint(options: {
18
- source: string;
19
- endpoint: string;
20
- active: string[];
21
- features: string[][];
22
- geometry: object;
23
- timeInterval: object;
24
- startTime?: string;
25
- endTime?: string;
26
- colors?: string[];
27
- }) {
28
- port1.postMessage({ type: "setSignalsEndpoint", body: { options } });
29
- }
30
- setSignalsGeometry(geometry: object) {
31
- port1.postMessage({ type: "setSignalsGeometry", body: { geometry } });
32
- }
33
- setOptions(options: object) {
34
- port1.postMessage({ type: "setOptions", body: { options } });
35
- }
36
- getFoo() {
37
- return new Promise((resolve) => {
38
- const ts = Date.now();
39
- port1.onmessage = (event) => {
40
- if (event.data.ts === ts) {
41
- resolve(event.data.body);
42
- }
43
- };
44
- port1.postMessage({ ts, type: "getFoo", body: "hello world" });
45
- });
46
- }
47
- }
48
-
49
- const createChart = (div: HTMLElement | null) => {
50
- if (!div) {
51
- console.error("no div selected");
52
- return null;
53
- }
54
- return new Promise<EOxChart>((resolve) => {
55
- const iframe = document.createElement("iframe");
56
- iframe.style.cssText =
57
- "width: 100%; height: 100%; display: block; margin: 0; border: none;";
58
- iframe.setAttribute(
59
- "src",
60
- import.meta?.url?.includes("localhost")
61
- ? "http://localhost:5173/index.html"
62
- : "about:blank"
63
- );
64
- iframe.setAttribute("id", "EOxChart");
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
- }
79
- let iframeLoaded = false;
80
- iframe.onload = () => {
81
- // store if the iFrame has already loaded, since in Cypress hovering over the test triggers
82
- // the onload event multiple times, causing an error with the previously neutered port2
83
- if (iframeLoaded) {
84
- return;
85
- }
86
- iframe.contentWindow?.postMessage("init", "*", [channel.port2]);
87
- iframeLoaded = true;
88
- resolve(new EOxChart(iframe));
89
- };
90
- });
91
- };
92
-
93
- export { createChart };
package/src/main.ts DELETED
@@ -1,111 +0,0 @@
1
- import Chart, { ChartDataset } from "chart.js/auto";
2
- import "chartjs-adapter-luxon";
3
- import SignalsDataManager from "./signalsDataManager";
4
- import ChartControls from "./chartControls";
5
-
6
- const eoxchart = new Chart(
7
- <HTMLCanvasElement>document.getElementById("chart"),
8
- {
9
- data: {
10
- labels: [],
11
- datasets: [],
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
- ],
36
- }
37
- );
38
-
39
- let sdmInstance: SignalsDataManager | null = null;
40
-
41
- let application: MessagePort;
42
-
43
- window.addEventListener("message", (event) => {
44
- if (event.data === "init") {
45
- application = event.ports[0];
46
- application.onmessage = onMessage;
47
- }
48
- });
49
-
50
- const onMessage = (event: MessageEvent) => {
51
- if (event.data.ts) {
52
- switch (event.data.type) {
53
- case "getFoo":
54
- console.log(event.data.body);
55
- application.postMessage({ ts: event.data.ts, body: "foo bar" });
56
- break;
57
- }
58
- } else {
59
- switch (event.data.type) {
60
- case "setData":
61
- eoxchart.data = event.data.body.data;
62
- eoxchart.update("none");
63
- break;
64
- case "setSignalsEndpoint":
65
- setSignalsEndpoint(event.data.body.options);
66
- break;
67
- case "setSignalsGeometry":
68
- setSignalsGeometry(event.data.body.geometry);
69
- break;
70
- case "setSignalsData":
71
- eoxchart.data = {
72
- datasets: Object.entries(event.data.body.data).map(
73
- ([key, item]): ChartDataset => {
74
- return {
75
- type: "line",
76
- label: key,
77
- data: <number[]>item,
78
- };
79
- }
80
- ),
81
- };
82
- eoxchart.update("none");
83
- break;
84
- case "setOptions":
85
- eoxchart.options = event.data.body.options;
86
- eoxchart.update("none");
87
- break;
88
- }
89
- }
90
- };
91
-
92
- const setSignalsEndpoint = (options: {
93
- source: string;
94
- endpoint: string;
95
- active: string[];
96
- features: string[][];
97
- geometry: object;
98
- timeInterval: object;
99
- startTime?: string;
100
- endTime?: string;
101
- }) => {
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
- }
111
- };