限时优惠活动
亲爱的苦力吧用户,我们为了回馈新老用户一直以来的鼎力支持,即日起(2025-02-06至2025-03-06)凡是购买苦力吧VIP/充值K币的新老用户,都直接可获得买一送一的优惠馈赠。例如:购买一个月的VIP会员可直接获得两个月的VIP会员;充值100K币可直接获得200K币,以此类推!有任何疑问可联系在线客服,感谢各位用户对苦力吧素材的信任与厚爱,我们将一如既往的给大家上新更多优质的素材源码,祝大家开工大吉、工作顺利、心想事成。

javascript基于D3.js库创建的可视化世界地图特效

所属分类: 网页特效-地图&标注    2024-03-06 08:36:27

javascript基于D3.js库创建的可视化世界地图特效 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

javascript基于D3.js库创建的可视化世界地图特效(共4个文件)

    • index.html

使用方法

  • code
  • source
  1. const svg = d3.select('svg');
  2. const projection = d3.geoNaturalEarth1();
  3. const pathGenerator = d3.geoPath().projection(projection);
  4. const g = svg.append('g');
  5. const colorLegendG = svg.append('g')
  6. .attr('transform', `translate(40,310)`);
  7. g.append('path')
  8. .attr('class', 'sphere')
  9. .attr('d', pathGenerator({type: 'Sphere'}));
  10. svg.call(d3.zoom().on('zoom', () => {
  11. g.attr('transform', d3.event.transform);
  12. }));
  13. const colorScale = d3.scaleOrdinal();
  14. const colorValue = d => d.properties.economy;
  15. const loadAndProcessData = () =>
  16. Promise.all([
  17. d3.tsv('https://unpkg.com/world-atlas@1.1.4/world/50m.tsv'),
  18. d3.json('https://unpkg.com/world-atlas@1.1.4/world/50m.json')
  19. ])
  20. .then(([tsvData, topoJSONdata]) => {
  21. const rowById = tsvData.reduce((accumulator, d) => {
  22. accumulator[d.iso_n3] = d;
  23. return accumulator;
  24. }, {});
  25. const countries = topojson.feature(topoJSONdata, topoJSONdata.objects.countries);
  26. countries.features.forEach(d =>{
  27. Object.assign(d.properties, rowById[d.id])
  28. });
  29. return countries;
  30. });
  31. const colorLegend = (selection, props) => {
  32. const {
  33. colorScale,
  34. circleRadius,
  35. spacing,
  36. textOffset,
  37. backgroundRectWidth
  38. } = props;
  39. const backgroundRect = selection.selectAll('rect')
  40. .data([null]);
  41. const n = colorScale.domain().length;
  42. backgroundRect.enter()
  43. .append('rect')
  44. .merge(backgroundRect)
  45. .attr('x', -circleRadius * 2)
  46. .attr('y', -circleRadius * 2)
  47. .attr('rx', circleRadius * 2)
  48. .attr('width', backgroundRectWidth)
  49. .attr('fill', 'white')
  50. .attr('height', spacing * n + circleRadius * 2)
  51. const groups = selection.selectAll('.tick')
  52. .data(colorScale.domain());
  53. const groupsEnter = groups
  54. .enter().append('g')
  55. .attr('class', 'tick');
  56. groupsEnter
  57. .merge(groups)
  58. .attr('transform', (d, i) =>
  59. `translate(0, ${i * spacing})`
  60. );
  61. groups.exit().remove();
  62. groupsEnter.append('circle')
  63. .merge(groups.select('circle'))
  64. .attr('r', circleRadius)
  65. .attr('fill', colorScale);
  66. groupsEnter.append('text')
  67. .merge(groups.select('text'))
  68. .text(d => d)
  69. .attr('dy', '0.32em')
  70. .attr('x', textOffset);
  71. }
  72. loadAndProcessData().then(countries => {
  73. console.log(colorScale);
  74. colorScale.domain(countries.features.map(colorValue))
  75. .domain(colorScale.domain().sort().reverse())
  76. .range(d3.schemeSpectral[colorScale.domain().length]);
  77. colorLegendG.call(colorLegend, {
  78. colorScale,
  79. circleRadius: 8,
  80. spacing: 20,
  81. textOffset: 15,
  82. backgroundRectWidth: 240
  83. });
  84. g.selectAll('path').data(countries.features)
  85. .enter().append('path')
  86. .attr('class', 'country')
  87. .attr('d', pathGenerator)
  88. .attr('fill', d => colorScale(colorValue(d)))
  89. .append('title')
  90. .text(d => d.properties.name + ' : ' + colorValue(d));
  91. });
