Skip to content

Commit a9fe536

Browse files
committed
feat: add dataset-link example
1 parent 65b48aa commit a9fe536

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

src/line.tsx renamed to src/charts.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import React, { forwardRef, useLayoutEffect, useRef } from 'react';
2-
import { LineChart as EChartLineChart, type LineSeriesOption } from 'echarts/charts';
2+
import {
3+
LineChart as EChartLineChart,
4+
PieChart as EChartPieChart,
5+
type LineSeriesOption,
6+
type PieSeriesOption,
7+
} from 'echarts/charts';
38
import type { ComposeOption } from 'echarts/core';
49
import { GridComponent, type GridComponentOption } from 'echarts/components';
510
import { ChartContext, defaultSetOptionOpt, echarts, useInitialChartContext, useRegister } from './shared.js';
@@ -10,7 +15,8 @@ function assignForwardedRef<T>(ref: React.ForwardedRef<T>, value: T | null) {
1015
else ref.current = value;
1116
}
1217

13-
type LineChartOption = echarts.EChartsCoreOption & ComposeOption<LineSeriesOption | GridComponentOption>;
18+
type LineChartOption = echarts.EChartsCoreOption &
19+
ComposeOption<LineSeriesOption | GridComponentOption | PieSeriesOption>;
1420

1521
interface LineChartProps extends LineChartOption {
1622
className?: string;
@@ -28,7 +34,8 @@ export const LineChart = forwardRef(
2834
const ctx = useInitialChartContext();
2935

3036
useRegister((echarts) => {
31-
echarts.use([EChartLineChart, GridComponent]);
37+
// TODO: Make different charts composable.
38+
echarts.use([EChartLineChart, GridComponent, EChartPieChart]);
3239
});
3340

3441
useLayoutEffect(() => {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { LineChart } from './charts.js';
12
export {
23
Dataset,
34
DataZoom,
@@ -13,5 +14,4 @@ export {
1314
VisualMap,
1415
} from './components.js';
1516
export { AxisBreak } from './features.js';
16-
export { LineChart } from './line.js';
1717
export { echarts } from './shared.js';

stories/line.stories.tsx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,3 +2181,63 @@ export function IntradayBreaks1() {
21812181
</LineChart>
21822182
);
21832183
}
2184+
2185+
export function DatasetLink() {
2186+
const chartRef = React.useRef<echarts.ECharts>(null);
2187+
const [dimension, setDimension] = React.useState('2012');
2188+
2189+
React.useEffect(() => {
2190+
const myChart = chartRef.current;
2191+
if (!myChart) return;
2192+
const listener = (event: any) => {
2193+
const xAxisInfo = event.axesInfo[0];
2194+
if (xAxisInfo) {
2195+
const dimension = xAxisInfo.value + 1;
2196+
setDimension(dimension);
2197+
}
2198+
};
2199+
myChart.on('updateAxisPointer', listener);
2200+
return () => {
2201+
myChart.off('updateAxisPointer', listener);
2202+
};
2203+
}, []);
2204+
2205+
return (
2206+
<LineChart
2207+
ref={chartRef}
2208+
style={{ width: 480, height: 360 }}
2209+
useUTC
2210+
xAxis={{ type: 'category' }}
2211+
yAxis={{ gridIndex: 0 }}
2212+
grid={{ top: '55%' }}
2213+
series={[
2214+
{ type: 'line', smooth: true, seriesLayoutBy: 'row', emphasis: { focus: 'series' } },
2215+
{ type: 'line', smooth: true, seriesLayoutBy: 'row', emphasis: { focus: 'series' } },
2216+
{ type: 'line', smooth: true, seriesLayoutBy: 'row', emphasis: { focus: 'series' } },
2217+
{ type: 'line', smooth: true, seriesLayoutBy: 'row', emphasis: { focus: 'series' } },
2218+
{
2219+
type: 'pie',
2220+
radius: '30%',
2221+
center: ['50%', '25%'],
2222+
emphasis: { focus: 'self' },
2223+
label: { formatter: `{b}: {@${dimension}} ({d}%)` },
2224+
encode: { itemName: 'product', value: dimension, tooltip: dimension },
2225+
},
2226+
]}
2227+
>
2228+
<Legend legend={{}} />
2229+
<Tooltip tooltip={{ trigger: 'axis', showContent: false }} />
2230+
<Dataset
2231+
dataset={{
2232+
source: [
2233+
['product', '2012', '2013', '2014', '2015', '2016', '2017'],
2234+
['Milk Tea', 56.5, 82.1, 88.7, 70.1, 53.4, 85.1],
2235+
['Matcha Latte', 51.1, 51.4, 55.1, 53.3, 73.8, 68.7],
2236+
['Cheese Cocoa', 40.1, 62.2, 69.5, 36.4, 45.2, 32.5],
2237+
['Walnut Brownie', 25.2, 37.1, 41.2, 18, 33.9, 49.1],
2238+
],
2239+
}}
2240+
/>
2241+
</LineChart>
2242+
);
2243+
}

0 commit comments

Comments
 (0)