@neo4j-ndl/react-charts
Version:
React implementation of charts from Neo4j Design System
128 lines • 5.31 kB
JavaScript
/**
*
* 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/>.
*/
import { tokens } from '@neo4j-ndl/base';
import { getInstanceByDom, init, registerTheme } from 'echarts';
import { useCallback, useEffect, useState } from 'react';
import { ndlEchartsTheme } from '../themes/ndl-echarts-theme';
import { useChartRefsContext } from './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.
*/
export function useChartLifecycle({ palette, theme }) {
const { chartEchartRef } = useChartRefsContext();
useEffect(() => {
if (chartEchartRef.current === null) {
return;
}
registerTheme('ndl-light', ndlEchartsTheme('light', palette));
registerTheme('ndl-dark', ndlEchartsTheme('dark', palette));
const currentChart = getInstanceByDom(chartEchartRef.current);
if (currentChart) {
return;
}
const echartsTheme = theme === 'light' ? 'ndl-light' : 'ndl-dark';
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.
*/
export function useChartLoading({ isLoading, isWaitingForFirstResize, theme, }) {
const { chartEchartRef } = useChartRefsContext();
useEffect(() => {
if (chartEchartRef.current === null) {
return;
}
const chart = getInstanceByDom(chartEchartRef.current);
if (isLoading === true || isWaitingForFirstResize) {
chart === null || chart === void 0 ? void 0 : chart.showLoading({
color: tokens.theme[theme].color.primary.bg.status,
fontFamily: tokens.typography['label'].fontFamily,
fontSize: tokens.typography['label'].fontSize,
fontWeight: tokens.typography['label'].fontWeight,
maskColor: `rgb( from ${tokens.theme[theme].color.neutral.text.inverse} r g b / 0.8)`,
text: 'Loading',
textColor: 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.
*/
export function useChartResize() {
const { chartEchartRef } = useChartRefsContext();
const [isWaitingForFirstResize, setIsWaitingForFirstResize] = useState(true);
const resizeChart = useCallback(() => {
if (chartEchartRef.current === null) {
return;
}
const chart = getInstanceByDom(chartEchartRef.current);
const { clientHeight: height, clientWidth: width } = chartEchartRef.current;
chart === null || chart === void 0 ? void 0 : chart.resize({
height,
width,
});
}, [chartEchartRef]);
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