JavaScript d3.js小练习中国地图
chinamap.html和china.json两个文件都放到自己的web服务上进行访问,比如nginx.
使用docker镜像搭建简单的静态nginx服务器
https://www.cnblogs.com/v5captain/p/14162506.html
http://localhost/chinamap.html
https://files.cnblogs.com/files/v5captain/china.json 右键保存该文件到本地.
chinamap.html文件内容:
<html> <head> </head> <body> <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> china map:<br> <script> var w = 500*4; var h = 300*4; var color = d3.scale.category10(); //Define default path generator var path = d3.geo.path(); //Create SVG element var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); var projection = d3.geo.mercator() .center([107, 31]) .scale(850) .translate([w/2, h/2]); var path = d3.geo.path() .projection(projection); d3.json("http://localhost/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)); }); }); </script> </body> </html>
参考:极客学院D3.js教程 https://wiki.jikexueyuan.com/project/d3wiki/chinamap.html
分类:
前端js vue
标签:
JavaScript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)