目录
(1)在VS Code中创建LineBarMashUp.html文件。
气候温度与降水量、蒸发量的关系分析
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>