@eosway/one-chart
v0.2.0
Published
Vue 3 runtime adapter for Apache ECharts.
Maintainers
Readme
@eosway/one-chart
Vue 3 component and composables adapter for Apache ECharts.
@eosway/one-chart 提供两层 API:
- 默认主路径:非控制模式,零配置可用
- 高级路径:控制模式,保留 runtime / modules / plugin 安装能力
特性
OneChart组件零配置模式useOneChart(chartRef, { option })composable 零配置模式plugins扩展模式createOneChartRuntime()高级 runtime 模式ResizeObserver自动 resize- ECharts 事件绑定与解绑
echarts-gl/echarts-stat通过可选插件入口延迟加载,不进入主包- 仅支持 ESM
兼容性
vue >=3.3.0echarts >=5.5.0node ^20.19.0 || ^22.12.0 || >=24.0.0
安装
pnpm add vue echarts @eosway/one-chart如果需要使用可选扩展,再单独安装对应依赖:
pnpm add echarts-gl
pnpm add echarts-stat组件零配置模式
默认组件模式直接使用内置完整 ECharts runtime,业务方不需要关心:
runtimemodulescreateOneChartRuntime()
option 直接使用原生 ECharts option。
<script setup lang="ts">
import { OneChart } from '@eosway/one-chart'
const option = {
xAxis: { type: 'category', data: ['A', 'B', 'C'] },
yAxis: { type: 'value' },
series: [{ type: 'bar', data: [12, 20, 15] }],
}
function handleClick(params: unknown) {
console.log(params)
}
</script>
<template>
<OneChart :option="option" :height="320" :events="{ click: handleClick }" />
</template>OneChart 默认 Props
option?: OneChartOptionloading?: boolean | OneChartLoadingOptionstheme?: OneChartThemeautoResize?: booleanevents?: OneChartEventMapwidth?: string | numberheight?: string | numberplugins?: OneChartPlugin[]
OneChart Emits
ready(chart)error(error)disposed()
composable 零配置模式
默认 composable 主路径为:
useOneChart(chartRef, { option })第一个参数是容器 ref,必传;第二个参数为配置对象。
<script setup lang="ts">
import { useTemplateRef } from 'vue'
import { useOneChart } from '@eosway/one-chart'
const chartRef = useTemplateRef<HTMLElement>('chart')
const option = {
tooltip: { trigger: 'axis' },
xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed'] },
yAxis: { type: 'value' },
series: [{ type: 'line', data: [15, 23, 21] }],
}
useOneChart(chartRef, {
option,
onReady(chart) {
console.log(chart)
},
})
</script>
<template>
<div ref="chart" style="width: 100%; height: 320px;" />
</template>useOneChart() 默认参数
option?: OneChartOptionloading?: boolean | OneChartLoadingOptionstheme?: OneChartThemeautoResize?: booleanevents?: OneChartEventMapplugins?: OneChartPlugin[]onReady?: (chart) => voidonError?: (error) => voidonDisposed?: () => void
useOneChart() 返回值
chartreadyerrorgetInstance()setOption(option, updateOptions?)resize(options?)clear()dispose()dispatchAction(payload)showLoading(type?, options?)hideLoading()
plugins 扩展模式
默认非控制模式也支持 plugins 扩展。
插件从 @eosway/one-chart/plugins 导入,调用形式统一为工厂函数:
import { glPlugin, statPlugin } from '@eosway/one-chart/plugins'<template>
<OneChart :option="option" :plugins="[glPlugin(), statPlugin()]" />
</template>useOneChart(chartRef, {
option,
plugins: [glPlugin(), statPlugin()],
})说明:
glPlugin()会动态导入echarts-glstatPlugin()会动态导入echarts-stat- 业务方仍需自行安装对应外部依赖
高级 runtime 模式
runtime 是高级能力,不是默认用法。
如果你需要按需注册 modules、复用 runtime 或安装自定义 plugin,可以继续使用:
import { createOneChartRuntime } from '@eosway/one-chart'
import { BarChart, LineChart } from 'echarts/charts'
import { GridComponent, TooltipComponent } from 'echarts/components'
import { CanvasRenderer } from 'echarts/renderers'
const runtime = createOneChartRuntime({
modules: [BarChart, LineChart, GridComponent, TooltipComponent, CanvasRenderer],
})高级控制模式 composable:
import { useOneChartRuntime } from '@eosway/one-chart'
useOneChartRuntime(chartRef, {
runtime,
option,
manualUpdate: true,
})自定义插件
import { defineOneChartPlugin } from '@eosway/one-chart'
export const customPlugin = defineOneChartPlugin({
name: 'custom-plugin',
async install(runtime) {
console.log(runtime.echarts)
},
})设计约束
- 默认主路径基于完整
echarts命名空间,常规图表开箱即用 option可为空;为空时只初始化实例,不自动执行setOptiontheme变化会触发实例重建events变化会先解绑旧事件,再绑定新事件autoResize依赖ResizeObserver;无该 API 时自动跳过- 异步插件安装期间如果组件已卸载,旧初始化流程不会继续创建实例
注意事项
容器尺寸
OneChart 通过 width / height props 控制尺寸。
number会自动转为pxstring原样透传给内联样式- 默认值均为
100%
高级模式兼容
如果你仍然需要这些高级参数,请使用 useOneChartRuntime(...):
runtimeinitOptionsmanualUpdate- 初始化级
updateOptions group
License
Apache-2.0
