在Vue中使用ECharts,按时间实现动态折线图,动态柱状图的功能

在Vue中使用ECharts,按时间实现动态折线图,动态柱状图的功能

先用vue创建项目,引入ECharts,并且写一个基础的折线图,直接上代码

<template>
  <div id="main" style="width: 60%;height:500px;" ref="main">
  </div>
</template>

<script>
import * as echarts from "echarts";
export default {
  name: 'HelloWorld',
  mounted() {
    this.test()
  },
  methods: {
    test() {
      // 官方示例 var myChart = echarts.init(document.getElementById('main'));  
      const myChart = echarts.init(this.$refs.main); // 我们可以这样写
      // 
      const time = (function () { // 立即执行函数
        let now = new Date();  // 获得当前的时间
        let res = []; // 存放时间的数组
        let len = 5; // 要存几个时间?
        while (len--) {
          res.unshift(now.toLocaleTimeString().replace(/^\D*/, '')); // 转换时间,大家可以打印出来看一下
          now = new Date(+now - 2000) // 延迟几秒存储一次?
        }
        return res;
      })();
      const dataOne = [11,4,7,8,13]
      const dataTwo = [9,7,7,13,15]
      //配置项,可以去查一下官方文档
      let options = {
        title: {
          text: '动态',
          textStyle: {
            color: 'black'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#283b56'
            }
          }
        },
        legend: {},
        xAxis: {
          type: 'category',
          data: time, // 把时间组成的数组接过来,放在x轴上
          boundaryGap: true
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: dataOne,
            type: 'line',
            name: '测试一',
            markPoint: {
              data: [
                { type: 'max', name: '最大值' },
                { type: 'min', name: '最小值' }
              ]
            },
            markLine: {
              data: [{ type: 'average', name: '平均值' }]
            }
          },
          {
            data: dataTwo,
            name: '测试二',
            type: 'line',
            markPoint: {
              data: [
                { type: 'max', name: '最大值' },
                { type: 'min', name: '最小值' }
              ]
            },
            markLine: {
              data: [{ type: 'average', name: '平均值' }]
            }
          }
        ]
      }
      myChart.setOption(options)
    }
  }
}
</script>

<style scoped lang="scss">
</style>

 现在就可以看到了

 但这只是静态的,现在让它动起来

动起来,就在定时器中不断改变X轴,及测试一,测试二的data即可,上代码 

<template>
  <div id="main" style="width: 100%;height:600px;" ref="main">
  </div>
</template>

<script>
import * as echarts from "echarts";
// import axios from "axios";
export default {
  name: 'homePage',
  mounted() {
    this.test()
  },
  methods: {
    test() {
      // 官方示例 var myChart = echarts.init(document.getElementById('main'));  
      const myChart = echarts.init(this.$refs.main); // 我们可以这样写
      // 
      const time = (function () { // 立即执行函数
        let now = new Date();  // 获得当前的时间
        let res = []; // 存放时间的数组
        let len = 5; // 要存几个时间?
        while (len--) {
          res.unshift(now.toLocaleTimeString().replace(/^\D*/, '')); // 转换时间,大家可以打印出来看一下
          now = new Date(+now - 2000) // 延迟几秒存储一次?
        }
        return res;
      })();
      const dataOne = (function () { // 5个随机数,大家可随意定义
        let res = [];
        let len = 5;
        while (len--) {
          res.push(Math.round(Math.random() * 1000));
        }
        return res;
      })();
      const dataTwo = (function () { // 5个随机数
        let res = [];
        let len = 5;
        while (len--) {
          res.push(Math.round(Math.random() * 1000));
        }
        return res;
      })();
      //配置项,可以去查一下官方文档
      let options = {
        title: {
          text: '动态',
          textStyle: {
            color: 'black'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#283b56'
            }
          }
        },
        legend: {},
        xAxis: {
          type: 'category',
          data: time, // 把时间组成的数组接过来,放在x轴上
          boundaryGap: true
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: dataOne,
            type: 'line',
            name: '测试一',
            markPoint: {
              data: [
                { type: 'max', name: '最大值' },
                { type: 'min', name: '最小值' }
              ]
            },
            markLine: {
              data: [{ type: 'average', name: '平均值' }]
            }
          },
          {
            data: dataTwo,
            name: '测试二',
            type: 'line',
            markPoint: {
              data: [
                { type: 'max', name: '最大值' },
                { type: 'min', name: '最小值' }
              ]
            },
            markLine: {
              data: [{ type: 'average', name: '平均值' }]
            }
          }
        ]
      }
      setInterval(function () {
        let nowTime = new Date().toLocaleTimeString().replace(/^\D*/, '');
        time.shift()
        time.push(nowTime)
        dataOne.shift()
        dataOne.push(Math.round(Math.random() * 1000))
        dataTwo.shift()
        dataTwo.push(Math.round(Math.random() * 1000))
        console.log(dataOne)
        //很多朋友可能要接后端接口,把数据替换下来既可以了
        // axios.get('你的url').then(res => {
        //   console.log(res)
        // })
        myChart.setOption({
          xAxis: [
            {
              data: time
            }
          ],
          series: [
            {
              data: dataOne
            },
            {
              data: dataTwo
            }
          ]
        })
      }, 2000)
      myChart.setOption(options)
    }
  }
}
</script>

