How to add zoom effect into dc.geoChoroplethChart?

后端 未结 1 1586
挽巷
挽巷 2021-01-06 03:07

I started to use dc.js Library to create all kinds of graphs and I bumped into a problem when I was trying to create a Geo Choropleth map using dc.js and couldn\'t add the a

相关标签:
1条回答
  • 2021-01-06 03:38

    So, the answer is:

    var width = 960,
        height = 400;
    
    var projection = d3.geo.mercator()
        .scale(200)
        .translate([width/2, height]);
    
    function zoomed() {
        projection
        .translate(d3.event.translate)
        .scale(d3.event.scale);
        geoChart.render();
    }
    
    var zoom = d3.behavior.zoom()
        .translate(projection.translate())
        .scale(projection.scale())
        .scaleExtent([height/2, 8 * height])
        .on("zoom", zoomed);
    
    var svg = d3.select("#geo-chart")
        .attr("width", width)
        .attr("height", height)
        .call(zoom);
    
    geoChart
        .projection(projection)
        .width(1000)
        .height(400)
        .transitionDuration(1000)
        .dimension(countryDim)
        .group(ctrGroup)
        .filterHandler(function(dimension, filter){     
            dimension.filter(function(d) {return geoChart.filter() != null ? d.indexOf
            (geoChart.filter()) >= 0 : true;}); // perform filtering
            return filter; // return the actual filter value
         })
        .colors(d3.scale.quantize().range(["#E2F2FF", "#C4E4FF", "#9ED2FF", "#81C5FF",     
         "#6BBAFF", "#51AEFF", "#36A2FF", "#1E96FF", "#0089FF", "#0061B5"]))
        .colorDomain([0, 200])
        .colorCalculator(function (d) { return d ? geoChart.colors()(d) : '#ccc'; })      
        .overlayGeoJson(statesJson.features, "state", function (d) { return d.id; })    
        .title(function (d) {
            return "State: " + d.key + " " + (d.value ? d.value : 0) + " Impressions";
        });
    
    0 讨论(0)
提交回复
热议问题