2017-09-08 28 views
1

我試圖在haxis上顯示刻度值。以下是動態獲取數據並生成列圖的代碼。 問題在於下面的代碼沒有顯示h軸dynamicTicks,在h軸上它顯示了cData中{「v」:i,「f」:hAxisValue}的f:屬性中給出的值。演示示例https://plnkr.co/edit/G5wUQjVZ0512RvqltIqm?p=preview中的工作方式也是如此,但是當應用於本地應用程序時,它不會在haxis上顯示刻度值。有什麼建議麼?無法在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

需要蜱分配給選項,結束後生成蜱...

  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; 
+0

+1您的建議,不過我還是不能看到haxis.When蜱值我有警報對於dynamicTicks,它顯示一個警告[對象對象],[對象對象] ... @ WhiteHat – user8401182

+0

對不起,我沒有。我從DB獲取值。幾乎我已經在https://plnkr.co/edit/G5wUQjVZ0512RvqltIqm?p=preview但以靜態方式複製我的代碼。當在dynamicTicks.push中使用cData [j] .c [0] .v時,我得到異常angular.min.js:sourcemap:119 TypeError:無法讀取未定義的屬性'c',因此用上面的值j替換它。@ WhiteHat – user8401182

+1

我注意到上面這個問題中發佈的代碼,第一列爲 - - >'{id:「label」,label:「Label」,type:「string」}' - 要讓'ticks'起作用,第一列必須是''number''類型,'date'等等 - 如[此前的回答](https://stackoverflow.com/a/46100941/5090771) - 'ticks'不適用於離散(「字符串」)軸 – WhiteHat