echarts中热力图和日历组件的结合

本文介绍如何在ECharts中结合热力图和日历组件,展示连续三个月的数据分布,通过颜色变化直观反映数值大小。配置了垂直布局的日历,设置了自定义颜色映射,并对上月和上上月数据进行可视化。

echarts中热力图和日历组件的结合

function getVirtulData(year, month) { //随机生成月份的值
	var date = +echarts.number.parseDate(year + '-' + month + '-01');
	var end = +echarts.number.parseDate(year + '-' + (month+1) + '-01');
	var dayTime = 3600 * 24 * 1000;
	var data = [];
	for (var time = date; time < end; time += dayTime) {
		data.push([
			echarts.format.formatTime('yyyy-MM-dd', time),
			Math.floor(Math.random() * 800)
		]);
	}
	return data;
}
let lastM; //上个月
let lastLM; //上上个月
if (month < 2) {
	lastM = 12;
	lastLM = 11; 
}else if (month < 3) {
	lastM = 1;
	lastLM = 12;
} else {
	lastM = month - 1;
	lastLM = month - 2;
}
const option = {   //以下为option的配置
		visualMap: {      
			min: 0,
			max: 800,
			calculable: true,
			orient: 'vertical',
			right: 10,
			top: '25%',
			color: ['#0088ff', '#fff']     //数值范围对应的颜色范围
	},
	calendar: [{                   //calendar若为数组,则是有多个日历,每个对象是一个日历
		orient: 'vertical',
		top: 35,
		left: 100,
		right: 100,
		range: year + '-' + month,       //range可以是一个月也可以是1年,一年的话每个月的日历会连在一起,因为年是整体
		itemStyle: {
			normal: {
				borderWidth: 0.5
			}
		},
		yearLabel: {
			show: false
		},
		monthLabel: {
			show: true,
			nameMap: 'cn',
			fontWeight: 'bold',
			fontSize: 18,
			vertical: 'middle',
			margin: 10
		},
		dayLabel: {
			nameMap: 'cn'
		}
	},{
		orient: 'vertical',
		top: 170,
		left: 100,
		right: 100,
		range: year + '-' + lastM,
		itemStyle: {
		normal: {
		borderWidth: 0.5
		}
		},
		yearLabel: {
		show: false
		},
		monthLabel: {
		show: true,
		nameMap: 'cn',
		fontWeight: 'bold',
		fontSize: 18,
		vertical: 'middle',
		margin: 10
		},
		dayLabel: {
		show: false
	}
	}, {
	orient: 'vertical',
	top: 305,
	left: 100,
	right: 100,
	range: year + '-' + lastLM,
	itemStyle: {
	normal: {
	borderWidth: 0.5
	}
	},
	yearLabel: {
	show: false
	},
	monthLabel: {
	show: true,
	nameMap: 'cn',
	fontWeight: 'bold',
	fontSize: 18,
	vertical: 'middle',
	margin: 10
	},
	dayLabel: {
	show: false
	}
	}],
series: [{                               //并不一定对应calendar中的各个
	type: 'heatmap',
	coordinateSystem: 'calendar',
	calendarIndex: 0,           //此属性是决定对应calendar数组中的哪个日历,相当于数组的index
	data: getVirtulData(year, month)
},{
	type: 'heatmap',
	coordinateSystem: 'calendar',
	calendarIndex: 1,
	data: getVirtulData(year, lastM)
},{
	type: 'heatmap',
	coordinateSystem: 'calendar',
	calendarIndex: 2,
	data: getVirtulData(year, lastLM)
}]
};

### ECharts 日历组件的使用方法 ECharts日历组件提供了一种灵活的方式来展示时间序列数据。以下是关于如何配置使用该组件的具体说明。 #### 配置项详解 日历组件的核心在于 `calendar` `series` 的配合使用。`calendar` 定义了日历的基本属性,如范围、布局等;而 `series` 则定义了具体的数据及其样式[^1]。 - **calendar**: 设置日历的基础参数。 - `range`: 指定显示的时间范围,可以是一个具体的年份(如 `'2023'`),也可以是自定义的日期区间(如 `['2023-01-01', '2023-12-31']`)。 - `cellSize`: 单元格大小,支持固定数值或自动调整。 - **series**: 数据部分。 - `type`: 设为 `'heatmap'` 或其他适合类型的表。 - `data`: 时间与对应值的数组,通常为 `[date, value]` 形式的二维数组。 - `symbol`: 可以修改默认形状,例如改为矩形 `symbol: 'rect'`[^2]。 #### 示例代码 以下是一段完整的示例代码,展示了如何利用 ECharts 创建一个简单的日历热力: ```javascript // 初始化 echarts 实例 var chartDom = document.getElementById('main'); var myChart = echarts.init(chartDom); // 配置选项 var option = { calendar: [ { top: 'middle', left: 'center', cellSize: ['auto', 25], // 自动宽度,高度为25px range: '2023', // 显示整个2023年的日历 dayLabel: { nameMap: 'en' }, // 英文星期名称 monthLabel: { nameMap: 'EN' } // 大写英文月份名称 } ], series: [ { type: 'heatmap', coordinateSystem: 'calendar', data: generateData(), // 动态生成数据函数 symbol: 'rect', // 修改单元格形状为矩形 itemStyle: { color: '#a9d7ff' }, label: { show: true, formatter: function (params) { return params.value[2]; // 展示第三个维度作为标签 } } } ] }; function generateData() { var date = new Date(2023, 0, 1); // 起始时间为2023年1月1日 var end = new Date(2024, 0, 1); // 结束时间为2024年1月1日 var result = []; while (date.getTime() < end.getTime()) { result.push([ echarts.time.format(date, '{yyyy}-{MM}-{dd}'), // 格式化日期 Math.floor(Math.random() * 100), // 随机生成值 echarts.time.format(date, '{ddd}') // 周几信息 ]); date.setDate(date.getDate() + 1); } return result; } myChart.setOption(option); ``` 上述代码实现了如下功能: 1. 使用 `generateData()` 函数动态生成一年内的随机数据,并将其绑定到日历上。 2. 将每个单元格的颜色设为浅蓝色 (`'#a9d7ff'`) 并启用了标签显示,标签内容为对应的周几信息。 #### 注意事项 为了实现更复杂的功能,比如点击事件变色或者高亮特定日期,可以通过监听鼠标交互来完成。例如,在初始化完成后调用 `on` 方法注册回调函数处理这些行为。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值