echarts在vue的基本使用

目录

 在HTML文件中使用echarts引入

安装ECharts :

正确导入 ECharts:

简单使用echarts

option配置  (图表中加入交互组件)

指示坐标轴当前刻度

图表中加入交互组件--鼠标缩放

整体效果和代码 


 在HTML文件中使用echarts引入
<script src="https://cdn.jsdelivr.net/npm/echarts@5.2.2/dist/echarts.min.js"></script>
安装ECharts

npm 或 yarn 安装了 ECharts。

npm install echarts --save
yarn add echarts
正确导入 ECharts:

在你的 Vue 组件中,确保你已经正确导入了 ECharts。

import * as echarts from 'echarts';
简单使用echarts
<script>  
import * as echarts from 'echarts';  
  
export default {  
  // ...  
  mounted() {  
    this.ChartView();  
  },  
  methods: {  
    ChartView() {  
      const myChart = echarts.init(document.getElementById('main'));  
      // 设置图表的配置项和数据  
      myChart.setOption(option);  
</script>
option配置  (图表中加入交互组件)
指示坐标轴当前刻度

 tooltip: {
          show : true,
          axisPointer: {
          type: 'cross'
        },
        backgroundColor: 'rgba(255, 255, 255, 0.8)',
        position: function (pos, size) {
          var obj = { top: 10 };
          obj[['left', 'right'][+(pos[0] < size.viewSize[0] / 2)]] = 30;
          return obj;
        },
        extraCssText: 'width: 170px'
        },
图表中加入交互组件--鼠标缩放

 

 dataZoom: [  
     {   
    // 这个dataZoom组件,默认控制x轴。
      type: 'slider', // 这个 dataZoom 组件是 slider 型 dataZoom 组件
      start: 10,      // 左边在 10% 的位置。
      end: 60         // 右边在 60% 的位置。
        },
     {   
     // 这个dataZoom组件,也控制x轴。
      type: 'inside', // 这个 dataZoom 组件是 inside 型 dataZoom 组件
      start: 10,      // 左边在 10% 的位置。
      end: 60         // 右边在 60% 的位置。
    },       
    {
      type: 'slider',
      show: true,
      yAxisIndex: [0],
      left: '93%',
      start: 10,
      end: 60
    },
    {
      type: 'inside',
      yAxisIndex: [0],
      start: 29,
      end: 36
    }
  ],
整体效果和代码 

<script>  
import * as echarts from 'echarts';  
  
export default {  
  // ...  
  mounted() {  
    this.ChartView();  
  },  
  methods: {  
    ChartView() {  
      const myChart = echarts.init(document.getElementById('main'));  
      // 设置图表的配置项和数据  
      myChart.setOption(option);  
      option = {
        title: {
          text:'电量显示',
          left:'center',    标题居中
          textStyle: {
            fontSize:20,
            // color:'#5ccaef',
            // textShadowOffsetY: 1
          }
        },
        tooltip: {
          show : true,
          axisPointer: {
          type: 'cross'
        },
        // 
        backgroundColor: 'rgba(255, 255, 255, 0.8)',
        position: function (pos, size) {
          var obj = { top: 10 };
          obj[['left', 'right'][+(pos[0] < size.viewSize[0] / 2)]] = 30;
          return obj;
        },
        extraCssText: 'width: 170px'
        },
    xAxis: {
        data: ['1', '2', '3', '4', '5', '6','7','8','9','10'],
        show:false   // 不显示x轴
    },
    yAxis: {},
    //begin 图表中加入交互组件
    dataZoom: [  
     {   
    // 这个dataZoom组件,默认控制x轴。
      type: 'slider', // 这个 dataZoom 组件是 slider 型 dataZoom 组件
      start: 10,      // 左边在 10% 的位置。
      end: 60         // 右边在 60% 的位置。
        },
     {   
     // 这个dataZoom组件,也控制x轴。
      type: 'inside', // 这个 dataZoom 组件是 inside 型 dataZoom 组件
      start: 10,      // 左边在 10% 的位置。
      end: 60         // 右边在 60% 的位置。
    },       
    {
      type: 'slider',
      show: true,
      yAxisIndex: [0],
      left: '93%',
      start: 10,
      end: 60
    },
    {
      type: 'inside',
      yAxisIndex: [0],
      start: 29,
      end: 36
    }
  ],
   // end
    series: [{
        name: '电量',
        type: 'line',
        smooth:true,
        data: [5, 20, 36, 10, 10, 20,5, 20, 36, 10, 10]
    }]
  }
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值