如何在vue中引用echarts

如何在vue中引用echarts

方法一

  • home.vue中使用
<template>
	<div ref="charts"></div>
</template>
<script>
import echarts from 'echarts'
export default {
	name: 'home',
	methods: {
		initCharts () {
			let option = {
			        title: {
			          text: 'ECharts 入门示例'
			        },
			        tooltip: {},
			        legend: {
			          data: ['销量']
			        },
			        xAxis: {
			          data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
			        },
			        yAxis: {},
			        series: [{
			          name: '销量',
			          type: 'bar',
			          data: [5, 20, 36, 10, 10, 20]
			        }]
			};
			this.chart = echarts.init(this.$refs.chart)
			this.chart.setOption(option)
		}
	},
	mounted () {
		this.initCharts();
	}
}
</script>

方法二

  • 把图的部分放入mapt.vue并在home.vue中引用,使代码简洁
  • map.vue部分
<template>
  <div ref="sjtu_map"></div>
</template>

<script>
import echart from 'echarts'

export default {
  name: 'sjtu-map',
  props: {
    text: {
      type: String,
      default: 'ECharts 入门示例'
    },
    legend: {
      type: [Array, String],
      default: () => ['销量']
    },
    xLabel: {
      type: [String, Number, Array],
      default: () => ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
    },
    yData: {
      type: [Number, Array],
      default: () => [5, 20, 36, 10, 10, 20]
    }
  },
  methods: {
    initCharts () {
      let option = {
        title: {
          text: this.text
        },
        tooltip: {},
        legend: {
          data: this.legend
        },
        xAxis: {
          data: this.xLabel
        },
        yAxis: {},
        series: [{
          name: this.legend[0],
          type: 'bar',
          data: this.yData
        }]
      };

      this.chart = echart.init(this.$refs.sjtu_map)
      this.chart.setOption(option)
    }
  },
  mounted () {
    this.initCharts();
  }
}
</script>

<style>
</style>
  • home.vue部分
<template>
  <body>
    <div>
      <div>
        1212
        <button @click="test">This is a button!</button>
        Hello Vue!
      </div>
        <sjtu-map
          style="width: 400px; height: 300px"
          :text="mapData[0].text"
          :legend="mapData[1].legend"
          :x-label="mapData[2].xLabel"
          :y-data="mapData[3].yData"
        ></sjtu-map>
      </div>
  </body>


</template>

<script>
import echarts from 'echarts'
import sjtuMap from '_c/charts/map'

export default {
  name: 'home',
  data () {
    return {
      mapData: [
        {text: '这次我用了新的标题'},
        {legend: ['新销量']},
        {xLabel: ["新衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]},
        {yData: [50, 20, 36, 10, 10, 20]}
      ]
    }
  },
  components: {
    sjtuMap
  },
  methods: {
    test () {
      console.log(this.mapData[0].text)
    }
  },
  mounted () {
  }
}


</script>

<style>
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值