@hisptz/react-ui
Version:
A collection of reusable complex DHIS2 react ui components.
31 lines • 900 B
JavaScript
import { useCallback, useEffect, useState } from "react";
import { getChartInstance, updateLayout } from "../utils/chart";
export function useChart(_ref) {
let {
id,
analytics,
config
} = _ref;
const [chart, setChart] = useState(getChartInstance(id, analytics, config).getOptions());
const changeChartType = useCallback(type => {
const updatedLayout = updateLayout(config, {
type
});
const updatedConfig = { ...config,
layout: updatedLayout,
type
};
const chartInstance = getChartInstance(id, analytics, updatedConfig);
setChart(chartInstance.getOptions());
}, [config, id, analytics]);
useEffect(() => {
if (analytics && config) {
const chartInstance = getChartInstance(id, analytics, config);
setChart(chartInstance.getOptions());
}
}, [analytics, config, id]);
return {
chart,
changeChartType
};
}