I have created a choropleth state map using the d3,datamaps and topojson. I am having issues changing the original map's data based on a button click. The preferred way is to just refresh the original map's data inside the change function. Instead, I have the button execution functions eliminate the div containing the map, then recreate the div, and then completely generate a new map (see my code below). This works, but I am thinking that there is a much easier and sophisticated way of refreshing the data. Any help would be appreciated.
<!DOCTYPE HTML>
<html>
<head>
<script src='js/d3.min.js'></script>
<script src='http://d3js.org/topojson.v1.min.js'></script>
<script src='js/datamaps.all.min.js'></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<style>
#map{height:400px; width: 600px; border-style: solid; border-color:white;}
#floating-panel1 {
position: absolute;
top: 10px;
left: 1%;
z-index: 5;
/*background-color: #fff;*/
padding: 5px;
border: 1px solid #999;
text-align: center;
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 1px;
}
</style>
<script>
var costChange = {
'AR':{'fillKey':'heavy','Percentage':'236%'},
'IL':{'fillKey':'light','Percentage':'5%'},
'IN':{'fillKey':'medium','Percentage':'20%'},
'KS':{'fillKey':'heavy','Percentage':'76%'},
'KY':{'fillKey':'heavy','Percentage':'289%'},
'MS':{'fillKey':'heavy','Percentage':'110%'},
'NC':{'fillKey':'heavy','Percentage':'261%'},
'TN':{'fillKey':'heavy','Percentage':'57%'},
'VA':{'fillKey':'heavy','Percentage':'57%'},
'WA':{'fillKey':'medium','Percentage':'18%'},
'WI':{'fillKey':'medium','Percentage':'18%'}
};
var rateChange = {'AL':{'fillKey':'medium','Percentage':'10%'},
'AR':{'fillKey':'medium','Percentage':'16%'},
'AZ':{'fillKey':'light','Percentage':'7%'},
'CO':{'fillKey':'heavy','Percentage':'44%'},
'CT':{'fillKey':'heavy','Percentage':'132%'},
'DE':{'fillKey':'light','Percentage':'6%'},
'FL':{'fillKey':'heavy','Percentage':'62%'},
'GA':{'fillKey':'medium','Percentage':'17%'},
'ID':{'fillKey':'heavy','Percentage':'66%'},
'IN':{'fillKey':'light','Percentage':'4%'},
'KS':{'fillKey':'medium','Percentage':'11%'},
'KY':{'fillKey':'medium','Percentage':'24%'},
'LA':{'fillKey':'medium','Percentage':'25%'},
'MA':{'fillKey':'heavy','Percentage':'55%'},
'MD':{'fillKey':'heavy','Percentage':'28%'}};
//initialize map with cost data
var map;
$(document).ready(function(){
map = new Datamap({
scope: 'usa',
element: document.getElementById('map'),
geographyConfig: {
highlightBorderColor: '#bada55',
popupTemplate: function(geography, data) {
return "<div class='hoverinfo'>" + geography.properties.name + ' %:' + data.Percentage + ' '
},
highlightBorderWidth: 3
},
fills: {
'light': '#ffad99',
'medium': '#ff704d',
'heavy': '#ff3300',
defaultFill: '#ffebe6'
},
data:costChange
});
map.labels();
});
//button click removes map and recreated with cost data
function cstchng(){
$("#map").remove();
$("#title").after("<div id='map'></div>");
map = new Datamap({
scope: 'usa',
element: document.getElementById('map'),
geographyConfig: {
highlightBorderColor: '#bada55',
popupTemplate: function(geography, data) {
return "<div class='hoverinfo'>" + geography.properties.name + ' %:' + data.Percentage + ' '
},
highlightBorderWidth: 3
},
fills: {
'light': '#ffad99',
'medium': '#ff704d',
'heavy': '#ff3300',
defaultFill: '#ffebe6'
},
data:costChange
});
map.labels();
}
//button click removes map and recreated with rate data
function rtchng(){
$("#map").remove();
$("#title").after("<div id='map'></div>");
map = new Datamap({
scope: 'usa',
element: document.getElementById('map'),
geographyConfig: {
highlightBorderColor: '#bada55',
popupTemplate: function(geography, data) {
return "<div class='hoverinfo'>" + geography.properties.name + ' %:' + data.Percentage + ' '
},
highlightBorderWidth: 3
},
fills: {
'light': '#ffad99',
'medium': '#ff704d',
'heavy': '#ff3300',
defaultFill: '#ffebe6'
},
data:rateChange
});
map.labels();
}
</script>
</head>
<body>
<div id="floating-panel1">
<button type="button" onclick = "cstchng()">Cost Change</button>
<button type="button" onclick = "rtchng()">Range Change</button>
</div>
<div id="title"></div>
<div id="map"></div>
</body>
</html>