Skip to content

Commit

Permalink
simplify bar3d example and add more test cases at global_options.py
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhailin-Leo committed Nov 12, 2019
1 parent 204eec5 commit 4c4a05d
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 91 deletions.
107 changes: 18 additions & 89 deletions example/bar3d_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
C = Collector()


def generate_data():
data = []
for j in range(10):
for k in range(10):
value = random.randint(0, 9)
data.append([j, k, value * 2 + 4])
return data


@C.funcs
def bar3d_base() -> Bar3D:
data = [(i, j, random.randint(0, 12)) for i in range(6) for j in range(24)]
Expand All @@ -29,100 +38,20 @@ def bar3d_base() -> Bar3D:

@C.funcs
def bar3d_stack() -> Bar3D:
def generate_data():
data = []
for j in range(10):
for k in range(10):
value = random.randint(0, 9)
data.append([j, k, value * 2 + 4])
return data

c = (
Bar3D()
.add(
"",
generate_data(),
shading="lambert",
xaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
yaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
zaxis3d_opts=opts.Axis3DOpts(type_="value"),
)
.add(
"",
generate_data(),
shading="lambert",
xaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
yaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
zaxis3d_opts=opts.Axis3DOpts(type_="value"),
)
.add(
"",
generate_data(),
shading="lambert",
xaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
yaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
zaxis3d_opts=opts.Axis3DOpts(type_="value"),
)
.add(
"",
generate_data(),
shading="lambert",
xaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
yaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
zaxis3d_opts=opts.Axis3DOpts(type_="value"),
)
.add(
"",
generate_data(),
shading="lambert",
xaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
yaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
zaxis3d_opts=opts.Axis3DOpts(type_="value"),
)
.add(
"",
generate_data(),
shading="lambert",
xaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
yaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
zaxis3d_opts=opts.Axis3DOpts(type_="value"),
)
.add(
x_data = y_data = list(range(10))
bar3d = Bar3D()
for _ in range(10):
bar3d.add(
"",
generate_data(),
shading="lambert",
xaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
yaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
xaxis3d_opts=opts.Axis3DOpts(data=x_data, type_="value"),
yaxis3d_opts=opts.Axis3DOpts(data=y_data, type_="value"),
zaxis3d_opts=opts.Axis3DOpts(type_="value"),
)
.add(
"",
generate_data(),
shading="lambert",
xaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
yaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
zaxis3d_opts=opts.Axis3DOpts(type_="value"),
)
.add(
"",
generate_data(),
shading="lambert",
xaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
yaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
zaxis3d_opts=opts.Axis3DOpts(type_="value"),
)
.add(
"",
generate_data(),
shading="lambert",
xaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
yaxis3d_opts=opts.Axis3DOpts(data=list(range(10)), type_="value"),
zaxis3d_opts=opts.Axis3DOpts(type_="value"),
)
.set_global_opts(title_opts=opts.TitleOpts("Bar3D-堆叠柱状图示例"))
.set_series_opts(**{"stack": "stack"})
)
return c
bar3d.set_global_opts(title_opts=opts.TitleOpts("Bar3D-堆叠柱状图示例"))
bar3d.set_series_opts(**{"stack": "stack"})
return bar3d


Page().add(*[fn() for fn, _ in C.charts]).render()
30 changes: 29 additions & 1 deletion test/test_bar3d.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import random
from unittest.mock import patch

from nose.tools import assert_equal
from nose.tools import assert_equal, assert_in

from pyecharts import options as opts
from pyecharts.charts import Bar3D
Expand All @@ -26,3 +26,31 @@ def test_bar3d_base(fake_writer):
_, content = fake_writer.call_args[0]
assert_equal(c.theme, "white")
assert_equal(c.renderer, "canvas")


@patch("pyecharts.render.engine.write_utf8_html_file")
def test_bar3d_stack(fake_writer):
data1 = [(i, j, random.randint(0, 12)) for i in range(6) for j in range(24)]
data2 = [(i, j, random.randint(13, 20)) for i in range(6) for j in range(24)]
c = (
Bar3D()
.add(
"1",
[[d[1], d[0], d[2]] for d in data1],
xaxis3d_opts=opts.Axis3DOpts(Faker.clock, type_="category"),
yaxis3d_opts=opts.Axis3DOpts(Faker.week_en, type_="category"),
zaxis3d_opts=opts.Axis3DOpts(type_="value"),
)
.add(
"2",
[[d[1], d[0], d[2]] for d in data2],
xaxis3d_opts=opts.Axis3DOpts(Faker.clock, type_="category"),
yaxis3d_opts=opts.Axis3DOpts(Faker.week_en, type_="category"),
zaxis3d_opts=opts.Axis3DOpts(type_="value"),
)
.set_global_opts(visualmap_opts=opts.VisualMapOpts(max_=20))
.set_series_opts(**{"stack": "stack"})
)
c.render()
_, content = fake_writer.call_args[0]
assert_in("stack", content)
129 changes: 128 additions & 1 deletion test/test_global_options.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,59 @@
from nose.tools import assert_equal

from pyecharts.commons.utils import remove_key_with_none_value
from pyecharts.options.global_options import ToolboxOpts
from pyecharts.options.global_options import (
AnimationOpts,
InitOpts,
ToolBoxFeatureOpts,
ToolboxOpts,
BrushOpts,
DataZoomOpts,
LegendOpts,
VisualMapOpts,
TooltipOpts,
)


def test_animation_options_remove_none():
option = AnimationOpts()
expected = {
"animation": True,
"animationDelay": 0,
"animationDelayUpdate": 0,
"animationDuration": 1000,
"animationDurationUpdate": 300,
"animationEasing": "cubicOut",
"animationEasingUpdate": "cubicOut",
"animationThreshold": 2000,
}
assert_equal(expected, remove_key_with_none_value(option.opts))


def test_init_options_remove_none():
option = InitOpts(animation_opts={})
expected = {
"animationOpts": {},
"height": "500px",
"page_title": "Awesome-pyecharts",
"renderer": "canvas",
"theme": "white",
"width": "900px",
}
assert_equal(expected, remove_key_with_none_value(option.opts))


def test_toolbox_feature_options_remove_none():
option = ToolBoxFeatureOpts()
expected = {
"dataView": {"readOnly": False, "show": True, "title": "data view"},
"dataZoom": {
"show": True,
"title": {"back": "data zoom restore", "zoom": "data zoom"},
},
"restore": {"show": True, "title": "restore"},
"saveAsImage": {"show": True, "title": "save as image", "type": "png"},
}
assert_equal(expected, remove_key_with_none_value(option.opts))


def test_toolbox_options_remove_none():
Expand All @@ -15,3 +67,78 @@ def test_toolbox_options_remove_none():
"feature": {},
}
assert_equal(expected, remove_key_with_none_value(option.opts))


