Uni ECharts
repository·dev·Indexed 41 days ago
https://github.com/xiaohe0601/uni-echartsA Vue 3 component library that brings Apache ECharts capabilities to the uni-app ecosystem. It enables high-performance data visualization across Web, Mini-programs, and mobile apps with a developer experience similar to vue-echarts. Version 2.5.3 supports TypeScript, Easycom, and provides a comprehensive API for managing chart options, themes, and instance methods via Vue refs.
What's inside Uni ECharts
- Uni ECharts is an Apache ECharts component designed specifically for the uni-app ecosystem. It allows developers to use ECharts across multiple platforms (Web, Mini-programs, and App) with minimal configuration. The library aims to provide a developer experience nearly identical to Vue ECharts.
Overview of Uni ECharts
devUni ECharts is an Apache ECharts component specifically designed for uni-app environments. It is built for Vue 3 and provides a cross-platform experience across Web, Mini-programs, and Mobile Apps (APP).
Key features include:
- Familiar API: Provides a developer experience nearly identical to vue-echarts.
- Multi-platform support: Compatible with Web, Mini-programs, and App.
- Easycom support: Simplifies component usage in uni-app.
- TypeScript support: Full type definitions available.
- Commercial use: Free for commercial applications.
Key features of Uni ECharts
devUni ECharts provides the following capabilities for uni-app developers:
- Cross-platform compatibility: Supports Web, Mini-programs (WeChat, etc.), and App environments.
- Easy Integration: Supports
easycomfor automatic component registration in uni-app. - Developer Experience: Near-identical usage pattern to
vue-echartsand full TypeScript support. - Licensing: Supports free commercial use under the MIT license.
Unsupported features in Uni ECharts
devDue to limitations in the uni-app API, the following ECharts features are currently not supported:
- SVG Renderer
toolbox.feature.saveAsImageseries-mapecharts-glseries-heatmap(Note:series-heatmapis supported in ECharts 6.0 and below, but not in 6.1+).
Customize and convert ECharts to ESM
devIf you need a custom ECharts build (e.g., from the ECharts Online Builder), you must convert the resulting
echarts.min.jsto ESM format because Vite only supports ESM modules.Use the
@uni-echarts/c2eCLI tool to perform this conversion:- Run the CLI tool via
pnpm dlxornpx. - Follow the prompts to specify your input
echarts.min.jsand desired output file. - Important: The resulting ESM file must be placed in your project directory, but do not put it in the
staticdirectory, as mini-programs cannot parse theexportsyntax instaticfiles.
Note: The converted ESM output does not currently support Tree-Shaking and requires a default import.
pnpm dlx @uni-echarts/c2e@latestnpx @uni-echarts/c2e@latest- Run the CLI tool via
Configure Uni ECharts via Dependency Injection (Provide/Inject)
devUni ECharts provides and exports
provideAPIs that allow you to configureoption,theme,init-options,update-options, andloading-optionsusing Vue's Dependency Injection pattern. This allows you to set configurations at a parent component level that will apply to all Uni ECharts components within that scope.Supported configuration types include literals,
refs, and getter functions, as they are parsed internally using Vue'stoValueutility.// Example of providing a theme import { provideEchartsTheme } from "uni-echarts/shared"; provideEchartsTheme("dark");Install Uni ECharts via Uni Modules
devTo use Uni ECharts as a uni-module, follow these steps:
- Install the core
echartspackage using your preferred package manager:
pnpm add echarts # or yarn add echarts # or npm install echarts- Download the Uni ECharts plugin from the uni-app plugin market.
Note: If you need a customized ECharts build, refer to the 'Customizing ECharts' section in the documentation.
pnpm add echarts- Install the core
Enable Auto-import for Uni ECharts components and APIs
devYou can achieve automatic on-demand import of components and APIs by combining Uni ECharts with
@uni-helper/vite-plugin-uni-componentsandunplugin-auto-import.Steps:
- Install the helper plugins.
- Configure
vite.config.jsusingUniEchartsResolver. - (PNPM only) Add
shamefully-hoist=trueto your.npmrc. - (TypeScript only) Add
uni-echarts/globalto yourtsconfig.jsontypes for IDE support.
// vite.config.js import Uni from "@dcloudio/vite-plugin-uni"; import UniComponents from "@uni-helper/vite-plugin-uni-components"; import { UniEchartsResolver } from "uni-echarts/resolver"; import AutoImport from "unplugin-auto-import/vite"; import { defineConfig } from "vite"; export default defineConfig({ plugins: [ AutoImport({ resolvers: [ UniEchartsResolver() ] }), UniComponents({ resolvers: [ UniEchartsResolver() ] }), Uni() ] });Reduce bundle size with on-demand ECharts imports
devBecause mini-programs have strict code size limits, it is highly recommended to import only the necessary ECharts components and charts instead of the full library.
You can use an import code generator to get precise import statements by pasting your
optioncode.Options:
- Use the Uni ECharts Import Code Generator online tool.
- Use the Vue ECharts Import Code Generator.
If you must use the full ECharts library, you can import it directly using:
import "echarts";import "echarts";Fix invalid class/style on Mini-Program platforms
devUni ECharts uses
virtualHostto make custom nodes behave like Vue components in mini-programs. This causes standardclassandstyleattributes to fail. You have three ways to resolve this:1. Enable
mergeVirtualHostAttributes(Recommended)Add
mergeVirtualHostAttributes: trueto yourmanifest.jsonunder the specific mini-program platform configuration (mp-weixinormp-alipay). This allows standardclassandstyleto work.2. Use
custom-classorcustom-styleIf you don't want to change the manifest, use the provided props to apply styles:
custom-class: For CSS classes.custom-style: For inline styles (accepts string or object).
3. Use non-scoped styles
Move your styles to a
<style>block without thescopedattribute so they can be shared with the component.// manifest.json { "mp-weixin": { "mergeVirtualHostAttributes": true }, "mp-alipay": { "mergeVirtualHostAttributes": true } } // Usage with custom-style <uni-echarts custom-style="height: 300px" :option="option"></uni-echarts> <uni-echarts :custom-style="{ height: '300px' }" :option="option"></uni-echarts>Handle events in uni-echarts
devYou can bind events to the
uni-echartscomponent using Vue'sv-ondirective. The component supports three categories of events:- ECharts Events: Standard ECharts events like
click,legendselectchanged,datazoom,finished, etc. - ZRender Events: Low-level ZRender events prefixed with
zr:, such aszr:click,zr:mousedown, andzr:mousewheel. - Native DOM Events: To bind native DOM events, prefix the event name with
native:. Note that inuni-app, you should usetapinstead ofclickfor touch interactions.
Important Note: Only the
.oncemodifier is supported (and it is not supported on mini-programs) because other modifiers are tightly coupled to the DOM event mechanism.Other internal events:
inited: Triggered when initialization is complete.showtip: Triggered when tooltip is shown.hidetip: Triggered when tooltip is hidden.
<template> <uni-echarts :option="option" @click="handleClick" @finished.once="handleFinished" @zr:click="handleZrClick" @native:tap="handleNativeTap" ></uni-echarts> </template>- ECharts Events: Standard ECharts events like
How to support Uni ECharts
devIf Uni ECharts is helpful to your project, you can support the development team through several channels:
Community Support
- Star the repository: Available on GitHub and Gitee.
- Bookmark the plugin: Available on the DCloud Plugin Market.
Financial Sponsorship
You can provide voluntary financial sponsorship via WeChat or Alipay. When making a payment, please use the following remark formats to ensure your support is recognized:
- Platform-specific remark:
uni-echarts.[platform]@[id]: [message]- Example:
uni-echarts.github@xiaohe0601: 这个插件真好用! - Example:
uni-echarts.csdn@xiaohe0601: 这个插件真好用!
- Example:
- General remark:
uni-echarts.[id]: [message]- Example:
uni-echarts.xiaohe0601: 这个插件真好用!
- Example: