UNPKG

@neo4j-ndl/react-charts

Version:

React implementation of charts from Neo4j Design System

133 lines 5.77 kB
"use strict"; /** * * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] * * This file is part of Neo4j. * * Neo4j is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.useChartLifecycle = useChartLifecycle; exports.useChartLoading = useChartLoading; exports.useChartResize = useChartResize; const base_1 = require("@neo4j-ndl/base"); const echarts_1 = require("echarts"); const react_1 = require("react"); const ndl_echarts_theme_1 = require("../themes/ndl-echarts-theme"); const use_chart_refs_1 = require("./use-chart-refs"); /** * Registers Needle ECharts themes and initializes the chart instance. * * Theme registration runs with the active palette so ECharts can resolve Needle * colors before options are applied. The hook only calls `init` when the DOM * element does not already have an ECharts instance, allowing later renders to * update registered theme data without replacing the existing chart. */ function useChartLifecycle({ palette, theme }) { const { chartEchartRef } = (0, use_chart_refs_1.useChartRefsContext)(); (0, react_1.useEffect)(() => { if (chartEchartRef.current === null) { return; } (0, echarts_1.registerTheme)('ndl-light', (0, ndl_echarts_theme_1.ndlEchartsTheme)('light', palette)); (0, echarts_1.registerTheme)('ndl-dark', (0, ndl_echarts_theme_1.ndlEchartsTheme)('dark', palette)); const currentChart = (0, echarts_1.getInstanceByDom)(chartEchartRef.current); if (currentChart) { return; } const echartsTheme = theme === 'light' ? 'ndl-light' : 'ndl-dark'; (0, echarts_1.init)(chartEchartRef.current, echartsTheme, { renderer: 'svg', }); }); } /** * Mirrors React loading state into ECharts' loading overlay. * * The overlay also stays visible while the chart waits for its first resize, * preventing users from seeing an unmeasured SVG before ECharts has been sized * to its container. Loading colors come from the active Needle theme. */ function useChartLoading({ isLoading, isWaitingForFirstResize, theme, }) { const { chartEchartRef } = (0, use_chart_refs_1.useChartRefsContext)(); (0, react_1.useEffect)(() => { if (chartEchartRef.current === null) { return; } const chart = (0, echarts_1.getInstanceByDom)(chartEchartRef.current); if (isLoading === true || isWaitingForFirstResize) { chart === null || chart === void 0 ? void 0 : chart.showLoading({ color: base_1.tokens.theme[theme].color.primary.bg.status, fontFamily: base_1.tokens.typography['label'].fontFamily, fontSize: base_1.tokens.typography['label'].fontSize, fontWeight: base_1.tokens.typography['label'].fontWeight, maskColor: `rgb( from ${base_1.tokens.theme[theme].color.neutral.text.inverse} r g b / 0.8)`, text: 'Loading', textColor: base_1.tokens.theme[theme].color.neutral.text.default, }); } else { chart === null || chart === void 0 ? void 0 : chart.hideLoading(); } }, [chartEchartRef, isLoading, isWaitingForFirstResize, theme]); } /** * Keeps the ECharts canvas sized to the DOM box assigned by the chart layout. * * The hook listens to both window resize events and direct element resize * observations because the chart can change size through parent layout changes * that do not emit a window resize. It exposes `isWaitingForFirstResize` so the * rest of the chart can delay rendering dependent UI until ECharts has been * given its initial dimensions. */ function useChartResize() { const { chartEchartRef } = (0, use_chart_refs_1.useChartRefsContext)(); const [isWaitingForFirstResize, setIsWaitingForFirstResize] = (0, react_1.useState)(true); const resizeChart = (0, react_1.useCallback)(() => { if (chartEchartRef.current === null) { return; } const chart = (0, echarts_1.getInstanceByDom)(chartEchartRef.current); const { clientHeight: height, clientWidth: width } = chartEchartRef.current; chart === null || chart === void 0 ? void 0 : chart.resize({ height, width, }); }, [chartEchartRef]); (0, react_1.useEffect)(() => { window.addEventListener('resize', resizeChart); const resizeObserver = new ResizeObserver(() => { resizeChart(); }); if (chartEchartRef.current) { resizeObserver.observe(chartEchartRef.current); } const animationFrame = requestAnimationFrame(() => { resizeChart(); setIsWaitingForFirstResize(false); }); return () => { cancelAnimationFrame(animationFrame); window.removeEventListener('resize', resizeChart); resizeObserver.disconnect(); }; }, [chartEchartRef, resizeChart]); return { isWaitingForFirstResize, resizeChart, }; } //# sourceMappingURL=use-chart-instance.js.map