在函数内调用Highcharts

时间:2014-03-12 17:59:11

标签: javascript html highcharts

我正在尝试调用一个包含构建Highcharts图表的代码的函数,但是我被抛出错误。

TypeError: $(...).highcharts is not a function    

data: (function() {

我确保在尝试构建图表之前调用highcharts.js。

如果我删除了这个函数并且正常地将它放在我的代码中,那么图表构建正常但是我想要的数据没有时间被提取,因此没有加载到图表中。因此,我使用setTimeout()等待在加载图表之前获取数据,但现在图表不会加载。

webiopi().callMacro("getTempHist", [], arrayTemp);
    function arrayTemp(macro, args, data) {
        testlist = data.split(" ");

        $(document).ready(function() {
            Highcharts.setOptions({
            global: {
                useUTC: false
            }
            });

            var chart;
            $('#temptracker').highcharts({
            chart: {
                type: 'line',
                animation: Highcharts.svg, // don't animate in old IE
                marginRight: 10,
                events: {
                load: function() {

                    // set up the updating of the chart each second
                    var series = this.series[0];
                    setInterval(function() {
                    var x = (new Date()).getTime(), // current time
                        y = Number(temp0);
                    series.addPoint([x, y], true, true);
                    }, 60000);
                }
                }
            },
            title: {
                text: 'Live Temperature'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 100
            },
            yAxis: {
                title: {
                text: 'Temperature'
                },
                plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
                }],
                min: -10,
                max: 45,
                startOnTick: false
            },
            tooltip: {
                formatter: function() {
                    return '<b>'+ this.series.name +'</b><br/>'+
                    Highcharts.dateFormat('%d-%m-%Y %H:%M', this.x) +'<br/>'+ 
                    '<b>' + Highcharts.numberFormat(this.y, 2) + '°C' + '</b>';
                }
            },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: 'Temperature',
                data: (function() {
                // generate an array of random data
                var data = [],
                    time = (new Date()).getTime(),
                    i;

                for (i = 0; i <= 19; i++) {
                    data.push({
                    x: time + ((i-19) * 1000),
                    y: Number(testlist[i])
                    });
                }
                return data;
                })()
            }]
            });
        });
    }

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

您可以删除创建数据数组的内联代码,并在创建图表之前定义另一个包含数据的变量。像这样:

webiopi().callMacro("getTempHist", [], arrayTemp);
function arrayTemp(macro, args, data) {
    testlist = data.split(" ");
    // generate an array of random data
    var data = [],
        time = (new Date()).getTime(),
        i;

    for (i = 0; i <= 19; i++) {
        data.push({
            x: time + ((i-19) * 1000),
            y: Number(testlist[i])
        });
    }

    $(document).ready(function() {
        Highcharts.setOptions({
        global: {
            useUTC: false
        }
        });

        var chart;
        $('#temptracker').highcharts({
        chart: {
            type: 'line',
            animation: Highcharts.svg, // don't animate in old IE
            marginRight: 10,
            events: {
            load: function() {

                // set up the updating of the chart each second
                var series = this.series[0];
                setInterval(function() {
                var x = (new Date()).getTime(), // current time
                    y = Number(temp0);
                series.addPoint([x, y], true, true);
                }, 60000);
            }
            }
        },
        title: {
            text: 'Live Temperature'
        },
        xAxis: {
            type: 'datetime',
            tickPixelInterval: 100
        },
        yAxis: {
            title: {
            text: 'Temperature'
            },
            plotLines: [{
            value: 0,
            width: 1,
            color: '#808080'
            }],
            min: -10,
            max: 45,
            startOnTick: false
        },
        tooltip: {
            formatter: function() {
                return '<b>'+ this.series.name +'</b><br/>'+
                Highcharts.dateFormat('%d-%m-%Y %H:%M', this.x) +'<br/>'+ 
                '<b>' + Highcharts.numberFormat(this.y, 2) + '°C' + '</b>';
            }
        },
        legend: {
            enabled: false
        },
        exporting: {
            enabled: false
        },
        series: [{
            name: 'Temperature',
            data: data
        }]
        });
    });
}

如果您仍有错误,请确保您拥有3.x +版本的Highcharts。