javascript - 无法在 haxis 上显示刻度

标签 javascript angularjs charts google-visualization

我试图在轴上显示刻度值。下面是动态获取数据并生成柱形图的代码。 以下代码的问题是未显示 h 轴 dynamicTicks,在 h 轴上它显示 f: cData 中 {"v": i, "f": hAxisValue} 的属性中给出的值。在演示示例中也是如此 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();    
}]);

最佳答案

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

            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;

关于javascript - 无法在 haxis 上显示刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46118381/

相关文章:

javascript - 单击提交按钮后jquery显示警报

javascript - 如何将javascript变量传递给rails部分 View

angularjs - ngModel 用于隔离指令定义

javascript - Chart Js 更改折线图 x 轴上的标签方向

javascript - 自定义谷歌图表中的轴标签

javascript - 从条形图中删除 0 值

javascript - 尝试发出 WebSocket 请求时,在 aync 任务中 HttpContext.Current.Session 为 null

javascript - 如何在 Phaser 3 中将一组对象移动相同的距离?

AngularJS 验证 ngModel 未定义

AngularJS:防止在子元素上触发 'mouseenter' 事件