react d3 中国地图的简单使用 尚未完善
refer: http://siwei.me/blog/posts/d3-svg-path
https://observablehq.com/@d3/testing-projection-visibility?collection=@d3/d3-geo
1.首先安装:
npm install d3 topojson
或者使用
yarn add d3 topojson
2.
import React, { useEffect, useRef } from 'react';
import * as d3 from 'd3';
import china from './china.json'; // 导入拓扑数据3.同样,创建钩子:
const ChinaMap = () => {
const svgRef = useRef(null);
useEffect(() => {
const svgElement = d3.select(svgRef.current);
// 创建地理投影
const projection = d3
.geoMercator()
.center([105, 38])
.scale(600)
.translate([500, 500]);
// 创建路径生成器
const pathGenerator = d3.geoPath().projection(projection);
// 渲染地图路径
svgElement
.selectAll('path')
.data(china.features)
.enter()
.append('path')
.attr('d', pathGenerator)
.attr('stroke', '#333')
.attr('fill', 'lightblue');
}, []);
return <svg ref={svgRef} width="1000" height="1000" />;
};上面的例子里使用了 D3 的
selectprojectionpathGenerator
多写几个例子应该会熟悉一点。
4.最后svg来渲染: