echarts 渐变柱状图 阴影与柱宽不一致

vue:2.6; "echarts": "^5.2.0", 

<template>
	<div :class="className" :style="{height:height,width:width}" id="myChartZyzfwsc" />
</template>

<script>
	import * as echarts from 'echarts';
	import resize from '@/utils/resize.js'
	
	export default {
		mixins: [resize],
		props: {
			className: {
				type: String,
				default: 'chart'
			},
			width: {
				type: String,
				default: '24vw'
			},
			height: {
				type: String,
				default: '28vh'
			},
			chartData: {
				type: Array,
				required: true
			}
		},
		data() {
			return {
				
				timer: 0,
				date: '',
				weekList: [],
			}
		},
		watch: {
			chartData: {
				deep: true,
				handler(val) {
					this.setOption(val)
				}
			},

		},
		mounted() {
			this.initChart();
		},
		beforeDestroy() {
			
		},
		methods: {
			initChart() {
				//获取一周日期
				this.getWeekDataList();
				var myChart = echarts.init(document.getElementById('myChartZyzfwsc'));
				var option = {
					animation: false,
					tooltip: {
						trigger: 'item',
						formatter: '{b}:{c}'
					},
					grid: {
						left: 40,
						right: 15,
						botom: 10,
						top: 35
					},
					legend: {
						show: false,

					},
					xAxis: {
						type: 'category',
						boundaryGap: ['0', '5%'],
						// boundaryGap: false,
						splitLine: {
							show: false
						},
						axisLabel: {
							color: "#8590A4",
							textStyle: {
								fontSize: 12,

							},
							interval: 0, //坐标全部显示
						},
						/*网格线*/
						axisLine: {
							// 轴线
							show: true,
							lineStyle: {
								color: "#56678D", //y轴线的颜色(若只设置了y轴线的颜色,未设置y轴文字的颜色,则y轴文字会默认跟设置的y轴线颜色一致)
								width: 1, //y轴线的宽度
								type: "solid" //y轴线为实线
							},
							// color: '#268C8C',
						},
						data: this.weekList
					},

					yAxis: {
						type: 'value',
						name: '单位/小时',
						nameTextStyle: {
							color: "#8590A4",
							fontSize: 12,
						},
						boundaryGap: false,
						min: 0,
						max: 2500,
						splitNumber: 5,
						axisTick: {
							// 轴刻度
							show: false,
						},
						axisLabel: {
							color: "#8590A4",
							textStyle: {
								fontSize: 12,
								fontWeight: 400,

							},
						},
						splitLine: {
							// 网格线
							show: true,
							lineStyle: { //分割线
								color: "#30436D",
								width: 1,
								type: "dashed" //dotted:虚线 solid:实线
							}

						},
						axisLine: {
							// 轴线
							show: false,
							lineStyle: {
								color: "#fff", //y轴线的颜色(若只设置了y轴线的颜色,未设置y轴文字的颜色,则y轴文字会默认跟设置的y轴线颜色一致)
								width: 1, //y轴线的宽度
								type: "solid" //y轴线为实线
							},
						},

					},
					series: [{
							name: '志愿者服务时长阴影',
							type: 'bar',
							data: [2500, 2500, 2500, 2500, 2500, 2500, 2500],
							barWidth: '40',
							z:"-1",
							barGap: '-78%',
							itemStyle: {
								normal: {
									opacity: 0.8,
									// 线性渐变,前四个参数分别是 x0, y0, x2, y2, 范围从 0 - 1,分别表示右,下,左,上。例如(0,0,0,1)表示从正上开始向下渐变;如果是(1,0,0,0),则是从正右开始向左渐变。
									// 相当于在图形包围盒中的百分比,如果最后一个参数传 true,则该四个值是绝对的像素位置
									color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
										offset: 0,
										color: 'rgba(255, 255, 255, 0.1)' //指0%处的颜色
									}, {
										offset: 1,
										color: 'rgba(255, 255, 255, 0.1)' //指100%处的颜色
									}], false)
								}
							},

						},
						{
								name: '志愿者服务时长',
								type: 'bar',
								data: [600, 1400, 1600, 1650, 1200, 1100, 1400],
								barWidth: '23',
								itemStyle: {
									normal: {
										opacity: 0.8,
										// 线性渐变,前四个参数分别是 x0, y0, x2, y2, 范围从 0 - 1,分别表示右,下,左,上。例如(0,0,0,1)表示从正上开始向下渐变;如果是(1,0,0,0),则是从正右开始向左渐变。
										// 相当于在图形包围盒中的百分比,如果最后一个参数传 true,则该四个值是绝对的像素位置
										color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
											offset: 0,
											color: 'rgb(3, 114, 229)' //指0%处的颜色
										}, {
											offset: 1,
											color: 'rgb(75, 175, 252)' //指100%处的颜色
										}], false)
									}
								},
						
							},


					]

				};
				myChart.setOption(option);
				// window.addEventListener('resize', myChart.resize);
				window.addEventListener('resize', function() {
					myChart.resize()
				})


			},
			//获取当日日期
			getNowDate() {
				const timeOne = new Date()
				const year = timeOne.getFullYear()
				let month = timeOne.getMonth() + 1
				let day = timeOne.getDate()
				month = month < 10 ? '0' + month : month
				day = day < 10 ? '0' + day : day
				const NOW_MONTHS_AGO = `${year}-${month}-${day}`
				return NOW_MONTHS_AGO
			},
			// 获取当前一周日期
			getWeekDataList() {
				//获取当前日期
				this.getNowDate();
				this.date = this.getNowDate();
				console.log(this.date, 'currentData')

				//根据日期获取本周周一~周日的年-月-日
				var weeks = [];
				var date = new Date(this.date);
				//判断本日期是否为周日,获取本周一日期
				if (date.getDay() == "0") {
					date.setDate(date.getDate() - 6);
				} else {
					date.setDate(date.getDate() - date.getDay() + 1);
				}
				var myDate = date.getDate();
				var myMonth = date.getMonth() + 1;
				if (date.getDate() < 10) {
					myDate = '0' + myDate;
				}
				if (date.getMonth() + 1 < 10) {
					myMonth = '0' + myMonth;
				}
				// weeks.push(date.getFullYear() + "-" + myMonth + "-" + myDate);
				weeks.push(myMonth + "-" + myDate);
				// 获取周二以后日期
				for (var i = 0; i < 6; i++) {
					date.setDate(date.getDate() + 1);
					myDate = date.getDate();
					myMonth = date.getMonth() + 1;
					if (date.getDate() < 10) {
						myDate = '0' + myDate;
					}
					if (date.getMonth() + 1 < 10) {
						myMonth = '0' + myMonth;
					}
					// weeks.push(date.getFullYear() + "-" + myMonth + "-" + myDate);
					weeks.push(myMonth + "-" + myDate);
				}
				this.weekList = weeks;
				console.log(this.weekList, 'weekList')
				// return weeks


			},

		}
	}
