forked from DogLooksGood/py-echarts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
205 lines (190 loc) · 5.86 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# -*- coding: utf-8 -*-
import json
class Chart(dict):
"""
图表模板
"""
def __init__(self):
super(Chart, self).__init__()
self["calculable"] = True
self["tooltip"] = {"show": True}
self["toolbox"] = {
"show": True,
"x": "left",
"feature": {
"dataView": {
"show": True,
"readOnly": False
},
"magicType": {
"show": True,
"type": ["line", "bar"]
},
"restore": {
"show": True
},
"saveAsImage": {
"show": True
},
"dataZoom": {
"show": True,
"title": {
"dataZoom": u"区域缩放",
"dataZoomReset": u"区域缩放后退"
}
}
}
}
self["legend"] = {
"show": True,
"data": []
}
self["series"] = []
def title(self, x="center", **kwargs):
"""
设置图表标题
"""
self["title"].update({
"x": x
})
self["title"].update(kwargs)
return self
def tooltip(self, show=True, trigger='axis', formatter=None, **kwargs):
"""
设置提示信息
"""
self["tooltip"].update({
"show": show,
"trigger": trigger
})
if formatter is not None:
self["tooltip"].update({"formatter": formatter})
self["tooltip"].update(kwargs)
return self
def legend(self, show=True, data=None, orient='horizontal', **kwargs):
"""
设置图例
`data`: [u"图例1", u"图例2", u"图例3"]
`orient`: "vertical"|"horizontal"
"""
data = [] if data is None else data
self["legend"].update({
"show": show,
"data": data,
"orient": orient
})
self["legend"].update(kwargs)
return self
def toolbox(self, show=True, x='left', **kwargs):
"""
设置工具箱
"""
self["toolbox"].update({
"show": show,
"x": x
})
self["toolbox"].update(kwargs)
return self
def pie(self, name, data=None, radius="55%", center=None, auto_legend=True, **kwargs):
"""
添加一个饼图
`data`: {u"名称": 100}, u"名称2": 200}
"""
center = ["50%", "60%"] if center is None else center
data = {} if data is None else data
self["series"].append(self.__merge_dict({
"type": "pie",
"name": name,
"radius": radius,
"center": center,
"data": [{"name": n, "value": v} for n, v in data.items()]
}, kwargs))
if auto_legend:
legend_data = self["legend"]["data"]
[legend_data.append(x) for x in data if x not in legend_data]
return self
def bar(self, name, data=None, auto_legend=True, y_axis_index=0, **kwargs):
"""
添加一个柱状图
`data`: [10, 20, 30, 40]
`auto_legend`: 自动生成图例
"""
data = [] if data is None else data
self["series"].append(self.__merge_dict({
"type": "bar",
"name": name,
"data": data,
"yAxisIndex": y_axis_index
}, kwargs))
if "yAxis" not in self:
self.y_axis()
if name not in self["legend"]["data"] and auto_legend:
self["legend"]["data"].append(name)
return self
def line(self, name, data=None, mark_max_point=False, mark_min_point=False, show_item_label=False, auto_legend=True, y_axis_index=0, **kwargs):
"""
添加一个折线图
`data`: [10, 20, 30, 40]
"""
data = [] if data is None else data
mark_point = []
if mark_max_point:
mark_point.append({"type": "max", "name": "最大值"})
if mark_min_point:
mark_point.append({"type": "min", "name": "最小值"})
self["series"].append(self.__merge_dict({
"type": "line",
"name": name,
"data": data,
"markPoint": {
"data":mark_point
},
"itemStyle": {
"normal": {
"label": {"show": show_item_label}
}
},
"yAxisIndex": y_axis_index
}, kwargs))
if "yAxis" not in self:
self.y_axis()
if name not in self["legend"]["data"] and auto_legend:
self["legend"]["data"].append(name)
return self
def x_axis(self, data=None, type_="category", name="", **kwargs):
"""
添加X轴
"""
data = [] if data is None else data
if "xAxis" not in self:
self["xAxis"] = []
self["xAxis"].append(self.__merge_dict({
"type": type_,
"name": name,
"data": data
}, kwargs))
return self
def y_axis(self, data=None, type_="value", name="", formatter=None, **kwargs):
"""
添加X轴
"""
if "yAxis" not in self:
self["yAxis"] = []
self["yAxis"].append(self.__merge_dict({
"type": type_,
"name": name,
}, {"axisLabel": {"formatter": formatter}} if formatter is not None else {}, kwargs))
if data is not None:
self["yAxis"] = data
return self
@staticmethod
def __merge_dict(*args):
"""
合并多个字典并返回
"""
return reduce(lambda x, y: dict(x.items() + y.items()), args)
def main():
c = Chart().tooltip()
print json.dumps(c)
if __name__ == "__main__":
main()