Update and Add test code && Format chart.py

This commit is contained in:
sunhailinLeo 2022-04-13 19:31:43 +08:00
parent d25cca137b
commit 3cde53e1f3
9 changed files with 130 additions and 147 deletions

View File

@ -246,7 +246,7 @@ class RectChart(Chart):
self.options.get("series").extend(chart.options.get("series"))
# to merge colors of chart
for c in chart.colors[:len(chart.colors) - self.default_color_n]:
self.colors.insert(len(self.colors)-self.default_color_n, c)
self.colors.insert(len(self.colors) - self.default_color_n, c)
return self

View File

@ -1,6 +0,0 @@
item,vol,chg
a,35585,-3918
b,26219,1568
c,24921,-1162
d,21255,960
e,16804,738
1 item vol chg
2 a 35585 -3918
3 b 26219 1568
4 c 24921 -1162
5 d 21255 960
6 e 16804 738

View File

@ -12,22 +12,22 @@ def test_chart_append_color(fake_writer):
c = (
Line()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="品类 1",
y_axis=y_data1,
color='#80FFA5')
.add_yaxis(
series_name="品类 2",
y_axis=y_data2,
color='#00DDFF')
.add_xaxis(xaxis_data=x_data)
.add_yaxis(series_name="品类 1", y_axis=y_data1, color="#80FFA5")
.add_yaxis(series_name="品类 2", y_axis=y_data2, color="#00DDFF")
)
c.render()
_, content = fake_writer.call_args[0]
# Old Version (Before 2.0)
# default_colors = (
# "#c23531 #2f4554 #61a0a8 #d48265 #749f83 #ca8622 #bda29a #6e7074 "
# "#546570 #c4ccd3 #f05b72 #ef5b9c #f47920 #905a3d #fab27b #2a5caa "
# "#444693 #726930 #b2d235 #6d8346 #ac6767 #1d953f #6950a1 #918597"
# ).split()
# New Version
default_colors = (
"#c23531 #2f4554 #61a0a8 #d48265 #749f83 #ca8622 #bda29a #6e7074 "
"#546570 #c4ccd3 #f05b72 #ef5b9c #f47920 #905a3d #fab27b #2a5caa "
"#444693 #726930 #b2d235 #6d8346 #ac6767 #1d953f #6950a1 #918597"
"#5470c6 #91cc75 #fac858 #ee6666 #73c0de #3ba272 #fc8452 #9a60b4 " "#ea7ccc"
).split()
expected_result = ['#80FFA5', '#00DDFF', *default_colors]
assert_equal(c.colors, expected_result)
expected_result = ["#80FFA5", "#00DDFF", *default_colors]
assert_equal(c.colors, expected_result)

48
test/test_custom.py Normal file
View File

@ -0,0 +1,48 @@
from unittest.mock import patch
from nose.tools import assert_greater, assert_in
from pyecharts.charts import Custom
from pyecharts.commons.utils import JsCode
@patch("pyecharts.render.engine.write_utf8_html_file")
def test_custom_base(fake_writer):
c = (
Custom()
.add(
series_name="",
render_item=JsCode("""
function (params, api) {
var categoryIndex = api.value(0);
var start = api.coord([api.value(1), categoryIndex]);
var end = api.coord([api.value(2), categoryIndex]);
var height = api.size([0, 1])[1] * 0.6;
var rectShape = echarts.graphic.clipRectByRect({
x: start[0],
y: start[1] - height / 2,
width: end[0] - start[0],
height: height
}, {
x: params.coordSys.x,
y: params.coordSys.y,
width: params.coordSys.width,
height: params.coordSys.height
});
return rectShape && {
type: 'rect',
shape: rectShape,
style: api.style()
};
}
"""),
data=None,
)
)
c.render()
_, content = fake_writer.call_args[0]
assert_greater(len(content), 2000)
assert_in("renderItem", content)

View File

@ -36,7 +36,7 @@ def test_animation_options_remove_none():
def test_init_options_remove_none():
option = InitOpts(animation_opts={})
option = InitOpts(animation_opts={}, aria_opts={})
expected = {
"animationOpts": {},
"height": "500px",
@ -44,6 +44,7 @@ def test_init_options_remove_none():
"renderer": "canvas",
"theme": "white",
"width": "900px",
"ariaOpts": {},
}
assert_equal(expected, remove_key_with_none_value(option.opts))
@ -118,6 +119,8 @@ def test_data_zoom_options_remove_none():
"start": 20,
"type": "slider",
"zoomLock": False,
"showDetail": True,
"showDataShadow": True,
}
assert_equal(expected, remove_key_with_none_value(option.opts))
@ -149,6 +152,8 @@ def test_visual_map_options_remove_none():
"inverse": False,
"splitNumber": 5,
"type": "continuous",
"hoverLink": True,
"padding": 5,
"borderWidth": 0,
}
assert_equal(expected, remove_key_with_none_value(option.opts))
@ -166,6 +171,11 @@ def test_tool_tip_options_remove_none():
"showContent": True,
"showDelay": 0,
"trigger": "item",
"enterable": False,
"confine": False,
"appendToBody": False,
"transitionDuration": 0.4,
"order": "seriesAsc",
"triggerOn": "mousemove|click",
}
assert_equal(expected, remove_key_with_none_value(option.opts))

