Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vue-echarts

ecomfe319.1kMIT7.0.3TypeScript support: included

Vue.js component for Apache ECharts™.

readme

Vue-ECharts

Apache ECharts™ 的 Vue.js 组件。

npm 版本 查看演示

Open in Codeflow Edit in CodeSandbox


还在使用 v6?可以继续阅读老版本的文档。前往 →

安装 & 使用

npm

npm add echarts vue-echarts

示例

<summary>Vue 3 Demo →</summary>
<template>
  <v-chart class="chart" :option="option" />
</template>

<script setup>
import { use } from "echarts/core";
import { CanvasRenderer } from "echarts/renderers";
import { PieChart } from "echarts/charts";
import {
  TitleComponent,
  TooltipComponent,
  LegendComponent
} from "echarts/components";
import VChart, { THEME_KEY } from "vue-echarts";
import { ref, provide } from "vue";

use([
  CanvasRenderer,
  PieChart,
  TitleComponent,
  TooltipComponent,
  LegendComponent
]);

provide(THEME_KEY, "dark");

const option = ref({
  title: {
    text: "Traffic Sources",
    left: "center"
  },
  tooltip: {
    trigger: "item",
    formatter: "{a} <br/>{b} : {c} ({d}%)"
  },
  legend: {
    orient: "vertical",
    left: "left",
    data: ["Direct", "Email", "Ad Networks", "Video Ads", "Search Engines"]
  },
  series: [
    {
      name: "Traffic Sources",
      type: "pie",
      radius: "55%",
      center: ["50%", "60%"],
      data: [
        { value: 335, name: "Direct" },
        { value: 310, name: "Email" },
        { value: 234, name: "Ad Networks" },
        { value: 135, name: "Video Ads" },
        { value: 1548, name: "Search Engines" }
      ],
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: "rgba(0, 0, 0, 0.5)"
        }
      }
    }
  ]
});
</script>

<style scoped>
.chart {
  height: 400px;
}
</style>
<summary>Vue 2 Demo →</summary>
<template>
  <v-chart class="chart" :option="option" />
</template>

<script>
import { use } from "echarts/core";
import { CanvasRenderer } from "echarts/renderers";
import { PieChart } from "echarts/charts";
import {
  TitleComponent,
  TooltipComponent,
  LegendComponent
} from "echarts/components";
import VChart, { THEME_KEY } from "vue-echarts";

use([
  CanvasRenderer,
  PieChart,
  TitleComponent,
  TooltipComponent,
  LegendComponent
]);

export default {
  name: "HelloWorld",
  components: {
    VChart
  },
  provide: {
    [THEME_KEY]: "dark"
  },
  data() {
    return {
      option: {
        title: {
          text: "Traffic Sources",
          left: "center"
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
          orient: "vertical",
          left: "left",
          data: [
            "Direct",
            "Email",
            "Ad Networks",
            "Video Ads",
            "Search Engines"
          ]
        },
        series: [
          {
            name: "Traffic Sources",
            type: "pie",
            radius: "55%",
            center: ["50%", "60%"],
            data: [
              { value: 335, name: "Direct" },
              { value: 310, name: "Email" },
              { value: 234, name: "Ad Networks" },
              { value: 135, name: "Video Ads" },
              { value: 1548, name: "Search Engines" }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            }
          }
        ]
      }
    };
  }
};
</script>

<style scoped>
.chart {
  height: 400px;
}
</style>

[!IMPORTANT] 我们鼓励手动从 ECharts 中引入组件和图表,以减小打包体积。我们已经为此构建了一个导入代码生成器。你只需要把option 代码粘贴进去,就可以得到精确的导入代码。

试一试 →

但如果你实在需要全量引入 ECharts 从而无需手动引入模块,只需要在代码中添加:

import "echarts";

CDN

用如下方式在 HTML 中插入 <script> 标签,并且通过 window.VueECharts 来访问组件接口:

<summary>Vue 3 Demo →</summary>
<script src="https://cdn.jsdelivr.net/npm/vue@3.4.33"></script>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.5.1"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@7.0.3"></script>
const app = Vue.createApp(...)

// 全局注册组件(也可以使用局部注册)
app.component('v-chart', VueECharts)
<summary>Vue 2 Demo →</summary>
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.16"></script>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.5.1"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@7.0.3"></script>
// 全局注册组件(也可以使用局部注册)
Vue.component("v-chart", VueECharts);

可以在这里查看更多例子。

