简体   繁体   English

如果数据为负,如何更改条形图中条形图的颜色 - Angular Charts

[英]How to change the color of the bar in barchart if the data is negative - Angular Charts

I have created a horizontal barchart with Angular Charts.The HTML looks like this:我用 Angular Charts 创建了一个水平条形图。 HTML 如下所示:

    <canvas id="base" class="chart-horizontal-bar"
            chart-data="vm.chartData" 
            chart-labels="vm.chartLabels">
    </canvas>

The JavaScript variables look like this : JavaScript 变量如下所示:

 var vm = this;
 vm.chartData = [10,20,-30,40,50];
 vm.chartLabels = ["A","B","C","D","E"];

I want to change the color of the bar to red for negative data(in this case -30) and all the bars with positive data should be green (10,20,40,50).我想将负数据的条形颜色更改为红色(在本例中为 -30),并且所有具有正数据的条形都应为绿色(10、20、40、50)。

Thanks in advance!!提前致谢!!

I would loop through your chartData and using a condition build out your array for colors.我会遍历您的chartData并使用条件构建您的颜色数组。 Fair warning, I'm not familiar with Angular Charts, so vm.chartColors may not be the right syntax, but the idea here holds, which is to build an array of your colors based on negative/positive numbers in your data.公平警告,我不熟悉 Angular Charts,所以vm.chartColors可能不是正确的语法,但这里的想法是成立的,即根据数据中的负数/正数构建一个颜色数组。 A quick look at angularcharts.js implied you can set the colors with an argument to override the default colors.快速浏览 angularcharts.js 暗示您可以使用参数设置颜色以覆盖默认颜色。

 var vm = this; vm.chartData = [10,20,-30,40,50]; vm.chartLabels = ["A","B","C","D","E"]; var colors = []; for(i=0; i < vm.chartData.length; i++) { if (vm.chartData[i] < 0) { colors[i] = "rgb(255,0,0)"; } else { colors[i] = "rgb(0,255,0)"; } } vm.chartColors = colors; console.log(vm.chartColors);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM