Skip to content

Commit 30273d9

Browse files
authored
Merge pull request #13 from kinegratii/dev
v0.3.3 released! A routine version
2 parents 42f6f73 + fda5c02 commit 30273d9

24 files changed

+494
-85
lines changed

django_echarts/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
A django app for Echarts integration with pyecharts as chart builder.
33
"""
44

5-
__version__ = '0.3.2'
5+
__version__ = '0.3.3'
66
__author__ = 'kinegratii'

django_echarts/datasets/fetch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from itertools import tee
77
from functools import partial
88

9-
__all__ = ['fetch', 'ifetch', 'fetch_single', 'ifetch_multiple']
9+
__all__ = ['fetch', 'ifetch', 'fetch_single', 'ifetch_multiple', 'ifetch_single']
1010

1111

1212
class Empty(object):

django_echarts/plugins/hosts.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
}
1313

1414
ECHARTS_MAP_HOSTS = {
15-
'pyecharts': 'https://pyecharts.github.io/jupyter-echarts/echarts',
16-
'echarts': 'http://echarts.baidu.com/asset/map/js'
15+
'echarts': 'http://echarts.baidu.com/asset/map/js',
16+
'china-provinces': 'https://echarts-maps.github.io/echarts-china-provinces-js/',
17+
'china-cities': 'https://echarts-maps.github.io/echarts-china-cities-js/',
18+
'united-kingdom': 'https://echarts-maps.github.io/echarts-united-kingdom-js'
1719
}
1820

1921

docs/changelog.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
更新日志
22
=========
33

4-
v0.3.2
5-
------
4+
v0.3.3 (20180404)
5+
-----------------
6+
7+
- 发布独立的 `fetch` 模块文档
8+
- 重写 example 项目的部分逻辑
9+
10+
v0.3.2 (20180313)
11+
-----------------
612

713
- 移除 Django 的显示依赖
814
- 移除对 `numpy.Array` 的默认json编码

docs/codes/backend_charts.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{% extends 'base.html' %}
2+
{% load echarts %}
3+
4+
{% block main_content %}
5+
<div class="row row-offcanvas row-offcanvas-right">
6+
<div class="col-xs-6 col-sm-2 sidebar-offcanvas" id="sidebar">
7+
<div class="list-group">
8+
<a href="?name=bar" class="list-group-item">柱形图(Bar)</a>
9+
<a href="?name=kine" class="list-group-item">K线图(KLine)</a>
10+
<a href="?name=map" class="list-group-item">地图(Map)</a>
11+
<a href="?name=pie" class="list-group-item">饼图(Pie)</a>
12+
</div>
13+
</div>
14+
<!--/.sidebar-offcanvas-->
15+
<div class="col-xs-12 col-sm-10">
16+
<p class="pull-right visible-xs">
17+
<button type="button" class="btn btn-primary btn-xs" data-toggle="offcanvas">Toggle nav</button>
18+
</p>
19+
{# 渲染容器 #}
20+
{% echarts_container echarts_instance %}
21+
22+
</div>
23+
<!--/.col-xs-12.col-sm-9-->
24+
</div>
25+
26+
{% endblock %}
27+
28+
{% block extra_script %}
29+
{# 渲染依赖文件 #}
30+
{% echarts_js_dependencies echarts_instance %}
31+
{# 渲染初始化文本 #}
32+
{% echarts_js_content echarts_instance %}
33+
{% endblock %}

docs/codes/bar_backend_view.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# coding=utf8
2+
3+
from pyecharts import Bar
4+
from django_echarts.views.backend import EChartsBackendView
5+
6+
7+
class BackendEChartsTemplate(EChartsBackendView):
8+
template_name = 'backend_charts.html'
9+
10+
def get_echarts_instance(self, *args, **kwargs):
11+
bar = Bar("我的第一个图表", "这里是副标题")
12+
bar.add("服装", ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"], [5, 20, 36, 10, 75, 90])
13+
return bar

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@
5555
# built documents.
5656
#
5757
# The short X.Y version.
58-
version = '0.3.2'
58+
version = '0.3.3'
5959
# The full version, including alpha/beta/rc tags.
60-
release = '0.3.2'
60+
release = '0.3.3'
6161

6262
# The language for content autogenerated by Sphinx. Refer to documentation
6363
# for a list of supported languages.

docs/data_builder.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ zip 函数
2727
fetch 函数
2828
+++++++++++
2929

30+
更多请查看 :doc:`fetch` 。
31+
3032
自 v0.2.1 起,新增 `django_echarts.datasets.fetch.fetch` 函数,该函数是对原有 pluck + zip 函数的进一步封装。
3133

3234

docs/fetch.rst

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
fetch模块文档
2+
==============
3+
4+
概述
5+
-----
6+
7+
`django_echarts.datasets.fetch` 模块实现了从数据列表按照指定的一个或多个属性/键选取数据。
8+
9+
`fetch` 模块包含了以下几个函数:
10+
11+
- `fetch(iterable, key, *keys, default=EMPTY, defaults=None, getter=None)`
12+
- `ifetch(iterable, key, *keys, default=EMPTY, defaults=None, getter=None)`
13+
- `fetch_single(iterable, key, default=EMPTY, getter=None)`
14+
- `ifetch_multiple(iterable, *keys, defaults=None, getter=None)`
15+
- `ifetch_single(iterable, key, default=EMPTY, getter=None)`
16+
17+
各个参数意义如下:
18+
19+
- iterable:数据列表
20+
- key / keys:键值、属性访问方式的索引
21+
- default:默认值,用于选择单个属性
22+
- defaults:默认值字典,用于选择多个属性
23+
- getter:自定义访问回调函数
24+
25+
通常使用 `fetch` 函数即可。
26+
27+
基本使用
28+
--------
29+
30+
选取单个属性
31+
++++++++++++
32+
33+
从 `objects` 数据获取 `name` 的数据。
34+
35+
::
36+
37+
from django_echarts.datasets.fetch import fetch
38+
39+
objects = [
40+
{'id': 282, 'name': 'Alice', 'age': 30},
41+
{'id': 217, 'name': 'Bob', 'age': 56},
42+
{'id': 328, 'name': 'Charlie', 'age': 56},
43+
]
44+
45+
names = fetch(objects, 'name')
46+
print(names)
47+
48+
输出
49+
50+
.. code-block:: none
51+
52+
['Alice', 'Bob', 'Charlie']
53+
54+
选取多个属性
55+
++++++++++++
56+
57+
从 `objects` 数据获取 `name` 和 `age` 的数据。
58+
59+
60+
::
61+
62+
from django_echarts.datasets.fetch import fetch
63+
64+
objects = [
65+
{'id': 282, 'name': 'Alice', 'age': 30},
66+
{'id': 217, 'name': 'Bob', 'age': 56},
67+
{'id': 328, 'name': 'Charlie', 'age': 56},
68+
]
69+
70+
names, ages = fetch(objects, 'name', 'age')
71+
print(names)
72+
print(ages)
73+
74+
输出
75+
76+
.. code-block:: none
77+
78+
['Alice', 'Bob', 'Charlie']
79+
[30, 56, 56]
80+
81+
提供默认值
82+
----------
83+
84+
当 `iterable` 数据列表缺少某个属性/键,可以通过指定 `default` 或 `defaults` 参数提供默认值。
85+
86+
::
87+
88+
from django_echarts.datasets.fetch import fetch
89+
90+
objects = [
91+
{'id': 282, 'name': 'Alice', 'age': 30, 'gender': 'female'},
92+
{'id': 217, 'name': 'Bob', 'age': 56},
93+
{'id': 328, 'name': 'Charlie', 'gender': 'male'},
94+
]
95+
96+
print('Demo for one default value')
97+
genders = fetch(objects, 'gender', default='unknown')
98+
print(genders)
99+
100+
print('Demo for multiple default values')
101+
ages, genders = fetch(objects, 'age', 'gender', defaults={'age': 0, 'gender':'unknown'})
102+
print(genders)
103+
print(ages)
104+
105+
结果输出
106+
107+
.. code-block:: none
108+
109+
Demo for one default value
110+
['female', 'unknown', 'male']
111+
Demo for multiple default values
112+
['female', 'unknown', 'male']
113+
[30, 56, 0]
114+
115+
属性访问
116+
--------
117+
118+
除了上述的键值访问方式,`fetch` 函数还内置属性访问的获取方式。
119+
120+
::
121+
122+
from django_echarts.datasets.fetch import fetch
123+
124+
125+
class Point:
126+
def __init__(self, x, y, z):
127+
self.x = x
128+
self.y = y
129+
self.z = z
130+
131+
132+
points = [
133+
Point(1, 2, 3),
134+
Point(4, 5, 6),
135+
Point(7, 8, 9)
136+
]
137+
138+
print('Fetch x values:')
139+
x = fetch(points, 'x')
140+
print(x)
141+
142+
print('Fetch x,y,z values:')
143+
x, y, z = fetch(points, 'x', 'y', 'z')
144+
print(x)
145+
print(y)
146+
print(z)
147+
148+
结果输出
149+
150+
.. code-block:: none
151+
152+
Fetch x values:
153+
[1, 4, 7]
154+
Fetch x,y,z values:
155+
[1, 4, 7]
156+
[2, 5, 8]
157+
[3, 6, 9]
158+
159+
自定义Getter
160+
------------
161+
162+
除了内置的属性访问方式(itemgetter_)和键值访问方式(attrgetter_)外,`fetch` 函数还通过 `getter` 参数支持自定义访问方式。
163+
164+
.. _itemgetter: https://docs.python.org/3.6/library/operator.html#operator.itemgetter
165+
.. _attrgetter: https://docs.python.org/3.6/library/operator.html#operator.attrgetter
166+
167+
getter 需满足下列的几个条件:
168+
169+
- 是一个函数,命名函数或匿名函数均可
170+
- 该函数必须含有 *item* 和 *key* 两个参数
171+
- 返回是具体的数值
172+
173+
例子:
174+
175+
::
176+
177+
from django_echarts.datasets.fetch import fetch
178+
179+
180+
class Point:
181+
def __init__(self, index, x, y, z):
182+
self.index = index
183+
self._data = {'x': x, 'y': y, 'z': z}
184+
185+
def get(self, key):
186+
return self._data.get(key)
187+
188+
189+
points = [
190+
Point('a', 1, 2, 3),
191+
Point('b', 4, 5, 6),
192+
Point('c', 7, 8, 9)
193+
]
194+
195+
196+
def point_getter(item, key):
197+
return item.get(key)
198+
199+
200+
print('Fetch x values:')
201+
x = fetch(points, 'x', getter=point_getter)
202+
print(x)
203+
204+
print('Fetch x,y,z values:')
205+
x, y, z = fetch(points, 'x', 'y', 'z', getter=point_getter)
206+
print(x)
207+
print(y)
208+
print(z)
209+
210+
211+
结果输出
212+
213+
.. code-block:: none
214+
215+
Fetch x values:
216+
[1, 4, 7]
217+
Fetch x,y,z values:
218+
[1, 4, 7]
219+
[2, 5, 8]
220+
[3, 6, 9]
221+
222+
需要注意的是,自定义 Getter 是应用至所有属性的,内置的 *属性访问方式* 和 *键值访问方式* 将不再使用,混用将可能无法获得期望的结果。
223+
224+
错误的示例1
225+
226+
>>> indexes, xs = fetch(points, 'index', 'x', getter=point_getter)
227+
[None, None, None]
228+
[1, 4, 7]
229+
230+
错误的示例2
231+
232+
>>> indexes, xs = fetch(points, 'index', 'x')
233+
Traceback (most recent call last):
234+
TypeError: 'Point' object is not subscriptable
235+
236+
应当分别调用 `fetch` 函数。
237+
238+
正确的用法
239+
240+
::
241+
242+
x, y = fetch(points, 'x', 'y', getter=point_getter)
243+
244+
print(x)
245+
print(y)
246+
247+
indexes = fetch(points, 'index')
248+
print(indexes)
249+
250+
结果输出
251+
252+
253+
.. code-block:: none
254+
255+
[1, 4, 7]
256+
[2, 5, 8]
257+
['a', 'b', 'c']

docs/index.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
Welcome to django_echarts's documentation!
77
==========================================
88

9-
django-echarts 是一个关于 Echarts_ 整合的 Django App,使用 pyecharts_ 作为图表构建库。
9+
django-echarts 是一个关于 Echarts_ 整合的 Django App,使用 pyecharts_ 作为图表构建库。主页: https://github.com/kinegratii/django-echarts
1010

1111
::
1212

1313
自v0.3起,django-echarts 仅支持 Python3.5+ 的开发环境, Django 版本要求为 2.0+ 或 1.11 LTS 。
1414

15-
主页: https://github.com/kinegratii/django-echarts
16-
1715
它提供了以下特性:
1816

1917
.. _Echarts: http://echarts.baidu.com/index.html
@@ -31,6 +29,7 @@ django-echarts 是一个关于 Echarts_ 整合的 Django App,使用 pyecharts_
3129
tutorial
3230
topics
3331
data_builder
32+
fetch
3433
api
3534
development
3635
changelog

0 commit comments

Comments
 (0)