Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions superset-frontend/plugins/plugin-chart-echarts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"esm",
"lib"
],
"devDependencies": {
"@types/react-redux": "^7.1.10"
},
"dependencies": {
"d3-array": "^1.2.0",
"lodash": "^4.17.21",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import {
Ref,
} from 'react';

import { useSelector } from 'react-redux';

import { styled } from '@superset-ui/core';
import { use, init, EChartsType } from 'echarts/core';
import { use, init, EChartsType, registerLocale } from 'echarts/core';
import {
SankeyChart,
PieChart,
Expand Down Expand Up @@ -60,6 +62,15 @@ import {
} from 'echarts/components';
import { LabelLayout } from 'echarts/features';
import { EchartsHandler, EchartsProps, EchartsStylesProps } from '../types';
import { DEFAULT_LOCALE } from '../constants';

// Define this interface here to avoid creating a dependency back to superset-frontend,
// TODO: to move the type to @superset-ui/core
interface ExplorePageState {
common: {
locale: string;
};
}

const Styles = styled.div<EchartsStylesProps>`
height: ${({ height }) => height};
Expand Down Expand Up @@ -123,24 +134,52 @@ function Echart(
getEchartInstance: () => chartRef.current,
}));

const locale = useSelector(
(state: ExplorePageState) => state?.common?.locale ?? DEFAULT_LOCALE,
).toUpperCase();

const handleSizeChange = useCallback(
({ width, height }: { width: number; height: number }) => {
if (chartRef.current) {
chartRef.current.resize({ width, height });
}
},
[],
);

Comment on lines +130 to +149

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpchev I think we need to use dynamic imports instead, but they're tricky to use in React. I tried a few ways and wasn't able to get it to render properly, so am asking the bot:

@dosu how do we refactor this to ensure we await the import and registerLocale calls before calling init below?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reply is here: #30131 (comment)

But it doesn't work :(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I tried something similar, without success. That's why I went for require.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpchev I found the culprit! handleSizeChange wasn't running because of race conditions on render.

See jpchev#2

useEffect(() => {
if (!divRef.current) return;
if (!chartRef.current) {
chartRef.current = init(divRef.current);
}
const loadLocaleAndInitChart = async () => {
if (!divRef.current) return;

Object.entries(eventHandlers || {}).forEach(([name, handler]) => {
chartRef.current?.off(name);
chartRef.current?.on(name, handler);
});
const lang = await import(`echarts/lib/i18n/lang${locale}`).catch(e => {
console.error(`Locale ${locale} not supported in ECharts`, e);
});
if (lang?.default) {
registerLocale(locale, lang.default);
}

Object.entries(zrEventHandlers || {}).forEach(([name, handler]) => {
chartRef.current?.getZr().off(name);
chartRef.current?.getZr().on(name, handler);
});
if (!chartRef.current) {
chartRef.current = init(divRef.current, null, { locale });
}

chartRef.current.setOption(echartOptions, true);
}, [echartOptions, eventHandlers, zrEventHandlers]);
Object.entries(eventHandlers || {}).forEach(([name, handler]) => {
chartRef.current?.off(name);
chartRef.current?.on(name, handler);
});

Object.entries(zrEventHandlers || {}).forEach(([name, handler]) => {
chartRef.current?.getZr().off(name);
chartRef.current?.getZr().on(name, handler);
});

chartRef.current.setOption(echartOptions, true);

// did mount
handleSizeChange({ width, height });
};

loadLocaleAndInitChart();
}, [echartOptions, eventHandlers, zrEventHandlers, locale]);

// highlighting
useEffect(() => {
Expand All @@ -158,22 +197,7 @@ function Echart(
});
}
previousSelection.current = currentSelection;
}, [currentSelection]);

const handleSizeChange = useCallback(
({ width, height }: { width: number; height: number }) => {
if (chartRef.current) {
chartRef.current.resize({ width, height });
}
},
[],
);

// did mount
useEffect(() => {
handleSizeChange({ width, height });
return () => chartRef.current?.dispose();
}, []);
}, [currentSelection, chartRef.current]);

useLayoutEffect(() => {
handleSizeChange({ width, height });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,5 @@ export const TOOLTIP_POINTER_MARGIN = 10;
// If no satisfactory position can be found, how far away
// from the edge of the window should the tooltip be kept
export const TOOLTIP_OVERFLOW_MARGIN = 5;

export const DEFAULT_LOCALE = 'en';