Skip to content

Commit d7b2087

Browse files
Extra text label (pyecharts#659)
* Docs: 文档完善 * Add: pyecharts#651 为 Scatter 图新增 extra_name 参数 * Update: 版本号更新 * Add: 新增 assert 判断 * Update: pyecharts#657 新增额外的文本标签 * Docs: 对应文档更新 * Format: 代码格式化
1 parent ab98007 commit d7b2087

File tree

9 files changed

+146
-25
lines changed

9 files changed

+146
-25
lines changed

docs/zh-cn/changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#### Added
66
* [issue#651](https://github.com/pyecharts/pyecharts/issues/651) Scatter 图新增 `extra_name` 参数,额外的数据项的名称,可以为每个数据点指定一个名称。
7+
* [issue#657](https://github.com/pyecharts/pyecharts/issues/657) 基本图形新增 `extra_html_text_label` 参数用于显示额外的文本标签,仅限于在单图形或者 Page 时使用。
8+
79

810
* ### version 0.5.6 - 2018.7.28(current)
911

docs/zh-cn/charts.md

+52-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@
7676
指定生成的 html 文件中 `<title>` 标签的值。默认为'Echarts'
7777
* renderer -> str
7878
指定使用渲染方式,有 'svg' 和 'canvas' 可选,默认为 'canvas'。3D 图仅能使用 'canvas'。
79-
79+
* extra_html_text_label -> list
80+
额外的 HTML 文本标签,(<p> 标签)。类型为 list,list[0] 为文本内容,list[1] 为字体风格样式(选填)。如 ["this is a p label", "color:red"]**仅限于在单个图形或者 page 类时使用。**
81+
8082

8183
# 通用配置项
8284
**通用配置项均在 ```add()``` 中设置**
@@ -709,6 +711,18 @@ bar.render()
709711
![bar-demo](https://user-images.githubusercontent.com/19553554/35081822-3e090748-fc51-11e7-8bba-b775d29671e4.png)
710712

711713

714+
**额外的文本标签**
715+
```python
716+
from pyecharts import Bar
717+
718+
bar = Bar("柱状图", extra_html_text_label=["bar_extra_html_text_label", "color:red"])
719+
bar.add("商家A", CLOTHES, clothes_v1, is_stack=True)
720+
bar.add("商家B", CLOTHES, clothes_v2, is_stack=True)
721+
bar.render()
722+
```
723+
![bar-demo](https://user-images.githubusercontent.com/19553554/43812932-31f22e0a-9af6-11e8-8fbe-c62b65daec41.png)
724+
725+
712726
## Bar3D(3D 柱状图)
713727
Bar3D.add() 方法签名
714728
```python
@@ -3822,6 +3836,43 @@ page.render()
38223836
```
38233837
![page-demo](https://user-images.githubusercontent.com/19553554/35104305-66f2a766-fca3-11e7-8ffd-8e85911fdea5.gif)
38243838

3839+
**Page 类的额外的文本标签,由各图形本身携带**
3840+
```python
3841+
from pyecharts import *
3842+
3843+
page = Page()
3844+
line = Line("折线图示例", extra_html_text_label=["LINE TEXT LABEL", "color:red"])
3845+
line.add(
3846+
"最高气温",
3847+
WEEK,
3848+
[11, 11, 15, 13, 12, 13, 10],
3849+
mark_point=["max", "min"],
3850+
mark_line=["average"],
3851+
)
3852+
page.add(line)
3853+
3854+
v1 = [11, 12, 13, 10, 10, 10]
3855+
pie = Pie("饼图-圆环图示例", title_pos="center", extra_html_text_label=["PIE TEXT LABEL"])
3856+
pie.add(
3857+
"",
3858+
CLOTHES,
3859+
v1,
3860+
radius=[40, 75],
3861+
label_text_color=None,
3862+
is_label_show=True,
3863+
legend_orient="vertical",
3864+
legend_pos="left",
3865+
)
3866+
page.add(pie)
3867+
3868+
v2 = [10, 25, 8, 60, 20, 80]
3869+
bar = Bar("柱状图", extra_html_text_label=["BAR TEXT LABEL"])
3870+
bar.add("商家B", CLOTHES, v2)
3871+
page.add(bar)
3872+
page.render()
3873+
```
3874+
![page-demo](https://user-images.githubusercontent.com/19553554/43813125-e4ef7e2c-9af6-11e8-96a0-a6b53b0c136f.gif)
3875+
38253876

38263877
## Timeline:提供时间线轮播多张图
38273878
Timeline 类的使用:

pyecharts/base.py

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def __init__(
2626
height=400,
2727
renderer=constants.CANVAS_RENDERER,
2828
page_title=constants.PAGE_TITLE,
29+
extra_html_text_label=None,
2930
):
3031
"""
3132
@@ -38,6 +39,9 @@ def __init__(
3839
3D 图仅能使用 'canvas'。
3940
:param page_title:
4041
指定生成的 html 文件中 <title> 标签的值。默认为 'Echarts'
42+
:param extra_html_text_label:
43+
额外的 HTML 文本标签,(<p> 标签)。类型为 list,list[0] 为文本内容,
44+
list[1] 为字体风格样式(选填)。如 ["this is a p label", "color:red"]
4145
"""
4246
self._option = {}
4347
self._js_dependencies = set()
@@ -49,6 +53,7 @@ def __init__(
4953
self.event_handlers = {}
5054
self.theme = None
5155
self.use_theme(CURRENT_CONFIG.theme)
56+
self.extra_html_text_label = extra_html_text_label
5257

5358
@property
5459
def chart_id(self):

pyecharts/chart.py

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def __init__(
2626
background_color=None,
2727
page_title=constants.PAGE_TITLE,
2828
renderer=constants.CANVAS_RENDERER,
29+
extra_html_text_label=None,
2930
):
3031
"""
3132
@@ -58,12 +59,16 @@ def __init__(
5859
:param renderer:
5960
指定使用渲染方式,有 'svg' 和 'canvas' 可选,默认为 'canvas'。
6061
3D 图仅能使用 'canvas'。
62+
:param extra_html_text_label:
63+
额外的 HTML 文本标签,(<p> 标签)。类型为 list,list[0] 为文本内容,
64+
list[1] 为字体风格样式(选填)。如 ["this is a p label", "color:red"]
6165
"""
6266
super(Chart, self).__init__(
6367
width=width,
6468
height=height,
6569
renderer=renderer,
6670
page_title=page_title,
71+
extra_html_text_label=extra_html_text_label,
6772
)
6873
self._colorlst = [
6974
"#c23531",

pyecharts/engine.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
CHART_EVENT_FORMATTER = """
2626
myChart_{chart_id}.on("{event_name}", {handler_name});
2727
"""
28+
EXTRA_TEXT_FORMATTER = """<p style="{style}">{text}</p>"""
2829

2930

3031
@environmentfunction
@@ -70,14 +71,22 @@ def echarts_container(env, chart):
7071
:param env:
7172
:param chart: A pyecharts.base.Base object
7273
"""
73-
74-
return Markup(
75-
CHART_DIV_FORMATTER.format(
76-
chart_id=chart.chart_id,
77-
width=utils.to_css_length(chart.width),
78-
height=utils.to_css_length(chart.height),
79-
)
74+
_container_and_text = ""
75+
_container_and_text += CHART_DIV_FORMATTER.format(
76+
chart_id=chart.chart_id,
77+
width=utils.to_css_length(chart.width),
78+
height=utils.to_css_length(chart.height),
8079
)
80+
_text_label = chart.extra_html_text_label
81+
if _text_label:
82+
if len(_text_label) == 1 and isinstance(_text_label, list):
83+
_text_label.append("")
84+
_text, _style = _text_label
85+
_container_and_text += EXTRA_TEXT_FORMATTER.format(
86+
text=_text,
87+
style=_style
88+
)
89+
return Markup(_container_and_text)
8190

8291

8392
def generate_js_content(*charts):

pyecharts/templates/simple_chart.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
{{ echarts_container(chart) }}
1010
{{ echarts_js_content(chart) }}
1111
</body>
12-
</html>
12+
</html>

pyecharts/templates/simple_page.html

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
{{ echarts_js_dependencies(page) }}
77
</head>
88
<body>
9-
{% for chart in page %}
10-
{{ echarts_container(chart) }}
11-
{% endfor %}
12-
{{ echarts_js_content(*page) }}
9+
{% for chart in page %}
10+
{{ echarts_container(chart) }}
11+
{% endfor %}
12+
{{ echarts_js_content(*page) }}
1313
</body>
14-
</html>
14+
</html>

test/test_bar.py

+8
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,11 @@ def test_bar_datazoom_xaxis_and_yaxis():
162162
assert ': "slider"' in html_content
163163
assert ': "vertical"' in html_content
164164
assert ': "horizontal"' in html_content
165+
166+
167+
def test_bar_extra_html_text_label():
168+
bar = Bar("柱状图", extra_html_text_label=["bar_extra_html_text_label"])
169+
bar.add("商家A", CLOTHES, clothes_v1, is_stack=True)
170+
bar.add("商家B", CLOTHES, clothes_v2, is_stack=True)
171+
html_content = bar._repr_html_()
172+
assert '<p style="">bar_extra_html_text_label</p>' in html_content

test/test_page.py

+52-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import sys
55
import json
66
import codecs
7+
import random
8+
79
from test.constants import RANGE_COLOR, CLOTHES, WEEK
810
from pyecharts import (
911
Bar,
@@ -53,7 +55,7 @@ def test_page_add_chart():
5355
)
5456

5557

56-
def create_three():
58+
def create_three_charts():
5759
page = Page(page_title=TEST_PAGE_TITLE)
5860

5961
# bar
@@ -65,8 +67,6 @@ def create_three():
6567
page.add(bar)
6668

6769
# scatter3D
68-
import random
69-
7070
data = [
7171
[
7272
random.randint(0, 100),
@@ -98,12 +98,12 @@ def create_three():
9898

9999
@raises(NotImplementedError)
100100
def test_no_image_rendering_for_page():
101-
page = create_three()
101+
page = create_three_charts()
102102
page.render(path="page.png")
103103

104104

105105
def test_two_bars():
106-
page = create_three()
106+
page = create_three_charts()
107107
page.render()
108108
with codecs.open("render.html", "r", "utf-8") as f:
109109
actual_content = f.read()
@@ -119,7 +119,7 @@ def test_two_bars():
119119

120120

121121
def test_page_get_js_dependencies():
122-
page = create_three()
122+
page = create_three_charts()
123123
dependencies = page.get_js_dependencies()
124124
eq_(dependencies[0], "echarts.min")
125125
assert "guangdong" in dependencies
@@ -128,14 +128,14 @@ def test_page_get_js_dependencies():
128128

129129

130130
def test_page_embed():
131-
page = create_three()
131+
page = create_three_charts()
132132
html = page.render_embed()
133133
assert "<html>" not in html
134134
assert json.dumps("柱状图数据堆叠示例") in html
135135

136136

137137
def test_page_in_notebook():
138-
page = create_three()
138+
page = create_three_charts()
139139
html = page._repr_html_()
140140

141141
assert "echartsgl" in html
@@ -148,7 +148,7 @@ def test_page_in_notebook():
148148
assert echarts_position < guangdong_position
149149

150150

151-
def test_more():
151+
def test_more_charts():
152152
page = Page()
153153

154154
# line
@@ -251,8 +251,6 @@ def test_more():
251251
page.add(radar)
252252

253253
# scatter3d
254-
import random
255-
256254
data = [
257255
[
258256
random.randint(0, 100),
@@ -325,3 +323,46 @@ def test_more():
325323
or ("echarts.min" in page.js_dependencies)
326324
)
327325
page.render()
326+
327+
328+
def test_page_extra_html_text_label():
329+
page = Page()
330+
line = Line(
331+
"折线图示例", extra_html_text_label=["LINE TEXT LABEL", "color:red"]
332+
)
333+
line.add(
334+
"最高气温",
335+
WEEK,
336+
[11, 11, 15, 13, 12, 13, 10],
337+
mark_point=["max", "min"],
338+
mark_line=["average"],
339+
)
340+
page.add(line)
341+
342+
v1 = [11, 12, 13, 10, 10, 10]
343+
pie = Pie(
344+
"饼图-圆环图示例",
345+
title_pos="center",
346+
extra_html_text_label=["PIE TEXT LABEL"],
347+
)
348+
pie.add(
349+
"",
350+
CLOTHES,
351+
v1,
352+
radius=[40, 75],
353+
label_text_color=None,
354+
is_label_show=True,
355+
legend_orient="vertical",
356+
legend_pos="left",
357+
)
358+
page.add(pie)
359+
360+
v2 = [10, 25, 8, 60, 20, 80]
361+
bar = Bar("柱状图", extra_html_text_label=["BAR TEXT LABEL"])
362+
bar.add("商家B", CLOTHES, v2)
363+
page.add(bar)
364+
365+
html_content = page._repr_html_()
366+
assert '<p style="">BAR TEXT LABEL</p>' in html_content
367+
assert '<p style="color:red">LINE TEXT LABEL</p>' in html_content
368+
assert '<p style="">PIE TEXT LABEL</p>' in html_content

0 commit comments

Comments
 (0)