ECharts实训案例(3)

目录

​编辑气候温度与降水量、蒸发量的关系分析

3.实现思路及步骤

(1)在VS Code中创建LineBarMashUp.html文件。

(2)绘制折线图与柱状图混搭图表。

配置项

降水量数据系列

 蒸发量数据系列

 温度折线数据列

完整代码


气候温度与降水量、蒸发量的关系分析


1.训练要点
掌握 ECharts混搭图表的绘制。
2.需求说明
基于“气候温度与降水量蒸发量.xlsx”数据,绘制折线图与柱状图混搭图表,分析气候温度与降水量、蒸发量的关系。


3.实现思路及步骤


(1)在VS Code中创建LineBarMashUp.html文件。


(2)绘制折线图与柱状图混搭图表。

首先,在LineBarMashUp.html文件中引入 echarts.js 库文件。

其次,准备一个具备大小(weight与height)的 div容器,并使用init0方法初始化容器。

16进制色卡

设置折线图和柱状图的配置项、“温度”“降水量”与“蒸发量”数据完成混搭图表的绘制。

设置背景颜色

backgroundColor: '#c0ebd7',

配置项

降水量数据系列

{
    name: '降水量',
	type: 'bar',
	itemStyle: { // 设置柱状图颜色
		normal: {
			color: function (params) {
				var colorList = [ //build a color map as your need
					'#fe9f4f', '#fead33', '#feca2b', '#fed728', '#c5ee4a',
					'#87ee4a', '#46eda9', '#47e4ed', '#4bbbee', '#4f8fa8',
					'#4586d8', '#4f68d8', '#F4E001', '#F0805A', '#26C0C0'
				];
				return colorList[params.dataIndex]
			},
		}
	}, // 设置柱形颜色
	data: [2.6, 5.9, 9, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6, 2.3]
},

 蒸发量数据系列

{
    name: '蒸发量',
	type: 'bar',
	itemStyle: { // 设置柱状图颜色
		normal: {
			color: function (params) {
				var colorList = [ //build a color map as your need
					'#fe9f4f', '#fead33', '#feca2b', '#fed728', '#c5ee4a',
					'#87ee4a', '#46eda9', '#47e4ed', '#4bbbee', '#4f8fa8',
					'#4586d8', '#4f68d8', '#F4E001', '#F0805A', '#26C0C0'
				];
				return colorList[params.dataIndex]
			},
		}
	}, // 设置柱形颜色
	data: [2, 4.9, 7, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20, 6.4, 3.3]
},

 温度折线数据列

{
    name: '温度',
	type: 'line',
	yAxisIndex: 1, //指定使用第2个y轴
	itemStyle: { // 设置柱状图颜色
		normal: {
			color: function (params) {
				var colorList = [ //build a color map as your need
					'#fe9f4f', '#fead33', '#feca2b', '#fed728', '#c5ee4a',
					'#87ee4a', '#46eda9', '#47e4ed', '#4bbbee', '#4f8fa8',
					'#4586d8', '#4f68d8', '#F4E001', '#F0805A', '#26C0C0'
				];
				return colorList[params.dataIndex]
			},
		}
	}, // 设置折线颜色
	data: [2, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23, 16.5, 12, 6.2]
}

完整代码

<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<script type="text/javascript" src='js/echarts.js'></script>
</head>

<body>
	<div id="main" style="width: 800px; height: 600px"></div>
	<script type="text/javascript">
		// 基于准备好的dom,初始化ECharts图表
		var myChart = echarts.init(document.getElementById("main"));
		var option = { // 指定图表的配置项和数据
			backgroundColor: '#c0ebd7', //rgba设置透明度0.1
			tooltip: {
				trigger: 'axis'
			},	
			legend: {
				data: ['降水量', '蒸发量', '温度'],
				left: 'center',
				top: 12
			},
			xAxis: [{
				type: 'category',
				data: ['1月', '2月', '3月', '4月', '5月', '6月',
					'7月', '8月', '9月', '10月', '11月', '12月'
				]
			}],
			yAxis: [{ // 设置2个Y轴之1:降水量、蒸发量
					type: 'value',
					name: '水量',
					min: 0,
					max: 200,
					interval: 50,
					axisLabel: {
						formatter: '{value}  ml'
					}
				},
				{ // 设置2个Y轴之2:温度
					type: 'value',
					name: '温度',
					min: 0,
					max: 30,
					position: 'right', // 设置y轴安置的位置
					offset: 0, // 设置向右偏移的距离
					axisLabel: {
						formatter: '{value} °C'
					}
				}
			],
			series: [{
					name: '降水量',
					type: 'bar',
					itemStyle: { // 设置柱状图颜色
						normal: {
							color: function (params) {
								var colorList = [ //build a color map as your need
									'#fe9f4f', '#fead33', '#feca2b', '#fed728', '#c5ee4a',
									'#87ee4a', '#46eda9', '#47e4ed', '#4bbbee', '#4f8fa8',
									'#4586d8', '#4f68d8', '#F4E001', '#F0805A', '#26C0C0'
								];
								return colorList[params.dataIndex]
							},
						}
					}, // 设置柱形颜色
					data: [2.6, 5.9, 9, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6, 2.3]
				},
				{
					name: '蒸发量',
					type: 'bar',
					itemStyle: { // 设置柱状图颜色
						normal: {
							color: function (params) {
								var colorList = [ //build a color map as your need
									'#fe9f4f', '#fead33', '#feca2b', '#fed728', '#c5ee4a',
									'#87ee4a', '#46eda9', '#47e4ed', '#4bbbee', '#4f8fa8',
									'#4586d8', '#4f68d8', '#F4E001', '#F0805A', '#26C0C0'
								];
								return colorList[params.dataIndex]
							},
						}
					}, // 设置柱形颜色
					data: [2, 4.9, 7, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20, 6.4, 3.3]
				},
				{
					name: '温度',
					type: 'line',
					yAxisIndex: 1, //指定使用第2个y轴
					itemStyle: { // 设置柱状图颜色
						normal: {
							color: function (params) {
								var colorList = [ //build a color map as your need
									'#fe9f4f', '#fead33', '#feca2b', '#fed728', '#c5ee4a',
									'#87ee4a', '#46eda9', '#47e4ed', '#4bbbee', '#4f8fa8',
									'#4586d8', '#4f68d8', '#F4E001', '#F0805A', '#26C0C0'
								];
								return colorList[params.dataIndex]
							},
						}
					}, // 设置折线颜色
					data: [2, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23, 16.5, 12, 6.2]
				}
			]
		};
		myChart.setOption(option); // 为echarts对象加载数据 
	</script>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发财糕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值