Skip to content

Commit 8b5acac

Browse files
committed
Merge branch 'dev'
2 parents 1fb38bf + 6604caf commit 8b5acac

File tree

16 files changed

+112
-62
lines changed

16 files changed

+112
-62
lines changed

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ env:
1111
- DJANGO_VERSION=2.0
1212
install:
1313
- pip install -r requirements.txt
14+
- pip install -r requirements_dev.txt
1415
- pip install -q Django==$DJANGO_VERSION
1516
- pip install -q pyecharts==0.4.1
17+
- python setup.py install
1618
script:
17-
make test
19+
- nosetests tests
20+
- python example/manage.py test --noinput
21+
- flake8 django_echarts tests example --ignore=E501 --max-complexity 10

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.4'
5+
__version__ = '0.3.5'
66
__author__ = 'kinegratii'

django_echarts/datasets/charts.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,59 @@
11
# coding=utf8
2-
2+
from collections import OrderedDict
33
from django_echarts.utils.interfaces import merge_js_dependencies
44

55

66
class NamedCharts:
77
"""
8-
A list containing multiple named charts.
8+
A data structure class containing multiple named charts.
99
"""
1010

1111
def __init__(self, page_title='EChart', **kwargs):
1212
self.page_title = page_title
13-
self._names = []
14-
self._charts = []
15-
16-
for name, chart in kwargs.items():
17-
self.add_chart(chart=chart, name=name)
13+
self._charts = OrderedDict()
14+
for k, v in kwargs.items():
15+
self.add_chart(chart=v, name=k)
1816

1917
def add_chart(self, chart, name=None):
2018
name = name or self._next_name()
21-
self._names.append(name)
22-
self._charts.append(chart)
19+
self._charts[name] = chart
2320
return self
2421

22+
def _next_name(self):
23+
return 'c{}'.format(len(self._charts))
24+
25+
# List-like feature
26+
2527
def __iter__(self):
26-
for chart in self._charts:
28+
for chart in self._charts.values():
2729
yield chart
2830

2931
def __len__(self):
3032
return len(self._charts)
3133

34+
# Dict-like feature
35+
3236
def __contains__(self, item):
33-
return item in self._names
37+
return item in self._charts
3438

35-
def __getattr__(self, item):
36-
try:
37-
i = self._names.index(item)
38-
return self._charts[i]
39-
except ValueError:
40-
return super().__getattr__(item)
39+
def __getitem__(self, item):
40+
if isinstance(item, int):
41+
# c[1], Just compatible with Page
42+
return list(self._charts.values())[item]
43+
return self._charts[item]
4144

42-
def _next_name(self):
43-
return 'c{}'.format(len(self._charts))
45+
def __setitem__(self, key, value):
46+
self._charts[key] = value
47+
48+
# Compatible
49+
50+
def add(self, achart_or_charts):
51+
if not isinstance(achart_or_charts, (list, tuple, set)):
52+
achart_or_charts = achart_or_charts, # Make it a sequence
53+
for c in achart_or_charts:
54+
self.add_chart(chart=c)
55+
56+
# Chart-like feature
4457

4558
@property
4659
def js_dependencies(self):

django_echarts/utils/interfaces.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ def to_css_length(l):
2626
return l
2727

2828

29+
def _flat(ele):
30+
if hasattr(ele, 'js_dependencies'):
31+
return list(ele.js_dependencies)
32+
if isinstance(ele, (list, tuple, set)):
33+
return ele
34+
return ele,
35+
36+
2937
def merge_js_dependencies(*chart_or_name_list):
3038
front_required_items = ['echarts']
3139
front_optional_items = ['echartsgl']
@@ -41,14 +49,8 @@ def _add(_item):
4149
dependencies.append(_item)
4250

4351
for d in chart_or_name_list:
44-
if hasattr(d, 'js_dependencies'):
45-
for x in d.js_dependencies:
46-
_add(x)
47-
elif isinstance(d, (list, tuple, set)):
48-
for x in d:
49-
_add(x)
50-
elif isinstance(d, str):
51-
_add(d)
52+
for _d in _flat(d):
53+
_add(_d)
5254
return front_required_items + [x for x in front_optional_items if x in fist_items] + dependencies
5355

