@eox/chart 0.1.0 → 0.2.0

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/package.json CHANGED
@@ -1,14 +1,8 @@
1
1
  {
2
2
  "name": "@eox/chart",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "devDependencies": {
6
- "@eox/eslint-config": "^1.0.0",
7
- "@typescript-eslint/eslint-plugin": "^7.0.1",
8
- "@typescript-eslint/parser": "^7.0.1",
9
- "eslint-plugin-typescript": "^0.14.0",
10
- "lit": "^3.0.2",
11
- "typescript": "^5.2.2",
12
6
  "vite": "^5.0.2"
13
7
  },
14
8
  "engines": {
@@ -17,19 +11,17 @@
17
11
  },
18
12
  "files": [
19
13
  "dist",
20
- "test"
14
+ "src"
21
15
  ],
22
- "main": "./dist/eox-chart.js",
16
+ "main": "./src/main.js",
23
17
  "scripts": {
24
18
  "build": "vite build",
25
- "watch": "vite build --watch",
26
- "format": "prettier --write .",
27
- "lint": "eslint --ext .js,.ts .",
28
- "lint:fix": "eslint --ext .js,.ts . --fix",
29
- "typecheck": "tsc"
19
+ "watch": "vite build --watch"
30
20
  },
31
21
  "dependencies": {
22
+ "@eox/elements-utils": "^0.0.1",
32
23
  "deepmerge-ts": "^7.1.3",
24
+ "lit": "^3.0.2",
33
25
  "vega": "^5.30.0",
34
26
  "vega-embed": "^6.26.0",
35
27
  "vega-lite": "^5.21.0"
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @type {import("vega-embed").VisualizationSpec}
3
+ */
4
+ export const DEFAULT_SPEC = {
5
+ // @ts-expect-error see https://vega.github.io/vega-lite/docs/size.html#specifying-responsive-width-and-height
6
+ width: "container",
7
+ // @ts-expect-error see https://vega.github.io/vega-lite/docs/size.html#specifying-responsive-width-and-height
8
+ height: "container",
9
+ autosize: "fit",
10
+ resize: true,
11
+ };
@@ -0,0 +1,3 @@
1
+ export { DEFAULT_SPEC } from "./default-spec";
2
+
3
+ export { TEST_SELECTORS, TEST_VALUES } from "./test";
@@ -0,0 +1,31 @@
1
+ export const TEST_SELECTORS = {
2
+ chart: "eox-chart",
3
+ vegaContainer: "#vis",
4
+ vegaCanvas: "canvas",
5
+ };
6
+
7
+ export const TEST_VALUES = {
8
+ chartHeight: 300,
9
+ chartSpec: {
10
+ $schema: "https://vega.github.io/schema/vega-lite/v5.json",
11
+ description: "A simple bar chart with embedded data.",
12
+ data: {
13
+ values: [
14
+ { a: "A", b: 28 },
15
+ { a: "B", b: 55 },
16
+ { a: "C", b: 43 },
17
+ { a: "D", b: 91 },
18
+ { a: "E", b: 81 },
19
+ { a: "F", b: 53 },
20
+ { a: "G", b: 19 },
21
+ { a: "H", b: 87 },
22
+ { a: "I", b: 52 },
23
+ ],
24
+ },
25
+ mark: { type: "bar", tooltip: true, fill: "#004170" },
26
+ encoding: {
27
+ x: { field: "a", type: "ordinal" },
28
+ y: { field: "b", type: "quantitative" },
29
+ },
30
+ },
31
+ };
package/src/main.js ADDED
@@ -0,0 +1,88 @@
1
+ import { LitElement, html } from "lit";
2
+ import { style } from "./style";
3
+ import { styleEOX } from "./style.eox";
4
+ import allStyle from "@eox/elements-utils/styles/dist/all.style";
5
+ import { renderChartMethod } from "./methods/render";
6
+
7
+ /**
8
+ * Chart component based on [Vega-Lite](https://vega.github.io/vega-lite/)/[Vega-Embed](https://github.com/vega/vega-embed).
9
+ * Pass a valid Vega spec as `spec` property in order to render a chart.
10
+ *
11
+ * The `eox-chart` provides some default `spec` settings (merged with the provided `spec` property) and helper functionalities on top of Vega-Lite.
12
+ *
13
+ * @element eox-chart
14
+ */
15
+ export class EOxChart extends LitElement {
16
+ static properties = {
17
+ dataValues: { attribute: false, type: Object },
18
+ spec: { attribute: false, type: Object },
19
+ noShadow: { attribute: "no-shadow", type: Boolean },
20
+ unstyled: { type: Boolean },
21
+ };
22
+
23
+ constructor() {
24
+ super();
25
+
26
+ /**
27
+ * [Vega-Lite spec](https://vega.github.io/vega-lite/docs/spec.html)
28
+ *
29
+ * @type {import("vega-embed").VisualizationSpec}
30
+ */
31
+ this.spec = undefined;
32
+
33
+ /**
34
+ * Data values passed on runtime. Requires a [named data source](https://vega.github.io/vega-lite/docs/data.html#named) in the provided `spec`
35
+ *
36
+ * @type {{[dataSourceName: string]: import("vega-lite/build/src/data").InlineData}}
37
+ */
38
+ this.dataValues = undefined;
39
+
40
+ /**
41
+ * Renders the element without a shadow root
42
+ *
43
+ * @type {Boolean}
44
+ */
45
+ this.noShadow = false;
46
+
47
+ /**
48
+ * Render the element without additional styles
49
+ *
50
+ * @type {Boolean}
51
+ */
52
+ this.unstyled = false;
53
+ }
54
+
55
+ /**
56
+ * Lifecycle method called when the element is updated
57
+ *
58
+ * @param {import("lit").PropertyValues} changedProperties
59
+ */
60
+ async updated(changedProperties) {
61
+ if (changedProperties.has("spec") || changedProperties.has("dataValues")) {
62
+ renderChartMethod(this, this.spec, this.dataValues);
63
+ }
64
+ }
65
+
66
+ /**
67
+ * Overrides createRenderRoot to handle shadow DOM creation based on the noShadow property.
68
+ */
69
+ createRenderRoot() {
70
+ return this.noShadow ? this : super.createRenderRoot();
71
+ }
72
+
73
+ /**
74
+ * Renders the component's HTML and CSS
75
+ */
76
+ render() {
77
+ return html`
78
+ <style>
79
+ ${style}
80
+ ${!this.unstyled && allStyle}
81
+ ${!this.unstyled && styleEOX}
82
+ </style>
83
+ <div id="vis"></div>
84
+ `;
85
+ }
86
+ }
87
+
88
+ customElements.define("eox-chart", EOxChart);
@@ -0,0 +1 @@
1
+ export { default as renderChartMethod } from "./render-chart";
@@ -0,0 +1,28 @@
1
+ import { default as vegaEmbed } from "vega-embed";
2
+ import { deepmerge } from "deepmerge-ts";
3
+ import { DEFAULT_SPEC } from "../../enums";
4
+
5
+ /**
6
+ * Render the Vega-Lite chart using vega-embed with a spec and (optionally) dataValues
7
+ *
8
+ * @param {import("../../main").EOxChart} EOxChart The eox-chart instance
9
+ * @param {import("vega-embed").VisualizationSpec} spec
10
+ * @param {Object=} dataValues
11
+ *
12
+ */
13
+ const renderChartMethod = (EOxChart, spec, dataValues) => {
14
+ const mergedSpec = deepmerge(DEFAULT_SPEC, spec);
15
+ vegaEmbed(
16
+ /** @type {HTMLElement} */ (EOxChart.renderRoot.querySelector("#vis")),
17
+ mergedSpec,
18
+ ).then((res) => {
19
+ if (dataValues) {
20
+ Object.keys(dataValues).forEach((dataSourceName) => {
21
+ res.view.insert(dataSourceName, dataValues[dataSourceName]);
22
+ });
23
+ res.view.run();
24
+ }
25
+ });
26
+ };
27
+
28
+ export default renderChartMethod;
@@ -0,0 +1,5 @@
1
+ export const styleEOX = `
2
+ eox-chart {
3
+ font-family: Roboto, sans-serif;
4
+ }
5
+ `;
package/src/style.js ADDED
@@ -0,0 +1,9 @@
1
+ export const style = `
2
+ :root, :host, eox-chart {
3
+ display: block;
4
+ }
5
+ #vis {
6
+ width: 100%;
7
+ height: 100%;
8
+ }
9
+ `;
@@ -1,2 +0,0 @@
1
- export { default as loadChartTest } from "./load-chart";
2
- export { default as setDataValuesTest } from "./set-data-values";
@@ -1,26 +0,0 @@
1
- import { TEST_SELECTORS, TEST_VALUES } from "../../src/enums";
2
-
3
- // Destructure TEST_SELECTORS object
4
- const { chart, vegaContainer, vegaCanvas } = TEST_SELECTORS;
5
-
6
- // Destructuring TEST_VALUES object
7
- const { chartHeight, chartSpec } = TEST_VALUES;
8
-
9
- /**
10
- * Test to verify if the chart component loads successfully.
11
- */
12
- const loadChartTest = () => {
13
- cy.get(chart).and(($el) => {
14
- const eoxChart = $el[0];
15
- eoxChart.spec = chartSpec;
16
- });
17
- // Find the chart element and access its shadow DOM
18
- cy.get(chart)
19
- .shadow()
20
- .within(() => {
21
- cy.get(vegaContainer);
22
- cy.get(vegaCanvas).invoke("outerHeight").should("eq", chartHeight);
23
- });
24
- };
25
-
26
- export default loadChartTest;
@@ -1,51 +0,0 @@
1
- import { TEST_SELECTORS, TEST_VALUES } from "../../src/enums";
2
-
3
- // Destructure TEST_SELECTORS object
4
- const { chart } = TEST_SELECTORS;
5
-
6
- // Destructuring TEST_VALUES object
7
- const { chartSpec } = TEST_VALUES;
8
-
9
- /**
10
- * Test to verify if the chart component loads successfully.
11
- */
12
- const setDataValuesTest = () => {
13
- return new Cypress.Promise((resolve) => {
14
- cy.get(chart).and(($el) => {
15
- const eoxChart = $el[0];
16
-
17
- const dataValueTestKey = "myData";
18
- const chartSpecDataValues = chartSpec.data.values;
19
- const chartSpecWithoutData = {
20
- ...chartSpec,
21
- data: {
22
- name: dataValueTestKey,
23
- },
24
- };
25
-
26
- // Set the spec with a data placeholder
27
- eoxChart.spec = chartSpecWithoutData;
28
-
29
- // Wait some time and set the data values afterwards
30
- setTimeout(() => {
31
- eoxChart.dataValues = {
32
- [dataValueTestKey]: chartSpecDataValues,
33
- };
34
- setTimeout(() => {
35
- const canvas = eoxChart.shadowRoot.querySelector("canvas");
36
- const context = canvas.getContext("2d");
37
- const pixel = context.getImageData(150, 150, 1, 1).data;
38
- const red = pixel[0];
39
- const green = pixel[1];
40
- const blue = pixel[2];
41
- expect(red).to.eq(0);
42
- expect(green).to.eq(65);
43
- expect(blue).to.eq(112);
44
- resolve();
45
- });
46
- }, 100);
47
- });
48
- });
49
- };
50
-
51
- export default setDataValuesTest;
@@ -1,26 +0,0 @@
1
- // Importing necessary modules, test cases, and enums
2
- import "../src/main";
3
- import { loadChartTest, setDataValuesTest } from "./cases";
4
- import { TEST_SELECTORS, TEST_VALUES } from "../src/enums";
5
-
6
- // Destructuring TEST_SELECTORS object
7
- const { chart } = TEST_SELECTORS;
8
-
9
- // Destructuring TEST_VALUES object
10
- const { chartHeight } = TEST_VALUES;
11
-
12
- // Test suite for Chart
13
- describe("Chart", () => {
14
- // Setting up the environment before each test
15
- beforeEach(() => {
16
- // Mounting eox-chart element
17
- cy.mount(
18
- `<eox-chart style="width: ${chartHeight}px; height: ${chartHeight}px"></eox-chart>`
19
- ).as(chart);
20
- });
21
-
22
- // Test case to ensure the chart component loads successfully
23
- it("loads the chart", () => loadChartTest());
24
-
25
- it("allows setting data values asynchronously", () => setDataValuesTest());
26
- });