change datalabels color in a HighCharts chart when hovering without updating the series

谁说我不能喝 提交于 2019-12-13 03:42:40

问题


The following code (built from the answer of a related question) changes the line color in a Highcharts line chart when hovering the series without using the heavier series.update method, but the code line which should change also the datalabels text color is broken. How can I achieve the desidered result?

Highcharts.chart('container', {
    plotOptions: {
        series: {
            dataLabels: {
                enabled: true
            },
            states: {
                hover: {
                    enabled: false
                },
                inactive: {
                    enabled: false
                }
            },
            pointStart: 2010,
            events: {
                mouseOver: function() {
                    this.graph.attr({
                        stroke: "rgb(255,0,0)"
                    });
                    this.points.forEach(p => {
                            p.graphic.attr({
                                fill: "rgb(255,0,0)"
                            });

                            p.dataLabel.options.color="rgb(255,0,0)" 
                            //the above line is broken
                        }
                    );
                },
                mouseOut: function() {
                    this.graph.attr({
                        stroke: this.color
                    });
                    this.points.forEach(p => p.graphic.attr({
                        fill: this.color
                    }));
                }
            }
        },
    },

    series: [{
        name: 'Installation',
        data: [43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175]
    }, {
        name: 'Manufacturing',
        data: [24916, 24064, 29742, 29851, 32490, 30282, 38121, 40434]
    }, {
        name: 'Sales & Distribution',
        data: [11744, 17722, 16005, 19771, 20185, 24377, 32147, 39387]
    }, {
        name: 'Other',
        data: [12908, 5948, 8105, 11248, 8989, 11816, 18274, 18111]
    }],

});

on JSfiddle


回答1:


To change data label color you have to set a new fill style for the label text element using css() method.

Code:

point.dataLabel.text.css({ fill: "rgb(255,0,0)" });

Demo:

  • https://jsfiddle.net/BlackLabel/csn2euL7/1/

API reference:

  • https://api.highcharts.com/class-reference/Highcharts.SVGElement#css


来源:https://stackoverflow.com/questions/55933474/change-datalabels-color-in-a-highcharts-chart-when-hovering-without-updating-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!