dc.js icon indicating copy to clipboard operation
dc.js copied to clipboard

Unable to create a choropleth map using latest dc.js and d3.js

Open jonscrut opened this issue 13 years ago • 4 comments

Hi,

I have copied the example choropleth map from http://nickqizhu.github.com/dc.js/vc/index.html and replaced the csv and the json with my own files.

The csv file works fine in producing a pie graph and I have used the json to produce a map using d3 before.

My example is currently here: http://dl.dropbox.com/u/135842/dcd3-dashboard/nq-ex-jf/map_index.html

And I get the following errors:

Uncaught Mandatory attribute chart.dimension is missing on chart[#exporters-map-chart] dc.v1.2.js:627
Uncaught SyntaxError: Unexpected token w 

I don't know what dimensions are missing as the same variables work for the pie graph and I have copied the same structure from the US Venture Capital example.

Should I use an older version of D3? Or the non-minified version?

jonscrut avatar Feb 20 '13 13:02 jonscrut

You need to call dc.renderAll(); inside d3.json("world-countries-mod.json", function(worldJson) {...}); currently you are calling it outside of it since json is loaded asynchronously the chart does not have the dimension set when its called. Also if you are projecting a different map make sure you set the projection function properly on choropleth chart.

Have fun! =)

NickQiZhu avatar Feb 20 '13 19:02 NickQiZhu

Thanks very much for responding so quickly, I know this is an issues queue and not a help forum :-)

I have tried your suggestion but still no joy. Here is my page: http://dl.dropbox.com/u/135842/dcd3-dashboard/nq-ex-jf/map_index.html

I am still getting the Uncaught syntax error, but in different places depending on if I try d3.v2.js or d3.v3.min.js.

With d3.v2.js I get: Uncaught SyntaxError: Unexpected token w d3.v2.js:514 (anonymous function) d3.v2.js:514 ready d3.v2.js:504 req.onreadystatechange d3.v2.js:497

and with d3.v3.min.js I get:

  Uncaught SyntaxError: Unexpected token w d3.v3.min.js:1
  m d3.v3.min.js:1
  r

Here is my code:

var exportersMapChart = dc.geoChoroplethChart("#exporters-map-chart");


  d3.csv("export-import.csv", function(csv) {
        // since its a csv file we need to format the data a bit

        var numberFormat = d3.format(".2f");

        // feed it through crossfilter
        var data = crossfilter(csv);
        var all = data.groupAll();

        var countrynameDimension = data.dimension(function(d){return d["countryname"];});
        var countrynameDimensionGroup = countrynameDimension.group().reduceSum(function(d){return d["amount"];});



 d3.json("world-countries-mod.json", function(worldcountries) {
        exportersMapChart
            .dimension(countrynameDimension)
            .group(countrynameDimensionGroup)
            .projection(d3.geo.mercator()
                .scale(26778)
                .translate([8227, 3207]))
            .width(990)
            .height(600)
            .colors(["#ccc", "#E2F2FF","#C4E4FF","#9ED2FF","#81C5FF","#6BBAFF","#51AEFF","#36A2FF","#1E96FF","#0089FF"])
            .colorDomain([0, 50000])
            .overlayGeoJson(worldcountries.features, "country", function(d) {
                return d.properties.name;
            })
            .title(function(d) {
                return d.key + " : " + d.value;
            });

    dc.renderAll();
  });
 });

jonscrut avatar Feb 20 '13 20:02 jonscrut

It seems that you geojson feed is not valid. Can you verify your feed in d3 alone first and make sure you can project it using d3 first. If you run into issue there you can get help from d3 group and mail list.

NickQiZhu avatar Feb 21 '13 03:02 NickQiZhu

You're right, somehow my json file had got corrupted. Works brilliantly now, thanks so much. Looking forward to putting it all together!

jonscrut avatar Feb 21 '13 09:02 jonscrut