Search code examples
amcharts

How to change the color of all columns when one of them was highlighted amchart


So i want to change the color of all columns except selected one. How do i do this?

var chart = AmCharts.makeChart( "chartdiv", {
 "type": "serial",
 "theme": "none",
 "dataProvider": [ {
   "country": "USA",
   "visits": 2025
 }, {
   "country": "China",
   "visits": 1882
 }, {
   "country": "Japan",
   "visits": 1809
 } ],
 "valueAxes": [ {
   "gridColor": "#FFFFFF",
   "gridAlpha": 0.2,
   "dashLength": 0
 } ],
 "gridAboveGraphs": true,
 "startDuration": 1,
 "graphs": [ {
   "balloonText": "[[category]]: <b>[[value]]</b>",
   "fillAlphas": 0.8,
   "lineAlpha": 0.2,
   "fillColors": changeColour,
   "type": "column",
   "valueField": "visits"
 } ],
 "chartCursor": {
   "categoryBalloonEnabled": false,
   "cursorAlpha": 0,
   "zoomable": false
 },
 "categoryField": "country",
 "categoryAxis": {
   "gridPosition": "start",
   "gridAlpha": 0,
   "tickPosition": "start",
   "tickLength": 20
 },
 "export": {
   "enabled": true
 }

  } );

I have created a function which I was trying to return the colour if it the value is within a certain number but this doesn't work.


Solution

  • There isn't a way to dynamically change the color of a bar through a function in fillColors in AmCharts 3 as it only accepts a string (AmCharts 4 has adapters, which lets you do this through a function).

    You have a couple of options to implement this in v3:

    1) Use a fillColorsField and mark off the item in your data with the desired color:

     "dataProvider": [ {
       "country": "USA",
       "visits": 2025
     }, {
       "country": "China",
       "visits": 1882
     }, {
       "country": "Japan",
       "visits": 1809,
       "fillColor": "#ff0000"
     } ],
     // ...
     "graphs": [ {
       "balloonText": "[[category]]: <b>[[value]]</b>",
       "fillAlphas": 0.8,
       "lineAlpha": 0.2,
       "fillColorsField": "fillColor", //match the property in your data 
       "type": "column",
       "valueField": "visits"
     } ],
    

    Demo below:

    var chart = AmCharts.makeChart("chartdiv", {
      "type": "serial",
      "theme": "none",
      "dataProvider": [{
        "country": "USA",
        "visits": 2025
      }, {
        "country": "China",
        "visits": 1882
      }, {
        "country": "Japan",
        "fillColor": "#ff0000", //make this one red
        "visits": 1809
      }],
      "valueAxes": [{
        "gridColor": "#FFFFFF",
        "gridAlpha": 0.2,
        "dashLength": 0
      }],
      "gridAboveGraphs": true,
      "startDuration": 1,
      "graphs": [{
        "balloonText": "[[category]]: <b>[[value]]</b>",
        "fillAlphas": 0.8,
        "lineAlpha": 0.2,
        "fillColorsField": "fillColor",
        "type": "column",
        "valueField": "visits"
      }],
      "chartCursor": {
        "categoryBalloonEnabled": false,
        "cursorAlpha": 0,
        "zoomable": false
      },
      "categoryField": "country",
      "categoryAxis": {
        "gridPosition": "start",
        "gridAlpha": 0,
        "tickPosition": "start",
        "tickLength": 20
      },
      "export": {
        "enabled": true
      }
    
    });
    html,
    body {
      width: 100%;
      height: 100%;
      margin: 0px;
    }
    
    #chartdiv {
      width: 100%;
      height: 100%;
    }
    <link href="//www.amcharts.com/lib/3/plugins/export/export.css" media="all" rel="stylesheet" type="text/css" />
    <script src="//www.amcharts.com/lib/3/amcharts.js"></script>
    <script src="//www.amcharts.com/lib/3/serial.js"></script>
    <script src="//www.amcharts.com/lib/3/themes/light.js"></script>
    <script src="//www.amcharts.com/lib/3/plugins/export/export.min.js"></script>
    <div id="chartdiv"></div>

    2) Use negativeFillColors and negativeBase to have the chart automatically change the color to the color in negativeFillColors if the column value is below the negativeBase value:

     "graphs": [ {
       "balloonText": "[[category]]: <b>[[value]]</b>",
       "fillAlphas": 0.8,
       "lineAlpha": 0.2,
       "negativeBase": 1810, //columns with values less than this will be filled red
       "negativeFillColors": "#ff0000",
       "type": "column",
       "valueField": "visits"
     } ],
    

    Demo below

    var chart = AmCharts.makeChart("chartdiv", {
      "type": "serial",
      "theme": "none",
      "dataProvider": [{
        "country": "USA",
        "visits": 2025
      }, {
        "country": "China",
        "visits": 1882
      }, {
        "country": "Japan",
        "visits": 1809
      }],
      "valueAxes": [{
        "gridColor": "#FFFFFF",
        "gridAlpha": 0.2,
        "dashLength": 0
      }],
      "gridAboveGraphs": true,
      "startDuration": 1,
      "graphs": [{
        "balloonText": "[[category]]: <b>[[value]]</b>",
        "fillAlphas": 0.8,
        "lineAlpha": 0.2,
        "negativeBase": 1810,
        "negativeFillColors": "#ff0000",
        "type": "column",
        "valueField": "visits"
      }],
      "chartCursor": {
        "categoryBalloonEnabled": false,
        "cursorAlpha": 0,
        "zoomable": false
      },
      "categoryField": "country",
      "categoryAxis": {
        "gridPosition": "start",
        "gridAlpha": 0,
        "tickPosition": "start",
        "tickLength": 20
      },
      "export": {
        "enabled": true
      }
    
    });
    html,
    body {
      width: 100%;
      height: 100%;
      margin: 0px;
    }
    
    #chartdiv {
      width: 100%;
      height: 100%;
    }
    <link href="//www.amcharts.com/lib/3/plugins/export/export.css" media="all" rel="stylesheet" type="text/css" />
    <script src="//www.amcharts.com/lib/3/amcharts.js"></script>
    <script src="//www.amcharts.com/lib/3/serial.js"></script>
    <script src="//www.amcharts.com/lib/3/themes/light.js"></script>
    <script src="//www.amcharts.com/lib/3/plugins/export/export.min.js"></script>
    <div id="chartdiv"></div>