如有需求,直接私信留下您的联系方式。谢谢。
我的邮箱:[email protected]
在前端开发中,数据可视化是一项非常重要的技能。饼图作为一种常见的数据可视化图表,能够直观地展示各部分数据占总体的比例关系。今天,我们将使用 HTML 和 JavaScript,借助 ECharts 库来创建一个复杂的自定义饼图。这个饼图包含了南丁格尔玫瑰图效果、自定义标签、引导线样式调整等功能,非常适合前端新手学习。
我们只需要一个文本编辑器(如 VS Code)和一个现代浏览器(如 Chrome、Firefox 等)就可以开始编写代码了。
ECharts 是一个强大的开源 JavaScript 图表库,我们可以通过 CDN 的方式引入它。在 HTML 文件的 标签中添加以下代码:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts.min.js">script>
首先,创建一个基本的 HTML 文件,包含必要的元数据和用于显示饼图的容器。以下是完整的 HTML 代码:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>复杂自定义饼图title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts.min.js">script>
head>
<body>
<div id="complexPieChart" style="width: 1000px; height: 800px;">div>
body>
html>
代码解释:
:声明文档类型为 HTML5。
:设置字符编码为 UTF - 8,确保页面能正确显示各种字符。
:设置视口,使页面在不同设备上能自适应显示。
标签:设置页面标题,用于在浏览器标签栏显示。
标签:通过 CDN 引入 ECharts 库,方便后续使用。:创建一个 div
元素作为饼图的容器,设置其宽度为 1000 像素,高度为 800 像素,id
为 complexPieChart
,方便后续通过 JavaScript 定位。
3.2 JavaScript 代码
在 HTML 文件的
标签中添加以下 JavaScript 代码:
<script>
// 初始化 ECharts 实例,将其绑定到指定的 DOM 元素上
var complexPieChart = echarts.init(document.getElementById('complexPieChart'));
// 定义数据
var data = [
{ value: 120, name: '类别 A' },
{ value: 200, name: '类别 B' },
{ value: 150, name: '类别 C' },
{ value: 80, name: '类别 D' },
{ value: 230, name: '类别 E' }
];
// 定义图表配置项
var option = {
// 标题配置
title: {
text: '复杂自定义饼图示例',
left: 'center',
top: '5%',
textStyle: {
fontSize: 24,
fontWeight: 'bold',
color: '#333'
}
},
// 提示框配置
tooltip: {
trigger: 'item',
formatter: '{a}
{b}: {c} ({d}%)',
backgroundColor: 'rgba(0, 0, 0, 0.7)',
textStyle: {
color: '#fff'
}
},
// 图例配置
legend: {
type: 'scroll',
orient: 'vertical',
right: 10,
top: 20,
bottom: 20,
data: data.map(item => item.name),
textStyle: {
fontSize: 14
},
pageIconColor: '#333',
pageIconInactiveColor: '#999'
},
// 系列配置
series: [
{
name: '数据占比',
type: 'pie',
// 南丁格尔玫瑰图模式,radius 决定扇区半径根据数值大小变化
roseType: 'radius',
radius: ['15%', '70%'],
center: ['40%', '50%'],
data: data,
// 标签配置
label: {
show: true,
formatter: '{b}: {d}%',
position: 'outside',
color: '#333',
fontSize: 14,
// 标签样式
rich: {
value: {
color: '#999',
fontSize: 12
}
}
},
// 标签引导线配置
labelLine: {
show: true,
length: 20,
length2: 30,
lineStyle: {
color: '#999',
width: 1
}
},
// 扇区样式配置
itemStyle: {
borderWidth: 2,
borderColor: '#fff',
// 为每个扇区设置不同的颜色
color: function (params) {
var colorList = [
'#5793f3', '#d14a61', '#675bba', '#ff8e72', '#91cc75'
];
return colorList[params.dataIndex];
}
},
// 高亮状态下扇区样式配置
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
// 使用配置项显示图表
complexPieChart.setOption(option);
script>
代码解释:
初始化 ECharts 实例
var complexPieChart = echarts.init(document.getElementById('complexPieChart'));
这行代码通过 echarts.init
方法将 ECharts 实例绑定到之前创建的 div
元素(id
为 complexPieChart
)上,后续可以通过这个实例来配置和显示图表。
定义数据
var data = [
{ value: 120, name: '类别 A' },
{ value: 200, name: '类别 B' },
{ value: 150, name: '类别 C' },
{ value: 80, name: '类别 D' },
{ value: 230, name: '类别 E' }
];
定义了一个包含多个对象的数组 data
,每个对象表示一个数据项,value
表示该数据项的数值,name
表示数据项的名称。
定义图表配置项 option
- 标题配置(
title
)
title: {
text: '复杂自定义饼图示例',
left: 'center',
top: '5%',
textStyle: {
fontSize: 24,
fontWeight: 'bold',
color: '#333'
}
}
设置图表的标题文本为“复杂自定义饼图示例”,水平居中显示,距离顶部为整个图表高度的 5%,并设置了标题的字体大小、粗细和颜色。
- 提示框配置(
tooltip
)
tooltip: {
trigger: 'item',
formatter: '{a}
{b}: {c} ({d}%)',
backgroundColor: 'rgba(0, 0, 0, 0.7)',
textStyle: {
color: '#fff'
}
}
trigger: 'item'
表示当鼠标悬停在数据项上时触发提示框。formatter
定义了提示框的显示格式,{a}
表示系列名称,{b}
表示数据项名称,{c}
表示数据项值,{d}
表示数据项所占百分比。同时设置了提示框的背景颜色和文本颜色。
- 图例配置(
legend
)
legend: {
type: 'scroll',
orient: 'vertical',
right: 10,
top: 20,
bottom: 20,
data: data.map(item => item.name),
textStyle: {
fontSize: 14
},
pageIconColor: '#333',
pageIconInactiveColor: '#999'
}
type: 'scroll'
表示当图例项过多时支持滚动显示。orient: 'vertical'
使图例垂直排列。通过 data.map(item => item.name)
从 data
数组中提取名称作为图例项。还设置了图例的位置、文本样式和分页图标颜色。
- 系列配置(
series
)
series: [
{
name: '数据占比',
type: 'pie',
roseType: 'radius',
radius: ['15%', '70%'],
center: ['40%', '50%'],
data: data,
// 标签配置
label: {
show: true,
formatter: '{b}: {d}%',
position: 'outside',
color: '#333',
fontSize: 14,
rich: {
value: {
color: '#999',
fontSize: 12
}
}
},
// 标签引导线配置
labelLine: {
show: true,
length: 20,
length2: 30,
lineStyle: {
color: '#999',
width: 1
}
},
// 扇区样式配置
itemStyle: {
borderWidth: 2,
borderColor: '#fff',
color: function (params) {
var colorList = [
'#5793f3', '#d14a61', '#675bba', '#ff8e72', '#91cc75'
];
return colorList[params.dataIndex];
}
},
// 高亮状态下扇区样式配置
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
- `name`:系列名称为“数据占比”。
- `type: 'pie'`:指定图表类型为饼图。
- `roseType: 'radius'`:使用南丁格尔玫瑰图模式,扇区半径根据数值大小变化。
- `radius`:设置饼图的内外半径范围。
- `center`:设置饼图的中心位置。
- `data`:使用之前定义的数据。
- **标签配置(`label`)**:显示标签,自定义标签显示格式,设置标签位置在扇区外部,并通过 `rich` 定义富文本样式。
- **标签引导线配置(`labelLine`)**:显示引导线,设置引导线的两段长度和样式。
- **扇区样式配置(`itemStyle`)**:设置扇区的边框宽度和颜色,通过自定义函数为每个扇区设置不同的颜色。
- **高亮状态下扇区样式配置(`emphasis`)**:设置鼠标悬停时扇区的阴影效果。
应用配置项
complexPieChart.setOption(option);
将配置项 option
应用到 ECharts 实例 complexPieChart
上,从而显示出复杂的自定义饼图。
四、总结
通过以上步骤,我们成功使用 HTML 和 JavaScript 借助 ECharts 库创建了一个复杂的自定义饼图。这个过程中,我们学习了如何初始化 ECharts 实例、定义数据、配置图表的各个部分(标题、提示框、图例、系列等)。希望这个教程能帮助前端新手更好地掌握数据可视化的相关知识。你可以根据自己的需求对代码进行修改和扩展,创造出更多个性化的图表。
完整代码
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>复杂自定义饼图title>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.3.2/dist/echarts.min.js">script>
head>
<body>
<div id="complexPieChart" style="width: 1000px; height: 800px;">div>
<script>
// 初始化 ECharts 实例,将其绑定到指定的 DOM 元素上
var complexPieChart = echarts.init(document.getElementById('complexPieChart'));
// 定义数据
var data = [
{ value: 120, name: '类别 A' },
{ value: 200, name: '类别 B' },
{ value: 150, name: '类别 C' },
{ value: 80, name: '类别 D' },
{ value: 230, name: '类别 E' }
];
// 定义图表配置项
var option = {
// 标题配置
title: {
text: '复杂自定义饼图示例',
left: 'center',
top: '5%',
textStyle: {
fontSize: 24,
fontWeight: 'bold',
color: '#333'
}
},
// 提示框配置
tooltip: {
trigger: 'item',
formatter: '{a}
{b}: {c} ({d}%)',
backgroundColor: 'rgba(0, 0, 0, 0.7)',
textStyle: {
color: '#fff'
}
},
// 图例配置
legend: {
type: 'scroll',
orient: 'vertical',
right: 10,
top: 20,
bottom: 20,
data: data.map(item => item.name),
textStyle: {
fontSize: 14
},
pageIconColor: '#333',
pageIconInactiveColor: '#999'
},
// 系列配置
series: [
{
name: '数据占比',
type: 'pie',
// 南丁格尔玫瑰图模式,radius 决定扇区半径根据数值大小变化
roseType: 'radius',
radius: ['15%', '70%'],
center: ['40%', '50%'],
data: data,
// 标签配置
label: {
show: true,
formatter: '{b}: {d}%',
position: 'outside',
color: '#333',
fontSize: 14,
// 标签样式
rich: {
value: {
color: '#999',
fontSize: 12
}
}
},
// 标签引导线配置
labelLine: {
show: true,
length: 20,
length2: 30,
lineStyle: {
color: '#999',
width: 1
}
},
// 扇区样式配置
itemStyle: {
borderWidth: 2,
borderColor: '#fff',
// 为每个扇区设置不同的颜色
color: function (params) {
var colorList = [
'#5793f3', '#d14a61', '#675bba', '#ff8e72', '#91cc75'
];
return colorList[params.dataIndex];
}
},
// 高亮状态下扇区样式配置
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
// 使用配置项显示图表
complexPieChart.setOption(option);
script>
body>
html>