I want to push the data from piedatalist which contains two attributes, value and key and I want to pass to jqplot like this [key,value] to render the chart but it doesn’t work.
JavaScript
x
35
35
1
$(document).ready(function () {
2
var json = JSON.parse("[{"pieDataList":
3
[{"value": 100, "key": "non"}],
4
"titre": "fghjklcom", "nbreReponse": 1}, {
5
"pieDataList": [{"value": 100, "key": "non"}],
6
"titre": "fghjklcom", "nbreReponse": 1}]");
7
// $.each(json, function (index,ch) {
8
for (i in json) {
9
var value = [];
10
//get the map
11
for (j = 0; j < json[i].PieDataList.length; j++) {
12
//iterate each item of list
13
value.push([json[i].PieDataList[j].key,
14
json[i].PieDataList[j].value]);
15
}
16
var plot1 = jQuery.jqplot('ch', [value],
17
{
18
seriesDefaults: {
19
// Make this a pie chart.
20
renderer: jQuery.jqplot.PieRenderer,
21
rendererOptions: {
22
// Put data labels on the pie slices.
23
// By default, labels show the percentage of the
24
slice.
25
showDataLabels: true
26
}
27
},
28
legend: {show: true, location: 'e'}
29
})
30
}
31
32
alert(item.PieDataList);
33
console.log("Titre=" + item.Titre);
34
});
35
Advertisement
Answer
The JSON at the start can’t just be pasted into the code. It’s a string, but there are newlines which break strings, and double-quote characters which would end the string also.
You can just paste in the JSON as a variable instead of a string and JavaScript will allow it just fine:
JavaScript
1
7
1
var json = [{"pieDataList":
2
[{"value":100,"key":"non"}],
3
4
"titre":"fghjklcom","nbreReponse":1},{"pieDataList":
5
[{"value":100,"key":"non"}],
6
"titre":"fghjklcom","nbreReponse":1}];
7