vue使用echarts实现水平柱形图实例


Posted in Javascript onSeptember 09, 2020

文件结构:

vue使用echarts实现水平柱形图实例

testData.js文件

const dtuEdition = {
 name: '有方有线',
 number: 60,
 proportion: 40,
 edition: {
 '有方有线V1.0.0': 20,
 '有方有线V1.2.0': 15,
 '有方有线V2.0.1': 10,
 '有方有线V3.0.0': 8,
 '有方有线V3.2.0': 5,
 '有方有线V3.4.0': 4,
 '有方有线V4.0.0': 3,
 '有方有线V4.0.2': 2,
 '有方有线V4.0.3': 1
 }
} 
export default {
 namespaced: true, // 用于在全局引用此文件里的方法时标识这一个的文件名
 dtuEdition
}

dtuDistributionCurve.js文件

// DTU连接率bar图的option
let barOption = {
 grid: {
 // width: '85%', // 设置gird宽度
 left: 40, // gird距离容器左边距
 right: 65,
 top: 20,
 bottom: 0,
 containLabel: true
 },
 xAxis: {
 show : false, // 不显示横轴
 type: 'value',
 max: 1000, // 横轴最大值
 },
 yAxis: {
 type: 'category',
 data: [],
 axisLine: {
  show: false
 },
 axisTick: {
  show: false
 },
 splitLine: {
  show: false
 }
 },
 series: [{
 type: 'bar',
 stack: 'chart',
 z: 3,
 itemStyle: {
  normal: {
  color: '#a7c7e9'
  }
 },
 data: []
 }, {
 type: 'bar',
 stack: 'chart',
 silent: true,
 label: {
  normal: {
  formatter: (params) => {
   // console.log(params)
   return barOption.xAxis.max-params.value
  },
  color: '#666666',
  position: 'right',
  distance: 10,
  show: true
  }
 },
 itemStyle: {
  normal: {
  color: '#f3f3f6'
  }
 },
 barWidth : 10,//柱图宽度
 data: []
 }]
}
 
// 设置y轴标签
export function setYAxisData(edition) {
 let data = []
 for (let key in edition) {
 data.push(key)
 }
 barOption.yAxis.data = data.reverse()
 console.log(barOption.yAxis.data)
}
 
// 设置x轴最大值
export function setXAxisMax(number) {
 barOption.xAxis.max = number
}
 
// 设置series的data数据
export function setSeriesData(edition, number) {
 let data0 = []
 let data1 = []
 for(let key in edition) {
 data0.push(edition[key])
 data1.push(number - edition[key])
 }
 barOption.series[0].data = data0.reverse()
 barOption.series[1].data = data1.reverse()
}
 
export default {
 barOption,
 setYAxisData,
 setXAxisMax,
 setSeriesData
}

vue文件

<template>
 <div ref="dtuEdition" class="project-survey-dtu-edition"></div>
</template>
 
