Echars 热力图 自定义颜色

以下仅供参考~
注意: 一定要注意echars版本
需要实现的效果是: target 、 Safe Time 单独指定颜色,超时时间数量根据条件指定对应颜色

效果图:
在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>热力图 自定义颜色</title>
		<style type="text/css">
			body {
				background-color: #183B64;
			}
		</style>
	</head>
	<body>
		<div id="echarsID" style="width: 100%;height: 400px;"></div>
	</body>
	<!-- echars版本一定要注意,否则无法使用itemStyle指定颜色 -->
	<script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.0.1/echarts.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		function showEchars() {
			let myChart = echarts.getInstanceByDom(document.getElementById("echarsID"));
			if (myChart === undefined) {
				myChart = echarts.init(document.getElementById("echarsID"));
			};
			//横坐标name
			var xAxis = ['IQC in', 'Flex bending', 'Aperture PF', 'RCVR', 'RCVR Inspection PDCA', '7P', 'Frame attach',
				'Overflow Cleaning', 'Ground Tape', 'Ring Welding', 'UMP', 'QQC-CG-ALT', 'OQC-Resistance', 'OQC-TSP', 'FQC in'
			];
			//纵坐标name
			var yAxis = ['target', 'Safe Time', '2H', '4H', '8H', '12H', '>12'];
			var data = [];
			for (var i = 0; i < xAxis.length; i++) {
				let arr = [];
				for (var j = 0; j < yAxis.length; j++) {
					let value = parseInt(Math.random() * 1200);
					//需求: 指定目标和安全时间固定颜色,剩下的超时时间数量根据条件判断来决定颜色
					// 值,下标
					let itemStyle = setColors(value, j);
					let obj = {};
					obj.itemStyle = itemStyle;
					obj.value = [i, j, value];
					data.push(obj)
				}
			}

			var option = {
				title: {
					text: "热力图 自定义颜色",
					left: 'center',
					textStyle: {
						color: "#fff"
					},
					top: '2%'
				},
				tooltip: {
					position: 'top',
					backgroundColor: 'rgba(42, 47, 53, 0.9)',
					borderColor: '#ffffff',
					textStyle: {
						color: "#fff"
					},
					axisPointer: {
						lineStyle: {
							color: '#fff'
						}
					}
				},
				grid: {
					left: '2%',
					right: '6%',
					bottom: '2%',
					top: '15%',
					containLabel: true
				},
				xAxis: [{
					show: false,
					type: '',
					data: xAxis,
					splitArea: {
						show: true
					},
					axisLabel: {
						show: true,
						textStyle: {
							color: '#fff',
						},
					},
				}, {
					type: 'category',
					data: xAxis,
					splitArea: {
						show: true
					},
					axisLabel: {
						show: true,
						textStyle: {
							color: '#fff',
						},
						interval: 0,
						rotate: 10
					},
				}],
				yAxis: {
					type: 'category',
					data: yAxis,
					splitArea: {
						show: true
					},
					axisLabel: {
						show: true,
						textStyle: {
							color: '#fff',
						},
						interval: 0,
						// rotate: 15
					},
				},
				visualMap: {
					min: 0,
					max: 1200,
					calculable: true,
					orient: 'vertical',
					left: 'right',
					top: 'center',
					textStyle: {
						color: "#fff"
					},
					// bottom: '15%',
					//自定义热力图颜色
					inRange: {
						color: ['#15B1F4', '#16c573', '#d18a17', '#ffac1d', '#D1621D']
					},
				},
				series: [{
					name: '数值',
					type: 'heatmap',
					data: data,
					label: {
						show: true,
						textStyle: {
							color: "#fff"
						}
					},
					emphasis: {
						itemStyle: {
							shadowBlur: 10,
							shadowColor: 'rgba(0, 0, 0, 0.5)'
						}
					}
				}]
			};
			myChart.clear();
			myChart.setOption(option);
			carouselTooltip(myChart, option);
			window.addEventListener('resize', function() {
				myChart.resize();
			})
		}

		//轮播展示tooltip提示框
		function carouselTooltip(chart, option) {
			var currentIndex = -1;
			let obj = {};
			obj.chart = chart;
			obj.time = setInterval(() => {
				let dataLen = option.series[0].data.length;
				// 取消之前高亮的图形
				chart.dispatchAction({
					type: 'downplay',
					seriesIndex: 0,
					dataIndex: currentIndex
				});
				currentIndex = (currentIndex + 1) % dataLen;
				// 高亮当前图形
				chart.dispatchAction({
					type: 'highlight',
					seriesIndex: 0,
					dataIndex: currentIndex,
				});
				// 显示 tooltip
				chart.dispatchAction({
					type: 'showTip',
					seriesIndex: 0,
					dataIndex: currentIndex
				});
			}, 1000)
		}
		
		// 筛选颜色
		function setColors(val, index) {
			let obj = {};
			let color = null;
			if (index == 0) {
				color = "#0288d1";
			} else if (index == 1) {
				color = "#16c573";
			} else {
				if (val <= 800) {
					color = '#15B1F4';
				} else if (val > 800 && val < 1000) {
					color = '#ffac1d';
				} else if (val >= 1000) {
					color = '#D1621D';
				}
			}
			if (color != null) {
				obj = {
					normal: {
						color: color,
					}
				}
			}
			return obj;
		}

		//调用方法
		showEchars()
	</script>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值