如何在Highcharts中仅隐藏Grand Summary分类的Grand Total For Each Bars
Highcharts柱状图隐藏指定分类下的特定柱形解决方案
问题说明
需要在Highcharts柱状图中仅隐藏Grand Summary分类对应的Grand Total For Each Bars柱形,保留April、May、Jun分类下的该柱形,但当前操作会移除所有分类下的该柱形。
原效果截图:
解决方案
方法1:直接修改数据源(最简单高效)
Highcharts中数据值为null时,对应柱形会自动隐藏。只需找到Grand Total For Each Bars系列里对应Grand Summary分类的数据项,将其设为null即可。
修改后的JavaScript代码:
var seriesData = [ { "data": [ null, // 将Grand Summary对应的数据设为null,隐藏该柱形 230, 230, 230 ], "name": "Grand Total For Each Bars" }, { "data": [ 230, 100, 100, 100 ], "name": "April" }, { "data": [ 230, 50, 50, 50 ], "name": "May" }, { "data": [ 230, 80, 80, 80 ], "name": "Jun" } ]; var categoriesData = ['Grand Summary', 'April', 'May', 'Jun']; Highcharts.chart('container', { chart: { type: 'column' }, title: { text: '仅隐藏Grand Summary分类下的Grand Total For Each Bars柱形', align: 'left' }, xAxis: { categories: categoriesData, title: { text: null } }, yAxis: { title: { text: '', align: 'high' }, labels: { overflow: 'justify' }, gridLineWidth: 0 }, series: seriesData });
方法2:通过图表事件动态隐藏(适合无法修改数据源的场景)
如果无法直接修改原始数据,可利用Highcharts的render事件定位对应柱形元素并隐藏:
Highcharts.chart('container', { chart: { type: 'column', events: { render: function() { var targetSeries = this.series.find(s => s.name === 'Grand Total For Each Bars'); if (targetSeries) { var targetPoint = targetSeries.points[0]; targetPoint.graphic?.hide(); } } } }, // 其余配置同原代码 });
完整可运行代码示例
HTML
<script src="https://code.highcharts.com/highcharts.js"></script> <script src="https://code.highcharts.com/modules/exporting.js"></script> <script src="https://code.highcharts.com/modules/export-data.js"></script> <script src="https://code.highcharts.com/modules/accessibility.js"></script> <figure class="highcharts-figure"> <div id="container"></div> <p class="highcharts-description"> </p> </figure>
CSS
.highcharts-figure, .highcharts-data-table table { min-width: 310px; max-width: 800px; margin: 1em auto; } #container { height: 400px; } .highcharts-data-table table { font-family: Verdana, sans-serif; border-collapse: collapse; border: 1px solid #ebebeb; margin: 10px auto; text-align: center; width: 100%; max-width: 500px; } .highcharts-data-table caption { padding: 1em 0; font-size: 1.2em; color: #555; } .highcharts-data-table th { font-weight: 600; padding: 0.5em; } .highcharts-data-table td, .highcharts-data-table th, .highcharts-data-table caption { padding: 0.5em; } .highcharts-data-table thead tr, .highcharts-data-table tr:nth-child(even) { background: #f8f8f8; } .highcharts-data-table tr:hover { background: #f1f7ff; }
JavaScript
使用方法1中的修改后代码即可。
内容的提问来源于stack exchange,提问作者Raheel Aslam
相关产品推荐
相关产品推荐

