如何在highcharts中找不到数据

时间:2015-06-08 10:59:47

标签: javascript highcharts

在highcharts中,如果在json值中找不到数据,我想显示错误信息。

我使用了这段代码:

if(options.series[0].data.length == 0) {
    alert('nodatafound');
} else {
    var chart = new Highcharts.Chart(options);
}

出现此错误:

  

TypeError:options.series [0] .data未定义

来自JSON的数据:

{
    "chart": {
        "renderTo": "container2",
        "type": "column",
        "marginRight": 10,
        "marginBottom‌": 125
    },
    "title": {
        "text": "",
        "x": -20
    },
    "exporting": {
        "enabled": false
    },
    "credits": {
        "enab‌led": false
    },
    "subtitle": {
        "text": "",
        "x": -20
    },
    "xAxis": {
        "labels": {
            "rotation": -90,
            "sty‌le": {
                "fontSize": "10px"
            }
        }
    },
    "yAxis": {
        "title": {
            "text": ""
        },
        "plotLines": [{
            "width": 1,
            "c‌olor": "#808080"
        }]
    },
    "tooltip": {
        "shared": true
    },
    "legend": {
        "align": "right",
        "verticalA‌lign": "top",
        "x": 0,
        "y": 20,
        "borderWidth": 0
    },
    "plotOptions": {
        "column": {
            "stacking": "no‌rmal",
            "dataLabels": {
                "enabled": false,
                "color": "white"
            }
        }
    },
    "series": [{
        "name": "2015-16-Q1"
    }, {
        "name": "2015-16-Q2"
    }, {
        "name": "2015-16-Q3"
    }, {
        "na‌me": "2015-16-Q4"
    }]
}

1 个答案:

答案 0 :(得分:3)

您可以使用no-data module来执行此操作。

参考:http://code.highcharts.com/modules/no-data-to-display.js

示例:http://jsfiddle.net/6o8o03fe/1/