Reputation: 21
I have two charts in ASP.Net MVC web application with Json datasource. I'm using amcharts to display the graphs.
In the first case it's working:
The javascript code:
<script type="text/javascript">
$(document).ready(function () {
LoadVisual();
});
function LoadVisualAjax() {
return $.ajax({
type: "Get",
url: '@Url.Action("GetReport5GraphData", "Report")',
});
}
function LoadVisual() {
$.when(LoadVisualAjax()).then((data) => {
DrawChart(data);
}).fail(() => {
alert("Fail to initialize Chart");
});
}
function DrawChart(dataVal) {
console.log(dataVal);
var chart = AmCharts.makeChart("chartdiv",
{
"type": "serial",
"categoryField": "gep",
"startDuration": 1,
"backgroundAlpha": 0.8,
"categoryAxis": {
"autoRotateAngle": 0,
"gridPosition": "start"
},
"trendLines": [],
"graphs": [
{
"balloonColor": "#008000",
"balloonText": "[[title]] of [[category]]:[[value]]",
"fillAlphas": 1,
"fillColors": "#428542",
"id": "AmGraph-1",
"legendColor": "#008000",
"lineColor": "undefined",
"markerType": "triangleUp",
"title": "ok",
"type": "column",
"valueField": "ok"
},
{
"animationPlayed": true,
"balloonText": "[[title]] of [[category]]:[[value]]",
"bulletColor": "#008000",
"customMarker": "",
"fillAlphas": 1,
"id": "AmGraph-2",
"labelText": "",
"markerType": "triangleDown",
"minDistance": 0,
"title": "selejt",
"type": "column",
"valueField": "selejt"
}
],
"guides": [],
"valueAxes": [
{
"id": "ValueAxis-1",
"stackType": "regular",
"title": "Gyártott mennyiség (db)"
}
],
"allLabels": [],
"balloon": {},
"legend": {
"enabled": true,
"useGraphSettings": true
},
"titles": [
{
"id": "Title-1",
"size": 15,
"text": "Előző műszakban gyártott mennyiség"
}
],
"dataProvider": dataVal
});
chart.addListener("rendered");
}
</script>
The dataVal variable value (from MVC controller):
Working
In the second case the chart is not displaying:
The javascript code:
<script type="text/javascript">
function LoadVisualAjax() {
return $.ajax({
type: "Get",
url: '@Url.Action("GetReport3Data", "Report")',
});
}
function LoadVisual() {
$.when(LoadVisualAjax()).then((data) => {
DrawChart(data);
}).fail(() => {
alert("Fail to initialize Chart");
});
}
function DrawChart(dataVal) {
console.log(dataVal);
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"theme": "light",
"marginRight": 40,
"marginLeft": 40,
"autoMarginOffset": 20,
"mouseWheelZoomEnabled": true,
"valueAxes": [{
"id": "ValueAxis-1",
"axisAlpha": 0,
"position": "left",
"ignoreAxisWidth": true,
"title": "Darabszám",
"position": "left",
"autoGridCount": false,
"labelFunction": function (value) {
return Math.round(value);
}
}],
"balloon": {
"borderThickness": 1,
"shadowAlpha": 0,
},
"graphs": [{
"id": "AmGraph-1",
"fillAlphas": 0.8,
"lineAlpha": 0.2,
"hideBulletsCount": 50,
"lineThickness": 2,
"title": "Cím ",
"type": "column",
"useLineColorForBulletBorder": true,
"valueField": "Db",
"balloonText": "[[category]]: <b style='font-size: 130%'>[[value]]</b>",
}],
"valueScrollbar": {
"oppositeAxis": false,
"offset": 50,
"scrollbarHeight": 10
},
"categoryField": "ErrorCode",
"categoryAxis": {
"parseDates": false,
"equalSpacing": true,
"minPeriod": "DD",
"dashLength": 1,
"minorGridEnabled": true,
},
"legend": {
"useGraphSettings": true,
"position": "top"
},
"export": {
"enabled": false,
},
"dataProvider": dataVal
});
}
$(document).ready(function () {
LoadVisual();
});
The dataVal variable value (from MVC controller):
Wrong
What is the difference? Why is my second chart not rendering?
Thanks for your help!
Upvotes: 2
Views: 613
Reputation: 16012
AmCharts expects an array of objects in the dataProvider
. The first one works because it's just an array of objects; the second one is an object containing several properties in addition to an array. Change the second chart to reference the array inside your dataVal variable ("dataProvider": dataVal.Data
) and it will work.
Upvotes: 2