Giter VIP home page Giter VIP logo

vue-echarts's Introduction

Vue-ECharts

Vue.js (v2/v3) component for Apache ECharts™ (v5).

View Demo →

Open in Codeflow Edit in CodeSandbox

Important

We have released an import code generator that can generate precise import code by pasting the option code.

Try it →


💡 Heads up 💡 前往中文版

If you are migrating from vue-echarts ≤ 5, you should read the Migration to v6 section before you update to v6.

Not ready yet? Read documentation for older versions here →

Installation & Usage

npm & ESM

npm i echarts vue-echarts

To make vue-echarts work for Vue 2 (<2.7.0), you need to have @vue/composition-api installed (@vue/runtime-core for TypeScript support):

npm i @vue/composition-api
npm i @vue/runtime-core # for TypeScript support

If you are using NuxtJS on top of Vue 2, you'll need @nuxtjs/composition-api:

npm i @nuxtjs/composition-api

And then add '@nuxtjs/composition-api/module' in the buildModules option in your nuxt.config.js.

Example

Vue 3 Demo →
<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>
Vue 2 Demo →
<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

We encourage manually importing components and charts from ECharts for smaller bundle size. We've built an import code generator to help you with that. You can just paste in your option code and we'll generate the precise import code for you.

Try it →

But if you really want to import the whole ECharts bundle without having to import modules manually, just add this in your code:

import "echarts";

CDN & Global variable

Drop <script> inside your HTML file and access the component via window.VueECharts.

Vue 3 Demo →
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
const app = Vue.createApp(...)

// register globally (or you can do it locally)
app.component('v-chart', VueECharts)
Vue 2 Demo →
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
// register globally (or you can do it locally)
Vue.component("v-chart", VueECharts);

See more examples here.

Props

  • init-options: object

    Optional chart init configurations. See echarts.init's opts parameter here →

    Injection key: INIT_OPTIONS_KEY.

  • theme: string | object

    Theme to be applied. See echarts.init's theme parameter here →

    Injection key: THEME_KEY.

  • option: object

    ECharts' universal interface. Modifying this prop will trigger ECharts' setOption method. Read more here →

    💡 When update-options is not specified, notMerge: false will be specified by default when the setOption method is called if the option object is modified directly and the reference remains unchanged; otherwise, if a new reference is bound to option, notMerge: true will be specified.

  • update-options: object

    Options for updating chart option. See echartsInstance.setOption's opts parameter here →

    Injection key: UPDATE_OPTIONS_KEY.

  • group: string

    Group name to be used in chart connection. See echartsInstance.group here →

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

    Whether the chart should be resized automatically whenever its root is resized. Use the options object to specify a custom throttle delay (in milliseconds) and/or an extra resize callback function.

  • loading: boolean (default: false)

    Whether the chart is in loading state.

  • loading-options: object

    Configuration item of loading animation. See echartsInstance.showLoading's opts parameter here →

    Injection key: LOADING_OPTIONS_KEY.

  • manual-update: boolean (default: false)

    For performance critical scenarios (having a large dataset) we'd better bypass Vue's reactivity system for option prop. By specifying manual-update prop with true and not providing option prop, the dataset won't be watched any more. After doing so, you need to retrieve the component instance with ref and manually call setOption method to update the chart.

Provide / Inject

Vue-ECharts provides provide/inject API for theme, init-options, update-options and loading-options to help configuring contextual options. eg. for init-options you can use the provide API like this:

Vue 3
import { THEME_KEY } from 'vue-echarts'
import { provide } from 'vue'

// composition API
provide(THEME_KEY, 'dark')

// options API
{
  provide: {
    [THEME_KEY]: 'dark'
  }
}
Vue 2
import { THEME_KEY } from 'vue-echarts'

// in component options
{
  provide: {
    [THEME_KEY]: 'dark'
  }
}

Note

You need to provide an object for Vue 2 if you want to change it dynamically.

// in component options
{
  data () {
    return {
      theme: { value: 'dark' }
    }
  },
  provide () {
    return {
      [THEME_KEY]: this.theme
    }
  }
}

Methods

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

Static Methods

Static methods can be accessed from echarts itself.

Events

You can bind events with Vue's v-on directive.

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

Note

Only the .once event modifier is supported as other modifiers are tightly coupled with the DOM event system.

Vue-ECharts support the following events:

  • 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
  • Mouse events
  • ZRender events
    • zr:click
    • zr:mousedown
    • zr:mouseup
    • zr:mousewheel
    • zr:dblclick
    • zr:contextmenu

See supported events here →

CSP: style-src or style-src-elem