View File

@ -71,3 +71,59 @@ def test_grid_options(fake_writer):
gc.render()
_, content = fake_writer.call_args[0]
assert_in("containLabel", content)
def test_chart_for_grid():
x_data = ["{}".format(i) for i in range(1, 13)]
bar = (
Bar()
.add_xaxis(x_data)
.add_yaxis(
"蒸发量",
[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],
yaxis_index=0,
color="red",
)
.add_yaxis(
"降水量",
[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],
yaxis_index=1,
color="green",
)
.extend_axis(yaxis=opts.AxisOpts(name="蒸发量", type_="value", position="right"))
.extend_axis(yaxis=opts.AxisOpts(type_="value", name="温度", position="left"))
.set_global_opts(
yaxis_opts=opts.AxisOpts(name="降水量", position="right", offset=80)
)
)
line = (
Line()
.add_xaxis(x_data)
.add_yaxis(
"平均温度",
[2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2],
yaxis_index=2,
color="blue",
label_opts=opts.LabelOpts(is_show=False),
)
)
bar.overlap(line)
assert_equal(bar.colors[:3], ["red", "green", "blue"])
bar.render("overlap_test_after_colors_update.html")
return bar
@patch("pyecharts.render.engine.write_utf8_html_file")
def test_grid_example_1(fake_writer):
bar = test_chart_for_grid()
gc = Grid().add(
bar, opts.GridOpts(pos_left="5%", pos_right="20%"), is_control_axis_index=True
)
expected_idx = (0, 1, 2)
for idx, series in enumerate(gc.options.get("series")):
assert_equal(series.get("yAxisIndex"), expected_idx[idx])
gc.render()
_, content = fake_writer.call_args[0]
assert_in("yAxisIndex", content)

View File

@ -1,27 +0,0 @@
# encoding: utf-8
"""
@file: test.grid.py
@desc:
@author: guozhen3
@time: 2022/2/18
"""
from nose.tools import assert_equal
from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Line
from test_overlap import test_chart_for_grid
def test_grid_control_axis_index():
bar = test_chart_for_grid()
gc = Grid().add(
bar, opts.GridOpts(pos_left="5%", pos_right="20%"), is_control_axis_index=True
)
expected_idx = (0, 1, 2)
for idx, series in enumerate(gc.options.get("series")):
assert_equal(series.get("yAxisIndex"), expected_idx[idx])
gc.render("grid_test.html")
test_grid_control_axis_index()

View File

@ -1,60 +0,0 @@
# encoding: utf-8
"""
@file: test_overlap.py
@desc:
@author: guozhen3
@time: 2022/2/18
"""
from unittest.mock import patch
from nose.tools import assert_equal, assert_in
from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Line
def test_chart_for_grid():
x_data = ["{}".format(i) for i in range(1, 13)]
bar = (
Bar()
.add_xaxis(x_data)
.add_yaxis(
"蒸发量",
[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],
yaxis_index=0,
color='red'
)
.add_yaxis(
"降水量",
[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],
yaxis_index=1,
color='green'
)
.extend_axis(yaxis=opts.AxisOpts(name="蒸发量", type_="value", position="right"))
.extend_axis(yaxis=opts.AxisOpts(type_="value", name="温度", position="left"))
.set_global_opts(
yaxis_opts=opts.AxisOpts(name="降水量", position="right", offset=80)
)
)
line = (
Line()
.add_xaxis(x_data)
.add_yaxis(
"平均温度",
[2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2],
yaxis_index=2,
color="blue",
label_opts=opts.LabelOpts(is_show=False),
)
)
bar.overlap(line)
assert_equal(bar.colors[:3], ['red', 'green', 'blue'])
bar.render("overlap_test_after_colors_update.html")
return bar
if __name__ == "__main__":
test_chart_for_grid()

View File

@ -1,38 +0,0 @@
# encoding: utf-8
"""
@file: test_stack2.py
@desc:
@author: guozhen3
@time: 2022/2/18
"""
import pandas as pd
from pyecharts.charts import Bar
data = pd.read_csv("test.csv")
data["base_vol"] = data["chg"].map(lambda x: -x if x > 0 else 0) + data["vol"]
bar = (
Bar()
.add_xaxis(xaxis_data=data["item"].tolist())
.add_yaxis(
series_name="Position Vol",
y_axis=data["base_vol"].tolist(),
color="LightSeaGreen",
stack="vol"
)
.add_yaxis(
series_name="Increased",
y_axis=data["chg"].map(lambda x: x if x > 0 else 0).tolist(),
color="red",
stack="vol"
).add_yaxis(
series_name="Dcreased",
y_axis=data["chg"].map(lambda x: -x if x < 0 else 0).tolist(),
color="green",
stack="vol"
)
)
bar.render()