I'm new to react and d3 and I'm trying to implement a simple SVG map with D3 v6.2 and ReactJS, which will indicate the location of some cities. Each city is being indicated by circles as shown below:
I need to change this marking to put a pin with the name of the city (using a div if possible) as shown in this image:
But the point's symbol shape is determined by D3-path itself and I can't find anything that can change or replace with another markup.
Note: I tried to put a div within the path tags of the points, but I realized that they were not rendered and the only change I was able to make was to change the radius of the mark with pointRadius () but I don't want to do that.
Code:
import React, { useEffect, useRef, useState } from 'react';
import { select, geoPath, geoMercator } from "d3";
import useResizeObserver from 'use-resize-observer';
import drawMap from './mapa.json';
import dataMap from './city.json';
const svgRef = useRef(null);
const wrapperRef = useRef(null);
const { widthRef, heightRef } = useResizeObserver({ wrapperRef });
function map(){
useEffect(() => {
const svg = select(svgRef.current);
const { width, height } = wrapperRef.current.getBoundingClientRect();
const projection = geoMercator().fitSize([width, height], selectedEstados || drawMap);
let pathGenerator = geoPath().projection(projection).pointRadius(4);
console.log("path: " + pathGenerator)
svg.selectAll(".estado")
.data(drawMap.features)
.join("path")
.attr("class", "estado")
.attr("d", features => pathGenerator(features));
svg.selectAll(".city")
.data(dataMap.features)
.join("path")
.attr("class", "city")
.attr("d", features => pathGenerator(features))
}, [drawMap, wrapperRef, widthRef, heightRef])
return(
<div className='map' ref={wrapperRef}>
<svg ref={svgRef}></svg>
</div>
)
}
city.json
{
"type": "FeatureCollection",
"name": "city",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [
{
"type": "Feature",
"properties": {
"municipio": "sapé"
},
"geometry": {
"type": "Point",
"coordinates": [
-41.821476100786512,
-8.282815195947387
]
}
},
{
"type": "Feature",
"properties": {
"municipio": "muito distante"
},
"geometry": {
"type": "Point",
"coordinates": [
-41.065329661162544,
-7.183034162673664
]
}
}
]
}
mapa.json

