可以看一下这个例子哈:
import { Chart } from '@antv/g2';
import { feature } from 'topojson';
Promise.all([
// fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/china-provinces.geo.json')
fetch('https://geojson.cn/api/data/china.topo.json') // t
// fetch('https://geojson.cn/api/data/china.json')
.then((res) => res.json()),
]).then((values) => {
const [chian] = values;
// topo 转换成 geo json
const counties = feature(chian, chian.objects.default).features;
const chart = new Chart({
container: 'container',
theme: 'classic',
width: 1000,
height: 1000,
});
const geoView = chart.geoView().coordinate({ type: 'mercator' });
const nameValue = {
新疆:100,
}
geoView
.geoPath()
.data(counties)
.encode('latitude', 'latitude')
.encode('longitude', 'longitude')
.encode('color', (d) => nameValue[d.properties.name] || 0)
.scale("color", {
relations:[[0, '#fff']]
})
.style("stroke", "#aaa")
// .scale('color', {
// palette: 'ylGnBu',
// unknown: '#fff',
// });
geoView
.text()
.data(counties.filter((d) => d.properties.centroid))
.encode('x', (d) => {
const [x] = d.properties.centroid;
return x;
})
.encode('y', (d) => {
const [_, y] = d.properties.centroid;
return y;
})
.encode('text', (d) => {
const name = d.properties.name;
console.log('name', name);
return d.properties.name;
})
// .style('color', 'black')
.style('fontSize', 12)
.style('textAlign', 'center')
.style("fill", d => (nameValue[d.properties.name] || 0) === 0 ? 'black':"white")
.style('opacity', 1);
geoView.legend(false);
chart.render();
});