【问题标题】:Highchart Data not getting displayed after looping through data from database循环访问数据库中的数据后,Highchart 数据未显示
【发布时间】:2019-05-06 06:24:27
【问题描述】:

我正在尝试从数据库中获取数据并将其动态显示到 highchart 系列数据中。为此,我使用以下代码:

$(function () {
                $.ajax({
                    url: "/WebApi/MIPResource_DistrictWise",
                    cache: false,
                    success: function (data) {
                        var categories ;
                        var i;
                        for (i = 0; i < data.length; i++) {
                            categories += data[i].DISTRICT_NAME;

                        }
                        alert(categories);

                        // var xx = [][][];
                        // var data = "";
                        // var i;
                        // for (i = 0; i < xx.length; i++) {
                        // data += xx[i];
                        // }

                        Highcharts.chart('MIPDistrictWise', {
                            chart: {
                                type: 'bar'
                            },
                            title: {
                                text: 'MIP Resource Requirement <br> (District Wise)'
                            },
                            xAxis: {
                                categories: categories
                            },
                            yAxis: {
                                min: 0,
                                title: {
                                    text: 'Total Resources Required'
                                }
                            },
                            legend: {
                                reversed: true
                            },

                            plotOptions: {
                                series: {
                                    stacking: 'percent',
                                    borderWidth: 1,
                                    groupPadding: 0,//add here
                                    pointPadding: 0//add here

                                },
                                bar: {
                                    allowPointSelect: true,
                                    cursor: 'pointer',
                                    dataLabels: {
                                        enabled: true,
                                        format: '<b>{point.name}</b><br>{point.percentage:.1f} %',
                                        color: 'white'

                                    }
                                }
                            },
                            series: [{
                                name: 'TVET',
                                data: [5, 3, 4, 7, 2, 4, 7, 2]
                            }, {
                                name: 'IGGs',
                                data: [2, 2, 3, 2, 1, 4, 7, 2]
                            }, {
                                name: 'CIF',
                                data: [3, 4, 4, 2, 5, 4, 7, 2]
                            }]
                        });

                    }

                });
            });

在“警报”中,它正确显示了地区的名称,但在它们的开头有一个额外的关键字“未定义”,在控制台面板中也显示以下错误:未捕获的类型错误:无法读取属性“部分/全局” .js' of undefined.我想从数据库中动态显示类别的名称。

【问题讨论】:

    标签: javascript c# jquery


    【解决方案1】:

    你需要检查你的循环

                            for (i = 0; i < data.length; i++) {
                            categories += data[i].DISTRICT_NAME;// Here some of the string output are undefined
    
                        }
    

    所以你可能需要检查

    数据[i].DISTRICT_NAME

    未定义或不在循环内。我附上了如何在下面的javascript中检查未定义的链接。

    JS: How to check if a variable is NOT undefined

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 2023-04-06
      • 2015-11-09
      • 2014-05-22
      • 1970-01-01
      相关资源
      最近更新 更多