If you are applying a CSP to prevent inline <style> injection, you need to use files from dist/csp directory and include dist/csp/style.css into your app manually.

Migration to v6

💡 Please make sure to read the migration guide for ECharts 5 as well.

The following breaking changes are introduced in vue-echarts@6:

Vue 2 support

  • If you are using version prior to [email protected], @vue/composition-api is required to be installed to use Vue-ECharts with Vue 2 (and also @vue/runtime-core for TypeScript support).

Props

  • options is renamed to option to align with ECharts itself.
  • Updating option will respect update-options configs instead of checking reference change.
  • watch-shallow is removed. Use manual-update for performance critical scenarios.

Methods

  • mergeOptions is renamed to setOption to align with ECharts itself.
  • showLoading and hideLoading is removed. Use the loading and loading-options props instead.
  • appendData is removed. (Due to ECharts 5's breaking change.)
  • All static methods are removed from vue-echarts. Use those methods from echarts directly.

Computed getters

  • Computed getters (width, height, isDisposed and computedOptions) are removed. Use the getWidth, getHeight, isDisposed and getOption methods instead.

Styles

  • Now the root element of the component have 100%×100% size by default, instead of 600×400.

Local development

pnpm i
pnpm serve

Open http://localhost:8080 to see the demo.

Notice

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.

vue-echarts's People

Contributors

565871779 avatar dependabot[bot] avatar hyf0 avatar justineo avatar meteorlxy avatar ovilia avatar reinisv avatar sadwood avatar tennyzhuang avatar zamplyy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-echarts's Issues

:options 初始必须有值是吧

因为我的options 会在异步之后才能确定,本来想把options设为空,然后再异步后mergeOptions,但是会报错

webpack 打包后IE浏览器无法浏览

之前的问题后来发现不是插件的问题,但是当使用vue-cli等脚手架开发页面,使用插件时,执行npm run dev时,在谷歌浏览器是正常的,但是在IE下是无法查看的,这个有解决方案吗

使用 vue-cli 构建的项目生产环境报错( TypeError: Cannot use 'in' operator to search for '_ec_inner')

在开发环境没有问题, 在生产环境中会有下面的报错:

Uncaught (in promise) TypeError: Cannot use 'in' operator to search for '�_ec_inner' in <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>fe</title>
  <link href="/static/css/app.cc1d0ba7c7152e99eff88bced225d9ff.css" rel="stylesheet"></head>
  <body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  <script type="text/javascript" src="/static/js/manifest.b255540fc86fd206b64e.js"></script><script type="text/javascript" src="/static/js/vendor.adec9a024f0aecb0a52e.js"></script><script type="text/javascript" src="/static/js/app.921d2b1a87d4571b4a03.js"></script></body>
</html>


    at i.setOption (Global.js:68)
    at o.X.setOption (echarts.js:283)
    at a.mergeOptions (app.921d2b1….js:157)
    at a.n [as mergeOptions] (vue.common.js:126)
    at a.<anonymous> (app.921d2b1….js:72)

.echarts 无法使用media自适应,取消就可以了

使用echarts3的时候 在移动端使用media自适应,无法动态改变大小(使用了auto-resize),发现是插件有样式
.echarts {
width: 600px;
height: 400px;
}
去掉width后就可以自适应了,请问如何使用media自适应?

替换$watch

代码中在mounted过程绑定this.$watch的方式监听options与group的变化。可以使用Watch Property代替在mounted过程中的手动绑定。watch property

this.$watch('options', options => {
  chart.setOption(options, true)
}, { deep: true })
...
mounted: {
...
}, 
watch: {
  options: {
     handler: function(val) {
        this.chart.setOption(options, true);
     }, 
     deep: true,
  },
  group: {
  ...
  },
}

打包还是会报错

使用的最新版的vue-cli,按照README配置的

      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules(?![\\/]vue-echarts[\\/])/,
        include: [
          resolve('src'),
          resolve('test'),
          resolve('node_modules/vue-echarts'),
          resolve('node_modules/echarts')
        ]
      }

util报错

ERROR in static/js/5.bd94499254ffc50f0d2b.js from UglifyJs
Unexpected token: name (warn) [./~/.2.3.3@vue-echarts/util.js:3,0][static/js/5.bd94499254ffc50f0d2b.js:22816,4]

这样导入的

  import Echarts from 'vue-echarts/components/ECharts.vue'
  import 'echarts/lib/chart/bar'
  import 'echarts/lib/component/tooltip'
  import 'echarts/lib/loading/default'
  Vue.component("Echarts",Echarts);

关于echarts表格初始化width和height问题

