Highcharts 饼图学习笔记
什么是 Highcharts 饼图?
Highcharts 是一款基于 JavaScript 的图表库,可用于创建交互式的饼图。饼图是一种显示数据占比的图表类型,它由多个扇形组成,每个扇形代表数据中的一部分。
如何创建 Highcharts 饼图?
以下是一个简单的 Highcharts 饼图示例:
javascriptCopy CodeHighcharts.chart('container', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: '2019 年销售数据占比'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
accessibility: {
point: {
valueSuffix: '%'
}
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %'
}
}
},
series: [{
name: '占比',
colorByPoint: true,
data: [{
name: '产品 A',
y: 61.41,
sliced: true,
selected: true
}, {
name: '产品 B',
y: 11.84
}, {
name: '产品 C',
y: 10.85
}, {
name: '产品 D',
y: 4.67
}, {
name: '其他',
y: 10.23
}]
}]
});
代码中的 Highcharts.chart
函数用于初始化一个图表,并将它渲染到指定的 HTML 容器中。其中,type
属性设置为 'pie'
,表示创建一个饼图。
此外,还可以通过 title
属性设置标题、tooltip
属性设置提示框内容和格式、plotOptions
属性设置数据标签样式等。
示例
下面是一个更具体的 Highcharts 饼图实例,展示了 2022 年各国家人口占比:
javascriptCopy CodeHighcharts.chart('container', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: '2022 年各国家人口占比'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
accessibility: {
point: {
valueSuffix: '%'
}
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %'
}
}
},
series: [{
name: '占比',
colorByPoint: true,
data: [{
name: '中国',
y: 18
}, {
name: '印度',
y: 17
}, {
name: '美国',
y: 4
}, {
name: '印度尼西亚',
y: 3
}, {
name: '巴西',
y: 3
}, {
name: '巴基斯坦',
y: 2
}, {
name: '孟加拉国',
y: 2
}, {
name: '尼日利亚',
y: 2
}, {
name: '俄罗斯',
y: 2
}, {
name: '日本',
y: 1
}, {
name: '其他',
y: 46
}]
}]
});
以上是 Highcharts 饼图的基本使用方法和语法,您可以根据自己的需求进行进一步的配置和定制。