Highcharts Highcarts条形图图例符号与标签对齐

Highcharts Highcarts条形图图例符号与标签对齐,highcharts,Highcharts,如果修改条形图中的符号宽度/高度,则符号“底部”与图例文本对齐。有没有办法“中间对齐”它们 我知道的唯一方法是使用CSS“它很脏,但很管用” 您可以计算并设置图例符号SVG元素的translateY属性: events: { load: function() { var legendItems = this.legend.allItems, textBbox, symbolBbox;

如果修改条形图中的符号宽度/高度,则符号“底部”与图例文本对齐。有没有办法“中间对齐”它们


我知道的唯一方法是使用CSS“它很脏,但很管用”


您可以计算并设置图例符号SVG元素的
translateY
属性:

    events: {
        load: function() {
            var legendItems = this.legend.allItems,
                textBbox,
                symbolBbox;

            legendItems.forEach(function(item) {
                textBbox = item.legendItem.getBBox();
                symbolBbox = item.legendSymbol.getBBox();

                item.legendSymbol.attr({
                    translateY: symbolBbox.height - textBbox.height / 2
                });
            });
        }
    }
现场演示:

API参考资料:

根据@ppotaczek的答案,我将Y对齐,因此无论符号大小如何,Y始终是中间对齐的

    events: {
        load: function() {
            var legendItems = this.legend.allItems,
                textBbox,
                symbolBbox;

            legendItems.forEach(function(item) {
                textBbox = item.legendItem.getBBox();
                symbolBbox = item.legendSymbol.getBBox();

                item.legendSymbol.attr({
                    y: textBbox.y + (textBbox.height - symbolBbox.height) / 2
                });
            });
        }
    }

我已经发布了一个更精确的修复程序y:textBbox.y+(textBbox.height-symbolBbox.height)/2
    events: {
        load: function() {
            var legendItems = this.legend.allItems,
                textBbox,
                symbolBbox;

            legendItems.forEach(function(item) {
                textBbox = item.legendItem.getBBox();
                symbolBbox = item.legendSymbol.getBBox();

                item.legendSymbol.attr({
                    translateY: symbolBbox.height - textBbox.height / 2
                });
            });
        }
    }
    events: {
        load: function() {
            var legendItems = this.legend.allItems,
                textBbox,
                symbolBbox;

            legendItems.forEach(function(item) {
                textBbox = item.legendItem.getBBox();
                symbolBbox = item.legendSymbol.getBBox();

                item.legendSymbol.attr({
                    y: textBbox.y + (textBbox.height - symbolBbox.height) / 2
                });
            });
        }
    }