如何在高图表饼图中显示单击哪个切片的名称

时间:2015-11-19 09:02:43

标签: javascript highcharts pie-chart

我有一个饼图(高图)来显示部门中各个员工所做的工作。一切都很好。现在我需要显示单击向下钻取图表时单击哪个切片的名称,以便在下载PDF报告后我可以知道向下钻取的来源。示例代码如下:

$(function () {

    $('#container').highcharts({
        chart: {
            type: 'pie'
        },
        title: {
            text: 'Employee Activities'
        },
        subtitle: {
            text: 'Click the slices to view drilldown.'
        },
        plotOptions: {
            series: {
                dataLabels: {
                    enabled: true,
                    format: '{point.name}: {point.y:.1f}%'
                }
            }
        },

        tooltip: {
            headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
            pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
        },
        series: [{
            name: 'Work Load',
            colorByPoint: true,
            data: [{
                name: 'Mr.A',
                y: 56.33,
                drilldown: 'Mr.A'
            }, {
                name: 'Mr.B',
                y: 24.03,
                drilldown: 'Mr.B'
            }, {
                name: 'Mr.C',
                y: 10.38,
                drilldown: 'Mr.C'
            }, {
                name: 'Ms.D',
                y: 4.77,
                drilldown: 'Ms.D'
            }, {
                name: 'Ms.F',
                y: 0.91,
                drilldown: 'Ms.F'
            }, {
                name: 'Mr.Z',
                y: 0.2,
                drilldown: null
            }]
        }],
        drilldown: {
            series: [{
                name: 'Mr.A',
                id: 'Mr.A',
                data: [
                    ['W1', 24.13],
                    ['W2', 17.2],
                    ['W3', 8.11],
                    ['W4', 5.33],
                    ['W5', 1.06],
                    ['W6', 0.5]
                ]
            }, {
                name: 'Mr.B',
                id: 'Mr.B',
                data: [
                    ['W1', 5],
                    ['W2', 4.32],
                    ['W3', 3.68],
                    ['W4', 2.96],
                    ['W5', 2.53],
                    ['W6', 1.45]
                ]
            }, {
                name: 'Mr.C',
                id: 'Mr.C',
                data: [
                    ['W1', 2.76],
                    ['W2', 2.32],
                    ['W3', 2.31],
                    ['W4', 1.27]
                ]
            }, {
                name: 'Ms.D',
                id: 'Ms.D',
                data: [
                    ['W1', 2.56],
                    ['W2', 0.77],
                    ['W3', 0.42],
                    ['W4', 0.3],
                    ['W5', 0.29],
                    ['W6', 0.26],
                    ['W7', 0.17]
                ]
            }, {
                name: 'Ms.F',
                id: 'Ms.F',
                data: [
                    ['W7', 0.34],
                    ['W9', 0.24],
                    ['W10', 0.17],
                    ['W11', 0.16]
                ]
            }]
        }
    });
});

现在,如果我下载向下钻取图表,则PDF不会显示下载切片的名称。

请帮我显示PDF中下载的向下钻取的名称。

3 个答案:

答案 0 :(得分:2)

您可以在图表中设置标题 - &gt;事件 - &gt;钻取/钻取事件。

events: {
    drilldown: function(e) {

        this.setTitle({
            text: e.seriesOptions.name
        });
    },
    drillup: function(e) {
        this.setTitle({
            text: 'Employee Activities'
        });
    }
}

可以找到here

的工作小提琴

所以你的最终图表节点将如下所示。

chart: {
    type: 'pie',
    events: {
        drilldown: function(e) {
            this.setTitle({
                text: e.seriesOptions.name
            });
        },
        drillup: function(e) {
            this.setTitle({
                text: 'Employee Activities'
            });
        }
    },


}

答案 1 :(得分:0)

尝试添加代码

下面的代码
 exporting: {
        filename: 'custom-file-name'
    }

现在根据您的要求设置文件名。

答案 2 :(得分:0)

据我了解,您在查找点击钻取的名称时遇到问题。 您可以在此处JSFiddle(以下代码)进行操作。然后您可以在下载中使用该名称。

这就是 plotOptions 的样子:

plotOptions: {
            series: {
                dataLabels: {
                    enabled: true,
                    format: '{point.name}: {point.y:.1f}%'
                },
                cursor: 'pointer',
                point: {
                    events: {
                        click: function () {
                            //Clicked drilldown name will be alerted here
                            //You can use it in naming PDF
                            alert(this.name);
                        }
                    }
                }
            }
        },

...这里有完整的代码:

$(function () {

    $('#container').highcharts({
        chart: {
            type: 'pie'
        },
        title: {
            text: 'Employee Activities'
        },
        subtitle: {
            text: 'Click the slices to view drilldown.'
        },
        plotOptions: {
            series: {
                dataLabels: {
                    enabled: true,
                    format: '{point.name}: {point.y:.1f}%'
                },
                cursor: 'pointer',
                point: {
                    events: {
                        click: function () {
                            //Clicked drilldown name will be alerted here
                            //You can use it in naming PDF
                            alert(this.name);
                        }
                    }
                }
            }
        },

        tooltip: {
            headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
            pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
        },
        series: [{
            name: 'Work Load',
            colorByPoint: true,
            data: [{
                name: 'Mr.A',
                y: 56.33,
                drilldown: 'Mr.A'
            }, {
                name: 'Mr.B',
                y: 24.03,
                drilldown: 'Mr.B'
            }, {
                name: 'Mr.C',
                y: 10.38,
                drilldown: 'Mr.C'
            }, {
                name: 'Ms.D',
                y: 4.77,
                drilldown: 'Ms.D'
            }, {
                name: 'Ms.F',
                y: 0.91,
                drilldown: 'Ms.F'
            }, {
                name: 'Mr.Z',
                y: 0.2,
                drilldown: null
            }]
        }],
        drilldown: {
            series: [{
                name: 'Mr.A',
                id: 'Mr.A',
                data: [
                    ['W1', 24.13],
                    ['W2', 17.2],
                    ['W3', 8.11],
                    ['W4', 5.33],
                    ['W5', 1.06],
                    ['W6', 0.5]
                ]
            }, {
                name: 'Mr.B',
                id: 'Mr.B',
                data: [
                    ['W1', 5],
                    ['W2', 4.32],
                    ['W3', 3.68],
                    ['W4', 2.96],
                    ['W5', 2.53],
                    ['W6', 1.45]
                ]
            }, {
                name: 'Mr.C',
                id: 'Mr.C',
                data: [
                    ['W1', 2.76],
                    ['W2', 2.32],
                    ['W3', 2.31],
                    ['W4', 1.27]
                ]
            }, {
                name: 'Ms.D',
                id: 'Ms.D',
                data: [
                    ['W1', 2.56],
                    ['W2', 0.77],
                    ['W3', 0.42],
                    ['W4', 0.3],
                    ['W5', 0.29],
                    ['W6', 0.26],
                    ['W7', 0.17]
                ]
            }, {
                name: 'Ms.F',
                id: 'Ms.F',
                data: [
                    ['W7', 0.34],
                    ['W9', 0.24],
                    ['W10', 0.17],
                    ['W11', 0.16]
                ]
            }]
        }
    });
});
相关问题