Skip to content

Commit

Permalink
To be able to set a key to null in Echarts options (pyecharts#724)
Browse files Browse the repository at this point in the history
* ✨ allow developer to signal a configuration is not set. fix pyecharts#703

* 📚 update usage and 🔬 provide one integration test

* 📚 update change log

* 🎨 update version number

* 🎨 🔨 address review feedback
  • Loading branch information
chfw authored and chenjiandongx committed Sep 4, 2018
1 parent d66c742 commit 637366c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
14 changes: 14 additions & 0 deletions docs/zh-cn/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,17 @@ bar2 = Bar("Bar chart", "precipitation and evaporation one year")
bar2.add("precipitation", attr, v1, is_label_show=True, label_formatter=label_formatter)
bar2.render()
```

## 编辑 _option

如果 pyecharts 的自带 options 不能满足要求的话,开发人员是可以自己插入自己的配置选项。唯一的问题是,pyecharts 不能把某选项设置为空 (null)。
从 0.5.10 起,这个问题得到了解决。

```python
from pyecharts import NULL, Kline

kline = Kline("K 线图-默认示例")
kline.add("日K", DATE, data)
kline._option['series'][0]['itemStyle']['normal']['borderColor'] = NULL
kline.render()
```
5 changes: 3 additions & 2 deletions docs/zh-cn/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

#### Added
* [issue#699](https://github.com/pyecharts/pyecharts/issues/699) 为漏斗图新增 `funnel_sort``funnel_gap` 分别用于控制漏斗图的排序方式和数据图形间隔。
* [issue#703](https://github.com/pyecharts/pyecharts/issues/715) 支持设置 Echarts 某选项为空 (null)
* [issue#720](https://github.com/pyecharts/pyecharts/issues/720) 新增 3D 曲面图图形种类。

#### Fixed
* [issue#715](https://github.com/pyecharts/pyecharts/issues/715) 修复 online() 方法不生效的 bug


* ### version 0.5.9 - 2018.8.26(current)

#### Added
* [pr#685](https://github.com/pyecharts/pyecharts/pull/685) 图表方法(`use_theme`/`config`/`add`)支持链式调用
* [pr#690](https://github.com/pyecharts/pyecharts/pull/690) Radar 新增 `set_radar_component` 方法,废弃 `config` 方法;Parallel 图新增 `set_schema` 方法,废弃 `confg` 方法
Expand Down
1 change: 1 addition & 0 deletions pyecharts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from pyecharts.conf import configure
from pyecharts.echarts.style import Style
from pyecharts.conf import jupyter_image
from pyecharts.utils import NULL

# alias
Candlestick = Kline
16 changes: 16 additions & 0 deletions pyecharts/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@
]


class Passport(object):
def __init__(self, reason, value=None):
self.reason = reason
self.value = value

def __str__(self):
return self.reason


NULL = Passport("To pass None type through remove_key_with_none_value")


def get_resource_dir(*paths):
"""
Return absolute path for a directory or file inside the project.
Expand Down Expand Up @@ -120,6 +132,10 @@ def _clean_dict(mydict):
# delete key with empty string
continue

elif isinstance(value, Passport):
passport = value
value = passport.value

yield (key, value)


Expand Down
15 changes: 15 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from nose.tools import eq_
from pyecharts.utils import (
NULL,
get_resource_dir,
merge_js_dependencies,
remove_key_with_none_value,
Expand Down Expand Up @@ -90,11 +91,25 @@ def test_remove_key_with_none_value():
{"nested": {"ac": 1, "bc": None, "nested": {"a": 1, "b": None}}},
{"normal": 1, "empty_string": ""},
],
"not_set": NULL,
}
actual_result = remove_key_with_none_value(fixture)
expected = {
"a": 1,
"array": [1, {"nested": {"ac": 1, "nested": {"a": 1}}}, {"normal": 1}],
"nested": {"ac": 1, "nested": {"a": 1}},
"not_set": None,
}
eq_(actual_result, expected)


def test_not_set():
from pyecharts import Kline

kline = Kline("K 线图-默认示例")
kline.add("日K", [], [])
kline._option["series"][0]["itemStyle"] = {
"normal": {"borderColor": NULL}
}
content = kline._repr_html_()
assert '"borderColor": null' in content

0 comments on commit 637366c

Please sign in to comment.