overlap method of chart.py

This commit is contained in:
guozhen3 2022-02-18 16:43:28 +08:00
parent 58ca07510f
commit c6197f6c43
7 changed files with 168 additions and 2 deletions

View File

@ -94,6 +94,7 @@ class Chart(Base):
# 这是一个bug
# 添加轴执行add_yaxis操作的顺序与新添加的color值设置color属性未一一对应正好颠倒
self.colors.insert(-self.default_color_n, color)
# self.colors = [color] + self.colors
if self.theme == ThemeType.WHITE:
self.options.update(color=self.colors)
@ -201,6 +202,9 @@ class RectChart(Chart):
chart.options.get("legend")[0].get("selected")
)
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)
return self

6
test/test.csv Normal file
View File

@ -0,0 +1,6 @@
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

@ -1,8 +1,11 @@
from unittest.mock import patch
from nose.tools import assert_equal
from pyecharts.charts import Line
def test_chart_append_color():
@patch("pyecharts.render.engine.write_utf8_html_file")
def test_chart_append_color(fake_writer):
x_data = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
y_data1 = [140, 232, 101, 264, 90, 340, 250]
y_data2 = [120, 282, 111, 234, 220, 340, 310]
@ -20,10 +23,11 @@ def test_chart_append_color():
color='#00DDFF')
)
c.render()
_, content = fake_writer.call_args[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()
expected_result = ['#80FFA5', '#00DDFF', *default_colors]
assert_equal(c.colors, expected_result)
assert_equal(c.colors, expected_result)

27
test/test_chart_diff.py Normal file
View File

@ -0,0 +1,27 @@
# encoding: utf-8
"""
@file: test_chart_diff.py
@desc:
@author: guozhen3
@time: 2022/1/28
"""
from pyecharts.charts import Line
x_data = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
y_data1 = [140, 232, 101, 264, 90, 340, 250]
y_data2 = [120, 282, 111, 234, 220, 340, 310]
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')
)
c.render('rend2.html')

27
test/test_grid2.py Normal file
View File

@ -0,0 +1,27 @@
# 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()

60
test/test_overlap.py Normal file
View File

@ -0,0 +1,60 @@
# 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()

38
test/test_stack2.py Normal file
View File

@ -0,0 +1,38 @@
# 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()