To render a chart, you need to:
- Import the chosen chart component and its corresponding renderer.
- Import necessary components and charts from
echarts/core, echarts/charts, and echarts/components. - Register the extensions using
echarts.use([...]), ensuring you include the library's renderer (e.g., SVGRenderer or SkiaRenderer). - Initialize the chart using
echarts.init() on a ref attached to the SvgChart or SkiaChart component. - Set the chart options using
chart.setOption(option).
// Choose your preferred renderer
import { SvgChart, SVGRenderer } from '@wuba/react-native-echarts';
import * as echarts from 'echarts/core';
import { useRef, useEffect } from 'react';
import { BarChart } from 'echarts/charts';
import { TitleComponent, TooltipComponent, GridComponent } from 'echarts/components';
// Register extensions
echarts.use([
TitleComponent,
TooltipComponent,
GridComponent,
SVGRenderer,
BarChart,
]);
const E_HEIGHT = 250;
const E_WIDTH = 300;
function ChartComponent({ option }) {
const chartRef = useRef<any>(null);
useEffect(() => {
let chart: any;
if (chartRef.current) {
// @ts-ignore
chart = echarts.init(chartRef.current, 'light', {
renderer: 'svg',
width: E_WIDTH,
height: E_HEIGHT,
});
chart.setOption(option);
}
return () => chart?.dispose();
}, [option]);
return <SvgChart ref={chartRef} />;
}
export default function App() {
const option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
},
yAxis: {
type: 'value',
},
series: [
{
data: [120, 200, 150, 80, 70, 110, 130],
type: 'bar',
},
],
};
return <ChartComponent option={option} />;
}