javascript - 如何在 Highcharts 中使 HTML 表格中的每一行成为饼图?

标签 javascript html highcharts pie-chart

例如我的表是这样的:

            agree neutral disagree
    q1      10     4       4
    q2      16     1       1
    q3      18     0       0
    q4      15     2       1
    q5      13     5       0

我想将 q1 做成一个饼图,以同意、中立和不同意作为不同的部门,对于 q2、q3 等也是如此。

    <script type="text/javascript">

         $(function () {
           var chart;
        $('#container').highcharts({
            data: {
                table: document.getElementById('datatable')
            },
            chart: {
                type: 'column'
            },
            title: {
                text: ''
            },
            yAxis: {
                allowDecimals: false,
                title: {
                    text: 'Persons'
                }
            },
            tooltip: {
                formatter: function() {
                    return '<b>'+ this.series.name +'</b><br/>'+
                        this.point.y +' individuals said '+ this.point.name.toLowerCase();
                }
            },

        });
    });

    /**********************************************

    **********************************************/


         $(function () {
           var chart;
        $('#container2').highcharts({
            data: {
                table: document.getElementById('datatable')
            },
            chart: {
                type: 'pie'
            },
            title: {
                text: ''
            },
            yAxis: {
                allowDecimals: false,
                title: {
                    text: 'Persons'
                }
            },
            tooltip: {
                formatter: function() {
                    return '<b>'+ this.series.name +'</b><br/>'+
                        this.point.y +' individuals said '+ this.point.name.toLowerCase();
                }
            },

        });
    });


    /**********************************************/

    /**********************************************/
    $(function () {
        var chart;
        $(document).ready(function() {
            chart = new Highcharts.Chart({
                chart: {
                    renderTo: 'container3',
                    plotBackgroundColor: null,
                    plotBorderWidth: null,
                    plotShadow: false,
                    margin: [0, 0, 0, 0],
                    spacingTop: 0,
                    spacingBottom: 0,
                    spacingLeft: 0,
                    spacingRight: 0             
                },
                title: {
                    text: ''
                },
                tooltip: {
                    formatter: function() {
                        return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                    }
                },
                legend: {
                    enabled:true
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        size: '50%',
                        dataLabels: {
                            enabled: false
                        }
                    }
                },
                series: 
                [
                    {
                    type: 'pie',
                    name: 'testname1',
                    center: [70, 140],//+130 to the x-axis
                    showInLegend:true,
                    data: [
                        ['Commerce',   33.0],
                        ['Engineering',       32.3],
                        {
                            name: 'Financial Services',
                            y: 18.8,
                            sliced: true,
                            selected: true
                        },
                        ['Logistics, Aviation & Shipping',    5.5],
                        ['Seafood & Marine',     9.2],
                        ['Corporate Services & others',   1.2]
                    ]
                    }
                    ,{
                    type: 'pie',
                    name: 'testname2',
                    center: [200, 140],
                    showInLegend:false,
                    data: [
                        ['Commerce',   33.0],
                        ['Engineering',       32.3],
                        {
                            name: 'Financial Services',
                            y: 18.8,
                            sliced: true,
                            selected: true
                        },
                        ['Logistics, Aviation & Shipping',    5.5],
                        ['Seafood & Marine',     9.2],
                        ['Corporate Services & others',   1.2]
                    ]
                    },
                    {
                    type: 'pie',
                    name: 'testname3',
                    center: [330, 140],
                    showInLegend:false,
                    data: [
                        ['Commerce',   33.0],
                        ['Engineering',       32.3],
                        {
                            name: 'Financial Services',
                            y: 18.8,
                            sliced: true,
                            selected: true
                        },
                        ['Logistics, Aviation & Shipping',    5.5],
                        ['Seafood & Marine',     9.2],
                        ['Corporate Services & others',   1.2]
                    ]
                    },
                    {
                    type: 'pie',
                    name: 'testname4',
                    center: [460, 140],
                    showInLegend:false,
                    data: [
                        ['Commerce',   33.0],
                        ['Engineering',       32.3],
                        {
                            name: 'Financial Services',
                            y: 18.8,
                            sliced: true,
                            selected: true
                        },
                        ['Logistics, Aviation & Shipping',    5.5],
                        ['Seafood & Marine',     9.2],
                        ['Corporate Services & others',   1.2]
                    ]
                    },
                    {
                    type: 'pie',
                    name: 'testname5',
                    center: [590, 140],
                    showInLegend:false,
                    data: [
                        ['Commerce',   33.0],
                        ['Engineering',       32.3],
                        {
                            name: 'Financial Services',
                            y: 18.8,
                            sliced: true,
                            selected: true
                        },
                        ['Logistics, Aviation & Shipping',    5.5],
                        ['Seafood & Marine',     9.2],
                        ['Corporate Services & others',   1.2]
                    ]
                    },
                    {
                    type: 'pie',
                    name: 'testname6',
                    center: [720, 140],
                    showInLegend:false,
                    data: [
                        ['Commerce',   33.0],
                        ['Engineering',       32.3],
                        {
                            name: 'Financial Services',
                            y: 18.8,
                            sliced: true,
                            selected: true
                        },
                        ['Logistics, Aviation & Shipping',    5.5],
                        ['Seafood & Marine',     9.2],
                        ['Corporate Services & others',   1.2]
                    ]
                    },
                    {
                    type: 'pie',
                    name: 'testname7',
                    center: [850, 140],
                    showInLegend:false,
                    data: [
                        ['Commerce',   33.0],
                        ['Engineering',       32.3],
                        {
                            name: 'Financial Services',
                            y: 18.8,
                            sliced: true,
                            selected: true
                        },
                        ['Logistics, Aviation & Shipping',    5.5],
                        ['Seafood & Marine',     9.2],
                        ['Corporate Services & others',   1.2]
                    ]
                    },
                    {
                    type: 'pie',
                    name: 'testname8',
                    center: [980, 140],
                    showInLegend:false,
                    data: [
                        ['Commerce',   33.0],
                        ['Engineering',       32.3],
                        {
                            name: 'Financial Services',
                            y: 18.8,
                            sliced: true,
                            selected: true
                        },
                        ['Logistics, Aviation & Shipping',    5.5],
                        ['Seafood & Marine',     9.2],
                        ['Corporate Services & others',   1.2]
                    ]
                    }

                ]

            },function(chart) {

            $(chart.series[0].data).each(function(i, e) {
                e.legendItem.on('click', function(event) {
                    var legendItem=e.name;

                    event.stopPropagation();

                    $(chart.series).each(function(j,f){
                           $(this.data).each(function(k,z){
                               if(z.name==legendItem)
                               {
                                   if(z.visible)
                                   {
                                       z.setVisible(false);
                                   }
                                   else
                                   {
                                       z.setVisible(true);
                                   }
                               }
                           });
                    });

                });
            });
        });
        });

    });


    </script>