const svg = d3.select('svg');

const projection = d3.geoNaturalEarth1();
const pathGenerator = d3.geoPath().projection(projection);

const g = svg.append('g');

const colorLegendG = svg.append('g')
   .attr('transform', `translate(40,310)`);


g.append('path')
    .attr('class', 'sphere')
    .attr('d', pathGenerator({type: 'Sphere'}));

svg.call(d3.zoom().on('zoom', () => {
  g.attr('transform', d3.event.transform);
}));
 const colorScale = d3.scaleOrdinal();

 const colorValue = d => d.properties.economy;

const loadAndProcessData = () =>

  Promise.all([
    d3.tsv('https://unpkg.com/world-atlas@1.1.4/world/50m.tsv'),
    d3.json('https://unpkg.com/world-atlas@1.1.4/world/50m.json')
  ])
  
  .then(([tsvData, topoJSONdata]) => {
  
  const rowById = tsvData.reduce((accumulator, d) => {
    accumulator[d.iso_n3] = d;
    return accumulator;
  }, {});
  
  const countries = topojson.feature(topoJSONdata, topoJSONdata.objects.countries);
    
     countries.features.forEach(d =>{
   Object.assign(d.properties, rowById[d.id])
  });
  
  return countries;
  
});

const colorLegend = (selection, props) => {
  const {
    colorScale,
    circleRadius,
    spacing,
    textOffset,
    backgroundRectWidth
  } = props;
  
  const backgroundRect = selection.selectAll('rect')
    .data([null]);
  
  const n = colorScale.domain().length;
  backgroundRect.enter()
    .append('rect')
    .merge(backgroundRect)
    .attr('x', -circleRadius * 2)
    .attr('y', -circleRadius * 2)
    .attr('rx', circleRadius * 2)
    .attr('width', backgroundRectWidth)
    .attr('fill', 'white')
    .attr('height', spacing * n + circleRadius * 2)

  const groups = selection.selectAll('.tick')
    .data(colorScale.domain());
  const groupsEnter = groups
    .enter().append('g')
      .attr('class', 'tick');
  groupsEnter
    .merge(groups)
      .attr('transform', (d, i) =>
        `translate(0, ${i * spacing})`
      );
  groups.exit().remove();
  
  groupsEnter.append('circle')
    .merge(groups.select('circle'))
      .attr('r', circleRadius)
      .attr('fill', colorScale);
  
  groupsEnter.append('text')
    .merge(groups.select('text'))
      .text(d => d)
      .attr('dy', '0.32em')
      .attr('x', textOffset);
}

  loadAndProcessData().then(countries => {
  console.log(colorScale);
 colorScale.domain(countries.features.map(colorValue))
    .domain(colorScale.domain().sort().reverse())
    .range(d3.schemeSpectral[colorScale.domain().length]);
    
  colorLegendG.call(colorLegend, {
  colorScale,
  circleRadius: 8,
  spacing: 20,
  textOffset: 15,
  backgroundRectWidth: 240
});
  
  g.selectAll('path').data(countries.features)
    .enter().append('path')
      .attr('class', 'country')
      .attr('d', pathGenerator)
      .attr('fill', d => colorScale(colorValue(d)))
    .append('title')
      .text(d => d.properties.name + ' : ' + colorValue(d));
  
});

站长提示:
1. 苦力吧素材官方QQ群:950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励2K币
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 地图&标注

jquery可动态着色添加图片的SVG地图插件

一个轻量级的SVG操作地图插件,可动态更改SVG图片的填充或背景色属性,带清除操作功能。
  地图&标注
 2371  0

div+css布局全中国地图各个城市分布营销网点

一款html中国地图矢量图特效,通常运用在公司集团营销布局统计图。
  地图&标注
 9484  0

jquery基于高德地图api开发实例

包含功能有:模糊地址搜索、搜索行车路径、搜索骑行路径、搜索步行路径。
  地图&标注
 3502  0

jquery可自定义的SVG缩放平移标注地图特效代码

一个高度自定义的缩放和平移插件,可使用鼠标拖动和鼠标滚轮平移和缩放svg自定义地图。
  地图&标注
 6389  0

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论