请问echarts 2.0如何引入到vue工程中?

新手上路,请多包涵

现在要做一个自定义的树图,echarts3引入方便,但是没有树图,遂想引echarts2进入vue工程中,请教一下大家,有什么办法吗?

阅读 5.2k
4 个回答

webpack中使用echarts
最近刚做的一个柱状图组件:

<template>
  <div :id="'chart_'+id" :style="{'width':width,'height':height}" v-loading="isLoading">

  </div>
</template>
<script>
  // 引入 ECharts 主模块
  var echarts = require('echarts/lib/echarts');
  // 引入柱状图
  require('echarts/lib/chart/bar');
  // 引入提示框和标题组件
  require('echarts/lib/component/tooltip');
  require('echarts/lib/component/title');

  var generateGUID = require('../utility.js').generateGUID

  export default {
    name: 'chartBar',
    props: {
      chartData: {
        type: Array,
        require: true,
        default: []
      },
      isLoading: {
        type: Boolean,
        default: false
      },
      width: {
        type: String,
        default: '830px'
      },
      height: {
        type: String,
        default: '250px'
      }
    },
    data() {
      return {
        id: generateGUID(),
        myChart: null
      }
    },
    methods: {
      setChart() {
        // 绘制图表
        this.myChart.setOption({
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          grid: {
            left: '16px',
            right: '29px',
            bottom: '43px',
            top: '24px',
            containLabel: true
          },
          xAxis: [{
            type: 'category',
            data: [
              "0-1", "1-2", "2-3", "3-4", "4-5", "5-6",
              "6-7", "7-8", "8-9", "9-10", "10-11", "11-12",
              "12-13", "13-14", "14-15", "15-16", "16-17", "17-18",
              "18-19", "19-20", "20-21", "21-22", "22-23", "23-24"
            ],
            axisTick: {
              interval: 0,
              alignWithLabel: true
            },
            axisLabel: {
              interval: 0,
              rotate: -45,
              fontSize: 10,
              color: '#475568'
            }
          }],
          yAxis: [
            {
              type: 'value',
              splitLine: {
                lineStyle: {
                  color: '#EFF2F7'
                }
              },
              axisLine: {
                show: false
              },
              axisTick: {
                show: false
              },
              axisLabel: {
                fontSize: 12,
                color: '#475568'
              }
            }
          ],
          series: [{
            name: '在线次数',
            type: 'bar',
            data: this.chartData,
            barWidth: '60%',
            itemStyle: {
              normal: {
                color: '#6890D4'
              },
              emphasis: {
                color: '#FF6633'
              }
            }
          }]
        });
      }
    },
    watch: {
      'chartData': function () {
        this.setChart()
      }
    },
    mounted() {
      // 基于准备好的dom,初始化echarts实例
      this.myChart = echarts.init(document.getElementById('chart_' + this.id))
    }
  }

</script>
<style scoped>
  #chart {
    width: 830px;
    height: 250px;
  }
</style>

图片描述

npm install echarts@2.2.7-beta7引入echarts2的依赖

clipboard.png

用Echarts.init方法初始化树图

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题