vue中echarts的用法及与elementui-select的协同绑定操作
作者:我是主角我不能死 发布时间:2024-05-10 14:20:13
标签:vue,echarts,elementui-select,绑定
1.vue中echarts的使用
引入echarts后
let myChart = echarts.init(document.getElementById('dsm'));//dsm为绑定的dom结构
var option = {
//backgroundColor:"#111c4e",
tooltip: {
trigger: 'axis'
},
legend: { //图表上方的图例显隐
data:['光合有效辐射'],
textStyle: {
color: '#fff'
}
},
color:['#E4FD0A'],
grid: { //图表里上下左右的空间 间隙
left: '3%',
right: '8%',
bottom: '3%',
containLabel: true
},
xAxis: { //x轴属性
type: 'category',
name: '日期/时间',
// boundaryGap: false,
data: this.xZhou,
axisLine:{
lineStyle:{color:'#fff'} // x轴坐标轴颜色
},
axisLabel: {
show: true,
color: '#fff',
fontSize:12,
// rotate: 30
}
},
yAxis: { //y轴属性
type: 'value',
name: '光合有效辐射',
axisLine:{
lineStyle:{color:'#fff'} // x轴坐标轴颜色
},
axisLabel: {
show: true,
color: '#fff',
fontSize:12,
// rotate: 30
}
},
series: [ //为鼠标在图表中划过时显示的数据
{
name:'光合有效辐射',
type:'line',
stack: '总量',
data:this.yZhou,
lineStyle:{
normal:{
color: '#E4FD0A'
}
}
}
]
};
myChart.setOption(option)
window.addEventListener("resize", function () { //设置图表因窗口大小的变化进行变化
myChart.resize();
});
上述图表的显示效果为:
2.echarts与elementui-select的协同绑定
实现依据elementui-select的变化而变化图表。
<template>
<div class="content">
<div class="contentDetail" v-show="isXM">
<div class="close" @click="close"></div>
<div class="chartContent">
<el-select
v-model="defaultyAxis" //利用v-model对默认数据defaultyAxis进行改变,实际绑定的数据是yAxisOption
placeholder="请选择"
class="chartSelect"
popper-class="trsfChartSelect-popper"
@change="renderChart()"
>
<el-option v-for="item in yAxisOption" :key="item" :label="item" :value="item"></el-option>
</el-select>
<div id="zsfChart"></div>
</div>
</div>
</div>
</template>
<script>
import { zsfEntity} from '@/api/sfcl.js'
export default {
name: 'Home',
data() {
return {
isXM: true,
yAxisOption: ['a', 'b'],
defaultyAxis: '',
dataOgj: {},
}
},
mounted() {
this.$comjs.addSimplePoint([100.62713539843939, 38.620863795306164]) //cesium挂载图标
this.getChartDataAndRender()
},
methods: {
close() {
this.isXM = false
this.$store.commit('getComponent1', '')
},
getChartDataAndRender(){ //axios获取异步数据
var _this = this
zsfEntity().then(res => {
if(res.obj.length == 0){
return
}
let keyArr = Object.keys(res.obj[0])
for (let item of keyArr) {
_this.dataOgj[item] = []
}
for (let item of res.obj) {
for (let item1 of keyArr) {
_this.dataOgj[item1].push(item[item1])
}
}
_this.yAxisOption = keyArr.slice(1)//y轴实际数据 四项
_this.defaultyAxis = _this.yAxisOption[0] //获取y轴默认数据
_this.renderChart()
})
},
//渲染图表
renderChart() {
let myChart = echarts.init(document.getElementById('zsfChart'))
let option = {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
label: {
backgroundColor: '#6a7985'
}
}
},
legend: {
data:[this.defaultyAxis],
textStyle: {
color: '#fff'
}
},
grid: {
right: '5%',
left: '5%'
},
color: ['#E4FD0A'],
xAxis: {
name: "观测时间",
type: 'category',
boundaryGap: false,
data: this.dataOgj.observeTime,
axisLabel: {
color: '#ffffff'
// fontSize: 10,
// rotate:30
},
axisLine: {
lineStyle: {
color: '#ffffff',
type: 'dashed'
}
}
},
yAxis: {
name: this.defaultyAxis,//挂载默认数据
type: 'value',
axisLabel: {
color: '#ffffff',
fontSize: 10
// rotate:30
},
axisLine: {
lineStyle: {
color: '#ffffff',
type: 'dashed'
}
}
},
series: [
{
data: this.dataOgj[this.defaultyAxis],
type: 'line',
name: this.defaultyAxis
}
]
}
myChart.setOption(option)
window.addEventListener('resize', function() {
myChart.resize()
})
}
},
destroyed(){
this.$comjs.removeSimplePoint()
}
}
</script>
<style lang="stylus">
.trsfChartSelect-popper
background: rgba(1,64,64,1)
.el-select-dropdown__item.hover, .el-select-dropdown__item:hover
background: rgba(0,0,0,0.5)
.el-select-dropdown__item
color: #fff
</style>
<style lang="stylus" scoped>
@import '../../assets/styles/varibles.styl'
.content
position: absolute
right: vw(10)
top:vh(71)
z-index: 100
color: #fff
background: $bgColor
.contentDetail
width:vw(1500)
height:vh(348)
position: fixed
right: 70px
bottom: 27px
margin:auto
z-index: 100
color: #fff
background: $bgColor
border: 1px solid rgba(3,240,240,1)
.close
position:absolute
right:vw(15)
top:vh(10)
cursor: pointer
background-image:url("/images/lanhu/close.png")
width: 20px;
height: 20px;
z-index: 1
.baseInfo
height: 75px;
padding-top: 30px;
.baseInfo-item
width: 33%;
display: inline-block;
text-align: left;
margin-bottom: 20px;
padding-left: 80px;
.baseInfo-item-icon
vertical-align: middle
margin-right: 14px
.baseInfo-item-text
vertical-align: middle
.separator
height: 1px
background: #03F0F0
.chartContent
height: 100%
.chartSelect
position:absolute
right: 63px
margin-top: 20px
width: 150px
z-index: 1
/deep/ .el-input__inner
height: 28px;
line-height: 28px;
background:rgba(1,64,64,1);
border-radius:2px;
border:1px solid rgba(0,252,252,1);
color: #fff
/deep/ .el-input__icon
line-height: 28px;
#zsfChart
height: 100%
width:100%
</style>
效果实现
补充知识:vue项目在同一页面中引入多个echarts图表 ,并实现封装,自适应和动态数据改变
vue-Echarts
公司最近做项目需要用到图表,以前是使用echarts,现在也是用这个,没什么好纠结的! 但是最近发现以前每次做图表之类的都没有封装,每次做图表都要从新去配置之类的,写了好多重复代码,感觉很累啊,所以自己把图表封装成子组件使用,代码工作量减轻了很多,而且子组件使用了数据进行监听和图表自适应屏幕大小,这样以后会方便很多了!
当然公司的项目肯定不能发出来了,我会做个简单的demo出来
先截个图吧!
其实我也未作什么太复杂的工作,以前工作中,页面中要2个图表,我在methods:{}中写两个方法配置之类的,类似这样子:
来源:https://blog.csdn.net/weixin_43163704/article/details/105140532


