无法在haxis上显示滴答声

时间:2017-09-08 14:07:11

标签: javascript angularjs charts google-visualization

我正试图在haxis上显示刻度值。下面是动态获取数据并生成柱形图的代码。 问题是下面的代码没有显示h轴dynamicTicks,在h轴上它显示了cData中{:v“:i,”f“:hAxisValue}的f:property中给出的值。同样在演示示例https://plnkr.co/edit/G5wUQjVZ0512RvqltIqm?p=preview中工作,但是当在我的本地应用程序中应用时,它不会在haxis上显示刻度值。有什么建议吗?

app.controller('myController', ['$scope', '$uibModal', 'MyService', function ($scope, $uibModal, MyService) {
    $scope.chart = {};
    $scope.chart.type = "ColumnChart";
    $scope.chart.displayed = false;
    var dynamicTicks = [];
    $scope.chart.options = {
         focusTarget: 'category',
        "fill": 20,
        "displayExactValues": true,
        "isStacked": 'true',
         tooltip: {text: 'value',textStyle: {fontName: '"Arial"'}},
        hAxis: {
            titleTextStyle: {bold: 'true'},
            slantedText: false,
            ticks: dynamicTicks
        },
      };

    $scope.chart.view = {
        columns: [0, 1, 2, 3]
    };
     $scope.chart.data = {
        "cols": [
            {id: "label", label: "Label", type: "string"},
            {id: "count", label: "Count", type: "number"},
            {id: "pizza", label: "Pizza", type: "number"},
            {id: "softDrink", label: "SoftDrink", type: "number"},
        ]
    };

    $scope.loadMyChart = function () {
        MyService.getChartData().then(
            function (response) {
                $scope.myResponse= response;
                //check for error
                var count = 0;
                var pizza = 0;
                var softDrink = 0;
                var myRows = [];
                $scope.chart.data.rows = {};
                  var i = 0; var cData=[];
                angular.forEach($scope.myResponse, function (value, key) {
                    count = value.myData.count;
                    pizza = value.myData.pizza;
                    softDrink = value.myData.softDrnk;
                    hAxisValue = value.myData.title;

                   cData = {
                        "c": [{"v": i, "f": hAxisValue}, {"v": passsed},
                            {"v": failed},
                            {"v": notExecute}, {"v": key}]
                    };
                     myRows.push(cData); i++;
           });
                alert("cData.length " + i);
                //assign the haxis ticks values
                 for (var j = 0; j < i; j++) {
                    alert("in for" + j + "Series" + (j+1)); //This alert is being executed..
                    dynamicTicks.push({
                        v: j, /*cData[j].c[0].v - when this is used, it is showing the error angular.min.js:sourcemap:119 TypeError: Cannot read property 'c' of undefined and chart is not displayed*/
                        f: 'Series ' + (j + 1)
                    });
                }
               $scope.chart.data.rows = myRows;
           },  
    }
         $scope.loadMyChart();    
}]);

1 个答案:

答案 0 :(得分:1)

需要在构建刻度之后为选项分配刻度......

            for (var j = 0; j < i; j++) {
                alert("in for" + j + "Series" + (j+1)); //This alert is being executed..
                dynamicTicks.push({
                    v: j, /*cData[j].c[0].v - when this is used, it is showing the error angular.min.js:sourcemap:119 TypeError: Cannot read property 'c' of undefined and chart is not displayed*/
                    f: 'Series ' + (j + 1)
                });
            }
            $scope.chart.options.hAxis.ticks = dynamicTicks;