javascript - D3 GeoJson Choropleth and different data files loading depending on onClick Button -
i want create choropleth showing data of production of different animals. depending on onclick-event button want load different files of data create colorpleth. have 4 different json-files sheep, pig, chick , cows. first i'm loading map data in europe.json , data chick.json/cow.json/sheep.json/pig.json. in different for-loops i'm summing numbers total years per country. tried different approaches create 4 buttons , depending on clicks load 1 of 4 json data files , read stackoverflow questions similar questions project bit different because of choropleth.
the jsons data this:
[{ "2005": 0, "2006": 0, "2007": 0, "2008": 0, "2009": 0, "2010": 0, "2011": 0, "2012": 0, "2013": 0, "2014": 0, "2015": 3.26, "2016": 2.27, "geo": "al", "fleischart": "sheep" }, { "2005": 7.15, "2006": 0, "2007": 0, "2008": 8.22, "2009": 7.87, "2010": 8.03, "2011": 8.35, "2012": 8.37, "2013": 8.45, "2014": 7.98, "2015": 7.77, "2016": 7.3, "geo": "at", "fleischart": "sheep" }, { .... different objects different countries. , code looks this:
<div id="container"> </div> <script type="text/javascript"> var w = 800; var h = 600; var projection = d3.geo.mercator() .center([ 13, 52 ]) .translate([ w/2, h/2 ]) .scale([ w/1.5 ]); var path = d3.geo.path() .projection(projection); var svg = d3.select("#container") .append("svg") .attr("width", w) .attr("height", h); d3.json("europe.json", function (json) { console.log(json); d3.json("chick.json", function (error, data) { if (error) { console.log(error); } else { console.log(data); dataset = data; } var mengeprojahrproland = new array(); var mengedurchschnittproland = new array; (var n = 0; n < 36; n++) { var landname = object(data[n])["geo"]; var total = 0; mengeprojahrproland.push(object.values(data[n])); (var q = 0; q < 12; q++) { total += object(mengeprojahrproland[n])[q]; } mengedurchschnittproland.push(parsefloat(total).tofixed(2)); var landmengeobjekt = [{land: 0, anzahl: 0}]; var laender = []; var laendermenge = []; (var m = 0; m < 36; m++) { var landname = object(data[m])["geo"]; var durchschnittmengeprojahr = mengedurchschnittproland[m]; laender.push(landname); laendermenge.push(durchschnittmengeprojahr); landmengeobjekt.push({land: landname, anzahl: durchschnittmengeprojahr}); } } (var = 0; < laender.length; a++) { var datastate = laender[a]; var datavalue = parsefloat(laendermenge[a]); var color = d3.scale.threshold() .domain([ 1, 2, 5, 10, 25, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 6000, 7000, 10000, 11000, 15000, 17500, 18000, 19000, 22000 ]) .range([ "#2438ff", "#2a36f8", "#3035f1", "#3633ea", "#3c32e3", "#4230dc", "#482fd5", "#4e2ece", "#542cc7", "#5a2bc0", "#6029b9", "#6628b2", "#6d27ab", "#7325a4", "#79249d", "#7f2296", "#85218f", "#8b1f88", "#911e82", "#971d7b", "#9d1b74", "#a31a6d", "#a91866", "#af175f", "#b61658", "#bc1451", "#c2134a", "#c81143", "#ce103c", "#d40e35", "#da0d2e", "#e00c27", "#e60a20", "#ec0919", "#f20712", "#f8060b", "#ff0505" ]); (var j = 0; j < json.features.length; j++) { var jsonstate = json.features[j].properties.wb_a2; if (jsonstate == datastate) { json.features[j].properties.value = datavalue; break; } } } svg.selectall("path") .data(json.features) .enter() .append("path") .attr("d", path) .style("fill", function (d) { var value = d.properties.value; if (value) { return color(value); } else { return "darkgray"; } }); }); }); this current choropleth chick.json data: insahne.com/colorpleth/indexchick.html how can achieve load different files depending on user's click?
Comments
Post a Comment