mirror of
https://github.com/pyecharts/pyecharts.git
synced 2025-12-08 20:59:23 +00:00
Add test cases
This commit is contained in:
parent
54eb10b044
commit
33fb3ecde4
@ -59,7 +59,7 @@ class Base(object):
|
||||
def show_config(self):
|
||||
""" 打印输出图形所有配置项
|
||||
"""
|
||||
print(json_dumps(self._option, indent=4))
|
||||
print(utils.json_dumps(self._option, indent=4))
|
||||
|
||||
def render_embed(self):
|
||||
""" 渲染图表的所有配置项,为 web pages 服务,不过需先提供
|
||||
@ -67,7 +67,7 @@ class Base(object):
|
||||
"""
|
||||
embed = 'chart_component.html'
|
||||
tmp = template.JINJA2_ENV.get_template(embed)
|
||||
my_option = json_dumps(self._option, indent=4)
|
||||
my_option = utils.json_dumps(self._option, indent=4)
|
||||
html = tmp.render(my_option=my_option,
|
||||
chart_id=self._chart_id,
|
||||
my_width=self.width,
|
||||
@ -100,7 +100,7 @@ class Base(object):
|
||||
文件保存路径
|
||||
"""
|
||||
_tmp = "local.html"
|
||||
my_option = json_dumps(self._option, indent=4)
|
||||
my_option = utils.json_dumps(self._option, indent=4)
|
||||
tmp = template.JINJA2_ENV.get_template(_tmp)
|
||||
script_list = template.produce_html_script_list(self._js_dependencies)
|
||||
html = tmp.render(
|
||||
@ -176,37 +176,8 @@ class Base(object):
|
||||
""" 为 notebook 渲染组件模板
|
||||
"""
|
||||
_tmp = "notebook_chart_component.html"
|
||||
my_option = json_dumps(self._option, indent=4)
|
||||
my_option = utils.json_dumps(self._option, indent=4)
|
||||
tmp = template.JINJA2_ENV.get_template(_tmp)
|
||||
component = tmp.render(
|
||||
my_option=my_option, chart_id=self._chart_id)
|
||||
return component
|
||||
|
||||
|
||||
class UnknownTypeEncoder(json.JSONEncoder):
|
||||
"""
|
||||
`UnknownTypeEncoder`类用于处理数据的编码,使其能够被正常的序列化
|
||||
"""
|
||||
|
||||
def default(self, obj):
|
||||
if isinstance(obj, (datetime.datetime, datetime.date)):
|
||||
return obj.isoformat()
|
||||
else:
|
||||
try:
|
||||
return obj.astype(float).tolist()
|
||||
except:
|
||||
try:
|
||||
return obj.astype(str).tolist()
|
||||
except:
|
||||
return json.JSONEncoder.default(self, obj)
|
||||
|
||||
|
||||
def json_dumps(data, indent=0):
|
||||
""" json 序列化编码处理
|
||||
|
||||
:param data: 字典数据
|
||||
:param indent: 缩进量
|
||||
:return:
|
||||
"""
|
||||
return json.dumps(data, indent=indent,
|
||||
cls=UnknownTypeEncoder)
|
||||
|
||||
@ -6,7 +6,7 @@ import codecs
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from datetime import datetime
|
||||
import datetime
|
||||
|
||||
import json
|
||||
|
||||
|
||||
@ -42,3 +42,23 @@ def test_custom_templates():
|
||||
with codecs.open('new_version_page.html', 'r', 'utf-8') as f:
|
||||
actual_content = f.read()
|
||||
assert "</html>" in actual_content
|
||||
|
||||
|
||||
def test_custom_template_for_chart():
|
||||
data = [{
|
||||
'name': '衬衫',
|
||||
'value': 5
|
||||
}, {
|
||||
'name': '羊毛衫',
|
||||
'value': 20
|
||||
}, {
|
||||
'name': '雪纺衫',
|
||||
'value': 36
|
||||
}]
|
||||
names, values = Bar.cast(data)
|
||||
bar = Bar("柱状图数据堆叠示例")
|
||||
bar.add("商家A", names, values, is_stack=True)
|
||||
bar.render(new_version=True, path='new_version_bar.html')
|
||||
with codecs.open('new_version_bar.html', 'r', 'utf-8') as f:
|
||||
actual_content = f.read()
|
||||
assert "</html>" in actual_content
|
||||
|
||||
@ -11,7 +11,6 @@ from pyecharts import (
|
||||
from pyecharts import Page
|
||||
from nose.tools import eq_
|
||||
|
||||
|
||||
TEST_PAGE_TITLE = "my awesome chart"
|
||||
|
||||
|
||||
@ -32,7 +31,7 @@ def create_three():
|
||||
[random.randint(0, 100),
|
||||
random.randint(0, 100),
|
||||
random.randint(0, 100)] for _ in range(80)
|
||||
]
|
||||
]
|
||||
scatter3d = Scatter3D("3D 散点图示例", width=1200, height=600)
|
||||
scatter3d.add("", data, is_visualmap=True, visual_range_color=RANGE_COLOR)
|
||||
page.add(scatter3d)
|
||||
@ -103,14 +102,14 @@ def test_more():
|
||||
mark_point=["max", "min"], mark_line=["average"])
|
||||
line.add("最低气温", WEEK, [1, -2, 2, 5, 3, 2, 0],
|
||||
mark_point=["max", "min"], mark_line=["average"])
|
||||
page.add(line)
|
||||
|
||||
# pie
|
||||
v1 = [11, 12, 13, 10, 10, 10]
|
||||
pie = Pie("饼图-圆环图示例", title_pos='center')
|
||||
pie.add("", CLOTHES, v1, radius=[40, 75], label_text_color=None,
|
||||
is_label_show=True, legend_orient='vertical', legend_pos='left')
|
||||
page.add(pie)
|
||||
|
||||
page.add([line, pie])
|
||||
|
||||
# kline
|
||||
v1 = [[2320.26, 2320.26, 2287.3, 2362.94],
|
||||
@ -169,7 +168,7 @@ def test_more():
|
||||
[random.randint(0, 100),
|
||||
random.randint(0, 100),
|
||||
random.randint(0, 100)] for _ in range(80)
|
||||
]
|
||||
]
|
||||
scatter3D = Scatter3D("3D 散点图示例", width=1200, height=600)
|
||||
scatter3D.add("", data, is_visualmap=True, visual_range_color=RANGE_COLOR)
|
||||
page.add(scatter3D)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user