Skip to content

Commit 9a3246e

Browse files
committed
vizzu style from CSS properties
1 parent ece3a48 commit 9a3246e

File tree

4 files changed

+70
-15
lines changed

4 files changed

+70
-15
lines changed

example/example.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ let anim = chart.initializing
9191
.then(chart => chart.animate({ tooltip: 8 }))*/
9292
.catch(console.log);
9393

94+
console.log(chart);
95+
9496
let slider = document.getElementById("myRange");
9597

9698
slider.oninput = (e)=>

example/index.html

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@
55
<meta charset="utf-8"/>
66
<meta name="viewport" content="width=device-width, initial-scale=1">
77
</head>
8+
<style>
9+
:root {
10+
--vizzu-plot-marker-colorPalette: #e655e8FF #123456FF #BDAF10FF;
11+
--vizzu-backgroundColor: #ffffffff;
12+
--vizzu-plot-xAxis-interlacing-color: #f7f7f7;
13+
}
14+
15+
@media (prefers-color-scheme: dark) {
16+
#vizzuCanvas {
17+
--vizzu-backgroundColor: #282828FF;
18+
--vizzu-plot-xAxis-interlacing-color: #444444FF;
19+
}
20+
}
21+
</style>
822
<body>
923
<noscript>
1024
<p class="noscript">To use Vizzu, please enable Javascript.</p>
@@ -25,4 +39,4 @@
2539

2640
<script type="module" src="example.js"></script>
2741
</body>
28-
</html>
42+
</html>

src/apps/weblib/js-api/utils.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const canBeAccessed = (stylesheet) => {
1+
export const isAccessibleStylesheet = (stylesheet) => {
22
try {
33
stylesheet.cssRules;
44
return true;
@@ -8,7 +8,7 @@ export const canBeAccessed = (stylesheet) => {
88
};
99

1010
export const getCSSCustomProps = (pfx = "") =>
11-
[...document.styleSheets].filter(canBeAccessed).reduce(
11+
[...document.styleSheets].filter(isAccessibleStylesheet).reduce(
1212
(finalArr, sheet) =>
1313
finalArr.concat(
1414
[...sheet.cssRules]
@@ -27,6 +27,35 @@ export const getCSSCustomPropsForElement = (el, pfx = "") => {
2727
const props = getCSSCustomProps(pfx);
2828
const style = getComputedStyle(el);
2929
return props
30-
.map((prop) => [prop, style.getPropertyValue(prop)])
30+
.map((prop) => [prop, style.getPropertyValue(prop).trim()])
3131
.filter((pv) => pv[1] !== "");
3232
};
33+
34+
export const propSet = (obj, path, value) => {
35+
path.reduce((acc, part, idx) => {
36+
if (!acc?.[part]) {
37+
acc[part] = idx === path.length - 1 ? value : {};
38+
}
39+
40+
return acc[part];
41+
}, obj);
42+
return obj;
43+
};
44+
45+
export const propGet = (obj, path) => {
46+
return path.reduce((acc, part) => acc?.[part], obj);
47+
};
48+
49+
export const propsToObject = (props, propObj, pfx = "") => {
50+
propObj = propObj || {};
51+
propObj = props.reduce((obj, [prop, val]) => {
52+
const propname = prop.replace("--" + (pfx ? pfx + "-" : ""), "");
53+
const proppath = propname.split("-");
54+
55+
propSet(obj, proppath, val);
56+
57+
return obj;
58+
}, propObj);
59+
60+
return propObj;
61+
};

src/apps/weblib/js-api/vizzu.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,23 @@ import Data from "./data.js";
44
import AnimControl from "./animcontrol.js";
55
import Tooltip from "./tooltip.js";
66
import VizzuModule from "./cvizzu.js";
7+
import { getCSSCustomPropsForElement, propsToObject } from "./utils.js";
78

89
export default class Vizzu {
910
constructor(container, initState) {
1011
this.container = container;
12+
13+
if (!(this.container instanceof HTMLElement)) {
14+
this.container = document.getElementById(container);
15+
}
16+
17+
if (!this.container) {
18+
throw new Error(
19+
`Cannot find container ${this.container} to render Vizzu!`
20+
);
21+
}
22+
23+
this._propPrefix = "vizzu";
1124
this.started = false;
1225

1326
this._resolveAnimate = null;
@@ -222,7 +235,14 @@ export default class Vizzu {
222235
}
223236

224237
this.data.set(obj.data);
225-
this.setStyle(obj.style);
238+
239+
// setting style, including CSS properties
240+
const style = JSON.parse(JSON.stringify(obj.style || {}));
241+
const props = getCSSCustomPropsForElement(
242+
this.container,
243+
this._propPrefix
244+
);
245+
this.setStyle(propsToObject(props, style, this._propPrefix));
226246
this.setConfig(obj.config);
227247
}
228248
}
@@ -329,16 +349,6 @@ export default class Vizzu {
329349
let canvas = null;
330350
let placeholder = this.container;
331351

332-
if (!(placeholder instanceof HTMLElement)) {
333-
placeholder = document.getElementById(placeholder);
334-
}
335-
336-
if (!placeholder) {
337-
throw new Error(
338-
`Cannot find container ${this.container} to render Vizzu!`
339-
);
340-
}
341-
342352
if (placeholder instanceof HTMLCanvasElement) {
343353
canvas = placeholder;
344354
} else {

0 commit comments

Comments
 (0)