Search code examples
angularjsd3.jsnvd3.jsangular-nvd3

how to change descrete bar color onmouseover - angular-nvd3


I want to create a chart, for view vs dates. So I used angular-nvd3 plugin. Chart is displaying fine. I have same color for all descrete bar elements. I need to change the color of specific bar on moseover event. I tried following methods.

(1) d3.select(this).atrr('rect').style('fill':'red');
(2) $scope.options.chart[e.index].color = "#222";

But non of them are working. Is there any way to do this.

$scope.options = {
  chart: {
  type: 'discreteBarChart',
  height: 450,
  x: function(d){return d.label;},
  y: function(d){return d.value;},
  showValues: false,
  transitionDuration: 500,
  xAxis: {
    axisLabel: 'Month'
  },
  yAxis: {
            axisLabel: 'Views',
            axisLabelDistance: 10,
            tickFormat: function (d) {
                    return d3.format('k')(d);
            }
        },
  color: ['#59ade8'],
  dispatch: {
              tooltipShow: function(e){ },
              tooltipHide: function(e){},
              beforeUpdate: function(e){}
            },
            discretebar: {
              dispatch: {
                //chartClick: function(e) {console.log("! chart Click !")},
                elementClick: function(e) {   
                    selected_element = e;
                    setVisibility();
                 },
                elementMouseout: function(e) {},
                elementMouseover: function(e) {
                    d3.select(e).color = '#222'
                }
              }
            }
  }
  }

  $scope.data = [{
  values: [{
    "label" : "10" ,
    "value" : 50
  },{
    "label" : "11" ,
    "value" : 20
  },{
    "label" : "13" ,
    "value" : 60
  },{
    "label" : "14" ,
    "value" : 90
  },{
    "label" : "15" ,
    "value" : 40
  },{
    "label" : "16" ,
    "value" : 50
  },{
    "label" : "17" ,
    "value" : 30
  }]
}];

Solution

  • I solved the problem by using $scope.apply() function. It made my day. I change each bar color by

    $scope.$apply(function(){
      $scope.data[0].values[e.index].color = '#59ade8';
    });