<script>
 import testData from '../constvalue/testData'
 import dtuDistributionOption from '../curveoption/dtuDistributionCurve'
 export default {
  name: 'ProjectSurvey',
  data() {
   return {
 dtuEditionChart: null
 }
  },
 
  methods: {
 // 点击DTU模块数量分布展示图的扇区item
 distributionChartClick(param) {
 console.log(param)
 let dtuEdition = testData.dtuEdition
 this.dtuName = dtuEdition.name
 this.dtuNumber = dtuEdition.number
 this.dtuProportion = dtuEdition.proportion + '%'
 dtuDistributionOption.setYAxisData(dtuEdition.edition)
 dtuDistributionOption.setXAxisMax(dtuEdition.number)
 dtuDistributionOption.setSeriesData(dtuEdition.edition, dtuEdition.number)
 this.dtuEditionChart.setOption(dtuDistributionOption.barOption)
 this.dtuEditionChart.resize()
 },
 // 点击tab的某页
 tabClick(tab, event) {
 console.log(this.activeName)
 if(this.activeName === 'first') { // 从后端获取连接率统计数据
 
 } else { // 从后端获取模块数量分布展示数据
  let distributionInfo = testData.dtuDistribution.distributionInfo
  this.deadline = testData.dtuDistribution.deadline
  dtuDistributionOption.setSectorValue(distributionInfo)
    dtuDistributionOption.setSectorName(testData.dtuDistribution.allDistribution)
  this.distributionChart.setOption(dtuDistributionOption.pieOption)
  this.distributionChart.resize()
  this.distributionChart.on('click', this.distributionChartClick)
 }
 }
 },
 mounted() {
 this.dtuEditionChart = this.$echarts.init(this.$refs.dtuEdition)
 this.distributionChart = this.$echarts.init(this.$refs.dtuDistribution)
 let maxV = this.getMaxV()
 let minV = this.getMinV()
 for(let item of this.connectionInfo) {
 this.charts[item.dtuName] = this.$echarts.init(document.getElementById(item.dtuName))
 let normalizationRatio = this.normalization(item.connectionRatio, maxV, minV)
 dtuConnectionOption.setSectorColor(normalizationRatio)
 dtuConnectionOption.setTitleText(item.dtuName)
 dtuConnectionOption.setSectorValue(item.connectionRatio)
 dtuConnectionOption.setSectorName(item.connectionRatio)
 // console.log(dtuConnectionOption.option)
 this.charts[item.dtuName].setOption(dtuConnectionOption.option)
 this.charts[item.dtuName].resize()
 }
 window.onresize = () => {
 this.distributionChart.resize()
 this.dtuEditionChart.resize()
  }
 },
 updated() {
 this.distributionChart.resize()
 for(let item of this.connectionInfo) {
 this.charts[item.dtuName].resize()
 }
 } 
 
 }
</script>
 
<style>
 .project-survey-dtu-edition {
 height: 580px;
 }
</style>

图表

vue使用echarts实现水平柱形图实例

补充知识:vue+echart实现 X轴 双柱状图 渐变色

一: 安装

1. 首先需要安装echarts依赖包

npm install echarts -S

2. 或者使用国内的淘宝镜像:

npm install -g cnpm --registry=https://registry.npm.taobao.org

二: 创建图表

全局引入

main.js

