滚动条的滑块置于右侧
dataZoom-slider. startValue和endValue取x轴数据的最右侧
思路:
1.获得x轴数据的长度,我这里是直接传进来的xData。
2.因为startValue和endValue取得是数据的下标,所以endValue需要长度减一,startValue同理。显示的长度为endValue减去startValue的值。
var startValue1 = xData.length -7
var endtValue1 = xData.length -1
3.将变量startValue1 、endtValue1 赋值给dataZoom下的startValue、endValue就可以了
startValue: startValue1,
endValue: endtValue1,
注意: 要在使用刚指定的配置项和数据 显示图表 myChart.setOption(option); 之前。
总之注意代码执行的顺序
举例一、
//折线图1
drawChart1(xData, yData){
var chartDom = document.getElementById('myEcharts1');
var myChart = echarts.init(chartDom);
var startValue1 = xData.length -7
var endtValue1 = xData.length -1
// 基于准备好的dom,初始化echarts实例
// let myChart = this.echarts.init(document.getElementById("myEcharts1"));
// 指定图表的配置项和数据
var option = {
title: {
left:'3%',
top: '5%',
textStyle:{
fontSize:14,
// color:'rgba(122,185,255,0.9)',118FFE
color: 'rgba(122,185,255,0.5)'
},
text:'单位:方',
x: 'left'
},
xAxis: {
type: 'category',
data: xData,
// data: ['2021-99-99','2021-99-99','2021-99-99','2021-99-99','2021-99-99','2021-99-99','2021-99-99','2021-99-99','2021-99-99','2021-99-99','2021-99-99','2021-99-99','2021-99-99','2021-99-99'],
axisLine: {
show: false,
margin:0,
lineStyle: {
color: "#7AB9FF",
}
},
splitLine: {
show:false
}
},
color:['#7AB9FF'],//折线颜色
yAxis: {
type: 'value',
// name: '方',
axisLabel:{
// formatter:'{value}(万元)',
interval:0,//横轴信息全部显示
rotate:0,//-15度角倾斜显示
color: '#7AB9FF',
fontSize: 12
},
axisLine: {
show: false
},
splitLine: {
show: true,
lineStyle: {
color: "rgba(17, 143, 254, 0.6)",
width: '0.5'
}
}
},
series: [{
data: yData,
// data: [1000,2000,1000,2000,1000,2000,1000,2000,1000,2000],
type: 'line'
}],
tooltip: {
trigger: 'axis',
axisPointer: { // 坐标轴指示器,坐标轴触发有效
type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
}
},
grid: {
left: '3%',
right: '5%',
bottom: '8%',
containLabel: true
},
dataZoom:[
{ //y轴固定,让内容滚动
type: 'slider',
borderColor: "rgba(237, 0, 0, 0)",
xAxisIndex: [0],
show:true, //显示滚轴
height:11, //设置滚轴的高度
bottom: '3%', //设置滚轴在底部
startValue: startValue1,
endValue: endtValue1,//设置X轴刻度之间的间隔(根据数据量来调整)
zoomLock: false, //锁定区域禁止缩放(鼠标滚动会缩放,所以禁止)
minValueSpan:6,//用于限制窗口大小的最小值(实际数值)。
// maxValueSpan:100,//用于限制窗口大小的最大值(实际数值)。
realtime:true,//拖动时,是否实时更新系列的视图。如果设置为 false,则只在拖拽结束的时候更新
showDetail: false,//即拖拽时候滚动条两侧不显示信息 默认true
filterMode:'empty', // 溢出窗口数据隐藏
backgroundColor: 'rgba(20, 68, 126, 1)', //背景颜色
// handleColor: '#CBBCDB',
areaStyle: {
color: '#fff'
},
handleStyle: {
borderColor: "#2264e9",
borderWidth: "0",
shadowBlur: 2,
// background: "#fff",
// shadowColor: "blue",
},
fillerColor: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
//给颜色设置渐变色 前面4个参数,给第一个设置1,第四个设置0 ,就是水平渐变
//给第一个设置0,第四个设置1,就是垂直渐变
offset: 0,
color: 'rgba(17, 143, 254, 0.63)'
}, {
offset: 1,
color: 'rgba(17, 143, 254, 0.63)'
}]),
},
],
};
//x轴数据length<7,隐藏滚动条
if (option.xAxis.data.length < 8) {
option.dataZoom[0].show = false
}
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
},
举例二、
自己封装的echarts写法
js2: 这个属于某个获取数据方法里面的部分内容,如果放在methods里面的话记得放在data后面执行,下面的js1
function(){
const vm = this
//质量统计X轴数据
vm.chartName[1].firstOption.xAxis.data = resultObj.date
//x轴数据小于7天,隐藏滚动条
if (vm.chartName[0].firstOption.xAxis.data.length < 8) {
vm.chartName.map(item =>{
item.firstOption.dataZoom[0].show = false
})
}else{
//将滑块置于右侧,只显示七天
vm.chartName.map(item =>{
item.firstOption.dataZoom[0].startValue = resultObj.date.length - 7 //滚动条滑块的开始位置
item.firstOption.dataZoom[0].endValue = resultObj.date.length - 1 //滚动条滑块的结束位置
})
}
}
js1:data模块
data() {
return {
chartName: [
{
title:'质量统计',///333
titleB:'',
name:'质量统计',
class:'iconfont icon-tongji2',
show:false,
// color:['#00C9E9','#4AC10F'],
firstOption:{
title: {
left:'12',
// top:'10',
textStyle:{
fontSize:12,
color:'rgba(122,185,255,0.5)',
},
text:'比率'
},
tooltip: {
trigger: 'axis'
},
legend: {//层面颜色设置
// right:0,
top:'-1%',
x: 'center',
data:[{
name:'上层面',
textStyle:{
color:'#118FFE',
fontSize:10
}
},{
name:'下层面',
textStyle:{
color:'#E8C53B',
fontSize:10,
borderWidth:20
}
}],
// itemStyle:{
// color: function(params) {
// var colorList = ['#118FFE','#E8C53B']
// return colorList[params.dataIndex]
// },
// }
},
color:['#118FFE', '#E8C53B'],
grid: {
//直角坐标系内绘图网格
y:35,
y2:50,
x:40,
x2:20,
left: '4%',
right: '4%',
bottom: '8%',
// top:'15%',
containLabel: true
},
xAxis: {
type: 'category',
boundaryGap: false,
data: [],
// axisLabel:{
// formatter:'{value}月'
// },
axisLine: {
show: false,
margin:0,
lineStyle: {
color: "#7AB9FF",
}
},
splitLine: {
show:false
}
},
yAxis: {
type: 'value',
axisLabel:{
// formatter:'{value}(万元)',
interval:0,//横轴信息全部显示
rotate:0,//-15度角倾斜显示
color: '#7AB9FF',
fontSize: 12
},
axisLine: {
show: false
},
splitLine: {
show: true,
lineStyle: {
color: "rgba(17, 143, 254, 0.6)",
width: '0.5'
}
}
},
series: [
{
name:'上层面',
type:'line',
data:[],
},
{
name:'下层面',
type:'line',
data:[],
}
],
dataZoom:[
{ //y轴固定,让内容滚动
type: 'slider',
borderColor: "rgba(237, 0, 0, 0)",
xAxisIndex: [0],
show:true, //显示滚轴
height:11, //设置滚轴的高度
bottom:0, //设置滚轴在底部
startValue: 90,
endValue: 100,//设置X轴刻度之间的间隔(根据数据量来调整)
minValueSpan:6,//用于限制窗口大小的最小值(实际数值)。
// maxValueSpan:1000,//用于限制窗口大小的最大值(实际数值)。
realtime:true,//拖动时,是否实时更新系列的视图。如果设置为 false,则只在拖拽结束的时候更新
showDetail: false,//即拖拽时候滚动条两侧不显示信息 默认true
filterMode:'none', // 溢出窗口数据隐藏
backgroundColor: 'rgba(20, 68, 126, 1)', //背景颜色
handleColor: '#CBBCDB',
areaStyle: {
color: '#fff'
},
handleStyle: {
borderColor: "#2264e9",
borderWidth: "0",
shadowBlur: 2,
// background: "#fff",
// shadowColor: "blue",
},
fillerColor: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
//给颜色设置渐变色 前面4个参数,给第一个设置1,第四个设置0 ,就是水平渐变
//给第一个设置0,第四个设置1,就是垂直渐变
offset: 0,
color: 'rgba(17, 143, 254, 0.63)'
}, {
offset: 1,
color: 'rgba(17, 143, 254, 0.63)'
}]),
},
],
},
},
]
}
}