我在带有Json数据源的ASP.Net MVC Web应用程序中有两个图表。我正在使用amcharts来显示图表。
在第一种情况下有效:
javascript代码:
<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>
dataVal变量值(来自MVC控制器):
工作
在第二种情况下,图表未显示:
javascript代码:
<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();
});
dataVal变量值(来自MVC控制器):
错
有什么区别?为什么我的第二张图表不显示?
感谢您的帮助!
答案 0 :(得分:2)
AmCharts需要dataProvider
中的对象数组。第一个起作用是因为它只是一个对象数组;第二个对象是一个对象,除了数组之外,它还包含多个属性。更改第二张图表以引用dataVal变量("dataProvider": dataVal.Data
)中的数组,它将起作用。