diff --git a/src/plugins/highcharts/renderer/components/HighchartsComponent.tsx b/src/plugins/highcharts/renderer/components/HighchartsComponent.tsx index 1b356123..c1ef9c27 100644 --- a/src/plugins/highcharts/renderer/components/HighchartsComponent.tsx +++ b/src/plugins/highcharts/renderer/components/HighchartsComponent.tsx @@ -101,11 +101,35 @@ export class HighchartsComponent extends React.PureComponent { }>(); componentDidMount() { - this.onLoad(); + if (this.props.onChartLoad) { + if (!this.state.isError && !this.props.splitTooltip) { + const widget = this.chartComponent.current + ? this.chartComponent.current.chart + : null; + + if (this.state.callback && widget) { + this.state.callback(widget); + } + + this.props.onChartLoad?.({ + widget, + }); + } + } else { + this.onLoad(); + } } componentDidUpdate() { - this.onLoad(); + if (this.props.onRender) { + if (!this.state.isError && !this.props.splitTooltip) { + this.props.onRender({ + renderTime: getChartPerformanceDuration(this.getId()), + }); + } + } else { + this.onLoad(); + } } render() { @@ -164,6 +188,10 @@ export class HighchartsComponent extends React.PureComponent { return `${this.props.id}_${this.id}`; } + /** + * @depricated: please use onRender & onChartLoad instead + * @private + */ private onLoad() { if (!this.state.isError && !this.props.splitTooltip) { const data = { diff --git a/src/plugins/indicator/renderer/IndicatorWidget.tsx b/src/plugins/indicator/renderer/IndicatorWidget.tsx index 1a5c03a2..1957f3d4 100644 --- a/src/plugins/indicator/renderer/IndicatorWidget.tsx +++ b/src/plugins/indicator/renderer/IndicatorWidget.tsx @@ -17,6 +17,8 @@ const IndicatorWidget = React.forwardRef { // TODO: swap to onRender after https://github.com/gravity-ui/chartkit/issues/33 onLoad?.(); + onRender?.(); // TODO renderTime ? }); + React.useLayoutEffect(() => { + onChartLoad?.({widget: null}); + }, []); + if (isEmpty(data)) { throw new ChartKitError({ code: CHARTKIT_ERROR_CODE.NO_DATA, diff --git a/src/plugins/yagr/renderer/YagrWidget.tsx b/src/plugins/yagr/renderer/YagrWidget.tsx index 6bc44f05..ca2bcbd1 100644 --- a/src/plugins/yagr/renderer/YagrWidget.tsx +++ b/src/plugins/yagr/renderer/YagrWidget.tsx @@ -19,6 +19,8 @@ const YagrWidget = React.forwardRef((props id, data: {data}, onLoad, + onRender, + onChartLoad, } = props; const yagrRef = React.useRef(null); @@ -34,6 +36,7 @@ const YagrWidget = React.forwardRef((props const handleChartLoading: NonNullable = React.useCallback( (chart, {renderTime}) => { onLoad?.({...data, widget: chart, widgetRendering: renderTime}); + onRender?.({renderTime}); }, [onLoad, data], ); @@ -95,6 +98,10 @@ const YagrWidget = React.forwardRef((props }); }); + React.useLayoutEffect(() => { + onChartLoad?.({widget: yagrRef.current?.chart}); + }, []); + return ( = { widgetRendering?: number; }; +export type ChartKitOnRenderData = { + renderTime?: number; +}; + +export type ChartKitOnChartLoad = { + widget?: YagrChartComponent['chart'] | Highcharts.Chart | null; +}; + export type ChartKitOnError = (data: {error: any}) => void; export type ChartKitProps = { @@ -26,7 +38,22 @@ export type ChartKitProps = { data: ChartKitWidget[T]['data']; id?: string; isMobile?: boolean; + /** + * @depricated: please use onRender & onChartLoad instead + * @private + */ onLoad?: (data?: ChartKitOnLoadData) => void; + /** + * called on each render + * @param data + */ + onRender?: (data?: ChartKitOnRenderData) => void; + /** + * called on mount + * @param data + */ + onChartLoad?: (data?: ChartKitOnChartLoad) => void; + onError?: ChartKitOnError; } & {[key in keyof Omit]: ChartKitWidget[T][key]};