如何在角图表条形图中使用纯色条

时间:2016-11-03 17:39:44

标签: javascript angularjs charts chart.js angular-chart

我使用角度图表(基于chart.js)创建一些条形图,但我无法获得我想要的条形图。我希望这些条纹是这样的纯色:

Desired Result

但我无法弄清楚如何摆脱chart.js默认添加的alpha:

Actual Result

我的HTML看起来像这样:

  <body ng-app="myApp" ng-controller="myController as ctrl">
    <canvas id="outreach" class="chart chart-bar" 
        chart-labels="ctrl.socialChart.labels" 
        chart-data="ctrl.socialChart.data" 
        chart-series="ctrl.socialChart.series"
        chart-colors="ctrl.socialChart.colors"
        chart-options="ctrl.socialChart.options"></canvas>      
  </body>

和javascript:

angular.module('myApp', ['chart.js'])

    .controller('myController', [function() {
        var ctrl = this;

        ctrl.socialChart = {
            options: {
                legend: {
                    display: true
                }
            },
            labels: ['2012'],
            series: ['FACEBOOK', 'GOOGLE', 'TWITTER', 'INSTAGRAM'],
            // colors: ['rgba(237, 64, 42, 1)', 'rgba(240, 171, 5, 1)', 'rgba(160, 180, 33, 1)', 'rgba(0, 163, 159, 1)'],
            colors: ['#ED402A', '#F0AB05', '#A0B421', '#00A39F'],
            data: [[1178], [652], [1004], [838]]
        }
    }]);

这是一个普鲁克斯展示它:Plunker

我发现了很多关于使用rgba表示法的信息(由于某种原因,它完全给了我错误的颜色)和fillColor(我根本无法工作)。很难说出chart.js版本1或2的信息是什么。

任何帮助都将不胜感激。

4 个答案:

答案 0 :(得分:4)

您可以使用chart-dataset-override="datasetOverride"

<强> HTML

 <div ng-controller="BarCtrl">
    <canvas id="bar" class="chart chart-bar" 
    chart-data="data" 
    chart-labels="labels" 
    chart-series="series" 
    chart-dataset-override="datasetOverride"></canvas>
  </div>

<强>控制器

  ctrl.datasetOverride = [{
        fill: true,
        backgroundColor: [
          "#ED402A",
          "#36A2EB",
          "#FFCE56"
        ]
      }, {
        fill: true,
        backgroundColor: [
          "#F0AB05",
          "#36A2EB",
          "#FFCE56"
        ]
      }, {
        fill: true,
        backgroundColor: [
          "#A0B421",
          "#36A2EB",
          "#FFCE56"
        ]
      }, {
        fill: true,
        backgroundColor: [
          "#00A39F",
          "#36A2EB",
          "#FFCE56"
        ]
      },
    ];

<强> DEMO

答案 1 :(得分:1)

仅使用RGBA(其中A为不透明度)并将其设置为1即可定义纯色。 例如:backgroundColor: [ 'rgba(255, 99, 132, 1)']

答案 2 :(得分:0)

我正在使用带有角度包装的chart.js版本2,执行以下编辑帮助了我。

更改 - 颜色以rgba(R,G,B,alpha)的形式进行参数 alpha = 1:纯色, alpha = 0:透明色

在HTML中: <canvas id="bar" class="chart chart-bar" chart-data="barData" chart-labels="filter" chart-series="barLabels" chart-options="baroptions" chart-colors="setColors" > </canvas>

在我的Controller类中,我按如下方式定义了setColors:

在controller.js中:  $scope.setColors = [{backgroundColor:'rgba(249,166,92,1)',pointBackgroundColor:'rgba(249,166,92,1),pointHoverBackgroundColor:'rgba(249,166,92,1)',borderColor:'rgba(249,166,92,1)',pointBorderColor:'rgba(249,166,92,1)',pointHoverBorderColor:'rgba(249,166,92,1)'}, {backgroundColor:'rgba(90,155,211,1)',pointBackgroundColor:'rgba(90,155,211,1)',pointHoverBackgroundColor:'rgba(90,155,211,1)',borderColor:'rgba(90,155,211,1)',pointBorderColor:'rgba(90,155,211,1)',pointHoverBorderColor:'rgba(90,155,211,1)'}, {backgroundColor:'rgba(120,194,107,1)',pointBackgroundColor:'rgba(120,194,107,1)',pointHoverBackgroundColor:'rgba(120,194,107,1)',borderColor:'rgba(120,194,107,1)',pointBorderColor:'rgba(120,194,107,1)',pointHoverBorderColor:'rgba(120,194,107,1)'}]

答案 3 :(得分:0)

以下是我制作带有自定义设置条形图的代码。在canvas html标记中使用此属性 chart-dataset-override =&#34; datasetOverride&#34;

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


var setGraphDate = function () {
    var xData = [];
    var yData = [];
    $scope.Data.ExpenseRecords.forEach(function (g) {
        yData.push(g.PercentOfCompletion);
        xData.push(g.TspShortName);
    });
    $scope.datasetOverride =
    {
        backgroundColor: "#4E4EFF",
        borderColor: "#2E2E99"
    };
    $scope.labels = xData;
    $scope.expenseData = yData;
    $scope.series = ['Expense Graph'];
   // more options
    $scope.chartOptions = {

        legend: {
            display: false
        },

        },
        title: {
            display: true,
            text: 'TSP Burn Rate'
        },
        events: false,
        tooltips: {
            enabled: false
        },
        hover: {
            animationDuration: 0
        }
};