本文介绍了如何添加缩放效果到dc.geoChoroplethChart?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用dc.js库创建各种图形,当我尝试使用dc.js创建Geo Choropleth地图时,遇到了一个问题,无法添加缩放和移动地图的功能。

我看到的所有例子都是使用d3和svg ..但一旦我使用这些例子,我无法使用dc.dimention的数据和所有的crossfilter计算。

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 ability to zoom and move the map.
All the examples I saw were using d3 and svg.. but once I used those examples, I couldn't use the data of dc.dimention and all the crossfilter calculations.

例如我的代码是:

 d3.json("world-countries.json", function (statesJson) {
        geoChart.width(1000)
                .height(600)
                .dimension(countryDim)
                .projection(d3.geo.mercator()
                .scale((960 + 1) / 4 )
                .translate([960 / 4, 960 / 4])
                .precision(.1))
                .group(countryGroup)
                .colors(d3.scale.quantize().range(["#E2F2FF","#C4E4FF","#9ED2FF","#81C5FF","#6BBAFF","#51AEFF","#36A2FF","#1E96FF","#0089FF","#0061B5"]))
                .colorDomain([0, 200])
                .colorCalculator(function(d){ returnd ?geoChart.colors()(d) :'#ccc'; })
                .overlayGeoJson(statesJson.features,"state",function(d){
                    return d.properties.name;
                })
                .title(function (d) {
                    return "State: " + d.key + (d.value ? d.value : 0) + "Impressions";
                });        

我该怎么办?!?!

提前感谢!

推荐答案

所以答案是:

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";
    });

这篇关于如何添加缩放效果到dc.geoChoroplethChart?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 21:26