</script>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
echarts中,要实现柱状图间隔阴影效果,可以通过对series配置项中的itemStyle进行设置。具体的做法如下所示: 1. 首先,需要在itemStyle中设置color字段为一个渐变色值。可以通过设置一个数组,将起始颜色和结束颜色传入,实现渐变效果。例如,设置["rgba(0, 0, 0, 0)", "rgba(0, 0, 0, 0.3)"],表示从透明到浅灰色的渐变。 2. 然后,通过设置emphasis字段为一个阴影效果,可以实现柱状图间隔的阴影效果。可以通过设置shadowColor、shadowBlur、shadowOffsetX和shadowOffsetY等属性来调整阴影的颜色、模糊度和位置。 下面是一个示例代码,展示了如何在echarts中实现柱状图间隔阴影的效果: ```javascript option = { xAxis: { type: 'category', data: ['A', 'B', 'C', 'D', 'E'], }, yAxis: { type: 'value', }, series: [{ type: 'bar', data: [200, 300, 400, 500, 600], itemStyle: { color: ["rgba(0, 0, 0, 0)", "rgba(0, 0, 0, 0.3)"], emphasis: { shadowColor: 'rgba(0, 0, 0, 0.5)', shadowBlur: 10, shadowOffsetX: 0, shadowOffsetY: 2, }, }, }], }; ``` 通过以上配置,就可以在echarts中实现柱状图间隔阴影的效果。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [echarts 柱状图渐变背景](https://download.csdn.net/download/qq_36437172/12418565)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [echarts柱状图背后阴影如何同步y轴最大值问题?](https://blog.csdn.net/Tombers/article/details/125017568)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值