开发者

Highcharts with ajax and json data what am i doing wrong?

I have been trying for days to plot a simple line graph utilising HighchartsJS via ajax. I have tried posting on their forum but help seem to be slow in coming forward.

I am using ASP.NET web forms.

Server Side

<WebMethod(EnableSession:=True)> _
    Public Function getData() As String

        Dim dr As DataRepository = New DataRepository
        Dim data As List(Of ArrayList) = New List(Of ArrayList)

        For Each q In dr.getAllData()
            Dim a As New ArrayList
            Dim d As Date = q.DateTime
            a.Add(d.Ticks)
            a.Add(q.TotalRevenue)
            data.Add(a)
        Next

        Dim ser As Serie = New Serie(data)

        Dim str As String = JsonConvert.SerializeObject(ser)

        Return str

    End Function

This returns the following json object, Please note the double quotes wrapping everything.

"{"data":
    [
        [634420512000000000,100000.0000],
        [634421376000000000,100086.0000],开发者_StackOverflow中文版
        [634422240000000000,100171.0000],
        [634423104000000000,100257.0000]
    ]
}"

Clientside i am calling the above function and rendering my chart like so.

$(document).ready(function () {
    var options = {
        chart: {
            renderTo: 'container',
            defaultSeriesType: 'line'
        },
        title: {},
        xAxis: {
            type: 'datetime'
        },
        yAxis: {},
        series: []
    };

    $.ajax({
        type: "POST",
        dataType: "json",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        url: "_services/ScriptService.asmx/getData",
        success: function (items) {

            var obj = jsonParse(items.d);
            var series = { data: [] };

            $.each(obj, function (itemNo, item) {
                series.data.push(item);
            });

            options.series.push(series);

        },
        cache: false,
        error: function (XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown); }
    });
    chart = new Highcharts.Chart(options);
});

'items' in the above call is as follows;

"{"data":
    [
        [634420512000000000,100000.0000],
        [634421376000000000,100086.0000],
        [634422240000000000,100171.0000],
        [634423104000000000,100257.0000]
    ]
}"

I use 'jsonParser(items.d') to create a correct json object(removing the double quotes).

'obj' now contains my json object and 'item' when inspected in firebug now looks like this;

[
    [634420512000000000, 100000], 
    [634421376000000000, 100086], 
    [634422240000000000, 100171], 
    [634423104000000000, 100257]
]

When my chart renders, the points do not get plotted. What am I doing wrong?

Solved the issue.

Needed to change the line

series.data.push(obj.data);

to;

series.data = obj.data;


Put chart = new Highcharts.Chart(options); inside success event.


Solved the issue.

Needed to change the line

series.data.push(obj.data);

to;

series.data = obj.data;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