首页 文章

带有Angularjs的amCharts在图表的值字段中显示未定义的对象

提问于
浏览
1

我是angularjs和amcharts的新手 . 我想要做的是我从控制器(asp.net核心)发送json中的一些数据 . 数据被发送到angularjs控制器,然后假设使用amCharts创建基本图表 .

在图表的类别字段中,我将编写“未定义”而不是国家/地区的名称 . 代码似乎运行正常,但我只是找不到错误 . 以下是我的代码 .

The following is the class and controller from which i am sending the data written in asp.net core.

public class tempCountry
    {
        public string Country { get; set; }
        public int Visits { get; set; }

    }

    [HttpGet]
    public IActionResult Charts_Json()
    {
        List<tempCountry> tempCountry = new List<chartController.tempCountry>();

        tempCountry tObj = new tempCountry();
        tObj.Country = "Pakistan";
        tObj.Visits = 500;

        tempCountry.Add(tObj);

        tObj = new tempCountry();
        tObj.Country = "Japan";
        tObj.Visits = 1000;

        tempCountry.Add(tObj);

        tObj = new tempCountry();
        tObj.Country = "India";
        tObj.Visits = 3000;

        tempCountry.Add(tObj);

        tObj = new tempCountry();
        tObj.Country = "Austrailia";
        tObj.Visits = 4000;

        tempCountry.Add(tObj);

        return Json(tempCountry);


    }

The following is my Angularjs controller.

var myApp = angular.module(“main”,[]);

myApp.controller(“myController”,function($ scope,$ http){

$http({
    method: "GET",
    url: "/chart/charts_json"
}).then(function mySuccess(response) {


    chartdata = response.data;
    var chart = new AmCharts.AmSerialChart();
    chart.dataProvider = response.data;
    chart.categoryFeild = "Country";
    var graph = new AmCharts.AmGraph();
    graph.valueField = "Visits";
    chart.addGraph(graph);
    chart.write('chartdiv');
});

});

The following the view code (MVC asp.net core).

@section scripts {
    <script src="~/js/amcharts/amcharts.js"></script>
    <script src="~/js/amcharts/serial.js"></script>
    <script src="~/lib/angular/angular.js"></script>
    <script src="~/js/mainApp.js"></script>
}
<p>this is the chart view page</p>
<body>
    <div ng-app="main">
        <div ng-controller="myController">
        </div>
    </div>
    <div id="chartdiv" style="width:640px; height: 480px;">
    </div>
</body>

以下是我得到的输出截图enter image description here

1 回答

相关问题