猜你喜欢
- 作者:catmelo 本文版权归作者所有链接:https://www.cnblogs.com/catmelo/p/4162101.html本
- 分割成一个包含两个元素列表的列对于一个已知分隔符的简单分割(例如,用破折号分割或用空格分割).str.split() 方法就足够了 。 它在
- 在python代码编写过程中,养成注释的习惯非常有用,可以让自己或别人后续在阅读代码时,轻松理解代码的含义。如果只是简单的单行注释,可直接用
- 备注: 关于label和tag,在中文中都翻译成标签,而下文中出现的标签,都是对label的翻译,比如”用户名”+输入框, 这里的”用户名”
- 1.python版本与matlab版本的对应关系在MAC中安装了Anaconda3,其中自带的python版本为3.8,通过python版本
- 效果图from wxpyimport *import requestsfrom datetimeimport datetimeimport
- 本文实例讲述了JS基于开关思想实现的数组去重功能。分享给大家供大家参考,具体如下:场景: 比如给你一个数组var Arr = [ 25, 7
- 利用ACCESS可以用查询创建视图这一功能来进行查询分析。选择查询->在设计视图中创建查询,然后关闭弹出的表选择对话框,在下面的窗口上
- Insus.NET解决这个问题,只有创建另外一个表,将存储用户决定要跟踪的表,以及这个表中需要跟踪的字段。 还要创建另外一个表[Audit]
- 不同的色彩空间中对图片的色彩体现有很大不同#色彩空间的相互转换:最常见的是HSV与RGB,YUV与RGB的相互转换#常见色彩空间有:#RGB
- 工具选择 一。sqlyog 这个工具还是很强大的,但是面对5个G的数据库来说,也只能罢工了,简单说,本机导入可以,从本机导入服务器不行。 二
- 本文实例为大家分享了JSP学生信息管理系统源码,供大家参考,具体内容如下新建学生信息数据库1.添加记录模块<%@ page conte
- 关于模型保存的一点心得saver = tf.train.Saver(max_to_keep=3)在定义 saver 的时候一般会定义最多保存
- Qt Designer用于像VC++的MFC一样拖放、设计控件PyUIC用于将Qt Designer生成的.ui文件转换成.py文件Qt D
- 在使用Python的过程中难免会遇到不同的项目使用不通同的Python环境,这就引出Python环境的切换问题这篇文章以3.11.0与3.1
- 在之前的博客 人脸识别经典算法一:特征脸方法(Eigenface)里面介绍了特征脸方法的原理,但是并没有对它用到的理论基础PCA做介绍,现在
- 前言最近在做一个人脸识别的项目,需要用数据库保存学生信息与前段交互。MySQL的优点1、mysql性能卓越,服务稳定,很少出现异常宕机。2、
- 学在前面从本篇博客起,我们将实际完成几个小案例,第一个就是银行卡号识别,预计本案例将写 5 篇左右的博客才可以完成,一起加油吧。本文的目标是
- 终于把promise, async, await的区别和联系弄清楚了,看下面代码写法1,2是promise的写法写法6是async和awai
- 目录前言:一、query和params(1)query方式传参和接收参数(2)params方式传参和接收参数二、从后台渲染列表(4)根据id