Skip to content

Commit 47f62a6

Browse files
committed
✨ upgrade to use pyecharts 1.x
1 parent 049b7e7 commit 47f62a6

File tree

1 file changed

+36
-68
lines changed

1 file changed

+36
-68
lines changed

pyecharts_extras/choropleth_map.py

Lines changed: 36 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,56 @@
11
# coding=utf-8
2-
from pyecharts.charts.map import Map
2+
from pyecharts import options as opts
3+
from pyecharts.commons.types import Union, Optional, Sequence, Numeric
4+
from pyecharts.charts import Map
35

46

57
class ChoroplethMap(Map):
6-
def get_series_options(self, index):
7-
return self._option["series"][index]
88

99
def add(
1010
self,
11-
name,
12-
attr,
13-
value,
14-
choropleth_legend,
15-
maptype="china",
16-
is_roam=True,
17-
is_map_symbol_show=False,
18-
name_map=None,
19-
**kwargs
11+
series_name: str,
12+
data_pair: Sequence,
13+
maptype: str = "china",
14+
*,
15+
choropleth_legend: Sequence = None,
16+
**kwds
2017
):
21-
"""
22-
it must use piece wise's visual map,otherwise visual_range_color takes
23-
no effect. visual_range_text default ['Legend'] and
24-
visual_text_color defaults to ['black']
25-
26-
:param name:
27-
series name for tooltip and legend
28-
:param attr:
29-
attributes
30-
:param value:
31-
values for corresponding attributes above
32-
:param choropleth_legend:
33-
custom legend names
34-
:param maptype:
35-
custom geo shape names.
36-
:param is_roam:
37-
mouse control, default true, scale & move
38-
set 'scale' if zooming, set 'move' if moving
39-
:param is_map_symbol_show:
40-
remove red map symbol
41-
:param name_map:
42-
custom choropleth name instead of region name in the map.
43-
:param kwargs:
44-
45-
"""
46-
_classes = list(set(value))
47-
_class_indices = []
48-
for __item__ in value:
49-
# exchange text value with unique internal index
50-
__index__ = _classes.index(__item__)
51-
_class_indices.append(__index__)
52-
5318
_piece_specs = []
5419
_piece_colors = []
55-
for __label__ in choropleth_legend:
20+
_classes = []
21+
for __index, __label in enumerate(choropleth_legend):
5622
# associate label and color with the
5723
# unique internal index
58-
__index__ = _classes.index(__label__["tag"])
24+
_classes.append(__label["tag"])
5925
_piece_specs.append(
6026
{
61-
"min": __index__,
62-
"max": __index__ + 0.1,
63-
"label": __label__["label"],
27+
"min": __index,
28+
"max": __index + 0.1,
29+
"label": __label["label"],
6430
}
6531
)
66-
_piece_colors.append(__label__["color"])
32+
_piece_colors.append(__label["color"])
33+
34+
_old_data_pair = list(data_pair)
35+
_new_data_pair = []
36+
for key, value in _old_data_pair:
37+
# exchange text value with unique internal index
38+
__index__ = _classes.index(value)
39+
_new_data_pair.append([key, __index__])
6740

6841
# dictate the arguments for Map.add()
69-
kwargs["is_piecewise"] = True
70-
kwargs["is_visualmap"] = True
71-
kwargs["visual_range_color"] = _piece_colors
72-
if "visual_range_text" not in kwargs:
73-
kwargs["visual_range_text"] = ["Legend"]
74-
if "visual_text_color" not in kwargs:
75-
kwargs["visual_text_color"] = ["black"]
42+
custom = opts.VisualMapOpts(
43+
is_piecewise=True,
44+
range_text=["Legend"],
45+
range_color=_piece_colors,
46+
pieces=_piece_specs
47+
)
48+
self.options.update(visualMap=custom)
7649

77-
Map.add(
78-
self,
79-
name,
80-
attr,
81-
_class_indices,
82-
maptype=maptype,
83-
is_roam=is_roam,
84-
is_map_symbol_show=is_map_symbol_show,
85-
name_map=name_map,
86-
pieces=_piece_specs,
87-
**kwargs
50+
super(ChoroplethMap, self).add(
51+
series_name,
52+
_new_data_pair,
53+
maptype,
54+
**kwds
8855
)
56+
return self

0 commit comments

Comments
 (0)