echats在vue2.0的使用

3 篇文章 0 订阅

安装依赖

npm install echarts -S

引入echats包

const echarts = require(‘echarts’)
在这里插入图片描述

初始化图表

饼图

	/*
	element:节点id
	title:表单姓名
	titleArr:x轴刻度
	dataArr:表格数据
	*/
    // 初始化饼图
    initChartPie (element, title, titleArr, dataArr, type) {
      // 基于准备好的dom,初始化echarts实例
      const myChart = echarts.init(element)
      // 绘制图表
      myChart.setOption({
        title: {
          show: true, // 显示策略,默认值true,可选为:true(显示) | false(隐藏)
          text: title, // 主标题文本,'\n'指定换行
          x: 'center',
          y: '10'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{b}: {c} ({d}%)'
        },
        legend: [{
          orient: 'vertical',
          // orient: 'horizontal',
          top: '150',
          left: '-10',
          data: titleArr.slice(0, 6),
          textStyle: {
            color: '#fff'
          }
          // data: ['炸药库南门', '钻场2', '钻场3', '禁区']
        }, {
          orient: 'vertical',
          // orient: 'horizontal',
          top: '150',
          right: '0',
          data: titleArr.slice(6),
          textStyle: {
            color: '#fff'
          }
          // data: ['炸药库南门', '钻场2', '钻场3', '禁区']
        }],

        label: {
          normal: {
            formatter: '{b}'
          },
          // formatter: '{b}({d}%)'
        },
        // 设置饼状图每个颜色块的颜色
        color: ['#e14745', '#d6ac46', '#00a0de', '#b5b7fa', '#0c2fe5', '#d32ae7', '#f06eff', '#9c60ff', '#4868ef', '#04a7c4', '#49bf8e', '#a912e9', '#4045fa'],
        series: [
          {
            name: this.chartTitle,
            type: 'pie',
            radius: ['50%', '70%'],
            center: ['50%', '50%'],
            label: {
              normal: {
                position: 'inner',
                show: false
              }
            },
            data: dataArr,
            // data: [
            //     {value: 1, name: '炸药库南门'},
            //     {value: 2, name: '钻场2'},
            //     {value: 4, name: '钻场3'},
            //     {value: 5, name: '禁区'}
            // ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })
    },

使用

在发起请求后,在发起画图请求。

 this.initChartBar(document.getElementById('chart-statistical'), '', dataArrType, numberArrType)

在这里插入图片描述
折线图:
在这里插入图片描述

    initChartLine (element, title, titleArr, numberArr) {
      // 基于准备好的dom,初始化echarts实例
      const myChart = echarts.init(element)
      // 绘制图表
      myChart.setOption({
        tooltip: {
          trigger: 'axis',
          formatter: '{b}: {c}告警',
          axisPointer: {            // 坐标轴指示器,坐标轴触发有效
            type: 'line'        // 默认为直线,可选为:'line' | 'shadow'
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          axisTick: {
            alignWithLabel: true
          },
          axisLine: {
            lineStyle: {
              color: '#7ecdf4',
              width: 2, // 这里是为了突出显示加上的
            }
          },
          axisLabel: {
            color: '#fff'
          },
          data: titleArr
          // data: ['WR', 'CL', 'D', 'Burr', 'Dep', 'Def', 'O', 'ML', 'L', 'Dp']
        },
        yAxis: {
          minInterval: 1,
          type: 'value',
          axisTick: {
            alignWithLabel: true
          },
          axisLine: {
            lineStyle: {
              color: '#7ecdf4',
              width: 2, // 这里是为了突出显示加上的
            }
          },
          axisLabel: {
            color: '#fff'
          }
        },
        series: [{
          name: this.chartTitle,
          data: numberArr,
          // data: [1, 2, 15, 3, 10, 6, 12, 6, 9, 10],
          type: 'line',
          // symbol: 'emptyCircle', //折线点设置为实心点
          symbolSize: 8, // 折线点的大小
          smooth: true,
          itemStyle: {
            normal: {
              color: '#e658ea', // 折线点的颜色
              lineStyle: {
                color: '#e658ea' // 折线的颜色
              }
            }
          },
          // areaStyle: {
          //   normal: {
          //     color: 'rgba(14,156,255,0.2)' //改变区域颜色
          //   }
          // }
        }]
      })

    },

// 初始化柱状图

    // 初始化柱状图
    initChartBar (element, title, titleArr, numberArr) {
      // 基于准备好的dom,初始化echarts实例
      const myChart = echarts.init(element)
      // 绘制图表
      myChart.setOption({
        title: {
          show: false, // 显示策略,默认值true,可选为:true(显示) | false(隐藏)
          text: 'title', // 主标题文本,'\n'指定换行
          x: 'center',
          y: '10'
        },
        color: new echarts.graphic.LinearGradient(
          0, 0, 0, 1,
          [
            { offset: 0, color: '#18cef8' },
            { offset: 1, color: '#384fb2' }
          ]
        ),
        // color: ['#63B1E9'],
        tooltip: {
          trigger: 'axis',
          formatter: '{b}: {c}次告警',
          axisPointer: {            // 坐标轴指示器,坐标轴触发有效
            type: 'shadow'        // 默认为直线,可选为:'line' | 'shadow'
          }
        },
        grid: {
          top: '20px',
          left: '20px',
          right: '15px',
          bottom: '20px'
        },

        xAxis: [
          {
            type: 'category',
            // data: titleArr,
            data: ['一级告警', '二级告警', '三级告警', '一级告警', '二级告警', '三级告警'],
            axisTick: {
              alignWithLabel: true
            },
            axisLine: {
              lineStyle: {
                color: '#7ecdf4',
                width: 2, // 这里是为了突出显示加上的
              }
            },
            axisLabel: {
              color: '#fff',
              interval: 0
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            splitLine: { // 刻度线虚线
              show: true,
              lineStyle: {
                color: '#4B75DE',
                type: 'solid'
              }
            },
            axisLine: {
              lineStyle: {
                color: '#7ecdf4',
                width: 2
                // width:8,//这里是为了突出显示加上的
              }
            },
            axisLabel: {
              color: '#fff',
            },
          },
        ],
        series: [
          {
            name: this.chartTitle,
            type: 'bar',
            barWidth: '30',
            // data: numberArr,
            data: [1, 2, 15, 1, 2, 15, 1, 2, 15],
            itemStyle: {
              normal: {
                // barBorderRadius:[15, 15, 0, 0],
                label: {
                  show: true, // 开启显示
                  position: 'top', // 在上方显示
                  textStyle: { // 数值样式
                    color: '#fff'
                  }
                }
              }
            }
          }
        ]
      })
    },

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值