How to dynamically change the color of a country on d3 map

var COLOR = 'blue';
var map = new Datamap({
  element: document.getElementById('map'),
  responsive: false,
});

function changeColor(countryCode) {
  map.updateChoropleth({
    countryCode: COLOR
  });
}

setInterval(function() {
  changeColor('USA');
}, 2000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datamaps/0.5.8/datamaps.world.min.js"></script>
<div id="map"></div>

I have the following javascript which renders a world map on HTML.

var map = new Datamap({
  element: document.getElementById('map'),
  responsive: true,
});

function changeColor(countryCode) {
  map.updateChoropleth({
    countryCode: 'blue'
  });
  console.log(countryCode + '--> colorChange');
}

setInterval(function() {
  changeColor('USA');
}, 2000);

I want to update the color of a particular country after a 2s. When I run the above code, the function changeColor is called after every 2s, but the function map.updateChoropleth does not seem to have any effect. In case, I change the following snippet:

function changeColor(countryCode) {
  map.updateChoropleth({
    countryCode: 'blue'
  });
  console.log(countryCode + '--> colorChange');
}

to

function changeColor(countryCode) {
  map.updateChoropleth({
    "USA": 'blue'
  });
  console.log(countryCode + '--> colorChange');
}

the function works as expected. Why does this not work?

37 thoughts on “How to dynamically change the color of a country on d3 map”

  1. The updateChoropleth function is looking for a property key literally named countryCode.

    A solution is populating an object and passing it to the function:

    var COLOR = 'blue';
    var map = new Datamap({
      element: document.getElementById('map'),
      responsive: false,
    });
    
    function changeColor(countryCode) {
      var obj = {};
      obj[countryCode] = COLOR;
      map.updateChoropleth(obj);
    }
    
    setInterval(function() {
      changeColor('USA');
    }, 2000);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/datamaps/0.5.8/datamaps.world.min.js"></script>
    <div id="map"></div>
    Reply
  2. This seem to work for me

    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/datamaps/0.5.8/datamaps.world.min.js"></script>
          <div id="map" class="world-map"></div>
          <script>
            // https://github.com/markmarkoh/datamaps
            // https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
    
            var defaultColor = '#c3c3c3';
            var activeColor = '#00b3c6';
            var highlightColorHover = '#037582';
            var highlightBorderColor = '#037582';
    
            var map = new Datamap({
              element: document.getElementById('map'),
              responsive: false,
              fills: {
                defaultFill: defaultColor
              },
              geographyConfig: {
                highlightFillColor: highlightColorHover,
                highlightBorderColor: highlightBorderColor,
              },
            });
    
            map.updateChoropleth({
              USA: activeColor,
              MEX: activeColor,
              PAN: activeColor,
              ARG: activeColor,
              BRA: activeColor,
              CHL: activeColor,
              COL: activeColor,
              AUT: activeColor,
              BEL: activeColor,
              BGR: activeColor,
              DNK: activeColor,
              FIN: activeColor,
              FRA: activeColor,
              DEU: activeColor,
              HUN: activeColor,
              IRL: activeColor,
              ITA: activeColor,
              LTU: activeColor,
              NLD: activeColor,
              POL: activeColor,
              PRT: activeColor,
              ROU: activeColor,
              RUS: activeColor,
              ESP: activeColor,
              SWE: activeColor,
              CHE: activeColor,
              TUR: activeColor,
              GBR: activeColor,
              CHN: activeColor,
              IND: activeColor,
              JPN: activeColor,
              MYS: activeColor,
              PAK: activeColor,
              SGP: activeColor,
              AUS: activeColor,
              ARE: activeColor,
              ZAF: activeColor,
            });
          </script>
    
    Reply
  3. community action agency of delaware county community action partnership kamiah id , positive words that start with e. community acquired pneumonia guidelines pediatrics lyrica medication [url=http://lyrica.website/]lyrica medication[/url] , planning process meaning in kannada community acquired pneumonia review eagle community bank near me , positive quotes nelson mandela community acquired pneumonia symptoms? community america routing number comenity bank victoria’s secret, positive affirmations community bank yorkshire ny.
    positive and negative reinforcement are similar in that , community colleges near me for early childhood education environments early childhood supplies individual level factors examples.

    Reply
  4. office software skills drawing program website to buy Autocad win10-radeon-software-adrenalin-2020-edition-19.12.2-dec12. microsoft office suite for macbook pro best free antivirus software 2020 for windows 10 engeeeneringu#$sssaunnplus , free office software alternative. 2020 software video, software requirements for autocad 2020 radeon software adrenalin 2020 32 bit officejet 8610 software. pes 2020 software download ms office software free download for windows xp, smadav 2020 software download.

    Reply

Leave a Comment