Prop

  • init-options: object

    初始化附加参数。请参考 echarts.initopts 参数。前往 →

    Inject 键名:INIT_OPTIONS_KEY

  • theme: string | object

    要应用的主题。请参考 echarts.inittheme 参数。前往 →

    Inject 键名:THEME_KEY

  • option: object

    ECharts 的万能接口。修改这个 prop 会触发 ECharts 实例的 setOption 方法。查看详情 →

    💡 在没有指定 update-options 时,如果直接修改 option 对象而引用保持不变,setOption 方法调用时将默认指定 notMerge: false;否则,如果为 option 绑定一个新的引用,将指定 notMerge: true

  • update-options: object

    图表更新的配置项。请参考 echartsInstance.setOptionopts 参数。前往 →

    Inject 键名:UPDATE_OPTIONS_KEY

  • group: string

    图表的分组,用于联动。请参考 echartsInstance.group前往 →

  • autoresize: boolean | { throttle?: number, onResize?: () => void }(默认值false

    图表在组件根元素尺寸变化时是否需要自动进行重绘。也可以传入一个选项对象来指定自定义的节流延迟和尺寸变化时的额外回调函数。

  • loading: boolean(默认值:false

    图表是否处于加载状态。

  • loading-options: object

    加载动画配置项。请参考 echartsInstance.showLoadingopts 参数。前往 →

    Inject 键名:LOADING_OPTIONS_KEY

  • manual-update: boolean(默认值false

    在性能敏感(数据量很大)的场景下,我们最好对于 option prop 绕过 Vue 的响应式系统。当将 manual-update prop 指定为 true 且不传入 option prop 时,数据将不会被监听。然后,需要用 ref 获取组件实例以后手动调用 setOption 方法来更新图表。

事件

可以使用 Vue 的 v-on 指令绑定事件。

<template>
  <v-chart :option="option" @highlight="handleHighlight" />
</template>

Note

仅支持 .once 修饰符,因为其它修饰符都与 DOM 事件机制强耦合。

Vue-ECharts 支持如下事件:

  • highlight
  • downplay
  • selectchanged
  • legendselectchanged
  • legendselected
  • legendunselected
  • legendselectall
  • legendinverseselect
  • legendscroll
  • datazoom
  • datarangeselected
  • timelinechanged
  • timelineplaychanged
  • restore
  • dataviewchanged
  • magictypechanged
  • geoselectchanged
  • geoselected
  • geounselected
  • axisareaselected
  • brush
  • brushEnd
  • brushselected
  • globalcursortaken
  • rendered
  • finished
  • 鼠标事件
  • ZRender 事件
    • zr:click
    • zr:mousedown
    • zr:mouseup
    • zr:mousewheel
    • zr:dblclick
    • zr:contextmenu

请参考支持的事件列表。前往 →

原生 DOM 事件

由于 Vue-ECharts 默认将事件绑定到 ECharts 实例,因此在使用原生 DOM 事件时需要做一些特殊处理。你需要在事件名称前加上 native: 前缀来绑定原生 DOM 事件(可以在 Vue 2 中也可以使用 .native 修饰符,但这在 Vue 3 中已被废弃)。

<template>
  <v-chart @native:click="handleClick" />
</template>

Provide / Inject

Vue-ECharts 为 themeinit-optionsupdate-optionsloading-options 提供了 provide/inject API,以通过上下文配置选项。例如:可以通过如下方式来使用 provide API 为 init-options 提供上下文配置:

<summary>Vue 3</summary>
import { THEME_KEY } from 'vue-echarts'
import { provide } from 'vue'

// 组合式 API
provide(THEME_KEY, 'dark')

// 选项式 API
{
  provide: {
    [THEME_KEY]: 'dark'
  }
}
<summary>Vue 2</summary>
import { THEME_KEY } from 'vue-echarts'

// 组件选项中
{
  provide: {
    [THEME_KEY]: 'dark'
  }
}

Note

在 Vue 2 中,如果你想动态地改变这些选项,那么你需要提供一个对象。

// 组件选项中
{
  data () {
    return {
      theme: { value: 'dark' }
    }
  },
  provide () {
    return {
      [THEME_KEY]: this.theme
    }
  }
}

方法

  • setOption
  • getWidth
  • getHeight
  • getDom
  • getOption
  • resize
  • dispatchAction
  • convertToPixel
  • convertFromPixel
  • containPixel
  • showLoading
  • hideLoading
  • getDataURL
  • getConnectedDataURL
  • clear
  • dispose

静态方法

静态方法请直接通过 echarts 本身进行调用。

CSP: style-srcstyle-src-elem

如果你正在应用 CSP 来防止内联 <style> 注入,则需要使用 vue-echarts/csp 代替 vue-echarts,并手动引入 vue-echarts/csp/style.css

迁移到 v7

Translate: Read the breaking changes document in the release log and the migration shoud be straightforward.

请阅读发布日志中的变更记录,之后迁移过程应该会相对简单。

本地开发

pnpm i
pnpm serve

打开 http://localhost:8080 来查看 demo。

声明

The Apache Software Foundation Apache ECharts, ECharts, Apache, the Apache feather, and the Apache ECharts project logo are either registered trademarks or trademarks of the Apache Software Foundation.

changelog

5.0.0-beta.0

  • Update peer dependency for vue to ^2.4.0. BREAKING

4.1.0

  • Fix the problem that mergeOptions didn't use the correct options if the instance is inited on-the-fly.
  • Expose ZRender events via zr: prefixed events.
  • Update to `echarts@4.5.0` (only affects the bundled version).

4.0.4

4.0.3

4.0.2

  • Make manual-update truely responsive.

4.0.1

  • Fix legendscroll event.

4.0.0

  • Release 4.0.0.

4.0.0-beta.1

  • Fix autoresize.

4.0.0-beta.0

  • Move echarts into peerDependencies. BREAKING
  • Rename auto-resize to autoresize. BREAKING
  • Point module entry to the source version. BREAKING
  • Switch to Vue CLI 3 for demo.

3.1.2

  • Fix the problem that setOption is always called with notMerge: true.

3.1.1

  • Fix the problem that options are not watched as expected.

3.1.0

  • Add manual-update prop to handle performance critical scenarios.
  • Deprecate watch-shallow prop as it was actually not working as expected.
  • Fix the computed getters by using Object.defineProperties directly instead of Vue's computed as it no longer works as expected after Vue 2.0.
  • Remove chart from data to gain a performance boost.

3.0.9

3.0.8

  • Add new events and API to adapt the latest version of ECharts.

3.0.7

  • Only apply optimization introduce in last version for charts resize from 0 area.

3.0.6

  • Optimize auto-resize for initially hidden (display: none) charts.

3.0.5

3.0.4

  • Fix misused MutationObserver (#200).

3.0.3

3.0.2

  • Update ECharts to 4.0.2.

3.0.1

  • Fix npm distribution.

3.0.0

  • Added support for ECharts 4.
  • auto-resize now listens to element size change instead of window.
  • Remove deprecated chart prefixed events.

2.6.0

  • Added watchShallow prop to manually disable deep watch on options to optimize performance for charts with large amout of data.
  • Made all props reactive.
  • Updated ECharts dependency to ^3.8.5.

2.5.1

  • Updated ECharts dependency to 3.8.2+ to fix module breaking change introduced in 3.8.0.

2.5.0

  • Fixed collision with Vue's internal methods by removing _ prefix.
  • mergeOptions now accept same arguments as ECharts' setOption method.
  • Updated ECharts dependency to 3.7.2+.

2.4.1

  • Made theme reactive.
  • Added focusnodeadjacency & unfocusnodeadjacency events.
  • Fixed the problem that charts won't refresh after keep-alive components are activated.

2.4.0

  • Add computedOptions.

2.3.9

  • Replace publish npm scripts with shell commands to prevent failure upon npm install.

2.3.8

  • Fixed the problem that styles are missing for precompiled version.

2.3.7

  • Switch back to Vue.util.warn.
  • Switch build tool to rollup.

2.3.6

  • Hot fix for last version. Use console.warn temporarily.

2.3.5

  • Mark Vue as an external dependency in webpack config.

2.3.4

  • Use Vue.util.warn directly.

2.3.3

  • Fix NPM package.

2.3.2

  • Fix the implementation of disconnect.

2.3.1

  • Correctly dispose ECharts instance before component is destroyed.
  • Fix the problem that group is not properly initialized.

2.3.0

  • As native events are now not listened by v-on in Vue.js 2.0, change mouse events name to original ones (keeping emitting chart* events for now).
  • Fix getter for width / height / isDisposed.
  • options is now optional to initialize the component and the chart will be initialized automatically when options is set.

2.2.0

  • Add auto-resize.
  • Refined demo.

2.1.0

  • Fix disconnect.
  • When importing ECharts.vue, only ECharts core will be imported instead of the whole ECharts bundle.

2.0.0

  • Update Vue dependency to 2.0.1.
  • Add support for new methods & events for ECharts.
  • Fix missing arguments for some APIs.

0.1.2

  • Update ECharts version.
  • Remove unnecessary files from NPM package.

0.1.1

  • Fix usage in README.

0.1.0

  • First version.