Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change bar color and legends in ColumnChart

I am using Google Chart to display Column Chart for 2 things :

1) Success

2) Failed

For Success : Color = Green

For Failed : Color = Red

But problem is ColumnChart always display bar in blue color and also i want legends as :

Success
Failed

But it displays Legends as "values" as shown below :enter image description here

Code :

 angular.module("google-chart-sample", ["googlechart"])
.controller("GenericChartCtrl", function ($scope) {
    var data = { "data": { "graphResponse": { "cols": [{ "label": "Types", "type": "string" }, { "label": "values", "type": "number" }], "rows": [{ "c": [{ "v": "success" }, { "v": 11 }] }, { "c": [{ "v": "failed" }, { "v": 0 }] }] } } };
    $scope.myChartObject = {};
    $scope.myChartObject.type = "ColumnChart";
    $scope.myChartObject.data = data.data.graphResponse;
    $scope.myChartObject.options = {
        slices: {
            0: { color: '#50ce68' },
            1: { color: '#ff7b7b' },
        },
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.18/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-google-chart/1.0.0-beta.1/ng-google-chart.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.13.4/ui-bootstrap-tpls.min.js"></script>
<ul ng-app="google-chart-sample" ng-controller="GenericChartCtrl">
    <div google-chart chart="myChartObject" style="height:600px; width:100%;"></div>
    </ul>

I want Success bar as Green and Failed as Red and i also want 2 Legends(Success -Green and Failed-Red)

I will appreciate any help :)

like image 216
ILoveStackoverflow Avatar asked Apr 18 '18 14:04

ILoveStackoverflow


People also ask

How do I change the legend color in Highcharts?

And usage in your chart options: $('#container'). highcharts({ series: [{ legendColor: 'black', data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4] }] });

How do I change the color of my Google bar graph?

To change the colors assigned to data series in a specific chart: Select that chart, then on the right, open the STYLE tab. In the Color by section, select Series order, Bar order, or Slice order, depending on the type of chart. Click a color box to set the color for each series.

How do you color code a bar graph in Excel?

In a chart, click to select the data series for which you want to change the colors. On the Format tab, in the Current Selection group, click Format Selection. tab, expand Fill, and then do one of the following: To vary the colors of data markers in a single-series chart, select the Vary colors by point check box.

How to change the color of the legend of a chart?

We must go to “Design” – click on “Change Colors” in the drop-down menu. We have the option of changing the colors of our legends. We also have the option of changing the names of the legend. First, we need to go to the option “Chart Filters.” It helps to edit and modify the names and areas of the chart elements.

How do I change the color of my chart bars?

To use a different colour for each bar, select the option ‘Vary colors by point’. Every bar in the chart now has its own colour fill and every single one can now be adjusted the way we saw a while ago.

How to display the column chart legend title?

TIP: To display the Column Chart Legend title, Please select the Show Legend Title option from the above screenshot Within the General Tab, change the Legend Position by select those dot positions Next, we can change the Font Size, Font Family, Font Style, and Color of a Column Chart Legend using Font Tab so on.

How do I change the chart type of a column?

First, select the Column chart and right-click on it will open the context menu. Please select the Change Chart Type… option from the context menu Once you select the Change Chart Type… option, it will open a new window to select the new chart.


2 Answers

To achieve expected result, use below option of using role and give color green for success and red for failed

{"role": "style", "type": "string"}

  var data = { "data": 
                { "graphResponse": 
                 { "cols": [{ "label": "Types", "type": "string" }, 
                            { "label": "values", "type": "number" },
                            {"role": "style", "type": "string"}], 
                  "rows": [{ "c": [{ "v": "success" }, { "v": 11 },{"v":"green"}] }, 
                           { "c": [{ "v": "failed" }, { "v": 0 },{"v":"red"}] }] 
                 } } };

Issue with legend is that it takes the label:"values" as legend by default and one way to customize is to make legend:none and customize with below code

HTML:

<div class="legend">
    <div ng-repeat="item in myChartObject.data.rows">
    <div class="{{item.c[2].v}}"></div>{{item.c[0].v}}
    </div>
  </div>

CSS:

 .legend{
  position:absolute;
  right:100px;
  top:30px;
}

.red{
  background:red;
  width:30px;
  height:10px;
  display:inline-block;
}

.green{
  background:green;
  width:30px;
  height:10px;
  display:inline-block;
}

JS: In JS add legend :none to chart options

$scope.myChartObject.options = {
        slices: {
            0: { color: '#50ce68' },
            1: { color: '#ff7b7b' },
        },
       legend:'none'
    };

updated code sample for reference - https://codepen.io/nagasai/pen/qYdpaE

like image 185
Naga Sai A Avatar answered Sep 28 '22 03:09

Naga Sai A


You can achieve the data plot selecting functionality by mouse over in legend using setSelection method of google chart. To use this method, at first get chart instance on agc-on-ready directive. Then on mouse over handler of legend, call setSelection method of chart instance.

 $scope.highLight = function(arg){
     if(arg == "red"){
    ChartInstance.setSelection([{row:1,column:1}])
     }
     if(arg=="green"){
       ChartInstance.setSelection([{row:0,column:1}])
     }
   };

Code is given in this link.

like image 22
Chinmoy Samanta Avatar answered Sep 28 '22 02:09

Chinmoy Samanta