I tried for several days to draw a simple line chart using HighchartsJS via ajax. I tried posting on my forum, but help seems to be slow in progress.
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 next json object. Note that double quotes complete everything.
"{"data":
[
[634420512000000000,100000.0000],
[634421376000000000,100086.0000],
[634422240000000000,100171.0000],
[634423104000000000,100257.0000]
]
}"
Clientside, I call the function above and draw my diagram like this.
$(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);
});
The 'items' in the above call is as follows:
"{"data":
[
[634420512000000000,100000.0000],
[634421376000000000,100086.0000],
[634422240000000000,100171.0000],
[634423104000000000,100257.0000]
]
}"
I am using jsonParser (items.d ') to create the correct json object (removing double quotes).
'obj' now contains my json object and 'item' when checked in firebug now looks like this:
[
[634420512000000000, 100000],
[634421376000000000, 100086],
[634422240000000000, 100171],
[634423104000000000, 100257]
]
, . ?
.
series.data.push(obj.data);
;
series.data = obj.data;