highcharts使用addSeries动态添加系列

时间:2012-12-28 19:05:39

标签: javascript dynamic highcharts

我正在创建一个动态创建的图表,如果没有一个系列,则会添加一个系列,如果有一个添加一个点。我得到一个未捕获的TypeError:无法调用未定义的方法'addSeries'。我环顾四周,无法找到为什么它说方法未定义。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script type="text/javascript" src="highcharts.js"></script>
$(document).ready(function () {

    var chart1 = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'column',
            events: {
                load: requestData
            }
        },
        title: {
            text: 'Survey Chart'

        },
        xAxis: {
            categories: [],
            title: {
                text: 'Question Number'
            }

        },
        yAxis: {
            title: {
                text: 'Total Answered'
            }
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            verticalAlign: 'top',
            x: 100,
            y: 70,
            floating: true
        },
        series: []
    });

文档就绪功能占用整个脚本我有以下功能

    function requestData() {
    ajaxCall(chartCreate, createSeries, "services/Survey.svc/DoWork", "{}");
    chart1.redraw();
};


function chartCreate(point) {
    var temp;
    temp = $.parseJSON(point.d);
    $.each(temp, function (key, p) {
        var seriesObj;
        seriesObj = seriesExists(p.mcAnswer);
        if (seriesObj.status == false) {
            chart1.addSeries({name: '' + p.mcAnswer + '', data: [] });
            chart1.series[seriesObj.count].addPoint(p.total, false);
        } else {
            chart1.series[seriesObj.count].addPoint(p.total, false);
        }
    });
};

//loops through all the series to see if the series exists.
//if true returns index and true if not just returns false
function seriesExists(name) {
    var ct = 0;
    //var len = chart1.series.length;
    var len = 0;
    if (len > 0) {
        $.each(chart1.series, function (count, curSeries) {
            if (curSeries.name == name) {
                return { 'count': count, 'status': true };
            }
            ct = count;
        });
    }
    return { 'count': ct, 'status': false };
};    function createSeries() {
    alert("error");
};

//$.ajaxCall({successFun: function, errorFun: function, source: "", data: {}});
function ajaxCall(myFunSuccess, myFunError, url, data) {
    //chart1 = chartTemp;
    $.ajax({
        type: "POST",
        async: false,
        url: url,
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: myFunSuccess,
        error: myFunError
    });
    //return chart1;
};

当我遇到我遇到问题的chartCreate时,我可以使用ajax函数调用。

1 个答案:

答案 0 :(得分:1)

问题在于您尝试在chart1获取图表参考之前添加系列。这就是chart1没有addSeries方法的原因。

您可以看到此问题here

要解决此问题,您可以在致电chart1之前手动将图表引用设为requestData 如下所示。

load: function() {
    chart1 = this; // `this` is the reference to the chart
    requestData();
}