Skip to content

Commit 57658fa

Browse files
authored
Merge pull request #3625 from VisActor/refactor/map-label
Refactor/map label
2 parents 264082d + ecb1484 commit 57658fa

File tree

28 files changed

+952
-105
lines changed

28 files changed

+952
-105
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@visactor/vchart",
5+
"comment": "refactor: migrate mapLabel component into vchart-extension package",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@visactor/vchart"
10+
}
Lines changed: 322 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,322 @@
1+
# Map Label Component In Scatter Map
2+
3+
The scatter map label component is an unconventional map label layout method. The labels are in the form of the [MarkPoint component](/vchart/guide/tutorial_docs/Chart_Concepts/marker), and are laid out around the perimeter of the map as much as possible. Currently, it is only recommended for use with maps of China.
4+
5+
![img](/vchart/guide/extension/map-label.png)
6+
7+
## How to Use This Component
8+
9+
The scatter map is supported by default in VChart and can be implemented through the composite chart (Common Chart).
10+
11+
```js
12+
const spec = {
13+
type: 'common',
14+
padding: 50,
15+
region: [
16+
{
17+
roam: true,
18+
coordinate: 'geo',
19+
longitudeField: 'lon',
20+
latitudeField: 'lat'
21+
}
22+
],
23+
series: [
24+
{
25+
type: 'map',
26+
map: 'china',
27+
nameField: 'name',
28+
valueField: 'value',
29+
seriesField: 'name',
30+
},
31+
{
32+
regionIndex: 0,
33+
id: 'scatter',
34+
type: 'scatter',
35+
data: {
36+
values: [
37+
...
38+
]
39+
},
40+
xField: 'name',
41+
yField: 'value',
42+
}
43+
]
44+
};
45+
```
46+
47+
The map label component needs to be registered on the chart. The registration and usage are as follows:
48+
49+
```js
50+
import VChart from '@visactor/vchart';
51+
import { registerMapLabel } from '@visactor/vchart-extension';
52+
53+
const spec = {
54+
// your spec
55+
type: 'common',
56+
...,
57+
mapLabel: {
58+
visible: true,
59+
position: 'outer',
60+
seriesId: 'scatter',
61+
nameField: 'name',
62+
valueField: 'value',
63+
space: 6,
64+
nameLabel: {},
65+
valueLabel: {},
66+
leader: {},
67+
background: {}
68+
}
69+
};
70+
registerMapLabel();
71+
72+
const vchart = new VChart(spec, { dom: 'chart' });
73+
vchart.renderSync();
74+
```
75+
76+
If using the CDN import method, the registration method is as follows:
77+
78+
```html
79+
<script src="https://cdn.jsdelivr.net/npm/@visactor/vchart/build/index.min.js"></script>
80+
<script src="https://cdn.jsdelivr.net/npm/@visactor/vchart-extension/build/index.min.js"></script>
81+
<script>
82+
const spec = {
83+
// your spec
84+
};
85+
VChartExtension.registerMapLabel();
86+
87+
const vchart = new VChart.default(spec, { dom: 'chart' });
88+
vchart.renderSync();
89+
</script>
90+
```
91+
92+
## Related Configuration Options
93+
94+
```ts
95+
export interface IMapLabelSpec extends IMapLabelStyleSpec {
96+
/** Associated series ID */
97+
seriesId: StringOrNumber;
98+
/** Data field for the name text */
99+
nameField?: string;
100+
/** Data field for the value text */
101+
valueField?: string;
102+
/**
103+
* Interaction trigger type
104+
* @default 'none'
105+
*/
106+
trigger?: 'hover' | 'click' | 'none';
107+
}
108+
109+
export interface IMapLabelStyleSpec {
110+
/**
111+
* Whether to display
112+
* @default false
113+
*/
114+
visible?: boolean;
115+
/**
116+
* The spacing between the label and the marker point when the label position is not 'outer'
117+
* @default 12
118+
*/
119+
offset?: number;
120+
/**
121+
* The distance between the icon and the label
122+
* @default 10
123+
*/
124+
space?: number;
125+
/**
126+
* Label position. Supports 'left' | 'top' | 'right' | 'bottom' | 'outer'
127+
* @default 'top'
128+
*/
129+
position?: LabelPosition;
130+
/** Settings for the name text style */
131+
nameLabel?: {
132+
visible?: boolean;
133+
style?: ITextMarkSpec;
134+
};
135+
/** Settings for the value text style */
136+
valueLabel?: {
137+
visible?: boolean;
138+
style?: ITextMarkSpec;
139+
};
140+
/** Icon style settings */
141+
icon?: {
142+
visible?: boolean;
143+
style?: ISymbolMarkSpec;
144+
};
145+
/** Background style settings */
146+
background?: {
147+
/** @default true */
148+
visible?: boolean;
149+
/** Background frame padding */
150+
padding?: IPadding;
151+
style?: IRectMarkSpec;
152+
};
153+
/** Leader line style settings */
154+
leader?: {
155+
visible?: boolean;
156+
style?: IPathMarkSpec;
157+
};
158+
}
159+
```
160+
161+
## Example
162+
163+
```javascript livedemo
164+
const spec = {
165+
type: 'common',
166+
padding: 50,
167+
region: [
168+
{
169+
roam: true,
170+
coordinate: 'geo',
171+
longitudeField: 'lon',
172+
latitudeField: 'lat'
173+
}
174+
],
175+
mapLabel: {
176+
visible: true,
177+
position: 'outer',
178+
seriesId: 'scatter',
179+
nameField: 'name',
180+
valueField: 'value',
181+
space: 6,
182+
icon: {
183+
visible: true,
184+
style: {
185+
size: 20,
186+
dx: -8,
187+
dy: -10,
188+
fill: 'red',
189+
symbolType: `<svg t="1735530419948" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="18309" width="200" height="200"><path d="M512 14.04046698C285.51594444 14.04046698 101.91573591 198.69592179 101.91573591 426.58535144c0 113.88587391 45.92898631 216.99267454 120.09577941 291.68660449L512 1009.95953302l289.98751211-291.68757709a412.48652979 412.48652979 0 0 0 120.09675198-291.68660449C922.08426409 198.69592179 738.48308299 14.04046698 512 14.04046698z m0 595.7939926a182.78032593 182.78032593 0 0 1-182.31251628-183.36678999A182.72197129 182.72197129 0 0 1 512 243.21856145a182.78032593 182.78032593 0 0 1 182.31154372 183.36678999A182.72197129 182.72197129 0 0 1 512 609.89281421z" fill="#F96C65" p-id="18310"></path></svg>`
190+
}
191+
},
192+
nameLabel: {
193+
style: { fill: '#1677ff' }
194+
},
195+
valueLabel: {
196+
style: { fill: '#1677ff' }
197+
},
198+
leader: {
199+
visible: true,
200+
style: { stroke: '#1677ff' }
201+
},
202+
background: {
203+
style: {
204+
fill: '#e6f4ff',
205+
stroke: '#1677ff'
206+
}
207+
}
208+
},
209+
series: [
210+
{
211+
type: 'map',
212+
map: 'china',
213+
nameField: 'name',
214+
valueField: 'value',
215+
seriesField: 'name',
216+
nameMap: {
217+
广东省: '广东',
218+
江苏省: '江苏',
219+
山东省: '山东',
220+
河南省: '河南',
221+
河北省: '河北',
222+
浙江省: '浙江',
223+
四川省: '四川',
224+
安徽省: '安徽',
225+
辽宁省: '辽宁',
226+
陕西省: '陕西',
227+
山西省: '山西',
228+
湖北省: '湖北',
229+
北京市: '北京',
230+
湖南省: '湖南',
231+
黑龙江省: '黑龙江',
232+
福建省: '福建',
233+
内蒙古自治区: '内蒙古',
234+
云南省: '云南',
235+
江西省: '江西',
236+
重庆市: '重庆',
237+
上海市: '上海',
238+
贵州省: '贵州',
239+
吉林省: '吉林',
240+
天津市: '天津',
241+
广西壮族自治区: '广西',
242+
甘肃省: '甘肃',
243+
新疆维吾尔自治区: '新疆',
244+
宁夏回族自治区: '宁夏',
245+
海南省: '海南',
246+
青海省: '青海',
247+
西藏自治区: '西藏',
248+
香港特别行政区: '中国香港',
249+
台湾省: '中国台湾',
250+
澳门特别行政区: '澳门'
251+
},
252+
area: {
253+
style: {
254+
fill: '#E9F2FF',
255+
stroke: 'grey',
256+
lineWidth: 1
257+
}
258+
}
259+
},
260+
{
261+
regionIndex: 0,
262+
id: 'scatter',
263+
type: 'scatter',
264+
data: {
265+
values: [
266+
{
267+
name: '乌鲁木齐',
268+
value: '2197',
269+
lat: '43.50',
270+
lon: '87.37'
271+
},
272+
{
273+
name: '济南',
274+
value: '2197',
275+
lat: '36.606472',
276+
lon: '117.164382'
277+
},
278+
{
279+
name: '江苏',
280+
value: '21197',
281+
lat: '32.983908',
282+
lon: '119.486395'
283+
},
284+
{
285+
name: '成都',
286+
value: '21197',
287+
lat: '30.659462',
288+
lon: '104.065735'
289+
}
290+
]
291+
},
292+
xField: 'name',
293+
yField: 'value',
294+
point: {
295+
style: {
296+
fill: '#1677ff',
297+
outerBorder: {
298+
distance: 2,
299+
lineWidth: 3,
300+
strokeOpacity: 0.2
301+
},
302+
fillOpacity: 1,
303+
size: 10
304+
}
305+
}
306+
}
307+
]
308+
};
309+
310+
const mapURL = 'https://geo.datav.aliyun.com/areas_v3/bound/100000_full.json';
311+
const res = await fetch(mapURL);
312+
const geojson = await res.json();
313+
VChart.registerMap('china', geojson, {
314+
rewind: true
315+
});
316+
317+
VChartExtension.registerMapLabel();
318+
const vchart = new VChart(spec, { dom: CONTAINER_ID });
319+
vchart.renderSync();
320+
// Just for the convenience of console debugging, DO NOT COPY!
321+
window['vchart'] = vchart;
322+
```

docs/assets/guide/menu.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,13 @@
898898
"zh": "转化率漏斗图",
899899
"en": "Conversion Funnel"
900900
}
901+
},
902+
{
903+
"path": "map_label",
904+
"title": {
905+
"zh": "地图标签组件",
906+
"en": "Map Label"
907+
}
901908
}
902909
]
903910
}

0 commit comments

Comments
 (0)