Skip to content

Commit

Permalink
True theme (pyecharts#546)
Browse files Browse the repository at this point in the history
* 🐛 fix theme color problem for themes other than dark, pyecharts#544

* 🔬 test remove_key_with_none_value and whitening

* 📚 update docs and pump version

* Update json_serializable.py

* Update __init__.py

* 💚 fix unit test failures

* 🔥 get rid of dev code

* 💚 pass all unit tests

* 👕 use python 2 to generate the fixtures

* 🔥 remove empty dictionary that resulted from cleaning

* 🔬 more test cases. 👕 whitening

* 💚 ignore basestring please, flake8

* 🔨 code refactor test

* 🔨 address review feedbacks

* 💄 change single quote

* Update: docs update

* Update: img link update

* Update: docs update
  • Loading branch information
chfw authored and chenjiandongx committed May 10, 2018
1 parent 47233f6 commit a130fe8
Show file tree
Hide file tree
Showing 23 changed files with 197 additions and 212 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ install:
build: off

test_script:
"%PYTHON%/Scripts/nosetests --with-coverage --cover-package pyecharts --cover-package test && cd .. && %PYTHON%/Scripts/flake8 --exclude docs --builtins=unicode,xrange,long"
"%PYTHON%/Scripts/nosetests --with-coverage --cover-package pyecharts --cover-package test && cd .. && %PYTHON%/Scripts/flake8 --exclude docs --builtins=unicode,xrange,long,basestring"
11 changes: 8 additions & 3 deletions docs/zh-cn/changelog.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# 版本日志

* ### version 0.5.3(dev)
* TODO
* ### version 0.5.4(dev)
* TODO

* ### version 0.5.2 - 2018.05.04(Current)
* ### version 0.5.3 - 2018.05.10(current)

#### Fixed
* [issue#544](https://github.com/pyecharts/pyecharts/issues/544) 修复 v0.5.2 主题颜色默认颜色配置与前版本有差别太大的 bug

* ### version 0.5.2 - 2018.05.04

#### Added
* [issue#512](https://github.com/pyecharts/pyecharts/issues/512) 新增自定义主题功能
Expand Down
63 changes: 43 additions & 20 deletions docs/zh-cn/themes.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,74 @@
> 主题自定义篇:适用在 pyecharts v0.5.2 以后的版本。
> 主题自定义篇:扩展主题插件,多样化图表配色。V0.5.2+ 新增
自 0.5.2+ 起,pyecharts 支持更换主体色系。下面是更换为 'dark' 的例子:
自 0.5.2+ 起,pyecharts 支持更换主题。下面是更换为 "dark" 的例子:

```python
import random

from pyecharts import Bar


X_AXIS = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
bar = Bar("我的第一个图表", "这里是副标题")
bar.use_theme('dark')
bar.add("服装", ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"], [5, 20, 36, 10, 75, 90])
bar.use_theme("dark")
bar.add("商家A", X_AXIS, [random.randint(10, 100) for _ in range(6)])
bar.add("商家B", X_AXIS, [random.randint(10, 100) for _ in range(6)])
bar.add("商家C", X_AXIS, [random.randint(10, 100) for _ in range(6)])
bar.add("商家D", X_AXIS, [random.randint(10, 100) for _ in range(6)])
bar.render()
```
![guide-2](https://user-images.githubusercontent.com/4280312/39617664-79789878-4f78-11e8-9f0e-c3a2c371b6cb.png)
![dark](https://user-images.githubusercontent.com/19553554/39868563-c136646a-548c-11e8-87c2-dbf7ae85e844.png)

默认主题的效果

![default](https://user-images.githubusercontent.com/19553554/39868566-c20b699e-548c-11e8-861f-5a1b063434c3.png)

## 如何获得更多主题

伴随 pyecharts v0.5.2, [echarts-themes-pypkg](https://github.com/pyecharts/echarts-themes-pypkg) 提供 dark, vintage, macarons, infographic, shine 和 roma 主题。
[echarts-themes-pypkg](https://github.com/pyecharts/echarts-themes-pypkg) 提供了 `dark`, `vintage`, `macarons`, `infographic`, `shine``roma` 主题。

## 安装主题插件
### 安装主题插件

```shell
$ pip install echarts-themes-pypkg
```

## 使用主题
### 使用主题

更换单个图形主题
```python
bar.use_theme('vintage') # 更换为你喜欢的主题
bar.use_theme("vintage")
```

更换运行环境内所有图表主题
```python
from pyecharts import configure

# 将这行代码置于首部
configure(global_theme='dark')

bar = Bar()
....
```

## 示例
### 示例

### vintage:
**vintage**

![vintage](https://user-images.githubusercontent.com/19553554/39620159-d42defa4-4fbc-11e8-8e8d-ea9158434ea0.png)
![vintage](https://user-images.githubusercontent.com/19553554/39868887-1bed3ae0-548e-11e8-99f5-8440ea578080.png)

### macarons:
**macarons**

![macarons](https://user-images.githubusercontent.com/19553554/39620161-d529169a-4fbc-11e8-9864-c246f9a2f677.png)
![macarons](https://user-images.githubusercontent.com/19553554/39868570-c3563a0e-548c-11e8-9795-e0ebea18853f.png)

### infographic
**infographic**

![infographic](https://user-images.githubusercontent.com/19553554/39620158-d3e6bfc6-4fbc-11e8-9564-9d2d9f62b2be.png)
![infographic](https://user-images.githubusercontent.com/19553554/39868564-c1884dac-548c-11e8-9009-f61162759be3.png)

### shine:
**shine**

![shine](https://user-images.githubusercontent.com/19553554/39620177-d6fd0a3a-4fbc-11e8-8abc-a99f6d1f9f74.png)
![shine](https://user-images.githubusercontent.com/19553554/39868565-c1c8951a-548c-11e8-8351-2973cce47679.png)

### roma:
**roma**

![roma](https://user-images.githubusercontent.com/19553554/39620162-d57380e0-4fbc-11e8-872b-2c1c43b7a1a4.png)
![roma](https://user-images.githubusercontent.com/19553554/39868568-c2c7b798-548c-11e8-9de8-3d3ae148f172.png)
2 changes: 1 addition & 1 deletion make.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cd test
nosetests --with-coverage --cover-package pyecharts --cover-package test && cd .. && flake8 --exclude docs --builtins=unicode,xrange,long
nosetests --with-coverage --cover-package pyecharts --cover-package test && cd .. && flake8 --max-complexity 12 --exclude docs --builtins=unicode,xrange,long,basestring
2 changes: 1 addition & 1 deletion pyecharts/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.5.2"
__version__ = "0.5.3"
__author__ = "chenjiandongx"
8 changes: 4 additions & 4 deletions pyecharts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def chart_id(self, chart_id):

@property
def options(self):
return self._option
return utils.remove_key_with_none_value(self._option)

@property
def js_dependencies(self):
Expand Down Expand Up @@ -170,9 +170,9 @@ def cast(seq):

def render_notebook(self):
warnings.warn(
"Implementation has been removed. "
+ "Please pass the chart instance directly to Jupyter."
+ "If you need more help, please read documentation"
"Implementation has been removed. " +
"Please pass the chart instance directly to Jupyter." +
"If you need more help, please read documentation"
)

def _get_all_options(self, **kwargs):
Expand Down
6 changes: 3 additions & 3 deletions pyecharts/echarts/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(
text_color=None,
formatter=None,
):
self.config = {
self._config = {
"interval": interval,
"formatter": formatter,
"rotate": rotate,
Expand Down Expand Up @@ -55,7 +55,7 @@ def __init__(
axis_type="value",
chart_type=None,
):
self.config = {
self._config = {
"name": name,
"show": visibility,
"nameLocation": name_location,
Expand All @@ -72,7 +72,7 @@ def __init__(
}

def set_split_line_visibility(self, flag):
self.config["splitLine"]["show"] = flag
self._config["splitLine"]["show"] = flag


class XAxis(Axis):
Expand Down
13 changes: 10 additions & 3 deletions pyecharts/echarts/json_serializable.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
from pyecharts.utils import remove_key_with_none_value


class JsonSerializable(object):

def __init__(self):
self.config = {}
self._config = {}

def __setitem__(self, key, value):
self.config[key] = value
self._config[key] = value

def __getitem__(self, key):
return self.config.get(key)
return self._config.get(key)

def update(self, **kwargs):
self.config.update(**kwargs)

@property
def config(self):
return remove_key_with_none_value(self._config)
4 changes: 2 additions & 2 deletions pyecharts/echarts/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(
formatter=None,
chart_type=None,
):
self.config = {
self._config = {
"show": visibility,
"position": position,
"textStyle": {"color": text_color, "fontSize": text_size},
Expand All @@ -23,7 +23,7 @@ def __init__(
if chart_type == "pie":
formatter = "{b}: {d}%"
if formatter:
self.config["formatter"] = formatter
self._config["formatter"] = formatter


class EmphasisLabel(Label):
Expand Down
2 changes: 1 addition & 1 deletion pyecharts/echarts/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(
):
if color is None and chart_type == "graph":
color = "#aaa"
self.config = {
self._config = {
"width": width,
"opacity": opacity,
"curveness": curve,
Expand Down
62 changes: 61 additions & 1 deletion pyecharts/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# coding=utf-8
from __future__ import unicode_literals

import codecs
from future.utils import viewitems

import os
import sys
import codecs

PY3 = sys.version_info[0] == 3

if PY3:
string_type = str
else:
string_type = basestring


__all__ = [
"get_resource_dir",
Expand Down Expand Up @@ -89,3 +100,52 @@ def _add(_item):
_add(_d)
fol = [x for x in front_optional_items if x in fist_items]
return front_required_items + fol + dependencies


def _expand(dict_generator):
return dict(list(dict_generator))


def _clean_dict(mydict):
for key, value in viewitems(mydict):
if value is not None:
if isinstance(value, dict):
if value:
value = _expand(_clean_dict(value))
else:
# delete key with empty dictionary
continue

if not value:
# detete empty dictionary resulted by
# previous cleanning function
continue

elif isinstance(value, (list, tuple, set)):
if value:
value = list(_clean_array(value))
else:
# delete key with empty array
continue

elif isinstance(value, string_type) and not value:
# delete key with empty string
continue

yield (key, value)


def _clean_array(myarray):
for value in myarray:
if isinstance(value, dict):
yield _expand(_clean_dict(value))

elif isinstance(value, (list, tuple, set)):
yield list(_clean_array(value))

else:
yield value


def remove_key_with_none_value(incoming_dict):
return _expand(_clean_dict(incoming_dict))
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cd test
nosetests --with-coverage --cover-package pyecharts --cover-package test && cd .. && flake8 --max-complexity 12 --exclude docs --builtins=unicode,xrange,long
nosetests --with-coverage --cover-package pyecharts --cover-package test && cd .. && flake8 --max-complexity 12 --exclude docs --builtins=unicode,xrange,long,basestring
Loading

0 comments on commit a130fe8

Please sign in to comment.