javascript - 如何在用户单击饼图时隐藏其余区域?

标签 javascript jquery html css highcharts

我在图 1 中有用户位置的饼图表示,我已经成功地使该表示正常工作,但我如何才能使其余用户当单击任何特定扇区时隐藏为图 2

图 1:

enter image description here

图 2:

enter image description here

Javascript:

$(function () {
    $('#container').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: 'users location'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                    style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                    }
                }
            }
        },
        series: [{
            type: 'pie',
            name: 'Browser share',
            data: [
                ['Africa',   45.0],
                ['Asia',       26.8],
                {
                    name: 'Australia',
                    y: 12.8,
                    sliced: true,
                    selected: true
                },
                ['Europe',    8.5],
                ['North America',     6.2],
                ['Others',   0.7]
            ]
        }]
    });
});

Fiddle Link

最佳答案

您可以使用 plotOptions.series.point.events.click 函数来告诉图表在单击切片后确切做什么:

series: {
    point: {
        events: {
            click: function () {
                var index = this.x;
                $('.highcharts-series-group g path').toggle();
                $('.highcharts-series-group g path:nth-child(' + (index+1) + ')').toggle();

                $('.highcharts-data-labels path').toggle();
                $('.highcharts-data-labels path:nth-child(' + (index+1) + ')').toggle();

                $('.highcharts-data-labels g').toggle();
                $($('.highcharts-data-labels g').get(index)).toggle();
            }
        }
    }
}

前两个开关用于切片本身。 $('.highcharts-series-group g path')指的是图表中所有的彩色切片,我通过添加改回了刚刚点击的一个用户>:nth-child.

第二对开关用于连接 datalabels 的切片中的线条。第三对用于数据标签

这是 DEMO .

关于javascript - 如何在用户单击饼图时隐藏其余区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30542294/

相关文章:

javascript - 无法使用 Node.js 从 REST 调用获取数据

javascript - Google Analytics 跟踪代码中 _trackEvent() 调用中的这个模糊错误是什么?

javascript - 为什么我在这段 javaScript 代码中得到 NaN?

jquery - 使用单选按钮显示/隐藏内容

asp.net - 使用 jquery 从 UI 调用 WCF 数据服务违反了 MVC 模式

html - 使网站在所有设备和屏幕尺寸上可用并保持一致的最佳实践是什么?

如果列有多个 DIV,JavaScript 函数会查找并更改列的 CSS

javascript - filereader shim,无法让它在 ie9 中工作

javascript - "Paste And Show"函数怎么做

javascript - 表单中是否允许多个 onsubmit javascript 函数?