由于vue 单组件加载时不会监听document.ready事件,我的组件是一个flex,在组件挂载时组件大小有一个从0变大的过程,在这个过程中,echarts初始化会根据默认值初始化,我想定义chart的宽高就成为问题了,如果在css里定义,那在组件mounted的时候css未加载会出现组件width =0的情况,那表格初始化会变成非常小。这个问题应该如何解决呢?

我引入注册地图时报错时怎么回事

import chinaMap from './china.json'
import ECharts from 'vue-echarts/components/ECharts.vue'
ECharts.registerMap('china', chinaMap)

会报一个错,Cannot read property 'apply' of undefined 在ECharts.vue 205行

demo运行不起来

Uncaught TypeError: Cannot read property 'split' of undefined 一直报这个错误

IE

这个插件在IE浏览器下打不开。。。。。。

缺少 Vue.use 注册方案

import Vue from 'vue'
import ECharts from 'vue-echarts'
Vue.use(Echarts)

可以参照其它插件,给vue-echarts 加入注册钩子

调用解除联动方法出现错误

image
在确保已经正确引入文件的情况下,调用ECharts.disconnect('groupName')的时候会出现上图错误。请问是为什么?

我的相关代码如下:

// template
#waveform
     chart(:options='chartOption', ref='chart1', group='waveform')
     chart(:options='chartOption2', ref='chart2', group='waveform')

// script
import ECharts from 'vue-echarts/components/ECharts.vue';
export default {
   ...
   mounted() {
        let chart1 = this.$refs.chart1;
        let chart2 = this.$refs.chart2;
        ECharts.connect([chart1, chart2]);
        ECharts.disconnect('waveform');
   }
}

在Edge浏览器报错,无法正常使用

在chrome,safari等浏览器都正常,但发现在Edge浏览器报错,不管是静态的option数据还是mergeOption都会提示

TypeError: 调用的对象无效
eval code (4100) (1446,7)
   {
      [functions]: ,
      __proto__: { },
      description: "调用的对象无效",
      message: "调用的对象无效",
      name: "TypeError",
      number: -2147418113,
      stack: "TypeError: 调用的对象无效
   at Animation.prototype._startLoop (eval code:177:13)
   at Animation.prototype.start (eval code:188:13)
   at ZRender (eval code:146:9)
   at zrender.init (eval code:47:9)
   at ECharts (eval code:125:9)
   at echarts.init (eval code:1536:9)
   at _init (eval code:133:7)
   at boundFn (eval code:122:5)
   at mounted (eval code:166:7)
   at callHook (eval code:2257:9)"
   }

theme 该怎么用?

这是问题,并不是bug,只是我不知道该在哪里提问,很抱歉。
我想改变图表颜色,通过你的demo的做法,确实能改变主题色,但是我该怎么改自己想要的颜色呢?ECharts官方文档我也并没有找到theme字段。
很疑惑,求解答,谢谢

按需引入出错

环境:
win10 vue-cli:2.8.1 vue : 2.2.6
在main.js里面引入

import` ECharts from 'vue-echarts/components/ECharts.vue'
import 'echarts/lib/chart/line'
import 'echarts/lib/component/tooltip'

在.vue文件使用:

报错

[Vue warn]: Error in mounted hook: 
TypeError: Cannot read property 'findAxisModel' of undefined

打包出错

node -v 
v7.6.0
npm -v
4.1.2
import  VueECharts from 'vue-echarts/components/ECharts.vue'
Vue.component('echart', VueECharts);

使用 webpack 打包出错

ERROR in ./~/buble-loader!./~/vue-loader/lib/selector.js?type=script&index=0!./~/vue-echarts/components/ECharts.vue
Module parse failed: /Users/lyfing/Code/laravel-vue-starter/node_modules/buble-loader/index.js!/Users/lyfing/Code/laravel-vue-starter/node_modules/vue-loader/lib/selector.js?type=script&index=0!/Users/lyfing/Code/laravel-vue-starter/node_modules/vue-echarts/components/ECharts.vue Unexpected token (155:13)
You may need an appropriate loader to handle this file type.
|         return
|       }
|       return var ref = this.chart;
|       ref[name].apply(ref, args)
|     },
 @ ./~/vue-echarts/components/ECharts.vue 8:18-101
 @ ./resources/assets/js/admin.js

已配置

 {
          test: /\.js$/,
          loader: 'babel',
          exclude: /node_modules/
 }

使用 import VueECharts from 'vue-echarts' 编译通过,页面显示正常,但是无法设置主题

如何引入主题

尝试了好几种方式,都没有成功,可以给一个例子不?

页面多个图表

如果页面上面有多个图表 使用这个应该怎么弄,
image 第二个div里面的数据不会渲染 应该怎么弄好

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.