【问题标题】:HighCharts : Highlight entire series when hover and restore state when outHighCharts:悬停时突出显示整个系列,退出时恢复状态
【发布时间】:2014-07-11 15:42:51
【问题描述】:

当悬停在柱形图中时,我尝试突出显示整个系列。由于所需图表的性质,所有列都并排设置,没有间距。

我通过在系列上使用 mouseOvermouseOut 部分成功了,但是当悬停到同一系列的下一个元素时它不起作用。整个系列应保持突出显示,但如果将鼠标悬停在同一系列中,我将无法禁用 mouseOut。

我尝试通过此代码进行操作:

        series: [
            {
                events: {
                mouseOver: function() {
                    for(var i=0; i<this.data.length; i++)
                    {
                        this.data[i].setState('hover');
                    }

                },
                mouseOut: function(){

                    for(var i=0; i<this.data.length; i++)
                    {
                        this.data[i].setState('');
                    }
                }},

我把这个放在jsFiddle

当您将鼠标悬停在浅蓝色系列时,它会完全突出显示,但是当悬停到该系列中的下一个元素时,该系列应该保持完全突出显示,但我的过期元素会随机突出显示。

任何帮助表示赞赏


更新:

基于Robert提供的解决方案我加了一点修改。通过在工具提示格式化程序事件上引入悬停状态的激活,mouseOver 行为变得多余,我将其删除。

解决方案:jsFiddle

【问题讨论】:

  • 史蒂文,检查我的答案...

标签: javascript jquery highcharts


【解决方案1】:

只是一点点改变:

tooltip: {
            formatter: function() {
                for(var i=0; i<5; i++)
                    {
                        this.series.data[i].setState('hover');
                    }


                return '<b>'+ this.x +'</b><br/>'+
                    this.series.name +': '+ this.y +'<br/>'+
                    'Total: '+ this.point.stackTotal;
            }

工作小提琴:http://jsfiddle.net/robertrozas/9W8h4/

【讨论】:

    【解决方案2】:

    你需要为每个系列添加事件请看这里

    或在这里:http://jsfiddle.net/uST2P/

    $(function () {
    
        var myCustomEvent = {
            mouseOver: function () {
                overSeriesIndex = this.index;
                for (var i = 0; i < this.data.length; i++) {
    
                    this.data[i].setState('hover');
                }
    
            },
            mouseOut: function () {
    
                for (var i = 0; i < this.data.length; i++) {
                    this.data[i].setState('');
                }
            }
        };
    
        $('#container').highcharts({
            chart: {
                type: 'column',
                width: 585
            },
            title: {
                text: 'Stacked column chart'
            },
            xAxis: {
                categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Total fruit consumption'
                },
                stackLabels: {
                    enabled: true,
                    style: {
                        fontWeight: 'bold',
                        color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                    }
                }
            },
            legend: {
                align: 'right',
                x: -70,
                verticalAlign: 'top',
                y: 20,
                floating: true,
                backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white',
                borderColor: '#CCC',
                borderWidth: 1,
                shadow: false
            },
            tooltip: {
                formatter: function () {
                    return '<b>' + this.x + '</b><br/>' + this.series.name + ': ' + this.y + '<br/>' +
                        'Total: ' + this.point.stackTotal;
                }
            },
            plotOptions: {
                column: {
                    pointPadding: 0,
                    groupPadding: 0,
                    borderColor: 'white',
                    borderWidth: 1,
                    stacking: 'normal',
                    dataLabels: {
                        enabled: true,
                        color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
                        style: {
                            textShadow: '0 0 3px black, 0 0 3px black'
                        }
                    }
                },
                series: {
                    pointWidth: 100
                }
            },
            series: [{
                events: myCustomEvent,
                name: 'John',
                data: [5, 3, 4, 7, 2]
            }, {
    
                events: myCustomEvent,
                name: 'Jane',
                data: [2, 2, 3, 2, 1]
            }, {
                events: myCustomEvent,
                name: 'Joe',
                data: [3, 4, 4, 2, 5]
            }]
        });
    });
    

    【讨论】:

    • 感谢 Sylwester,但我知道我需要将它们应用于所有系列。问题是当从一个系列的一个块移动到下一个块时,突出显示的状态没有保持,您的解决方案没有解决这个问题。
    • @Steven 你找到解决方案了吗?我已经看到了一些使用系列 ID 的选项。
    • @Jyothu 事实上,正如问题编辑中提到的那样,我做到了。这是一个工作示例。 jsfiddle.net/9W8h4/3
    猜你喜欢
    • 2018-01-07
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多