vue-cli项目中使用动态echarts

1、安装:npm i -g vue-cli
初始化vue 项目:vue init webpack vue-demo02
项目安装过程:

? Project name # 回车
? Project description # 回车
? Author  # 回车
? Vue build standalone  # =>  # 回车
? Install vue-router? # Yes
? Use ESLint to lint your code? # Yes
? Pick an ESLint preset Standard  # standard
? Set up unit tests # No
? Setup e2e tests with Nightwatch? # No
? Should we run `npm install` for you after the project has been created? # (recommended) npm

2、配置app.vue文件

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
</script>

<style>
</style>

3、安装 ECharts:npm install echarts --save
4、配置HelloWorld.vue文件

<template>
  <div id="main" :style="{width: '800px', height: '500px'}"></div>
</template>

<script>
import echarts from 'echarts'
export default {
  data () {
    return {
      timer: null
    }
  },
  mounted () {
    this.timer = setInterval(this.drawLine, 1000)
  },
  methods: {
    drawLine () {
      var base = +new Date()
      var oneSecond = 1000
      var date = []
      var data = []
      for (var i = 1; i <= 10; i++) {
        var now = new Date((base += oneSecond))
		date.push(
          [
            ('0' + now.getHours()).slice(-2),
            ('0' + now.getMinutes()).slice(-2),
            ('0' + now.getSeconds()).slice(-2)
          ].join(':')
        )
        data.push(Math.round((Math.random() - 0.5) * 600))
      }
      // 基于准备好的dom,初始化echarts实例
      var myChart = echarts.init(document.getElementById('main'))
      // 绘制图表
      myChart.setOption({
        tooltip: {
          trigger: 'axis',
          position: function (pt) {
            return [pt[0], '10%']
          }
        },
        title: {
          left: 'center',
          text: '大数据量面积图'
        },
        toolbox: {
          feature: {
            dataZoom: {
              yAxisIndex: 'none'
            },
            restore: {},
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: date
        },
        yAxis: {
          type: 'value',
          boundaryGap: [0, '100%']
        },
        series: [
          {
            name: '模拟数据',
            type: 'line',
            smooth: true,
            symbol: 'none',
            sampling: 'average',
            itemStyle: {
              color: 'rgb(255, 70, 131)'
            },
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgb(255, 158, 68)'
                },
                {
                  offset: 1,
                  color: 'rgb(255, 70, 131)'
                }
              ])
            },
            data: data
          }
        ]
      })
    }
  },
  beforeDestroy () {
    clearInterval(this.timer)
    this.timer = null
  }
}
</script>

<style scoped>
</style>

5、运行项目:npm run dev,浏览器打开 http://localhost:8080
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值