在vue项目中使用Echarts

169 阅读1分钟

该示例使用 vue-cli 脚手架搭建

安装echarts依赖

npm install echarts -S

全局引入

main.js

import echarts from 'echarts'

Vue.prototype.$echarts = echarts 

xxx.vue

<div id="myChart" :style="{width: '300px', height: '300px'}"></div>
export default {
  name: 'hello',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  },
  mounted(){
    this.drawLine();
  },
  methods: {
    drawLine(){
        // 基于准备好的dom,初始化echarts实例
        let myChart = this.$echarts.init(document.getElementById('myChart'))
        // 绘制图表
        myChart.setOption({
            title: { text: '在Vue中使用echarts' },
            tooltip: {},
            xAxis: {
                data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
            },
            yAxis: {},
            series: [{
                name: '销量',
                type: 'bar',
                data: [5, 20, 36, 10, 10, 20]
            }]
        });
    }
  }
}

---------------------

注意: 这里echarts初始化应在钩子函数mounted()中,这个钩子函数是在el 被新创建的 vm.$el 替换,并挂载到实例上去之后调用
参考地址:blog.csdn.net/mr_wuch/art…

图例换行且对齐


legend:{
                show: true,
                type: 'plain',
                bottom: '2%',
                orient: 'vertical',
                data:[
                        {
                            name: '无抵押大幅度',
                            icon: 'circle',                          
                        },
                        {
                            name: '有抵押',
                            icon: 'circle',                          
                        },
                        {
                            name: ''
                        },
                        {
                            name: '万商贷',
                            icon: 'circle',
                        },
                        {
                            name: 'O2O',
                            icon: 'circle',
                        },
                        {
                            name: ''
                        },
                        {
                            name: '直销',
                            icon: 'circle',
                        }
                ]

            },
            

data数组在需要换行的图例后面加入 ‘’,即可,中间不要有空格,否则无效 参考:segmentfault.com/a/119000001…