<style scoped lang="scss">
</style>

柱状图只需把  series中的line改为bar即可,但是可以看一下官方文档,会发现toolbox,

具体可直接搜索查看,在我们的options中加上

let options = {
        title: {
          text: '动态',
          textStyle: {
            color: 'black'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#283b56'
            }
          }
        },
        legend: {},
        //----------------------------我在这!!!
        toolbox: {
          show: true,
          feature: {
            dataView: { readOnly: false },
            magicType: { type: ['bar', 'line','stack'] },
            restore: {},
            saveAsImage: {}
          }
        }, 
        // --------------------------------
        xAxis: {
          type: 'category',
          data: time, // 把时间组成的数组接过来,放在x轴上
          boundaryGap: true
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: dataOne,
            type: 'line',
            name: '测试一',
            markPoint: {
              data: [
                { type: 'max', name: '最大值' },
                { type: 'min', name: '最小值' }
              ]
            },
            markLine: {
              data: [{ type: 'average', name: '平均值' }]
            }
          },
          {
            data: dataTwo,
            name: '测试二',
            type: 'line',
            markPoint: {
              data: [
                { type: 'max', name: '最大值' },
                { type: 'min', name: '最小值' }
              ]
            },
            markLine: {
              data: [{ type: 'average', name: '平均值' }]
            }
          }
        ]
      }

 大家自己点点看吧!

  • 10
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个使用echartsvue2实现柱状图折线图结合使用的示例代码,你可以根据自己的需求进行修改和调整: ```html <template> <div class="chart-container"> <div ref="chart" class="chart"></div> </div> </template> <script> import echarts from 'echarts' export default { data() { return { chartData: { xAxisData: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], seriesData1: [320, 332, 301, 334, 390, 330, 320, 330, 350, 380, 360, 390], seriesData2: [220, 182, 191, 234, 290, 330, 310, 320, 330, 300, 280, 260] } } }, mounted() { this.initChart() }, methods: { initChart() { const chart = echarts.init(this.$refs.chart) chart.setOption({ tooltip: { trigger: 'axis' }, legend: { data: ['Series1', 'Series2'] }, xAxis: { type: 'category', data: this.chartData.xAxisData }, yAxis: [{ type: 'value', name: 'Series1', position: 'left', axisLabel: { formatter: '{value} units' } }, { type: 'value', name: 'Series2', position: 'right', axisLabel: { formatter: '{value} units' } }], series: [{ name: 'Series1', type: 'bar', data: this.chartData.seriesData1 }, { name: 'Series2', type: 'line', yAxisIndex: 1, data: this.chartData.seriesData2 }] }) } } } </script> <style> .chart-container { width: 100%; height: 400px; display: flex; justify-content: center; align-items: center; } .chart { width: 80%; height: 80%; } </style> ``` 在这个示例代码,我们首先引入了echarts依赖,然后在data定义了需要展示的图表数据。在mounted方法,我们调用了initChart方法,创建了一个echarts实例并初始化了图表。 在initChart方法,我们使用echarts的API来配置了图表的tooltip、legend、xAxis、yAxis和series等属性,最终使用setOption方法将图表配置应用到了echarts实例。 在样式方面,我们在容器元素和图表元素上设置了一些基本的样式。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值