GithubHelp home page GithubHelp logo

d3-map's Introduction

投影函数:转换经纬度

var projection = d3.geo.mercator() .center([107, 31]) //中心位置 .scale(850) //放大比例 .translate([width/2, height/2]); //设置平移

路径生成器:根据地图数据生成 SVG

var path = d3.geo.path() .projection(projection); //projection() 是设定生成器的投影函数,把上面定义的投影传入即可,计算路径时,会自己加入投影的影响。

向服务器请求文件并绘制地图

d3.json("china.json", function(error, root) {

if (error)
    return console.error(error);
console.log(root.features);

svg.selectAll("path")
    .data( root.features )
    .enter()
    .append("path")
    .attr("stroke","#000")
    .attr("stroke-width",1)
    .attr("fill", function(d,i){
        return color(i);
    })
    .attr("d", path )   //使用地理路径生成器
    .on("mouseover",function(d,i){
                d3.select(this)
                   .attr("fill","yellow");
            })
            .on("mouseout",function(d,i){
                d3.select(this)
                   .attr("fill",color(i));
            });

}); d3.json() 不能直接读取本地文件,因此你需要搭建一个服务器,例如 Apache

d3-map's People

Watchers

sixben avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.