Array non nel formato corretto in Javascript per l'utilizzo da HighChart
-
25-10-2019 - |
Domanda
Ciao io sto passando un oggetto List dal mio controller di Javascript in cui viene rimesso in un array e usato come fonte di dati per HighChart grafico a barre.
Nel mio codice qui sotto, se io commento di nuovo nei dati grezzi funziona. Se uso la mia variabile array chiamato 'risultato' non ottengo niente
Se io passo in Javascript con debugger e risultato in uscita a finestra immediata sembra che:
? Risultato [0] {...} [0]: "2" [1]: {...}
? Risultato [0] [1] {...} [0]: 0 [1]: 0 [2]: 0 [3]: 0 [4]: 0 [5]: 0 [6]: 1 [7]: 0 [8]: 0 [9]: 0 [10]: 0 [11]: 0
Ecco il codice:
function CreateChart3(surl) {
// Code block for fetching Array as jsonResult (js)
var url = $("#AbsolutePath").val() + "Complaint.mvc/GetChartData_MonthlyByType/" + surl;
var chart;
$.getJSON(url, null, function(data) {
var result = [];
jQuery.each(data, function(i, val) {
result[i] = [val.Type, val.Count];
});
// var seriesData = [];
// for (var i = 0; i < result.length; i++) {
// seriesData.push({ data: result[i][1] ,name: result[i][0]});
// }
debugger;
chart = new Highcharts.Chart({
chart: {
renderTo: 'chart3',
defaultSeriesType: 'column',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: 'Stacked Bar Monthly By Type'
},
yAxis: {
min: 0,
title: {
text: 'Total No Of Complaints'
},
xAxis: {
categories: ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'],
title: 'Month'
},
stackLabels: {
enabled: true,
style: {
fontWeight: 'bold',
color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
}
}
},
legend: {
backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
borderColor: '#CCC',
borderWidth: 1,
shadow: false
},
tooltip: {
formatter: function() {
return '<b>' + this.x + '</b><br/>' +
this.series.name + ': ' + this.y + '<br/>' +
'Total: ' + this.point.stackTotal;
}
},
plotOptions: {
column: {
stacking: 'normal',
dataLabels: {
enabled: true,
color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
}
}
},
// series:
// [{
// name: "2",
// data: [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
// }, {
// name: "3",
// data: [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 3, 0]
// }]
series: [{
data: result
}]
});
});
};
ESTRATTO DI CONTROLLO CODICE se aiuta ......
var qry = from i in _db.Complaints
where i.Site.SiteDescription.Contains(searchTextSite)
&& (i.Raised >= startDate && i.Raised <= endDate)
group i by i.ComplaintNatureTypeId.ToString()
into grp
select new
{
Type = grp.Key,
Count = new int[] {
grp.Where(c => c.Raised.Month == 1).Count(),
grp.Where(c => c.Raised.Month == 2).Count(),
grp.Where(c => c.Raised.Month == 3).Count(),
grp.Where(c => c.Raised.Month == 4).Count(),
grp.Where(c => c.Raised.Month == 5).Count(),
grp.Where(c => c.Raised.Month == 6).Count(),
grp.Where(c => c.Raised.Month == 7).Count(),
grp.Where(c => c.Raised.Month == 8).Count(),
grp.Where(c => c.Raised.Month == 9).Count() ,
grp.Where(c => c.Raised.Month == 10).Count() ,
grp.Where(c => c.Raised.Month == 11).Count(),
grp.Where(c => c.Raised.Month == 12).Count() }
};
return Json(qry.ToArray(), JsonRequestBehavior.AllowGet);
Soluzione
Per quanto mi ricordo di proprietà data
di series
è matrice di
- valori y
- coppia di valori x-y
- punto oggetti
Mi pare di capire ogni elemento dell'array è array in cui l'elemento [0] è name
e l'elemento [1] è array di valori y. In modo da avere un determinato numero di serie. È necessario convertire manualmente voi risultato in array di oggetti { name: ..., data: ...}
e passarlo al costruttore Highcharts. Uso ciclo for
:
var seriesData = [];
for (var i = 0; i < result.length; i++){
seriesData.push({ name: result[i][0], data: result[i][1] });
}
Dati ??e serie uso come:
series: seriesData
Altri suggerimenti
Guardando il debugger uscita il JSON non è quello che ci si aspetta.
Difficile da dire per la definitiva ma stai tornando l'oggetto corretto dal controllore? Sembra che tu stai tornando qry
quando si dovrebbe utilizzare grp
.