def test_brush_options_remove_none():
option = BrushOpts()
expected = {
"brushMode": "single",
"brushStyle": {
"borderColor": "rgba(120,140,180,0.8)",
"borderWidth": 1,
"color": "rgba(120,140,180,0.3)",
},
"brushType": "rect",
"removeOnClick": True,
"throttleDelay": 0,
"throttleType": "fixRate",
"toolbox": ["rect", "polygon", "keep", "clear"],
"transformable": True,
}
assert_equal(expected, remove_key_with_none_value(option.opts))


def test_data_zoom_options_remove_none():
option = DataZoomOpts()
expected = {
"end": 80,
"orient": "horizontal",
"realtime": True,
"show": True,
"start": 20,
"type": "slider",
"zoomLock": False,
}
assert_equal(expected, remove_key_with_none_value(option.opts))


def test_legend_options_remove_none():
option = LegendOpts()
expected = {
"show": True,
"padding": 5,
"itemGap": 10,
"itemWidth": 25,
"itemHeight": 14,
}
assert_equal(expected, remove_key_with_none_value(option.opts))


def test_visual_map_options_remove_none():
option = VisualMapOpts()
expected = {
"calculable": True,
"inRange": {"color": ["#50a3ba", "#eac763", "#d94e5d"]},
"itemHeight": 140,
"itemWidth": 20,
"max": 100,
"min": 0,
"orient": "vertical",
"show": True,
"showLabel": True,
"splitNumber": 5,
"type": "continuous",
}
assert_equal(expected, remove_key_with_none_value(option.opts))


def test_tool_tip_options_remove_none():
option = TooltipOpts(textstyle_opts=None)
expected = {
"axisPointer": {"type": "line"},
"borderWidth": 0,
"show": True,
"trigger": "item",
"triggerOn": "mousemove|click",
}
assert_equal(expected, remove_key_with_none_value(option.opts))

0 comments on commit 4c4a05d

Please sign in to comment.