如何在highcharts的click事件中获取被点击图表的值

irlmq6kh  于 2022-11-10  发布在  Highcharts
关注(0)|答案(1)|浏览(185)

如何在highcharts的click事件中获取被点击图表的值

"series": [
        {
            "name": "Requests",
            "colorByPoint": true,
            "data": [
                {
                    "name": "Pending",
                    "y": 62,
                    "drilldown": "Pending"
                },
                {
                    "name": "Completed",
                    "y": 20,
                    "drilldown": "Completed"
                },
                {
                    "name": "Rejected",
                    "y": 18,
                    "drilldown": "Rejected"
                },
            ]
        }
    ],
    "drilldown": {
        "series": [
            {
                "name": "Requests",
                "id": "Pending",
                "data": [
                    [
                        "New Version",
                        21
                    ],
                    [
                        "HotFixes",
                        13
                    ],
                        ],
                  point: {
                        events: {
                                click: function() {
                                        alert("here we are");
                                }
                        }
                        }

            },

这里假设我点击了“新版本”,那么我如何在点击事件中获取“新版本”被点击的值呢?

mlnl4t2r

mlnl4t2r1#

您需要将函数粘贴到plotOptions中不是series中,而是主配置中

plotOptions: {
        series: {
            cursor: 'pointer',
            point: {
                events: {
                    click: function () {
                        alert('Category: ' + this.category + ', value: ' + this.y);
                    }
                }
            }
        }
    },

这会在单击时提醒给定类别,并加上y值。
您可以在Highcharts API中找到更多信息。此API还链接到this JSFiddle,显示如何使用警报功能。
总的来说,您的代码应该是这样的(即使您给我们的不是完整的代码)。您可能需要对alert函数做一点修改,以准确地显示您想要的内容。

plotOptions: {
        series: {
            cursor: 'pointer',
            point: {
                events: {
                    click: function () {
                        alert('Category: ' + this.category + ', value: ' + this.y);
                    }
                }
            }
        }
    },
 "series": [
    {
        "name": "Requests",
        "colorByPoint": true,
        "data": [
            {
                "name": "Pending",
                "y": 62,
                "drilldown": "Pending"
            },
            {
                "name": "Completed",
                "y": 20,
                "drilldown": "Completed"
            },
            {
                "name": "Rejected",
                "y": 18,
                "drilldown": "Rejected"
            },
        ]
    }
],
"drilldown": {
    "series": [
        {
            "name": "Requests",
            "id": "Pending",
            "data": [
                [
                    "New Version",
                    21
                ],
                [
                    "HotFixes",
                    13
                ],
                    ],
        },

相关问题