javascript - 通过光标拖动使 d3.js 数据图旋转

标签 javascript d3.js svg rotation datamaps

我做了this map使用datamaps通过 @markmarkoh

我喜欢它的结果,但我希望当你用光标拖动它时我可以让它旋转,这样你就可以看到所有的大陆。就像例子 herehere .

这是我的代码片段:

//basic map config with custom fills, mercator projection
               
               var series = [
        ["USA",36.2],["GBR",7.4],["CAN",6.2],["DEU",5.7],["FRA", 4.1],["ESP",4.1],["ITA",3.3],["MEX",3.0],["AUS",2.5],["NLD",2.4],
        ["IND",2.1],["BRA",2.0],["GRC",1.4],["AUT",1.2],["ROU",1.2],["SRB",1.0],["COL",0.8],["POL",0.8],["ZAF",0.7],["SWE",0.7],
        ["DNK",0.6],["VEN",0.6],["JPN",0.6],["KOR",0.6],["BEL",0.5],["RUS",0.5],["PRT",0.5]
                            ];
                       
         var dataset = {};
            // We need to colorize every country based on "percent"
            // colors should be uniq for every value.
            // For this purpose we create palette(using min/max series-value)
            var onlyValues = series.map(function(obj){ return obj[1]; });
            var minValue = Math.min.apply(null, onlyValues),
                    maxValue = Math.max.apply(null, onlyValues);
            // create color palette function
            // color can be whatever you wish
            var paletteScale = d3.scale.linear()
                    .domain([minValue,maxValue])
                    .range(["rgb(0,0,0)","rgb(219,219,219)"]);  // color
            // fill dataset in appropriate format
            series.forEach(function(item){ //
                // item example value ["USA", 36.2]
                var iso = item[0],
                        value = item[1];
                dataset[iso] = { percent: value, fillColor: paletteScale(value) };
            });
              var map = new Datamap({
                scope: 'world',
                element: document.getElementById('world'),
                projection: 'orthographic',
                projectionConfig: {
                  rotation: [90,-30]
                },
                 fills: {defaultFill: 'rgba(30,30,30,0.1)'},
                data: dataset,
                geographyConfig: {
                    borderColor: 'rgba(222,222,222,0.2)',
                    highlightBorderWidth: 1,
                    // don't change color on mouse hover
                    highlightFillColor: function(geo) {
                        return geo['fillColor'] || 'rgba(30,30,30,0.5)';
                    },
                    // only change border
                    highlightBorderColor: 'rgba(222,222,222,0.5)',
                    // show desired information in tooltip
                    popupTemplate: function(geo, data) {
                        // don't show tooltip if country don't present in dataset
                        if (!data) { return ; }
                        // tooltip content
                        return ['',
                        	'<div style="opacity:0.7;" class="hoverinfo">% of visitors in ' + geo.properties.name,
                                ': ' + data.percent,
                                ''].join('');        
                                
                    }
                }
            });
            
         
            //draw a legend for this map
            map.legend();
            
              map.graticule();
<script src="http://cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js"></script>
<script src="http://unilogue.github.io/js/map/datamaps.world.min.js"></script>

<div id="world" style="fill-opacity:0.7; height: 600px; width: 500px; margin-top:-100px;"></div>

编辑:显然完成的回调允许您使用事件,我创建了这个缩放/平移函数作为测试,但是我如何使用它来使用 d3.behavior.drag 和欧拉 Angular 旋转我的 map ?

var map = new Datamap({
                done: function(datamap) {
            datamap.svg.call(d3.behavior.zoom().on("zoom", redraw));
            function redraw() {
                datamap.svg.selectAll("g").attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
            }
        },

编辑2:这看起来可行!取自here .

我试过copying它进入完成回调但什么也没发生,有什么想法吗?

    var dragBehaviour = d3.behavior.drag()
    .on('drag', function(){
        var dx = d3.event.dx;
        var dy = d3.event.dy;

        var rotation = projection.rotate();
        var radius = projection.scale();
        var scale = d3.scale.linear()
            .domain([-1 * radius, radius])
            .range([-90, 90]);
        var degX = scale(dx);
        var degY = scale(dy);
        rotation[0] += degX;
        rotation[1] -= degY;
        if (rotation[1] > 90)   rotation[1] = 90;
        if (rotation[1] < -90)  rotation[1] = -90;

        if (rotation[0] >= 180) rotation[0] -= 360;
        projection.rotate(rotation);
        redraw();
    })

最佳答案

var livemap;
scope.rotation = [97, -30];

function redraw() {
  d3.select("#map-wrapper").html('');
  init();
}// redraw


function init() {
  livemap = new Datamap({...})

  var drag = d3.behavior.drag().on('drag', function() {
    var dx = d3.event.dx;
    var dy = d3.event.dy;

    var rotation = livemap.projection.rotate();
    var radius = livemap.projection.scale();
    var scale = d3.scale.linear().domain([-1 * radius, radius]).range([-90, 90]);
    var degX = scale(dx);
    var degY = scale(dy);
    rotation[0] += degX;
    rotation[1] -= degY;
    if (rotation[1] > 90) rotation[1] = 90;
    if (rotation[1] < -90) rotation[1] = -90;

    if (rotation[0] >= 180) rotation[0] -= 360;
    scope.rotation = rotation;
    redraw();
  })

 d3.select("#map-wrapper").select("svg").call(drag);

}// init

关于javascript - 通过光标拖动使 d3.js 数据图旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33850471/

相关文章:

javascript - 如何在 false 时触发函数?

javascript - jQuery XML : Count number of leaf nodes

html - 部分 SVG 蒙版不透明且颜色反转

javascript - 我在使用 javascript 中的回文函数时遇到问题

d3.js - D3 在将 .data() 设置为附加元素时抛出 "Cannot read property ' ownerDocument' of null"

javascript - 如何在 d3 js 中进行 map 重投影的平滑过渡

javascript - 图片显示在 chrome 但不是 safari

javascript - SVG 矩形的一致填充

javascript - 如何防止默认工具提示悬停在 svg 元素上?

javascript - 获取与正则表达式匹配的 HTML 单词的 XPATH