Community, I would like to create a Choropleth Map using D3.js similar to the one found here.
My map differs from this example, in that, I would like to use a SVG I created and resides in my index.html,instead of using a TopoJSON or GeoJson file for shape data, as I do not have that shape data for the countries districts elsewhere. My SVG uses paths or polygons with an id value for each district in my map that correspond with the my .tsv file's ids.
I have my .tsv data set file with id, an range equivalent to the example the example.
I considered converting the SVG to GeoJSON then to TopoJson using this tool and that tool, however I have no Command Line experience. More over, After reading the third answer to this question on Stackoverflow. which states:
"If you're creating a choropleth and have no geolocation requirements then you can simply add the SVG directy to your page and use D3 to map data to your image! Once you have the SVG in your page then you can even manually edit all path data to include classes and ids that will make your D3 job easier. "-Roberto Sterling
I attempted to go for using the SVG, using the corresponding id values to my data for the path and polygon id attribute.
Below is my customized D3 code from the example:
<script>
var width = 960,
height = 600;
var rateById = d3.map();
var quantize = d3.scale.quantize()
.domain([0, .15])
.range(d3.range(9).map(function(i) { return "q" + i + "-9"; }));
var projection = d3.geo.albersUsa()
.scale(1280)
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body svg");
var constituencies = svg.selectAll("path, polygon");
queue()
//.defer(d3.json, "/mbostock/raw/4090846/us.json")
.defer(d3.tsv, "JM_election_results_2015.tsv", function(d) { rateById.set(d.id, +d.rate); })
.await(ready);
function ready(error, us) {
if (error) throw error;
constituencies.data(function(d){return d.id;})
.enter().append("path")
.attr("class", function(d) { return quantize(rateById.get(d.id)); })
.attr("d", path);
svg.append("path")
.datum(topojson.mesh(constituencies, constituencies.d.id, function(a, b) { return a !== b; }))
.attr("class", "constituency")
.attr("d", path);
}
d3.select(self.frameElement).style("height", height + "px");
</script>
The is line of code :
.datum(topojson.mesh(constituencies, constituencies.d.id, function(a, b) { return a !== b; }))
is causing the following error:
Uncaught TypeError: Cannot read property 'id' of undefined
Where have I gone wrong? and or How do I Bind Data from a .tsv to an SVG's paths using d3.js for a Choropleth Map?