ECharts 制作子弹图

5 min read

前段时间在公众号上看到一个可视化图,感觉效果很有意思,今天用 ECharts5.0 来复刻一下。

效果图
效果图

主要代码是由豆包生成的,我将效果图上传到豆包上,并让其制作子弹图。生成的结果和效果图有些许差异,实际值没有叠在分类值上面,目标值显示的也不一致,右侧提示也没有显示出来,又手动调整了些代码。

原理:

  1. 分类值通过堆叠柱状图来实现,通过属性 stack:‘range’ 来实现堆叠;
  2. 实际值使用柱状图,默认显示在分类值的下面,通过属性 barGap: ‘-75%’ 进行向上平移,使其叠在分类值上;
  3. 目标值通过散点图来实现,样式设置为矩形,并适当调整大小、位置,本例为:{symbol: ‘rect’, symbolSize: [1, 34],symbolOffset: [0, 3]};
  4. 右侧提示也是通过散点图来实现,但是需要隐藏图形样式,通过设置颜色为透明色来实现,color: ‘rgba(0, 0, 0, 0)’。

代码如下:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>ECharts子弹图 销售达成</title>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js"></script>
</head>
<body>
<div id="chart" style="width:1200px; height:300px; margin:20px;"></div>
<script>
const chartDom = document.getElementById('chart')
const myChart = echarts.init(chartDom)
// ============ 数据(和截图完全对应)============
const category = ['产品E', '产品D', '产品C', '产品B', '产品A'] // y轴类目(echarts横向bar从上到下顺序反转)
// 四层背景分段:较差 / 一般 / 良好 / 优秀
const rangePoor = [25, 25, 25, 25, 25] // 0~25
const rangeNormal = [25, 25, 25, 25, 25] // 25~50
const rangeGood = [25, 25, 25, 25, 25] // 50~75
const rangeExe = [25, 25, 25, 25, 25] // 75~100
const actual = [100, 95, 85, 70, 65] // 实际值 E,D,C,B,A
const target = [75, 75, 75, 75, 75] // 目标红线位置
const diffText = ['+35%', '-5%', '+20%', '-15%', '+12%'] // 右侧差值文字
const option = {
title:{
text: '示例:不同产品的销售达成情况',
left: 'center'
},
tooltip: {
trigger: 'axis',
},
legend: {
top: 30,
data: ['较差', '一般', '良好', '优秀', '实际值', '目标值']
},
grid: {
left: '8%',
right: '10%', //预留右侧放差值文字
top: '16%',
bottom: '8%'
},
xAxis: {
type: 'value',
min: 0,
max: 100,
axisLabel: {
formatter:'{value}%',
},
splitLine: {
lineStyle: {
color:'#eeeeee',
}
},
},
yAxis: {
type: 'category',
data: category,
axisLabel:{
fontSize: 14,
color: '#999999'
},
axisTick: {
show:false,
},
axisLine: {
lineStyle: {
color:'#eeeeee',
},
},
},
series:[
// 【1】背景四层堆积条 较差
{name:'较差', type:'bar', barWidth:24, stack:'range', silent:true, itemStyle:{color:'#eeeeee'}, data:rangePoor},
//【2】一般
{name:'一般', type:'bar', barWidth:24, stack:'range', silent:true, itemStyle:{color:'#dddddd'}, data:rangeNormal},
//【3】良好
{name:'良好', type:'bar', barWidth:24, stack:'range', silent:true, itemStyle:{color:'#bbbbbb'}, data:rangeGood},
//【4】优秀
{name:'优秀', type:'bar', barWidth:24, stack:'range', silent:true, itemStyle:{color:'#99aabb'}, data:rangeExe},
//【5】实际值 前景深蓝色条
{
name: '实际值',
type: 'bar',
barGap: '-75%', // 平移自身一个身位,使之与前一个柱状图重叠
barWidth: 12,
z: 10, // 层级更高,盖在背景上面
itemStyle: {
color:'#104e9b',
borderRadius: [0, 5, 5, 0]
},
data: actual
},
//【6】目标红线 markLine
{
name: '目标值',
type: 'scatter',
symbol: 'rect',
symbolSize: [1, 34],
symbolOffset: [0, 3],
data: target,
color: 'red',
hoverAnimation: false,
z: 20, // 层级最高,盖在实际值上面
},
//【7】右侧差值文字 + 虚线箭头
{
type: 'scatter',
symbol: 'rect',
symbolSize: [1, 34],
symbolOffset: [0, 1],
color: 'rgba(0, 0, 0, 0)', // 设置为透明色,达到隐藏的目的
label: {
show: true,
position: 'right',
distance: 0,
formatter: params => `----> ${ diffText[params.dataIndex] }`,
fontSize: 14,
color: '#0044aa'
},
data: [100, 100, 100, 100, 100],
}
]
}
myChart.setOption(option)
window.addEventListener('resize', () => myChart.resize())
</script>
</body>
</html>

复刻图
复刻图


更多文章