@openmaic/renderer 0.1.11 → 0.1.12
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.
|
@@ -4,6 +4,7 @@ import { useRef, useState, useMemo, useCallback, useEffect } from 'react';
|
|
|
4
4
|
import tinycolor from 'tinycolor2';
|
|
5
5
|
import { getChartOption } from './chartOption.js';
|
|
6
6
|
import { loadChartRuntime } from './chartRuntime.js';
|
|
7
|
+
import { isWebKitEngine, repaintClipPaths } from './clipRepaint.js';
|
|
7
8
|
|
|
8
9
|
function Chart({ width: _width, height: _height, type, data, themeColors: rawThemeColors, textColor, lineColor, options, importedStyle, }) {
|
|
9
10
|
const chartRef = useRef(null);
|
|
@@ -57,7 +58,16 @@ function Chart({ width: _width, height: _height, type, data, themeColors: rawThe
|
|
|
57
58
|
.then((echarts) => {
|
|
58
59
|
if (!mounted || !chartRef.current)
|
|
59
60
|
return;
|
|
60
|
-
|
|
61
|
+
const chartElement = chartRef.current;
|
|
62
|
+
const chart = echarts.init(chartElement, null, { renderer: 'svg' });
|
|
63
|
+
chartInstance.current = chart;
|
|
64
|
+
if (isWebKitEngine(globalThis.navigator?.userAgent)) {
|
|
65
|
+
// WebKit does not repaint a series clipped by an animated clip path
|
|
66
|
+
// (see clipRepaint.ts). `rendered` fires after every painted frame,
|
|
67
|
+
// so the clipped series shows as it would elsewhere, on first
|
|
68
|
+
// render, re-render and resize alike.
|
|
69
|
+
chart.on('rendered', () => repaintClipPaths(chartElement));
|
|
70
|
+
}
|
|
61
71
|
setChartState('ready');
|
|
62
72
|
updateOptionRef.current();
|
|
63
73
|
const resizeObserver = new ResizeObserver(() => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Chart.js","sources":["../../../src/elements/chart/Chart.tsx"],"sourcesContent":["'use client';\n\nimport { useEffect, useRef, useMemo, useState, useCallback } from 'react';\nimport tinycolor from 'tinycolor2';\nimport type { ChartData, ChartOptions, ChartType, ImportedChartStyle } from '@openmaic/dsl';\nimport { getChartOption } from './chartOption';\nimport { loadChartRuntime } from './chartRuntime';\n\ninterface ChartProps {\n width: number;\n height: number;\n type: ChartType;\n data: ChartData;\n themeColors: string[];\n textColor?: string;\n lineColor?: string;\n options?: ChartOptions;\n importedStyle?: ImportedChartStyle;\n}\n\nexport function Chart({\n width: _width,\n height: _height,\n type,\n data,\n themeColors: rawThemeColors,\n textColor,\n lineColor,\n options,\n importedStyle,\n}: ChartProps) {\n const chartRef = useRef<HTMLDivElement>(null);\n const chartInstance = useRef<import('echarts/core').ECharts | null>(null);\n const resizeObserverRef = useRef<ResizeObserver | null>(null);\n const updateOptionRef = useRef<() => void>(() => undefined);\n const [chartState, setChartState] = useState<'loading' | 'ready' | 'error'>('loading');\n\n const themeColors = useMemo(() => {\n let colors: string[] = [];\n if (rawThemeColors.length >= 10) {\n colors = rawThemeColors;\n } else if (rawThemeColors.length === 1) {\n colors = tinycolor(rawThemeColors[0])\n .analogous(10)\n .map((color) => color.toRgbString());\n } else {\n const len = rawThemeColors.length;\n const supplement = tinycolor(rawThemeColors[len - 1])\n .analogous(10 + 1 - len)\n .map((color) => color.toRgbString());\n colors = [...rawThemeColors.slice(0, len - 1), ...supplement];\n }\n return colors;\n }, [rawThemeColors]);\n\n const updateOption = useCallback(() => {\n if (!chartInstance.current) return;\n\n const option = getChartOption({\n type,\n importedStyle,\n data,\n themeColors,\n textColor,\n lineColor,\n lineSmooth: options?.lineSmooth || false,\n stack: options?.stack || false,\n percentStack: options?.percentStack || false,\n });\n\n if (option) {\n chartInstance.current.setOption(option, true);\n }\n }, [type, data, themeColors, textColor, lineColor, options, importedStyle]);\n\n useEffect(() => {\n updateOptionRef.current = updateOption;\n }, [updateOption]);\n\n useEffect(() => {\n let mounted = true;\n\n void loadChartRuntime()\n .then((echarts) => {\n if (!mounted || !chartRef.current) return;\n\n
|
|
1
|
+
{"version":3,"file":"Chart.js","sources":["../../../src/elements/chart/Chart.tsx"],"sourcesContent":["'use client';\n\nimport { useEffect, useRef, useMemo, useState, useCallback } from 'react';\nimport tinycolor from 'tinycolor2';\nimport type { ChartData, ChartOptions, ChartType, ImportedChartStyle } from '@openmaic/dsl';\nimport { getChartOption } from './chartOption';\nimport { loadChartRuntime } from './chartRuntime';\nimport { isWebKitEngine, repaintClipPaths } from './clipRepaint';\n\ninterface ChartProps {\n width: number;\n height: number;\n type: ChartType;\n data: ChartData;\n themeColors: string[];\n textColor?: string;\n lineColor?: string;\n options?: ChartOptions;\n importedStyle?: ImportedChartStyle;\n}\n\nexport function Chart({\n width: _width,\n height: _height,\n type,\n data,\n themeColors: rawThemeColors,\n textColor,\n lineColor,\n options,\n importedStyle,\n}: ChartProps) {\n const chartRef = useRef<HTMLDivElement>(null);\n const chartInstance = useRef<import('echarts/core').ECharts | null>(null);\n const resizeObserverRef = useRef<ResizeObserver | null>(null);\n const updateOptionRef = useRef<() => void>(() => undefined);\n const [chartState, setChartState] = useState<'loading' | 'ready' | 'error'>('loading');\n\n const themeColors = useMemo(() => {\n let colors: string[] = [];\n if (rawThemeColors.length >= 10) {\n colors = rawThemeColors;\n } else if (rawThemeColors.length === 1) {\n colors = tinycolor(rawThemeColors[0])\n .analogous(10)\n .map((color) => color.toRgbString());\n } else {\n const len = rawThemeColors.length;\n const supplement = tinycolor(rawThemeColors[len - 1])\n .analogous(10 + 1 - len)\n .map((color) => color.toRgbString());\n colors = [...rawThemeColors.slice(0, len - 1), ...supplement];\n }\n return colors;\n }, [rawThemeColors]);\n\n const updateOption = useCallback(() => {\n if (!chartInstance.current) return;\n\n const option = getChartOption({\n type,\n importedStyle,\n data,\n themeColors,\n textColor,\n lineColor,\n lineSmooth: options?.lineSmooth || false,\n stack: options?.stack || false,\n percentStack: options?.percentStack || false,\n });\n\n if (option) {\n chartInstance.current.setOption(option, true);\n }\n }, [type, data, themeColors, textColor, lineColor, options, importedStyle]);\n\n useEffect(() => {\n updateOptionRef.current = updateOption;\n }, [updateOption]);\n\n useEffect(() => {\n let mounted = true;\n\n void loadChartRuntime()\n .then((echarts) => {\n if (!mounted || !chartRef.current) return;\n\n const chartElement = chartRef.current;\n const chart = echarts.init(chartElement, null, { renderer: 'svg' });\n chartInstance.current = chart;\n if (isWebKitEngine(globalThis.navigator?.userAgent)) {\n // WebKit does not repaint a series clipped by an animated clip path\n // (see clipRepaint.ts). `rendered` fires after every painted frame,\n // so the clipped series shows as it would elsewhere, on first\n // render, re-render and resize alike.\n chart.on('rendered', () => repaintClipPaths(chartElement));\n }\n setChartState('ready');\n updateOptionRef.current();\n\n const resizeObserver = new ResizeObserver(() => {\n chartInstance.current?.resize();\n });\n resizeObserver.observe(chartRef.current);\n resizeObserverRef.current = resizeObserver;\n })\n .catch(() => {\n if (mounted) setChartState('error');\n });\n\n return () => {\n mounted = false;\n resizeObserverRef.current?.disconnect();\n resizeObserverRef.current = null;\n chartInstance.current?.dispose();\n chartInstance.current = null;\n };\n // The runtime and ECharts instance are initialized once per mounted chart.\n // Option changes are applied by the effect below without re-initializing.\n }, []);\n\n useEffect(() => {\n updateOption();\n }, [updateOption]);\n\n return (\n <div\n ref={chartRef}\n className=\"chart\"\n data-chart-state={chartState}\n aria-busy={chartState === 'loading'}\n style={{ width: '100%', height: '100%' }}\n />\n );\n}\n"],"names":[],"mappings":";;;;;;;;AAqBM;AAWJ;AACA;AACA;;;AAIA;;AAEE;;;AAEO;AACL;;;;;AAIA;;AAEG;;AAEH;;AAEF;AACF;AAEA;;;;;;;;;;AAUI;AACA;AACA;AACD;;;;AAKH;;AAGE;AACF;;;AAKE;AACG;AACC;;AAEA;AACA;AACA;;;;;;AAME;;;;AAKF;AACE;AACF;AACA;AACA;AACF;;AAEE;;AACF;AAEF;;AAEE;AACA;AACA;AACA;AACF;;;;;AAMA;AACF;AAEA;AASF;;"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebKit (Safari) workaround for SVG charts whose series are revealed through
|
|
3
|
+
* an animated `<clipPath>`.
|
|
4
|
+
*
|
|
5
|
+
* ECharts animates line and area series in by growing a clip path from a
|
|
6
|
+
* sliver to the full plot width. WebKit keeps painting the clipped group as it
|
|
7
|
+
* was clipped on the first, nearly empty frame: the clip geometry reaches its
|
|
8
|
+
* final size, but the group is never repainted, so the points and labels show
|
|
9
|
+
* and the line does not. Setting the clip shape's geometry again, even to the
|
|
10
|
+
* same value, makes WebKit repaint the group against it.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Whether the page runs on WebKit, whose SVG painter needs the repaint.
|
|
14
|
+
*
|
|
15
|
+
* Blink-based browsers also report `AppleWebKit` in their user agent, so they
|
|
16
|
+
* are excluded by their own tokens. Browsers on iOS are all WebKit and report
|
|
17
|
+
* `CriOS`/`FxiOS`/`EdgiOS` instead of `Chrome/`, so they count as WebKit.
|
|
18
|
+
* Erring towards WebKit is harmless: the repaint does not change the DOM.
|
|
19
|
+
*/
|
|
20
|
+
export declare function isWebKitEngine(userAgent: string | undefined): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Re-set the attributes of every shape inside a `<clipPath>` under `root` to
|
|
23
|
+
* their current values, so WebKit repaints the clipped content against the
|
|
24
|
+
* clip path's current geometry. The DOM ends exactly as it was; other engines
|
|
25
|
+
* see no change.
|
|
26
|
+
*/
|
|
27
|
+
export declare function repaintClipPaths(root: Element): void;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebKit (Safari) workaround for SVG charts whose series are revealed through
|
|
3
|
+
* an animated `<clipPath>`.
|
|
4
|
+
*
|
|
5
|
+
* ECharts animates line and area series in by growing a clip path from a
|
|
6
|
+
* sliver to the full plot width. WebKit keeps painting the clipped group as it
|
|
7
|
+
* was clipped on the first, nearly empty frame: the clip geometry reaches its
|
|
8
|
+
* final size, but the group is never repainted, so the points and labels show
|
|
9
|
+
* and the line does not. Setting the clip shape's geometry again, even to the
|
|
10
|
+
* same value, makes WebKit repaint the group against it.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Whether the page runs on WebKit, whose SVG painter needs the repaint.
|
|
14
|
+
*
|
|
15
|
+
* Blink-based browsers also report `AppleWebKit` in their user agent, so they
|
|
16
|
+
* are excluded by their own tokens. Browsers on iOS are all WebKit and report
|
|
17
|
+
* `CriOS`/`FxiOS`/`EdgiOS` instead of `Chrome/`, so they count as WebKit.
|
|
18
|
+
* Erring towards WebKit is harmless: the repaint does not change the DOM.
|
|
19
|
+
*/
|
|
20
|
+
function isWebKitEngine(userAgent) {
|
|
21
|
+
if (!userAgent || !/AppleWebKit\//.test(userAgent))
|
|
22
|
+
return false;
|
|
23
|
+
return !/(?:Chrome|Chromium|Edg|OPR|SamsungBrowser)\//.test(userAgent);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Re-set the attributes of every shape inside a `<clipPath>` under `root` to
|
|
27
|
+
* their current values, so WebKit repaints the clipped content against the
|
|
28
|
+
* clip path's current geometry. The DOM ends exactly as it was; other engines
|
|
29
|
+
* see no change.
|
|
30
|
+
*/
|
|
31
|
+
function repaintClipPaths(root) {
|
|
32
|
+
for (const clipPath of root.querySelectorAll('clipPath')) {
|
|
33
|
+
for (const shape of Array.from(clipPath.children)) {
|
|
34
|
+
for (const { name, value } of Array.from(shape.attributes)) {
|
|
35
|
+
shape.setAttribute(name, value);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { isWebKitEngine, repaintClipPaths };
|
|
42
|
+
//# sourceMappingURL=clipRepaint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clipRepaint.js","sources":["../../../src/elements/chart/clipRepaint.ts"],"sourcesContent":["/**\n * WebKit (Safari) workaround for SVG charts whose series are revealed through\n * an animated `<clipPath>`.\n *\n * ECharts animates line and area series in by growing a clip path from a\n * sliver to the full plot width. WebKit keeps painting the clipped group as it\n * was clipped on the first, nearly empty frame: the clip geometry reaches its\n * final size, but the group is never repainted, so the points and labels show\n * and the line does not. Setting the clip shape's geometry again, even to the\n * same value, makes WebKit repaint the group against it.\n */\n\n/**\n * Whether the page runs on WebKit, whose SVG painter needs the repaint.\n *\n * Blink-based browsers also report `AppleWebKit` in their user agent, so they\n * are excluded by their own tokens. Browsers on iOS are all WebKit and report\n * `CriOS`/`FxiOS`/`EdgiOS` instead of `Chrome/`, so they count as WebKit.\n * Erring towards WebKit is harmless: the repaint does not change the DOM.\n */\nexport function isWebKitEngine(userAgent: string | undefined): boolean {\n if (!userAgent || !/AppleWebKit\\//.test(userAgent)) return false;\n return !/(?:Chrome|Chromium|Edg|OPR|SamsungBrowser)\\//.test(userAgent);\n}\n\n/**\n * Re-set the attributes of every shape inside a `<clipPath>` under `root` to\n * their current values, so WebKit repaints the clipped content against the\n * clip path's current geometry. The DOM ends exactly as it was; other engines\n * see no change.\n */\nexport function repaintClipPaths(root: Element): void {\n for (const clipPath of root.querySelectorAll('clipPath')) {\n for (const shape of Array.from(clipPath.children)) {\n for (const { name, value } of Array.from(shape.attributes)) {\n shape.setAttribute(name, value);\n }\n }\n }\n}\n"],"names":[],"mappings":"AAAA;;;;;;;;;;AAUG;AAEH;;;;;;;AAOG;AACG,SAAU,cAAc,CAAC,SAA6B,EAAA;IAC1D,IAAI,CAAC,SAAS,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,SAAS,CAAC;AAAE,QAAA,OAAO,KAAK;AAChE,IAAA,OAAO,CAAC,8CAA8C,CAAC,IAAI,CAAC,SAAS,CAAC;AACxE;AAEA;;;;;AAKG;AACG,SAAU,gBAAgB,CAAC,IAAa,EAAA;IAC5C,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,gBAAgB,CAAC,UAAU,CAAC,EAAE;AACxD,QAAA,KAAK,MAAM,KAAK,IAAI,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE;AACjD,YAAA,KAAK,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,EAAE;AAC1D,gBAAA,KAAK,CAAC,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC;YACjC;QACF;IACF;AACF;;;;"}
|