Drawing states separately with d3 js and country topojson file - javascript
I have a topojson which contains state's paths. I want the user to be able to hover over a state and the state to appear in a different svg. So far, I've tried to extract the geometry out of the topojson (d.geometry , d.geometry.coordinates etc) But I'm not able to do it.
Maybe I need to draw a polygon out of that, but some states are of type "Polygon" and some of them are of type "MultiPolgyon".
Any ideas/suggestions?
Edit : Here's my code
var svg = d3.select("#india-map")
.append("svg")
.attr("width",width).attr("preserveAspectRatio", "xMidYMid")
.attr("viewBox", "0 0 " + width + " " + height)
.attr("height", height)
var stateSvg = d3.select("#state-map")
.append("svg")
.append("g")
.attr("height", height)
.attr("width", width);
var g = svg.append("g");
var projection = d3.geo.mercator()
.center([86, 27])
.scale(1200);
var path = d3.geo.path().projection(projection);
var pc_geojson = topojson.feature(pc, pc.objects.india_pc_2014);
var st_geojson = topojson.feature(state_json, state_json.objects.india_state_2014);
g.selectAll(".pc")
.data(pc_geojson.features)
.enter().append("path")
.attr("class", "pc")
.attr("d", path)
.attr("id", function(d){ return d.properties.Constituency;})
.attr("fill", "orange")
.on("click", function(d){
drawCons(d);
});
function drawCons(d){
stateSvg.selectAll(".pc2")
.data(d)
.enter().append("path")
.attr("class","pc2")
.attr("d", path)
}
.data() expects to be given an array of objects to be matched against the selection. You're passing a single object, so it doesn't work. You can either use .datum(d) or .data([d]) to make it work.
Quick and dirty demo here.
Related
D3 skipping first two features in a TopoJson
I am creating an interactive visualization tool with D3. I just got my map working today and I noticed that the first two features from the topojson are not loading in. The first two entries of the topojson look like this and geojson.io reads these countries in fine. (They appear on the map) "objects":{"ne_10m_admin_0_countries":{"type":"GeometryCollection","geometries":[{"arcs":[[[0,1]],[[2,3,4,5]],[[6,7]],[[8,9]],[[10]],[[11]],[[12]],[[13]],[[14]],[[15]],[[16]],[[17]],[[18]],[[19]],[[20]],[[21]],[[22]],[[23]],[[24]],[[25]],[[26]],[[27]],[[28]],[[29]],[[30]],[[31]],[[32]],[[33]],[[34]],[[35]],[[36]],[[37]],[[38]],[[39]],[[40]],[[41]],[[42]],[[43]],[[44]],[[45]],[[46]],[[47]],[[48]],[[49]],[[50]],[[51]],[[52]],[[53]],[[54]],[[55]],[[56]],[[57]],[[58]],[[59]],[[60]],[[61]],[[62]],[[63]],[[64]],[[65]],[[66]],[[67]],[[68]],[[69]],[[70]],[[71]],[[72]],[[73]],[[74]],[[75]],[[76]],[[77]],[[78]],[[79]],[[80]],[[81]],[[82]],[[83]],[[84]],[[85]],[[86]],[[87]],[[88]],[[89]],[[90]],[[91]],[[92]],[[93]],[[94]],[[95]],[[96]],[[97]],[[98]],[[99]],[[100]],[[101]],[[102]],[[103]],[[104]],[[105]],[[106]],[[107]],[[108]],[[109]],[[110]],[[111]],[[112]],[[113]],[[114]],[[115]],[[116]],[[117]],[[118]],[[119]],[[120]],[[121]],[[122]],[[123]],[[124]],[[125]],[[126]],[[127]],[[128]],[[129]],[[130]],[[131]]],"type":"MultiPolygon","properties":{"featurecla":"Admin-0 country","scalerank":5,"LABELRANK":2,"SOVEREIGNT":"Indonesia","SOV_A3":"IDN","ADM0_DIF":0,"LEVEL":2,"TYPE":"Sovereign country","ADMIN":"Indonesia","ADM0_A3":"IDN","GEOU_DIF":0,"GEOUNIT":"Indonesia","GU_A3":"IDN","SU_DIF":0,"SUBUNIT":"Indonesia","SU_A3":"IDN","BRK_DIFF":0,"NAME":"Indonesia","NAME_LONG":"Indonesia","BRK_A3":"IDN","BRK_NAME":"Indonesia","BRK_GROUP":"","ABBREV":"Indo.","POSTAL":"INDO","FORMAL_EN":"Republic of Indonesia","FORMAL_FR":"","NAME_CIAWF":"Indonesia","NOTE_ADM0":"","NOTE_BRK":"","NAME_SORT":"Indonesia","NAME_ALT":"","MAPCOLOR7":6,"MAPCOLOR8":6,"MAPCOLOR9":6,"MAPCOLOR13":11,"POP_EST":260580739,"POP_RANK":17,"GDP_MD_EST":3028000,"POP_YEAR":2017,"LASTCENSUS":2010,"GDP_YEAR":2016,"ECONOMY":"4. Emerging region: MIKT","INCOME_GRP":"4. Lower middle income","WIKIPEDIA":-99,"FIPS_10_":"ID","ISO_A2":"ID","ISO_A3":"IDN","ISO_A3_EH":"IDN","ISO_N3":"360","UN_A3":"360","WB_A2":"ID","WB_A3":"IDN","WOE_ID":23424846,"WOE_ID_EH":23424846,"WOE_NOTE":"Exact WOE match as country","ADM0_A3_IS":"IDN","ADM0_A3_US":"IDN","ADM0_A3_UN":-99,"ADM0_A3_WB":-99,"CONTINENT":"Asia","REGION_UN":"Asia","SUBREGION":"South-Eastern Asia","REGION_WB":"East Asia & Pacific","NAME_LEN":9,"LONG_LEN":9,"ABBREV_LEN":5,"TINY":-99,"HOMEPART":1,"MIN_ZOOM":0,"MIN_LABEL":1.7,"MAX_LABEL":6.7,"NE_ID":1159320845,"WIKIDATAID":"Q252","NAME_AR":"إندونيسيا","NAME_BN":"ইন্দোনেশিয়া","NAME_DE":"Indonesien","NAME_EN":"Indonesia","NAME_ES":"Indonesia","NAME_FR":"Indonésie","NAME_EL":"Ινδονησία","NAME_HI":"इंडोनेशिया","NAME_HU":"Indonézia","NAME_ID":"Indonesia","NAME_IT":"Indonesia","NAME_JA":"インドネシア","NAME_KO":"인도네시아","NAME_NL":"Indonesië","NAME_PL":"Indonezja","NAME_PT":"Indonésia","NAME_RU":"Индонезия","NAME_SV":"Indonesien","NAME_TR":"Endonezya","NAME_VI":"Indonesia","NAME_ZH":"印度尼西亚"}},{"arcs":[[[132,-1]],[[133,134]],[[135,-8,136,137,138,139]],[[140]],[[141]],[[142]],[[143]],[[144]],[[145]]],"type":"MultiPolygon","properties":{"featurecla":"Admin-0 country","scalerank":5,"LABELRANK":3,"SOVEREIGNT":"Malaysia","SOV_A3":"MYS","ADM0_DIF":0,"LEVEL":2,"TYPE":"Sovereign country","ADMIN":"Malaysia","ADM0_A3":"MYS","GEOU_DIF":0,"GEOUNIT":"Malaysia","GU_A3":"MYS","SU_DIF":0,"SUBUNIT":"Malaysia","SU_A3":"MYS","BRK_DIFF":0,"NAME":"Malaysia","NAME_LONG":"Malaysia","BRK_A3":"MYS","BRK_NAME":"Malaysia","BRK_GROUP":"","ABBREV":"Malay.","POSTAL":"MY","FORMAL_EN":"Malaysia","FORMAL_FR":"","NAME_CIAWF":"Malaysia","NOTE_ADM0":"","NOTE_BRK":"","NAME_SORT":"Malaysia","NAME_ALT":"","MAPCOLOR7":2,"MAPCOLOR8":4,"MAPCOLOR9":3,"MAPCOLOR13":6,"POP_EST":31381992,"POP_RANK":15,"GDP_MD_EST":863000,"POP_YEAR":2017,"LASTCENSUS":2010,"GDP_YEAR":2016,"ECONOMY":"6. Developing region","INCOME_GRP":"3. Upper middle income","WIKIPEDIA":-99,"FIPS_10_":"MY","ISO_A2":"MY","ISO_A3":"MYS","ISO_A3_EH":"MYS","ISO_N3":"458","UN_A3":"458","WB_A2":"MY","WB_A3":"MYS","WOE_ID":23424901,"WOE_ID_EH":23424901,"WOE_NOTE":"Exact WOE match as country","ADM0_A3_IS":"MYS","ADM0_A3_US":"MYS","ADM0_A3_UN":-99,"ADM0_A3_WB":-99,"CONTINENT":"Asia","REGION_UN":"Asia","SUBREGION":"South-Eastern Asia","REGION_WB":"East Asia & Pacific","NAME_LEN":8,"LONG_LEN":8,"ABBREV_LEN":6,"TINY":-99,"HOMEPART":1,"MIN_ZOOM":0,"MIN_LABEL":3,"MAX_LABEL":8,"NE_ID":1159321083,"WIKIDATAID":"Q833","NAME_AR":"ماليزيا","NAME_BN":"মালয়েশিয়া","NAME_DE":"Malaysia","NAME_EN":"Malaysia","NAME_ES":"Malasia","NAME_FR":"Malaisie","NAME_EL":"Μαλαισία","NAME_HI":"मलेशिया","NAME_HU":"Malajzia","NAME_ID":"Malaysia","NAME_IT":"Malesia","NAME_JA":"マレーシア","NAME_KO":"말레이시아","NAME_NL":"Maleisië","NAME_PL":"Malezja","NAME_PT":"Malásia","NAME_RU":"Малайзия","NAME_SV":"Malaysia","NAME_TR":"Malezya","NAME_VI":"Malaysia","NAME_ZH":"马来西亚"}},{"arcs":[[[146,147,148,149]],[[150,151,152,153]],[[154]],[[155]],[[156]],[[157]],[[158]],[[159]],[[160]],[[161]],[[162]],[[163]],[[164]],[[165]],[[166]],[[167]],[[168]],[[169]],[[170]],[[171]],[[172]],[[173]],[[174]],[[175]],[[176]],[[177]],[[178]],[[179]],[[180]],[[181]],[[182]],[[183]],[[184]],[[185]],[[186]],[[187]],[[188]],[[189]],[[190]],[[191]],[[192]],[[193]],[[194]],[[195]],[[196]],[[197]],[[198]],[[199]],[[200]],[[201]],[[202]],[[203]],[[204]],[[205]],[[206]],[[207]],[[208]],[[209]],[[210]],[[211]],[[212]],[[213]],[[214]],[[215]],[[216]],[[217]],[[218]],[[219]],[[220]],[[221]],[[222]],[[223]],[[224]],[[225]],[[226]],[[227]],[[228]],[[229]],[[230]],[[231]],[[232]],[[233]],[[234]],[[235]],[[236]],[[237]]], Here is my JS: <script>window.onload = setMap(); function setMap(){ d3.csv("/data/fdata.csv").then(function(data) { //console.log(data); d3.json("/data/a.topojson").then(function(data2) { //console.log(data2); var width = 960, height = 460; //create new svg container for the map var map = d3.select("body") .append("svg") .attr("class", "map") .attr("width", width) .attr("height", height); var projection = d3.geoNaturalEarth1() .center([0, 0]) .rotate([-2, 0, 0]) //.parallels([43, 62]) .scale(175) .translate([width / 2, height / 2]); var path = d3.geoPath() .projection(projection); d3.selectAll(".boundary") .style("stroke-width", 1 / 1); var b = topojson.feature(data2, data2.objects.ne_10m_admin_0_countries); var graticule = d3.geoGraticule(); var attrArray = ["x1","x2","x3" ]; function joinData(b, data){ for (var i=0; i<data.length; i++){ var csvRegion = data[i]; //the current region var csvKey = data[i].Country; //the CSV primary key for (var a=0; a<b.features.length; a++){ var geojsonProps = b.features[a].properties; //gj props var geojsonKey = geojsonProps.ADMIN; //the geojson primary key if (geojsonKey == csvKey){ attrArray.forEach(function(attr){ var val = parseFloat(csvRegion[attr]); geojsonProps[attr] = val; }); }; }; }; return b; }; joinData(b,data); var tooltip = d3.select("body").append("div") .attr("class", "tooltip") .style("opacity", 0); var currentF = "B2"; var color = d3.scaleQuantile() .domain(d3.range(0, 1000)) .range(d3.schemeReds[7]); map.append("path") .datum(graticule) .attr("class", "graticule") .attr("d", path); map.append("path") .datum(graticule.outline) .attr("class", "graticule outline") .attr("d", path); map.selectAll("path") .data(b.features) .enter() .append("path") .attr("d", path) //.style("stroke", "black") .on("mouseover", function(d) { tooltip.transition() .duration(200) .style("opacity", .9) .style("stroke-opacity", 1.0); tooltip.html(d.properties.ADMIN) .style("left", (d3.event.pageX) + "px") .style("top", (d3.event.pageY - 28) + "px"); }) .on("mouseout", function(d) { tooltip.transition() .duration(500) .style("opacity", 0) .style("stroke-opacity", 0); }) .style("fill", function(d) { return color(d.properties[currentF]); }); }); //csv }); //json }; I believe the issue is with the map.selectAll("path") chunk. When I run it without D3 iterating through features it seems to come out fine. (With Indonesia and Malaysia still there. )
The issue is here: map.append("path") .datum(graticule) .attr("class", "graticule") .attr("d", path); map.append("path") .datum(graticule.outline) .attr("class", "graticule outline") .attr("d", path); map.selectAll("path") .data(b.features) .enter() .append("path") .attr("d", path) The two append statements create paths, you then select these existing paths when you do: map.selectAll("path"). To confirm, before you append the features, you can do console.log(map.selectAll("path").size()) to see how many elements are selected, it should be two. An enter selection can be used to create elements in the DOM for each item in the data array that does not have a corresponding element in the DOM. As there are two paths in your selection, the first two items in the data array correspond to these two elements, and thus do not need to be entered: they already exist (By binding the data with .data() you are binding the first two datums in the data array to the already existing paths). D3 doesn't "know" that these paths aren't supposed to be affiliated with those data array items. In order to ensure all items are entered you can make sure that you have an empty selection before you bind data: map.selectAll(null).data()... map.selectAll().data()... map.selectAll(".className").data()... // where no element has that class yet
D3 Map - Cannot apply the suitable projection for indian map
I am trying to draw the states of India map (with disputed territories ) by D3 Map. I successfully generate the topojson file which looks good in http://www.mapshaper.org/ And the json link is https://dl.dropboxusercontent.com/s/wk9qd47wn1nhsjm/dddtopo.json But I failed to draw the map. The link http://jsfiddle.net/sEFjd/47/ is how I did under jsfiddle. var topoJsonUrl = "https://dl.dropboxusercontent.com/s/wk9qd47wn1nhsjm/dddtopo.json"; var width = 360, height = 360; d3.json(topoJsonUrl, function(error, mx) { var projection = d3.geo.mercator(); // create the path var path = d3.geo.path().projection(projection); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height); console.log(mx); var geoPaths = svg.append("g") .attr("class", "municipalities") .selectAll("path") .data(topojson.feature(mx, mx.objects.india).features); geoPaths.enter().append("path") .attr("d", path); var p= svg.append("path") .datum(topojson.mesh(mx, mx.objects.india)) .attr("d", path) .attr("class", "state-boundary"); geoPaths.style("fill", function(d) { return Math.random() > 0.5 ?'gray' : 'blue'; }); }); The code works well with other countries(Germany, Mexico) Not sure why it does not work this time. Any help will be very appreciated.
How to add Label to each state in d3.js (albersUsa)?
The us.json loaded, but when i try to add Label name i can't make it work. I don't see the name property in .json file so how can i add each state name? I'm really new to this framework. I try different Tutorial on Google and Stackoverflow, but none of them work for me. Here is the link to couple tutorial i tried, that i think is worthy. Add names of the states to a map in d3.js State/County names in TopoJSON or go back GeoJSON? The concerns I have: I think I'm missing name property in us.json file. (if that's the issue, is there any other .json file that includes state name? And how to use the state name with that file?) Is the US state name included in http://d3js.org/topojson.v1.min.js? .html File (Framework Loaded) <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> <script src="http://d3js.org/topojson.v1.min.js"></script> .js File: var width = 1500, height = 1100, centered; var usData = ["json/us.json"]; var usDataText = ["json/us-states.json"]; var projection = d3.geo.albersUsa() .scale(2000) .translate([760, height / 2]); var path = d3.geo.path() .projection(projection); var svg = d3.select("body").append("svg") .style("width", "100%") .style("height", "100%"); svg.append("rect") .attr("class", "background") .attr("width", width) .attr("height", height) .on("click", clicked); var g = svg.append("g"); d3.json(usData, function(unitedState) { g.append("g") .attr("class", "states-bundle") .selectAll("path") .data(topojson.feature(unitedState, unitedState.objects.states).features) .enter() .append("path") .attr("d", path) .attr("class", "states") .on("click", clicked); }); Thank you everyone in advanced. I also appreciate if you tell me where did you learn d3.js.
As you stated your us.json doesn't have state names in it. What it has, though, are unique ids and luckily, Mr. Bostock has mapped those ids to names here. So, let's fix up this code a bit. First, make the json requests to pull the data: // path data d3.json("us.json", function(unitedState) { var data = topojson.feature(unitedState, unitedState.objects.states).features; // our names d3.tsv("us-state-names.tsv", function(tsv){ // extract just the names and Ids var names = {}; tsv.forEach(function(d,i){ names[d.id] = d.name; }); Now add our visualization: // build paths g.append("g") .attr("class", "states-bundle") .selectAll("path") .data(data) .enter() .append("path") .attr("d", path) .attr("stroke", "white") .attr("class", "states"); // add state names g.append("g") .attr("class", "states-names") .selectAll("text") .data(data) .enter() .append("svg:text") .text(function(d){ return names[d.id]; }) .attr("x", function(d){ return path.centroid(d)[0]; }) .attr("y", function(d){ return path.centroid(d)[1]; }) .attr("text-anchor","middle") .attr('fill', 'white'); .... Here's a working example.
plots on multiple instances of graph
I'm using the d3 library to plot a bar graph with JSON objects recieved from the server through websockets. What is happening though is that each time the graph is plotted it draws a new instance of a graph. So I end up with multiple graphs. But I want the JSON data to be all plotted onto the same one graph. Here's my code: ws = new WebSocket("ws://localhost:8888/dh"); var useData = [] //var chart; var chart = d3.select("body") .append("svg:svg") .attr("class", "chart") .attr("width", 420) .attr("height", 200); ws.onmessage = function(evt) { var distances = JSON.parse(evt.data); data = distances.miles; console.log(data); if(useData.length <= 10){ useData.push(data) } else { var draw = function(data){ // Set the width relative to max data value var x = d3.scale.linear() .domain([0, d3.max(useData)]) .range([0, 420]); var y = d3.scale.ordinal() .domain(useData) .rangeBands([0, 120]); var rect = chart.selectAll("rect") .data(useData) // enter rect rect.enter().append("svg:rect") .attr("y", y) .attr("width", x) .attr("height", y.rangeBand()); // update rect rect .attr("y", y) .attr("width", x) .attr("height", y.rangeBand()); var text = chart.selectAll("text") .data(useData) // enter text text.enter().append("svg:text") .attr("x", x) .attr("y", function (d) { return y(d) + y.rangeBand() / 2; }) .attr("dx", -3) // padding-right .attr("dy", ".35em") // vertical-align: middle .attr("text-anchor", "end") // text-align: right .text(String); // update text text .data(useData) .attr("x", x) .text(String); } useData.length = 0; } } How can I plot all points onto on graph which is being constantly updated? It's a shame that d3 cannot handle data in real-time and update charts accordingly, or if it can that there's no clear tutorial/ explanation of how to. Thanks
My guess is because you're creating a chart every time with: var chart = d3.select("body") .append("svg:svg") .attr("class", "chart") .attr("width", 420) .attr("height", 20 * useData.length); Rather you need to check if chart exists, and if so, don't call that line. // outside of .onmessage var chart; // inside of .onmessage if (!chart) { chart = d3.select("body") .append("svg:svg") .attr("class", "chart") .attr("width", 420) .attr("height", 20 * useData.length); }
Like Brian said you keep creating a new svg element at every onmessage event. However, in D3 you don't need to use an if statement to check for element existence; after you do a data join, the enter() selection will only contain the elements that did not exist yet: // data join var chart = d3.select("body").selectAll(".chart") .data([useData]); // if you wanted multiple charts: .data([useData1, useDate2, useData3]) // enter chart.enter().append("svg") // this will only execute if the .chart did not exist yet .attr("class", "chart") .attr("width", 420); // update (both new and existing charts) chart .attr("height", function(d) { return 20 * d.length; }); The concepts of the data join, enter(), update(), and exit() selections are explained in the Thing with Joins article. See also the 3 General Update Pattern articles. You have to use a similar approach when you update or add new rect elements in your chart. Assuming for the moment that useData contains all accumulated data (although the useData.length = 0 might mean that is not the case): // data join var rects = chart.selectAll("rect") .data(function(d) { return d; }); // use the data bound to each chart // enter rects.enter().append("rect"); // update rects .attr("y", function(d) { return y(d.yValue); }) // not sure what your data structure looks like .attr("width", function(d) { return x(d.xValue); }) .attr("height", y.rangeBand()); // exit rects.exit().remove(); Some suggestions how to update a path with real time data are given in Path Transitions.
Using arc.Centroid to put an svg:image in the middle of the arc - but not appearing
I am currently trying to place a svg:image in the centre of my arc: var arcs = svg.selectAll("path"); arcs.append("svg:image") .attr("xlink:href", "http://www.e-pint.com/epint.jpg ") .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; }) .attr("width", "150px") .attr("height", "200px"); I would appreciate it if someone could give me any advice on why it isn't appearing thanks : http://jsfiddle.net/xwZjN/17/
Looking at the jsfiddle, you are creating the path elements after you try to append the svg:image elements to the them. It should be the other way around. You should first create the arcs and then append the images. Second, as far as I know, the svg:path element should not contain any svg:image tags. It doesn't seem to display them if you place some inside. Instead what you should do is create svg:g tags with class arc and then use those to place the svg:images Slightly modifying your jsfiddle could look something like this: var colours = ['#909090','#A8A8A8','#B8B8B8','#D0D0D0','#E8E8E8']; var arcs = svg.selectAll("path"); for (var z=0; z<30; z++){ arcs.data(donut(data1)) .enter() //append the groups .append("svg:g") .attr("class", "arc") .append("svg:path") .attr("fill", function(d, i) { return colours[(Math.floor(z/6))]; }) .attr("d", arc[z]) .attr("stroke","black") } //here we append images into arc groups var pics = svg.selectAll(".arc").append("svg:image") .attr("xlink:href", "http://www.e-pint.com/epint.jpg ") .attr("transform", function(d,i) { //since you have an array of arc generators I used i to find the arc return "translate(" + arc[i].centroid(d) + ")"; }) .attr("x",-5) .attr("y",-10) .attr("width", "10px") .attr("height", "20px"); Where I also decreased the size of the images and offset them so that they fit into the arc.