Highchart无法在Ajax函数angularjs

时间:2018-07-08 13:55:13

标签: angularjs highcharts

Highchart不能从ajax调用中呈现数据。如果我在$ http函数之外使用静态数据,则将显示highchart。高图表未触发。

html:

  <hc-pie-chart title="Browser usage" data="pieData">Placeholder for pie chart</hc-pie-chart>

控制器:

$scope.homeallpcn = function(){
  var url = "homechartall";
  var data = "";
  $http.get(url).then( function(response) {
    $scope.pieData = [{
                    name: "Cancelled",
                    y: response.c
                }, {
                    name: "Closed",
                    y: response.cl,
                    sliced: true,
                    selected: true
                }, {
                    name: "Open",
                    y: response.o
                }, {
                    name: "Rejected",
                    y: response.r
              }]

  });


}
$scope.homeallpcn();

服务:

mainApp.directive('hcPieChart', function () {
            return {
                restrict: 'E',
                template: '<div></div>',
                scope: {
                    title: '@',
                    data: '='
                },
                link: function (scope, element) {
                    Highcharts.chart(element[0], {
                        chart: {
                            type: 'pie'
                        },
                        title: {
                            text: scope.title
                        },
                        plotOptions: {
                            pie: {
                                allowPointSelect: true,
                                cursor: 'pointer',
                                dataLabels: {
                                    enabled: true,
                                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                                }
                            }
                        },
                        series: [{
                            data: scope.data
                        }]
                    });
                }
            };
        })

请给我建议。

1 个答案:

答案 0 :(得分:0)

一些建议:

1-通话后console.log(response)确实显示了数据吗?

2-请记住,pie无法使用字符串数据,因此response.cresponse.oresponse.r必须为整数或浮点数。