Uni ECharts

repository·dev·Indexed 41 days ago

https://github.com/xiaohe0601/uni-echarts

A 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.

Tokens
27K
Snippets
98
Records
110
Agent score
51%

What's inside Uni ECharts

  1. Overview of Uni ECharts

    dev

    Uni 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.
  2. Key features of Uni ECharts

    dev

    Uni ECharts provides the following capabilities for uni-app developers:

    • Cross-platform compatibility: Supports Web, Mini-programs (WeChat, etc.), and App environments.
    • Easy Integration: Supports easycom for automatic component registration in uni-app.
    • Developer Experience: Near-identical usage pattern to vue-echarts and full TypeScript support.
    • Licensing: Supports free commercial use under the MIT license.
  3. Unsupported features in Uni ECharts

    dev

    Due to limitations in the uni-app API, the following ECharts features are currently not supported:

    • SVG Renderer
    • toolbox.feature.saveAsImage
    • series-map
    • echarts-gl
    • series-heatmap (Note: series-heatmap is supported in ECharts 6.0 and below, but not in 6.1+).
  4. Customize and convert ECharts to ESM

    dev

    If you need a custom ECharts build (e.g., from the ECharts Online Builder), you must convert the resulting echarts.min.js to ESM format because Vite only supports ESM modules.

    Use the @uni-echarts/c2e CLI tool to perform this conversion:

    1. Run the CLI tool via pnpm dlx or npx.
    2. Follow the prompts to specify your input echarts.min.js and desired output file.
    3. Important: The resulting ESM file must be placed in your project directory, but do not put it in the static directory, as mini-programs cannot parse the export syntax in static files.

    Note: The converted ESM output does not currently support Tree-Shaking and requires a default import.

    pnpm dlx @uni-echarts/c2e@latest
    npx @uni-echarts/c2e@latest
  5. Configure Uni ECharts via Dependency Injection (Provide/Inject)

    dev

    Uni ECharts provides and exports provide APIs that allow you to configure option, theme, init-options, update-options, and loading-options using 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's toValue utility.

    // Example of providing a theme
    import { provideEchartsTheme } from "uni-echarts/shared";
    provideEchartsTheme("dark");
  6. Install Uni ECharts via Uni Modules

    dev

    To use Uni ECharts as a uni-module, follow these steps:

    1. Install the core echarts package using your preferred package manager:
    pnpm add echarts
    # or
    yarn add echarts
    # or
    npm install echarts
    1. 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
  7. Enable Auto-import for Uni ECharts components and APIs

    dev

    You can achieve automatic on-demand import of components and APIs by combining Uni ECharts with @uni-helper/vite-plugin-uni-components and unplugin-auto-import.

    Steps:

    1. Install the helper plugins.
    2. Configure vite.config.js using UniEchartsResolver.
    3. (PNPM only) Add shamefully-hoist=true to your .npmrc.
    4. (TypeScript only) Add uni-echarts/global to your tsconfig.json types 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()
      ]
    });
  8. Reduce bundle size with on-demand ECharts imports

    dev

    Because 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 option code.

    Options:

    1. Use the Uni ECharts Import Code Generator online tool.
    2. 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";
  9. Fix invalid class/style on Mini-Program platforms

    dev

    Uni ECharts uses virtualHost to make custom nodes behave like Vue components in mini-programs. This causes standard class and style attributes to fail. You have three ways to resolve this:

    Add mergeVirtualHostAttributes: true to your manifest.json under the specific mini-program platform configuration (mp-weixin or mp-alipay). This allows standard class and style to work.

    2. Use custom-class or custom-style

    If 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 the scoped attribute 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>
  10. Handle events in uni-echarts

    dev

    You can bind events to the uni-echarts component using Vue's v-on directive. The component supports three categories of events:

    1. ECharts Events: Standard ECharts events like click, legendselectchanged, datazoom, finished, etc.
    2. ZRender Events: Low-level ZRender events prefixed with zr:, such as zr:click, zr:mousedown, and zr:mousewheel.
    3. Native DOM Events: To bind native DOM events, prefix the event name with native:. Note that in uni-app, you should use tap instead of click for touch interactions.

    Important Note: Only the .once modifier 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>
  11. How to support Uni ECharts

    dev

    If Uni ECharts is helpful to your project, you can support the development team through several channels:

    Community Support

    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:

    1. Platform-specific remark: uni-echarts.[platform]@[id]: [message]
      • Example: uni-echarts.github@xiaohe0601: 这个插件真好用!
      • Example: uni-echarts.csdn@xiaohe0601: 这个插件真好用!
    2. General remark: uni-echarts.[id]: [message]
      • Example: uni-echarts.xiaohe0601: 这个插件真好用!