mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
Fixed chart legend not rendering.
This commit is contained in:
parent
35fde36b61
commit
dfe7a573fa
@ -48,6 +48,7 @@ export function BarChart({
|
|||||||
const [tooltip, setTooltipPopup] = useState(null);
|
const [tooltip, setTooltipPopup] = useState(null);
|
||||||
const { locale } = useLocale();
|
const { locale } = useLocale();
|
||||||
const { theme, colors } = useTheme();
|
const { theme, colors } = useTheme();
|
||||||
|
const [legendItems, setLegendItems] = useState([]);
|
||||||
|
|
||||||
const getOptions = useCallback(() => {
|
const getOptions = useCallback(() => {
|
||||||
return {
|
return {
|
||||||
@ -133,13 +134,15 @@ export function BarChart({
|
|||||||
});
|
});
|
||||||
|
|
||||||
onCreate?.(chart.current);
|
onCreate?.(chart.current);
|
||||||
|
|
||||||
|
setLegendItems(chart.current.legend.legendItems);
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateChart = (datasets: any[]) => {
|
const updateChart = (datasets: any[]) => {
|
||||||
setTooltipPopup(null);
|
setTooltipPopup(null);
|
||||||
|
|
||||||
chart.current.data.datasets.forEach((dataset: { data: any }, index: string | number) => {
|
chart.current.data.datasets.forEach((dataset: { data: any }, index: string | number) => {
|
||||||
dataset.data = datasets[index].data;
|
dataset.data = datasets[index]?.data;
|
||||||
});
|
});
|
||||||
|
|
||||||
chart.current.options = getOptions();
|
chart.current.options = getOptions();
|
||||||
@ -148,6 +151,8 @@ export function BarChart({
|
|||||||
onUpdate?.(chart.current);
|
onUpdate?.(chart.current);
|
||||||
|
|
||||||
chart.current.update(updateMode);
|
chart.current.update(updateMode);
|
||||||
|
|
||||||
|
setLegendItems(chart.current.legend.legendItems);
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -160,13 +165,21 @@ export function BarChart({
|
|||||||
}
|
}
|
||||||
}, [datasets, unit, theme, animationDuration, locale]);
|
}, [datasets, unit, theme, animationDuration, locale]);
|
||||||
|
|
||||||
|
const handleLegendClick = (index: number) => {
|
||||||
|
const meta = chart.current.getDatasetMeta(index);
|
||||||
|
|
||||||
|
meta.hidden = meta.hidden === null ? !chart.current.data.datasets[index].hidden : null;
|
||||||
|
|
||||||
|
chart.current.update();
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className={classNames(styles.chart, className)}>
|
<div className={classNames(styles.chart, className)}>
|
||||||
{isLoading && <Loading position="page" icon="dots" />}
|
{isLoading && <Loading position="page" icon="dots" />}
|
||||||
<canvas ref={canvas} />
|
<canvas ref={canvas} />
|
||||||
</div>
|
</div>
|
||||||
<Legend chart={chart.current} />
|
<Legend items={legendItems} onClick={handleLegendClick} />
|
||||||
{tooltip && (
|
{tooltip && (
|
||||||
<HoverTooltip>
|
<HoverTooltip>
|
||||||
<div className={styles.tooltip}>{tooltip}</div>
|
<div className={styles.tooltip}>{tooltip}</div>
|
||||||
|
@ -6,38 +6,34 @@ import { useLocale } from 'components/hooks';
|
|||||||
import { useForceUpdate } from 'components/hooks';
|
import { useForceUpdate } from 'components/hooks';
|
||||||
import styles from './Legend.module.css';
|
import styles from './Legend.module.css';
|
||||||
|
|
||||||
export function Legend({ chart }) {
|
export function Legend({
|
||||||
|
items = [],
|
||||||
|
onClick,
|
||||||
|
}: {
|
||||||
|
items: any[];
|
||||||
|
onClick: (index: number) => void;
|
||||||
|
}) {
|
||||||
const { locale } = useLocale();
|
const { locale } = useLocale();
|
||||||
const forceUpdate = useForceUpdate();
|
const forceUpdate = useForceUpdate();
|
||||||
|
|
||||||
const handleClick = (index: string | number) => {
|
|
||||||
const meta = chart.getDatasetMeta(index);
|
|
||||||
|
|
||||||
meta.hidden = meta.hidden === null ? !chart.data.datasets[index].hidden : null;
|
|
||||||
|
|
||||||
chart.update();
|
|
||||||
|
|
||||||
forceUpdate();
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
forceUpdate();
|
forceUpdate();
|
||||||
}, [locale, forceUpdate]);
|
}, [locale, forceUpdate]);
|
||||||
|
|
||||||
if (!chart?.legend?.legendItems.find(({ text }) => text)) {
|
if (!items.find(({ text }) => text)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.legend}>
|
<div className={styles.legend}>
|
||||||
{chart.legend.legendItems.map(({ text, fillStyle, datasetIndex, hidden }) => {
|
{items.map(({ text, fillStyle, datasetIndex, hidden }) => {
|
||||||
const color = colord(fillStyle);
|
const color = colord(fillStyle);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={text}
|
key={text}
|
||||||
className={classNames(styles.label, { [styles.hidden]: hidden })}
|
className={classNames(styles.label, { [styles.hidden]: hidden })}
|
||||||
onClick={() => handleClick(datasetIndex)}
|
onClick={() => onClick(datasetIndex)}
|
||||||
>
|
>
|
||||||
<StatusLight color={color.alpha(color.alpha() + 0.2).toHex()}>
|
<StatusLight color={color.alpha(color.alpha() + 0.2).toHex()}>
|
||||||
<span className={locale}>{text}</span>
|
<span className={locale}>{text}</span>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user