@chainsys/sab-react-charts
v1.0.1
Published
Extended ECharts React wrapper - full Apache ECharts functionality for React applications
Readme
@chainsys/sab-react-charts
Extended ECharts React wrapper – use Apache ECharts in React with full API parity. All chart types and options from the ECharts Examples gallery are supported via a thin React layer over the official ECharts API.
Installation
ECharts is bundled inside this package. Install only the wrapper (and React):
npm install @chainsys/sab-react-charts
# or
yarn add @chainsys/sab-react-charts
# or
pnpm add @chainsys/sab-react-chartsPeer dependencies: react >=19.2.0, react-dom >=19.2.0 and node.js >=22.21.1. You do not need to install echarts separately for 2D charts.
Quick start
import { SabReactCharts } from '@chainsys/sab-react-charts';
import type { EChartsOption } from 'echarts';
const option: EChartsOption = {
xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] },
yAxis: { type: 'value' },
series: [{ type: 'line', data: [120, 200, 150, 80, 70, 110, 130] }],
};
export function MyChart() {
return <SabReactCharts option={option} style={{ height: 400 }} />;
}API
<SabReactCharts> props
Options are aligned with ngx-echarts where applicable.
| Prop | Type | Default | Description |
|------|------|--------|-------------|
| option | EChartsOption \| null | required | Full ECharts option object (all 2D and 3D chart types). |
| merge | EChartsOption \| null | - | Extra option merged on top when calling setOption (ngx-echarts parity). |
| notMerge | boolean | false | Do not merge with previous option when updating. |
| lazyUpdate | boolean | false | Lazy update (e.g. for large data). |
| replaceMerge | string[] | - | Replace specified components when updating. |
| theme | string \| object | - | Theme name (e.g. 'light', 'dark') or custom theme object for echarts.init(). |
| opts | EChartsInitOpts | - | Init options: devicePixelRatio, renderer ('canvas' \| 'svg'), useDirtyRect, useCoarsePointer, pointerSize, width, height, locale, ssr. |
| echarts | typeof echarts | default import | Custom echarts namespace (e.g. with echarts-gl registered) for 3D charts. |
| style | CSSProperties | - | Container div style (e.g. width, height). |
| className | string | - | Container div class name. |
| onChartReady | (chart: ECharts) => void | - | Callback when the chart instance is ready. |
| onEvents | Record<string, (params: unknown) => void> | - | ECharts event handlers, e.g. { click: (params) => {} }. |
| shouldSetOption | (prev, next) => boolean | () => true | Return false to skip calling setOption for a given update. |
| loading | boolean \| EChartsLoadingOption | false | Show loading overlay. |
| loadingType | string | 'default' | Loading type passed to showLoading(type, opts). |
| autoResize | boolean | true | Resize chart when container size changes (ResizeObserver). |
Ref API
Attach a ref to access the ECharts instance:
import { useRef } from 'react';
import { SabReactCharts } from '@chainsys/sab-react-charts';
import type { SabReactChartsRef } from '@chainsys/sab-react-charts';
function MyChart() {
const chartRef = useRef<SabReactChartsRef>(null);
const handleResize = () => chartRef.current?.getEchartsInstance().resize();
const handleExport = () => {
const url = chartRef.current?.getEchartsInstance().getDataURL({ type: 'png' });
// use url
};
return (
<>
<button onClick={handleResize}>Resize</button>
<button onClick={handleExport}>Export PNG</button>
<SabReactCharts ref={chartRef} option={option} />
</>
);
}getEchartsInstance()– Returns the ECharts instance. Use it forresize(),getDataURL(),getOption(),appendData(),clear(), etc. See ECharts API.
useECharts hook
Optional hook for ref + getter:
import { SabReactCharts, useECharts } from '@chainsys/sab-react-charts';
function MyChart() {
const [chartRef, getInstance] = useECharts();
return <SabReactCharts ref={chartRef} option={option} />;
}Chart types and options
All ECharts chart types and options are supported. Pass any valid ECharts option; the wrapper forwards it to ECharts without modification.
2D chart types: line, bar, pie, scatter, radar, map, heatmap, candlestick, gauge, funnel, treemap, graph, sunburst, boxplot, parallel, sankey, themeRiver, custom series, and combo charts (e.g. line + bar).
3D chart types (require echarts-gl): bar3D, line3D, scatter3D, surface, map3D, lines3D. See 3D charts below.
For themes and maps, use the echarts package directly:
import * as echarts from 'echarts';
echarts.registerTheme('myTheme', { ... });
echarts.registerMap('myMap', geoJSON);
// Then use theme="myTheme" or the map in your option3D charts (echarts-gl, optional)
ECharts is bundled in this package; 3D charts (bar3D, line3D, scatter3D, surface, map3D, lines3D) are not bundled. To use them, install and register echarts-gl and pass the extended echarts via the echarts prop:
npm install echarts-glimport * as echarts from 'echarts';
import 'echarts-gl';
import { SabReactCharts } from '@chainsys/sab-react-charts';
const option = {
grid3D: {},
xAxis3D: {}, yAxis3D: {}, zAxis3D: {},
series: [{ type: 'bar3D', data: [...] }],
};
<SabReactCharts echarts={echarts} option={option} style={{ height: 400 }} />License
MIT
