|
1 | 1 | # 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 |
3 | 5 |
|
4 | 6 |
|
5 | 7 | class ChoroplethMap(Map): |
6 | | - def get_series_options(self, index): |
7 | | - return self._option["series"][index] |
8 | 8 |
|
9 | 9 | def add( |
10 | 10 | 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 |
20 | 17 | ): |
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 | | - |
53 | 18 | _piece_specs = [] |
54 | 19 | _piece_colors = [] |
55 | | - for __label__ in choropleth_legend: |
| 20 | + _classes = [] |
| 21 | + for __index, __label in enumerate(choropleth_legend): |
56 | 22 | # associate label and color with the |
57 | 23 | # unique internal index |
58 | | - __index__ = _classes.index(__label__["tag"]) |
| 24 | + _classes.append(__label["tag"]) |
59 | 25 | _piece_specs.append( |
60 | 26 | { |
61 | | - "min": __index__, |
62 | | - "max": __index__ + 0.1, |
63 | | - "label": __label__["label"], |
| 27 | + "min": __index, |
| 28 | + "max": __index + 0.1, |
| 29 | + "label": __label["label"], |
64 | 30 | } |
65 | 31 | ) |
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__]) |
67 | 40 |
|
68 | 41 | # 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) |
76 | 49 |
|
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 |
88 | 55 | ) |
| 56 | + return self |
0 commit comments