Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. // 用外層的 div 當 svg 的寬高
  2. const width = (this.$refs.map.offsetWidth).toFixed(),
  3. height = (this.$refs.map.offsetHeight).toFixed();
  4.  
  5. // 讓d3抓svg,並寫入寬高
  6. var svg = d3.select('#svg')
  7. .attr('width', width)
  8. .attr('height', height)
  9. .attr('viewBox', `0 0 ${width} ${height}`);
  10.  
  11. // 讓d3抓GeoJSON檔,並寫入path的路徑
  12. var url = 'dist/taiwan.geojson'; // GeoJSON的檔案路徑
  13. d3.json(url, (error, geometry) => {
  14. if (error) throw error;
  15.  
  16. svg
  17. .selectAll('path')
  18. .data(geometry.features)
  19. .enter().append('path')
  20. .attr('d', path)
  21. .attr({
  22. // 設定id,為了click時加class用
  23. id: (d) => 'city' + d.properties.COUNTYCODE
  24. })
  25. .on('click', d => {
  26. this.h1 = d.properties.COUNTYNAME; // 換中文名
  27. this.h2 = d.properties.COUNTYENG; // 換英文名
  28. // 有 .active 存在,就移除 .active
  29. if(document.querySelector('.active')) {
  30. document.querySelector('.active').classList.remove('active');
  31. }
  32. // 被點擊的縣市加上 .active
  33. document.getElementById('city' + d.properties.COUNTYCODE).classList.add('active');
  34. });
  35. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement