javascript - 如何将数据表添加到动态生成的 Highcharts 中

标签 javascript jquery highcharts

我想将数据表添加到图表中。

我尝试了此处显示的实现:http://jsfiddle.net/highcharts/z9zXM/

但它对我不起作用。

我怀疑这是因为我实例化 highcharts 的方式。

在上面的示例中,图表是通过实例化 Highcharts 对象生成的。

我的代码:

// data from an ajax call
$.each(data, function(indicator, questions) {

            indicator_type = "";

            $.each(questions, function(question, value) {

                dataChartType =  "column";

                series = [];

                categories = [];

                category_totals = {};

                if(value.programs == null) {
                    return true;
                }

                $.each(value.programs, function(program, body) {

                    total = 0;

                    values = [];

                    $.each(body, function(j, k) {

                        if (categories.indexOf(j) == -1) {

                            categories.push(j);

                            category_totals[j] = 0;

                        }

                        if(k != 0) {
                            values.push(k); 
                        } else {
                            values.push(null);
                        }


                        category_totals[j] += parseInt(k, 10);

                        total += k;


                    });

                    series.push({
                        data: values,
                        total: total,
                        name: program //question
                    });

                }); // eo each program

                var chartDiv = document.createElement('div'); 

                chartDiv.className = "chart";

                $('.charts_wrap').append(chartDiv);

                $(chartDiv).highcharts({
                    events: {
                        load: Highcharts.drawTable
                    },
                    chart: {
                        type: dataChartType
                    },
                    xAxis: {
                        categories: categories
                    },
                    legend: {
                        layout: 'vertical',
                        backgroundColor: '#FFFFFF',
                        align: 'right',
                        verticalAlign: 'top',
                        y: 60,
                        x: -60
                    },
                    tooltip: {
                        formatter: function () {
                            return '<strong>' + this.series.name + '</strong><br/>' + this.x + ': ' + this.y;
                        }
                    },
                    plotOptions: {
                        line: {
                            connectNulls: true
                        },
                        column: {
                            stacking: 'normal',
                            dataLabels: {
                                enabled: false,
                                color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
                                style: {
                                    textShadow: '0 0 3px w'
                                }
                            }
                        }
                    },
                    series: series,
                    title:{ text: indicator },
                    subtitle:{ text: question }
                });


            }); // EO each question

        }); // eo each indicator

最佳答案

像这样实例化 highcharts 时:

$("#container").highcharts({ ...

事件选项需要包含在图表选项中:

$("#container").highcharts({
    chart: {
        type: 'column',
        events: {
           load: Highcharts.drawTable
        },
    },
    ...

关于javascript - 如何将数据表添加到动态生成的 Highcharts 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38774628/

相关文章:

jquery - 如何与 jquery promise ?

javascript - 如何在表格中显示每一行的堆栈条形图?

javascript - 正则表达式匹配列表

javascript - jQuery AJAX 与 PHP 将内容上传到 MYSQL DB

javascript - 无法在 React 中访问嵌套的 JSON 对象

javascript - 使用 Highcharts-ng 不显示图表

javascript - Highcharts 列范围日期图表

javascript - 在 React 中,onChange 和 onInput 有什么区别?

javascript - 将标签 “series:” 从响应服务器 JSON 插入到 Highcharts 中。格式错误的 JSON?

javascript - 未捕获的类型错误 : Cannot read property '0' of undefined javascript error and with highcharts