如何在角图js中的每个条上设置单一颜色

时间:2018-01-07 11:12:31

标签: angularjs chart.js angular-chartist.js

这是我的HTML代码:

<canvas id="bar" class="chart chart-bar"
            chart-data="expenseData" chart-labels="labels" chart-series="series" chart-options="chartOptions" colours= "colours">

    </canvas>

这是js代码:

var setGraphDate = function () {
        var xData = [];
        var yData = [];
        $scope.Data.ExpenseRecords.forEach(function (g) {
            yData.push(g.PercentOfCompletion);
            xData.push(g.TspShortName);
        });
        $scope.colours = "rgba(224, 108, 112, 1)";
        $scope.labels = xData;
        $scope.expenseData = yData;
        $scope.series = ['Expense Graph'];
        $scope.chartOptions = {

            legend: {
                display: false
            },

            },
            title: {
                display: true,
                text: 'TSP Burn Rate'
            },

            // Option to show VALUES on chart
            events: false,
            tooltips: {
                enabled: false
            },
            hover: {
                animationDuration: 0
            }
    };

bar chart

每个条形图的颜色都不同,每次重新加载也会发生变化。

我想在所有酒吧都只有蓝色。

2 个答案:

答案 0 :(得分:4)

使用

chart-dataset-override="colors"

<强>样本

&#13;
&#13;
angular.module("app", ["chart.js"]).controller("DoughnutCtrl", function ($scope) {
  $scope.results = {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0, '9': 0, '10': 0};
  $scope.labels = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
  
  $scope.data = [
    [1, 3, 5, 8, 9, 10, 11, 12, 33, 5]
  ];
  $scope.colors = [{ 
    fillColor: 'rgba(59, 89, 152,0.2)',
    strokeColor: 'rgba(59, 89, 152,1)',
    pointColor: 'rgba(59, 89, 152,1)',
    pointStrokeColor: '#fff',
    pointHighlightFill: '#fff',
    pointHighlightStroke: 'rgba(59, 89, 152,0.8)'
  }];
});
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Multi Slot Transclude</title>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-chart.js/1.0.3/angular-chart.min.js"></script>
</head>
<body ng-app="app" ng-controller="DoughnutCtrl">
  <canvas
        class="chart chart-bar"
        chart-data="data"
        chart-labels="labels"
        chart-dataset-override="colors">
      </canvas>
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

我已通过以下方式解决了这个问题:

 $scope.datasetOverride =
        {
            backgroundColor: "#4E4EFF",
            borderColor: "#2E2E99"
        };


<canvas id="bar" class="chart chart-bar"
            chart-data="expenseData" chart-labels="labels" chart-series="series" chart-options="chartOptions" chart-dataset-override="datasetOverride">
</canvas>