更新记录

2.0.0(2026-07-18)

新功能

  • option prop:直接传入图表配置,自动 setOption,支持响应式更新,无需 useEcharts
  • autoRestore prop:v-if 切换时自动缓存/回放图表数据,默认开启
  • lStyle prop:自定义组件根元素样式
  • landscape prop:横屏旋转模式,触摸坐标自动交换
  • Web 端支持 ResizeObserver 自动响应容器尺寸变化
  • Web 端完整支持 PC 浏览器鼠标/滚轮操作
  • chart.value 直接就是 echarts 实例,chart.value.setOption(...) 可直接调用

平台兼容性

uni-app(5.21)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
- -
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 小红书小程序 快应用-华为 快应用-联盟
- - - - - - - - - - -

uni-app x(5.21)

Chrome Safari Android iOS 鸿蒙 微信小程序
- - - - - -

lime-echarts 图表组件

echarts 图表适配组件,兼容 uni-app / uni-app-x,支持 APP、小程序、H5、鸿蒙多端运行。

插件依赖:lime-shared

安装方法

  1. 导入插件:在 uni-app 插件市场中搜索并导入 lime-echarts(lime-shared 会自动导入)
  2. 安装 echarts

    # 使用 pnpm(推荐)
    pnpm add echarts
    
    # 或使用 npm
    npm install echarts
  3. 首次导入后需重新编译项目

打包体积说明

echarts 完整包体积较大(约 1MB),小程序平台有 2MB 包体积限制,建议根据实际情况选择引入方式:

  • 按需引入:从 echarts/core 导入核心模块,再按需注册组件,能有效减小体积
    import * as echarts from 'echarts/core'
    import { BarChart } from 'echarts/charts'
    import { GridComponent } from 'echarts/components'
    import { CanvasRenderer } from 'echarts/renderers'
    echarts.use([BarChart, GridComponent, CanvasRenderer])
  • 下载定制包:访问 echarts 在线构建器 按需选择图表类型和组件后下载
  • 全量包:APP / H5 端无体积限制,可直接使用 import * as echarts from 'echarts'

代码演示

基础用法

通过 option prop 直接传入图表配置,无需 useEcharts

<l-echarts chartId="my-chart" :option="chartOption" />
import { setEcharts } from '@/uni_modules/lime-echarts'
import * as echarts from 'echarts'

setEcharts(echarts)

const chartOption = ref({
  xAxis: { type: 'category', data: ['A', 'B', 'C'] },
  yAxis: { type: 'value' },
  series: [{ type: 'bar', data: [10, 20, 30] }]
})

高级用法(useEcharts)

需要直接操作 echarts 实例时,使用 useEcharts 获取引用,在 @ready 回调中设置 option。

<l-echarts chartId="my-chart" @ready="onChartReady" />
import { useEcharts, setEcharts } from '@/uni_modules/lime-echarts'
import * as echarts from 'echarts'

setEcharts(echarts)

const chart = useEcharts('my-chart')

function onChartReady() {
  chart.value.setOption({
    xAxis: { type: 'category', data: ['A', 'B', 'C'] },
    yAxis: { type: 'value' },
    series: [{ type: 'bar', data: [10, 20, 30] }]
  })
}

自动恢复模式(默认)

v-if 切换时组件自动缓存并回放 option,无需手动处理。

<l-echarts v-if="show" chartId="auto-chart" />
const show = ref(true)
const chart = useEcharts('auto-chart')

function onChartReady() {
  chart.value.setOption({ ... })
}
// v-if 切换后 chart.value 会自动重建并回放 option

手动模式

设置 autoRestore=false,在 @ready 回调中自行管理 option。适用于 swiper 多图表等场景。

<l-echarts :autoRestore="false" chartId="manual-chart" @ready="onChartReady" />

Swiper 多图表

在 swiper 中使用多图表时,使用 autoRestore=false + @ready 模式。

<swiper>
  <swiper-item v-for="(item, i) in 3" :key="i">
    <l-echarts
      :autoRestore="false"
      :chartId="`chart-${i}`"
      @ready="onSwiperReady($event, i)" />
  </swiper-item>
</swiper>
const options = [
  { xAxis: { type: 'category', data: ['A','B'] }, series: [{ type: 'bar', data: [10,20] }] },
  { xAxis: { type: 'category', data: ['C','D'] }, series: [{ type: 'line', data: [30,40] }] },
  { xAxis: { type: 'category', data: ['E','F'] }, series: [{ type: 'pie', data: [50,60] }] },
]

function onSwiperReady(_, index) {
  const chart = useEcharts(`chart-${index}`)
  chart.value?.setOption(options[index])
}

横屏模式

设置 landscape 开启横屏旋转,触摸坐标自动交换。

<l-echarts chartId="landscape-chart" :landscape="true" />

API 文档

Props

参数 说明 类型 默认值 可选值
chartId 图表唯一标识,与 useEcharts 关联 string '' -
option 图表配置,传入后自动 setOption,支持响应式更新 object null -
lStyle 自定义根元素样式 string | object '' -
landscape 是否横屏旋转 boolean false true, false
beforeDelay 初始化延迟(ms) number 30 -
autoRestore v-if 切换时是否自动缓存/回放 option boolean true true, false

Events

事件名 说明 回调参数
ready 图表实例就绪后触发 -

useEcharts

函数 说明 参数
setEcharts(echarts) 注册全局 echarts,使用前必须调用一次 echarts 模块
getEcharts() 获取已注册的全局 echarts -
useEcharts(chartId) 获取图表引用,返回 ShallowRef<EChartsInstance \| null> 与组件 chartId 对应

两种使用模式

模式 适用场景 配置
自动恢复(默认) 单图表、v-if 动态切换 autoRestore=true,组件自动缓存/回放 option
手动管理 swiper 多图表、需要精细控制 autoRestore=false + @ready 回调中 setOption

平台差异说明

平台 渲染方式 交互事件 响应式
APP (Android/iOS/鸿蒙) canvas 2d + EChartsCanvas 适配 手动派发 zrender 事件 canvas 固定尺寸
小程序 canvas 2d + EChartsCanvas 适配 手动派发 zrender 事件 canvas 固定尺寸
H5 / PC echarts.init(dom) echarts 自带鼠标/触摸 ResizeObserver 自动 resize

注意事项

  1. chartId 必须对应<l-echarts chartId="xxx" />useEcharts('xxx') 的 chartId 必须一致
  2. v-if 切换:默认开启 autoRestore,组件会自动缓存/回放 option;如需手动控制请设为 false
  3. 鸿蒙 legend 黑块:已修复(echarts.esm.js 中 'transparent''rgba(0,0,0,0)'),升级 echarts 后需重新应用此 patch
  4. 请不要直接在 l-echarts 上使用 class 属性,推荐使用 lStyle 或在父级设置 class

Vue2 使用说明

插件使用了 composition-api,如需在 Vue2 项目中使用,请按照官方教程配置。

关键配置代码(在 main.js 中添加):

// vue2
import Vue from 'vue'
import VueCompositionAPI from '@vue/composition-api'
Vue.use(VueCompositionAPI)

快速预览

导入插件后,可以直接使用以下标签查看演示效果:

<!-- 代码位于 uni_modules/lime-echarts/components/lime-echarts -->
<lime-echarts />

插件标签说明

标签类型 示例 说明
组件标签 l-echarts 图表核心组件
演示标签 lime-echarts 演示组件,用于查看完整功能演示

文档链接

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

暂无用户评论。