注意:第一个图表成功地从我正在使用的表中提取数据。

另外两个是我试图回答我的问题的尝试。

这是我发布的代码的结果:

http://tinypic.com/r/11uyj3s/8

最佳答案

示例:http://jsfiddle.net/4Kn5t/

您需要为每个图表设置三个额外选项,以告知饼图应使用哪些行:

$('#container2').highcharts({
    data: {
        table: document.getElementById('datatable'),
        switchRowsAndColumns: true, // use rows as points
        startRow: 1, 
        endRow: 2 // use row == 2
    },
    chart: {
        type: 'pie'
    },
    title: {
        text: 'Data extracted from a HTML table in the page'
    },
    yAxis: {
        allowDecimals: false,
        title: {
            text: 'Units'
        }
    },
    tooltip: {
        formatter: function() {
            return '<b>'+ this.series.name +'</b><br/>'+
                this.point.y +' '+ this.point.name.toLowerCase();
        }
    }
});

关于javascript - 如何在 Highcharts 中使 HTML 表格中的每一行成为饼图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24511304/

相关文章:

javascript - Meteor 如何创建邀请系统

Javascript onblur 事件警报框不会消失

javascript - 在 highcharter 中包含导出 csv

javascript - Highcharts - 隐藏绘图标签后面的绘图线

javascript - 主干 el view 的 CSS 属性

javascript - 循环遍历 json 数据以创建下拉列表

javascript - 是否可以将自定义属性/属性注入(inject) native Web 组件?

html - 使祖先 flexbox div 仅占用其具有设置宽度的 child 的宽度

java - Highcharts用0填充散点图数据

javascript - 无法设置 JQuery UI slider 的值