vue cli3 使用 echarts

138 篇文章 1 订阅
2 篇文章 0 订阅

1 首先安装:

npm install echarts --save

2 在main.js中导入并挂载上

import echarts from "echarts"
Vue.prototype.$echarts = echarts

3、使用:在app.vue中使用

<template>
  <div id="app">
    <div id="box" style="width: 50%; height: 300px"></div>
  </div>
</template>
<script>
  export default {
    name: 'app',
    methods:{
      // 折线图
      polygonal_chart(){
        let my_echarts = this.$echarts.init(document.getElementById("box"))
        let option = {
          xAxis: {
            type: 'category',
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
          },
          yAxis: {
            type: 'value'
          },
          series: [{
            data: [820, 932, 901, 934, 1290, 1330, 1320],
            type: 'line'
          }]
        };

        my_echarts.setOption(option)
      }
    },
    mounted(){
      this.polygonal_chart()
    },
  }
</script>

 效果如图:

需要注意的是

<div id="box" style="width: 50%; height: 300px"></div>

一定要有高并且不能是百分比

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue CLI使用echarts的步骤如下: 第一步:在main.js中引入echarts库。 ```javascript import echarts from 'echarts' Vue.prototype.$echarts = echarts ``` 第二步:创建一个Vue组件并在template中添加一个div元素用于渲染图表。 ```html <template> <div class> <div id="myChart" :style="{ height: '500px',}"></div> </div> </template> ``` 在script中,引入echarts需要使用的组件和插件,然后在mounted钩子函数中调用drawLine方法绘制图表。 ```javascript <script> import echarts from 'echarts/lib/echarts' require('echarts/lib/chart/bar') require('echarts/lib/component/tooltip') require('echarts/lib/component/title') export default { name: 'hello', data() { return { msg: 'Welcome to Your Vue.js App' } }, mounted() { this.drawLine(); }, methods: { drawLine() { let myChart = echarts.init(document.getElementById('myChart')) myChart.setOption({ title: { text: 'ECharts' }, tooltip: {}, xAxis: { data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20] }] }); } } } </script> ``` 第三步:创建echarts.vue组件并在template中添加一个div元素用于渲染图表。 ```html <template> <div id="myChart" :style="{ width: '300px', height: '300px' }"></div> </template> ``` 在script中,使用this.$echarts.init方法初始化echarts实例,然后调用setOption方法绘制图表。 ```javascript <script> export default { name: 'echarts', data() { return { msg: 'Welcome to Your Vue.js App' } }, mounted() { this.drawLine(); }, methods: { drawLine() { 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] }] }); } } } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值