5456

8.52 KB
Loading

docs/changelog.rst

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

4+
v0.3.5 (20180504)
5+
-----------------
6+
7+
- `NamedCharts` 命名图表访问改为字典访问方式
8+
49
v0.3.4 (20180423)
510
-----------------
611

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.4'
58+
version = '0.3.5'
5959
# The full version, including alpha/beta/rc tags.
60-
release = '0.3.4'
60+
release = '0.3.5'
6161

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

docs/topics.rst

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ django_echarts 提供两种方式的渲染视图,即:
155155
基本使用
156156
++++++++
157157

158-
视图代码
158+
159+
在创建一个 `NamedCharts` 实例 `charts` ,后,使用 `add_chart` 添加一个图表对象,可以使用 `name` 为之起一个引用名称,如果没有指定引用名称,则使用 c0,c1 命名。
159160

160161
::
161162

@@ -174,25 +175,50 @@ django_echarts 提供两种方式的渲染视图,即:
174175
names, lifes = fetch(battery_lifes, 'name', 'battery_life')
175176
bar = Bar('设备电量', page_title='设备电量', width='100%')
176177
bar.add("设备电量", names, lifes)
177-
charts = NamedCharts().add_chart(pie, name='pie').add_chart(bar, name='bar')
178+
charts = NamedCharts().add_chart(pie, name='pie').add_chart(bar)
178179
return charts
179180

180-
在创建一个 `NamedCharts` 实例 `charts` ,后,使用 `add_chart` 添加一个图表对象,可以使用 `name` 为之起一个引用变量,之后可以像属性一样访问该图表对象。
181+
元素访问
182+
++++++++
183+
184+
.. versionchanged:: 0.3.5
185+
图表访问方式从 *属性访问* 改为 *字典访问* 。
186+
187+
对于 包含若干图表的 `NamedCharts` 实例,可以像字典一样访问该图表对象。
188+
189+
Python 代码的访问方式
190+
191+
::
192+
193+
# 访问 pie 对象 page_title
194+
print(charts['pie'].page_title)
195+
196+
# 访问 bar 对象 page_title
197+
print(charts['c1'].page_title) # 推荐
198+
print(charts[1].page_title) # 不再推荐
199+
200+
模板代码的访问方式:
181201

182202
::
183203

184-
print(charts.pie.page_title) # 等价于 charts[0].page_title
204+
{{ charts.pie.page_title }}
205+
{{ charts.c1.page_title }}
185206

186-
但是在 Django 模板中不推荐使用 `{{ charts.0.page_title }}` 的方式引用列表的元素,可以使用 `{{ charts.pie.page_title }}` 。
207+
注意
208+
209+
::
210+
211+
无论是 Jinja2 模板还是 Django 模板,均不提倡使用 `charts.1` 形式访问列表中的某一个元素。
187212

188213
NamedCharts VS Page
189214
+++++++++++++++++++
190215

191-
区别。
216+
`NamedCharts` 内部使用 `collections.OrderedDict` 保存图表名称和实例,支持字典访问方式,同时扩展原有的 `Page` 的列表特性。
217+
218+
具体差别如下表:
192219

193220
.. image:: /_static/namedcharts-vs-page.png
194221

195-
`NamedCharts` 内部使用两个列表分别保存图表名称和实例,对外保持一个列表的相关方法,同时支持属性访问,
196222

197223
模板标签
198224
---------
@@ -352,7 +378,7 @@ django-echarts 提供了一个包含若干个命令的 CLI 工具,这些命令
352378
++++++++
353379

354380
.. versionadded:: 0.2.2
355-
新增 `download_lib_js` 和 `download_map_js` 命令。
381+
新增 `download_lib_js` 和 `download_map_js` 命令。
356382

357383
django-echarts 提供了一些下载命令,可以从远程地址下载文件到项目的静态目录中。这些命令包括:
358384

example/demo/demo_data.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# coding=utf8
22

3-
import json
43

54
from pyecharts import Bar, Kline, Map, Pie, WordCloud
65

example/example/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# coding=utf8
22

33

4-
from django.conf.urls import url, include
4+
from django.conf.urls import url
55

66
from demo import frontend_views, backend_views
77

0 commit comments

Comments
 (0)