2015-11-16 55 views
3

我正在玩餅圖angular-charts.js。我已經這樣做了,以便我可以設置數據和標籤。無論何時添加或從「餡餅」項目列表中移除某個觀察者函數。如何在角度chart.js中設置餅圖顏色

識別標籤和數據,但不識別顏色。我嘗試了幾個不同的拼寫。

app.controller("PieCtrl", function ($scope, $timeout, pieItems) { 
    $scope.labels = pieItems.labelsItems(); 
    $scope.data = pieItems.data(); 


    function watcherFunction(newData) { 
     $scope.labels = pieItems.labelsItems(); //just an array of strings. 
     $scope.data = pieItems.data(); //just an array of number values 

     $scope.colours = ["rgba(224, 108, 112, 1)", 
      "rgba(224, 108, 112, 1)", 
      "rgba(224, 108, 112, 1)"] //not working 

     $scope.colors = ["rgba(224, 108, 112, 1)", 
      "rgba(224, 108, 112, 1)", 
      "rgba(224, 108, 112, 1)"] //also not working 

    } 

    $scope.$watch(pieItems.list, watcherFunction, true); 
    $scope.$watch(pieItems.getTitle, watcherFunction, true); 

}); 

它似乎爲切片產生隨機顏色。我想重寫這個。肯定有可能做到這一點?

+0

你在哪裏設置圖表默認設置?我們可以看到這些代碼嗎? – Brant

+0

您不需要觀看'colors'或'colours'數組。所以,它應該在'watcherFunction'之外聲明'color array'。 –

回答

1

您的JS:

$scope.colours = ["rgba(224, 108, 112, 1)", 
      "rgba(224, 108, 112, 1)", 
      "rgba(224, 108, 112, 1)"] 

您的指示標記:

<canvas id="pie" class="chart chart-pie" chart-colours="colours"></canvas> 

文檔說,你可以通過設置陣列覆蓋默認的顏色:

Chart.defaults.global.colours 
+0

不幸的是,這似乎並沒有爲我工作:(它仍然產生隨機顏色 –

+2

對不起。是的,它確實工作:)我有一些錯誤的代碼。謝謝! –

+1

在我的情況下,我需要設置圖表顏色。我正在使用版本1.1.1 – Evan

相關問題