>```javascript
// 引入echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts

Hello.vue

<div id="myChart" :style="{width: '300px', height: '300px'}"></div>

export default {
 data(){
  return {}
 }, 
  mounted(){
  this.myChart() //函数调用
  },
  methods:{
  	myChart(){
	  let myChart= this.$echarts.init(document.getElementById('myChart'));
	  // var colors = ['rgba(15,115,255,0.6)', 'rgba(15,235,255,0.6)'];
	  var data1 = [350, 250, 170, 360, 240];
	  var data2 = [187, 146, 129, 174,245];
	  var xData = ['3.12','3.13','3.14','3.15','3.16']
	  rightBtns.setOption({
   // backgroundColor:'#fff',
   tooltip: {
    trigger: "axis",
    // formatter: '{b}<br/>{a1}-违规率:{c1}<br/>{a0}-违规率:{c0}',
    axisPointer: {
     type: "shadow",
     textStyle: {
     color: "#fff"
     }
    },
   },
   grid: {
    top: '8%',
    right: '8%',
    bottom: '60%'
   },
   legend: {
    data: ['省内', '省外'],
    align: 'left',
    left: '30%',
    top: '4%',
    textStyle:{
     color:'#fff'
    }
   },
   calculable: true,
   xAxis: [{
    type: "category",
    data: xData,
    axisLine: {
    lineStyle: {
     color: 'rgba(255,255,255,0.1)'
    },
    },
    axisLabel: {
    show: true,
    textStyle: {
     color: '#fff'
    }
    },
   }],
   yAxis: {
    type: 'value',
    // name:'单位:(人次 )',
    min: 0,
    max: 500,
    interval: 100,
    axisLine: {
    lineStyle: {
     color: 'rgba(255,255,255,0.1)'
    }
    },
    splitLine: {
    lineStyle: {
     type: 'dashed',
    },
    show:false
    },
    axisLabel: {
    show: true,
    textStyle: {
     color: '#fff'
    }
    },
   },
   series: [{
    name: '省内',
    type: 'bar',
    // color: colors[0],
    data: data1,
    itemStyle:{
     normal: {
     //每个柱子的颜色即为colorList数组里的每一项,如果柱子数目多于colorList的长度,则柱子颜色循环使用该数组
     //此处的箭头函数是为了不改变this的指向
     color: (params) => {
      var index = params.dataIndex;
      var colorList = [
      // 渐变颜色的色值和透明度
      //双柱状图渐变的 第一个柱子的渐变色['rgba(15,235,255,0)','rgba(15,235,255,0)','rgba(15,235,255,0)','rgba(15,235,255,0)','rgba(15,235,255,0)'],
      ['rgba(15,235,255,0.6)','rgba(15,235,255,0.6)','rgba(15,235,255,0.6)','rgba(15,235,255,0.6)','rgba(15,235,255,0.6)'] 
      
      ];
      if(params.dataIndex >= colorList.length){
      index=params.dataIndex-colorList.length;
      }
      //方法一:
      //不使用箭头函数的写法改变渐变色
      // return {
      // colorStops: [{
      //  offset: 0, //颜色开始的位置
      //  color: colorList[0][index] // 0% 处的颜色
      // },{
      //  offset: 0.6, //颜色结束的位置
      //  color: colorList[1][index] // 100% 处的颜色
      // }]
      // }
      //方法二:使用箭头函数的写法 改变双柱状图的渐变颜色
      return new this.$echarts.graphic.LinearGradient(0,0,0,1,[
      {offset: 0.2, color: colorList[1][index]},
      {offset: 1, color: colorList[0][index]}
      ])
     }
     }
    }
   },
   {
    name: '省外',
    type: 'bar',
    // color: colors[1],
    data: data2,
    itemStyle:{
     normal: {
     //每个柱子的颜色即为colorList数组里的每一项,如果柱子数目多于colorList的长度,则柱子颜色循环使用该数组
     color: (params) => {
      var index = params.dataIndex;
      var colorList = [
      // 渐变颜色的色值和透明度
      //双柱状图渐变的 渐变第二个柱子的渐变色['rgba(15,115,255,0)','rgba(15,115,255,0)','rgba(15,115,255,0)','rgba(15,115,255,0)','rgba(15,115,255,0)'],
      ['rgba(15,115,255,0.6)','rgba(15,115,255,0.6)','rgba(15,115,255,0.6)','rgba(15,115,255,0.6)','rgba(15,115,255,0.6)'] 
      ];
      //方法一:
      //不使用箭头函数的写法改变渐变色
      // return {
      // colorStops: [{
      //  offset: 0,
      //  color: colorList[0][index] // 0% 处的颜色
      // },{
      //  offset:0.6,
      //  color: colorList[1][index] // 100% 处的颜色
      // }]
      // }
      //方法二:使用箭头函数的写法 改变双柱状图的渐变颜色
      return new this.$echarts.graphic.LinearGradient(0,0,0,1,[
      {offset: 0.2, color: colorList[1][index]},
      {offset: 1, color: colorList[0][index]}
      ])
     }
     }
    }
   }]
   })
  }
  }
	}

最终结果

vue使用echarts实现水平柱形图实例

以上这篇vue使用echarts实现水平柱形图实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
取得一定长度的内容,处理中文
Dec 20 Javascript
jquery 屏蔽一个区域内的所有元素,禁止输入
Oct 22 Javascript
juqery 学习之四 筛选查找
Nov 30 Javascript
js购物车实现思路及代码(个人感觉不错)
Dec 23 Javascript
js正则表达式验证邮件地址
Nov 12 Javascript
BootStrap表单验证实例代码
Jan 13 Javascript
js实现前面自动补全位数的方法
Oct 10 Javascript
JS通过识别id、value值对checkbox设置选中状态
Feb 19 Javascript
JavaScript闭包原理与用法学习笔记
May 29 Javascript
JavaScript检测是否开启了控制台(F12调试工具)
Oct 02 Javascript
vue 解决mintui弹窗弹起来,底部页面滚动bug问题
Nov 12 Javascript
微信小程序onShareTimeline()实现分享朋友圈
Jan 07 Javascript
在vue中实现清除echarts上次保留的数据(亲测有效)
Sep 09 #Javascript
vue 项目引入echarts 添加点击事件操作
Sep 09 #Javascript
Vue this.$router.push(参数)实现页面跳转操作
Sep 09 #Javascript
Vue页面跳转传递参数及接收方式
Sep 09 #Javascript
微信小程序实现身份证取景框拍摄
Sep 09 #Javascript
vue实现点击按钮“查看详情”弹窗展示详情列表操作
Sep 09 #Javascript
微信小程序实现点击生成随机验证码
Sep 09 #Javascript
You might like
PHP文件上传实例详解!!!
2007/01/02 PHP
PHP中改变图片的尺寸大小的代码
2011/07/17 PHP
探讨:parse url解析URL,返回其组成部分
2013/06/14 PHP
PHP快速按行读取CSV大文件的封装类分享(也适用于其它超大文本文件)
2014/04/10 PHP
PHP遍历目录并返回统计目录大小
2014/06/09 PHP
php建立Ftp连接的方法
2015/03/07 PHP
PHP调用存储过程返回值不一致问题的解决方法分析
2016/04/26 PHP
PHP实现适用于自定义的验证码类
2016/06/15 PHP
PHP在弹框中获取foreach中遍历的id值并传递给地址栏
2017/06/13 PHP
Yii2压缩PHP中模板代码的输出问题
2018/08/28 PHP
Laravel框架控制器,视图及模型操作图文详解
2019/12/04 PHP
js中同步与异步处理的方法和区别总结
2013/12/25 Javascript
js中自定义方法实现停留几秒sleep
2014/07/11 Javascript
javascript实现网页背景烟花效果的方法
2015/08/06 Javascript
js实现图片360度旋转
2017/01/22 Javascript
JavaScript中常见内置函数用法示例
2018/05/14 Javascript
了解重排与重绘
2019/05/29 Javascript
python中的lambda表达式用法详解
2016/06/22 Python
浅谈python字典多键值及重复键值的使用
2016/11/04 Python
对python 自定义协议的方法详解
2019/02/13 Python
详解pandas库pd.read_excel操作读取excel文件参数整理与实例
2019/02/17 Python
python使用Word2Vec进行情感分析解析
2020/07/31 Python
python生成word合同的实例方法
2021/01/12 Python
纯CSS实现右侧底部悬浮效果(悬浮QQ、微信、微博、邮箱等联系方式)
2015/04/24 HTML / CSS
HTML5 实现一个访问本地文件的实例
2012/12/13 HTML / CSS
世界上最全面的草药补充剂和顶级品牌维生素网站:HerbsPro
2019/01/20 全球购物
党的群众路线教育实践活动学习心得体会
2014/03/03 职场文书
地理科学专业自荐信
2014/09/01 职场文书
2014年仓库工作总结
2014/11/20 职场文书
男方婚礼答谢词
2015/01/20 职场文书
2015高三毕业寄语赠言
2015/02/27 职场文书
行政申诉状范文
2015/05/20 职场文书
2019数学教师下学期工作总结
2019/06/27 职场文书
《家世》读后感:看家训的力量
2019/12/30 职场文书
Python中X[:,0]和X[:,1]的用法
2021/05/10 Python
PHP遍历数组的6种方式总结
2021/11/17 PHP