Skip to content

Commit

Permalink
update some charts configure with ChartItem
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhailin-Leo committed Oct 27, 2020
1 parent f5bd011 commit 50f8da2
Show file tree
Hide file tree
Showing 19 changed files with 283 additions and 51 deletions.
8 changes: 7 additions & 1 deletion pyecharts/charts/basic_charts/effectscatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def add_yaxis(
):
self._append_color(color)
self._append_legend(series_name, is_selected)

if all([isinstance(d, opts.EffectScatterItem) for d in y_axis]):
y_axis = y_axis
else:
y_axis = [list(z) for z in zip(self._xaxis_data, y_axis)]

self.options.get("series").append(
{
"type": ChartType.EFFECT_SCATTER,
Expand All @@ -41,7 +47,7 @@ def add_yaxis(
"symbol": symbol,
"symbolSize": symbol_size,
"symbolRotate": symbol_rotate,
"data": [list(z) for z in zip(self._xaxis_data, y_axis)],
"data": y_axis,
"label": label_opts,
"tooltip": tooltip_opts,
"itemStyle": itemstyle_opts,
Expand Down
11 changes: 8 additions & 3 deletions pyecharts/charts/basic_charts/funnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ def add(
itemstyle_opts: types.ItemStyle = None,
):
self._append_color(color)
data = [{"name": n, "value": v} for n, v in data_pair]
for a, _ in data_pair:
self._append_legend(a, is_selected)
if all([isinstance(d, opts.FunnelItem) for d in data_pair]):
data = data_pair
for a in data_pair:
self._append_legend(a.opts.get("name"), is_selected)
else:
data = [{"name": n, "value": v} for n, v in data_pair]
for a, _ in data_pair:
self._append_legend(a, is_selected)

_dset = set(self.options.get("legend")[0].get("data"))
self.options.get("legend")[0].update(data=list(_dset))
Expand Down
4 changes: 2 additions & 2 deletions pyecharts/charts/basic_charts/heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def __init__(self, init_opts: types.Init = opts.InitOpts()):
def add_yaxis(
self,
series_name: str,
yaxis_data: types.Sequence[types.Union[opts.HeatMapItem, dict]],
value: types.Sequence[types.Union[opts.HeatMapItem, dict]],
yaxis_data: types.Sequence[types.Union[dict]],
value: types.Sequence[types.Union[dict]],
*,
is_selected: bool = True,
xaxis_index: types.Optional[types.Numeric] = None,
Expand Down
10 changes: 7 additions & 3 deletions pyecharts/charts/basic_charts/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ def add_yaxis(
):
self._append_color(color)
self._append_legend(series_name, is_selected)
# 合并 x 和 y 轴数据,避免当 X 轴的类型设置为 'value' 的时候,
# X、Y 轴均显示 Y 轴数据
data = [list(z) for z in zip(self._xaxis_data, y_axis)]

if all([isinstance(d, opts.LineItem) for d in y_axis]):
data = y_axis
else:
# 合并 x 和 y 轴数据,避免当 X 轴的类型设置为 'value' 的时候,
# X、Y 轴均显示 Y 轴数据
data = [list(z) for z in zip(self._xaxis_data, y_axis)]

self.options.get("series").append(
{
Expand Down
2 changes: 1 addition & 1 deletion pyecharts/charts/basic_charts/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def add_schema(
def add(
self,
series_name: str,
data: types.Sequence[types.Union[opts.ParallelItem, dict]],
data: types.Sequence[types.Union[dict]],
*,
is_smooth: bool = False,
is_selected: bool = True,
Expand Down
6 changes: 5 additions & 1 deletion pyecharts/charts/basic_charts/radar.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ def add(
areastyle_opts: opts.AreaStyleOpts = opts.AreaStyleOpts(),
tooltip_opts: types.Tooltip = None,
):
self._append_legend(series_name, is_selected)
if all([isinstance(d, opts.RadarItem) for d in data]):
for a in data:
self._append_legend(a.get("name"), is_selected)
else:
self._append_legend(series_name, is_selected)
self.options.get("series").append(
{
"type": ChartType.RADAR,
Expand Down
2 changes: 1 addition & 1 deletion pyecharts/commons/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _clean_dict(mydict):
# delete key with empty string
continue

yield (key, value)
yield key, value


def _clean_array(myarray):
Expand Down
3 changes: 1 addition & 2 deletions pyecharts/options/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
CandleStickItem,
ComponentTitleOpts,
EffectScatterItem,
FunnelItem,
GaugeDetailOpts,
GaugePointerOpts,
GaugeTitleOpts,
Expand All @@ -28,7 +29,6 @@
GraphicTextStyleOpts,
GraphLink,
GraphNode,
HeatMapItem,
LineItem,
MapItem,
Map3DColorMaterialOpts,
Expand All @@ -39,7 +39,6 @@
Map3DRealisticMaterialOpts,
Map3DViewControlOpts,
PageLayoutOpts,
ParallelItem,
PieItem,
PieLabelLineOpts,
RadarItem,
Expand Down
64 changes: 29 additions & 35 deletions pyecharts/options/charts_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
class BarItem(BasicOpts):
def __init__(
self,
name: Optional[str] = None,
value: Optional[Numeric] = None,
name: Union[int, str],
value: Numeric,
*,
label_opts: Union[LabelOpts, dict, None] = None,
itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,
tooltip_opts: Union[TooltipOpts, dict, None] = None,
Expand All @@ -39,8 +40,9 @@ def __init__(
class BoxplotItem(BasicOpts):
def __init__(
self,
name: Optional[str] = None,
value: Optional[Sequence] = None,
name: Union[int, str],
value: Sequence,
*,
itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,
tooltip_opts: Union[TooltipOpts, dict, None] = None,
):
Expand All @@ -55,8 +57,9 @@ def __init__(
class CandleStickItem(BasicOpts):
def __init__(
self,
name: Optional[str] = None,
value: Optional[Sequence] = None,
name: Union[str, int],
value: Sequence,
*,
itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,
tooltip_opts: Union[TooltipOpts, dict, None] = None,
):
Expand All @@ -71,8 +74,9 @@ def __init__(
class EffectScatterItem(BasicOpts):
def __init__(
self,
name: Union[str, Numeric] = None,
value: Union[str, Numeric] = None,
name: Union[str, Numeric],
value: Union[str, Numeric],
*,
symbol: Optional[str] = None,
symbol_size: Union[Sequence[Numeric], Numeric] = None,
symbol_rotate: Optional[Numeric] = None,
Expand All @@ -96,17 +100,28 @@ def __init__(
}


class HeatMapItem(BasicOpts):
class FunnelItem(BasicOpts):
def __init__(
self,
name: Optional[str] = None,
value: Optional[Sequence] = None,
name: Union[str, int],
value: Union[Sequence, str, Numeric],
*,
is_show_label_line: Optional[bool] = None,
label_line_width: Optional[int] = None,
label_line_linestyle_opts: Union[LineStyleOpts, dict, None] = None,
label_opts: Union[LabelOpts, dict, None] = None,
itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,
tooltip_opts: Union[TooltipOpts, dict, None] = None,
):
self.opts: dict = {
"name": name,
"value": value,
"labelLine": {
"show": is_show_label_line,
"length": label_line_width,
"lineStyle": label_line_linestyle_opts,
},
"label": label_opts,
"itemStyle": itemstyle_opts,
"tooltip": tooltip_opts,
}
Expand All @@ -117,6 +132,7 @@ def __init__(
self,
name: Union[str, Numeric] = None,
value: Union[str, Numeric] = None,
*,
symbol: Optional[str] = "circle",
symbol_size: Numeric = 4,
symbol_rotate: Optional[Numeric] = None,
Expand Down Expand Up @@ -144,7 +160,7 @@ class MapItem(BasicOpts):
def __init__(
self,
name: Optional[str] = None,
value: Optional[Numeric] = None,
value: Union[Sequence, Numeric, str] = None,
is_selected: bool = False,
label_opts: Union[LabelOpts, dict, None] = None,
itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,
Expand All @@ -160,28 +176,6 @@ def __init__(
}


class ParallelItem(BasicOpts):
def __init__(
self,
name: Optional[str] = None,
value: Optional[Sequence] = None,
linestyle_opts: Union[LineStyleOpts, dict, None] = None,
color: Union[str, dict] = "#000",
width: Numeric = 2,
type_: str = "solid",
opacity: Numeric = 0.45,
):
self.opts: dict = {
"name": name,
"value": value,
"lineStyle": linestyle_opts,
"color": color,
"width": width,
"type": type_,
"opacity": opacity,
}


class PieItem(BasicOpts):
def __init__(
self,
Expand All @@ -206,7 +200,7 @@ class RadarItem(BasicOpts):
def __init__(
self,
name: Optional[str] = None,
value: Optional[Numeric] = None,
value: Union[Sequence, Numeric, str] = None,
symbol: Optional[str] = None,
symbol_size: Union[Sequence[Numeric], Numeric] = None,
symbol_rotate: Optional[Numeric] = None,
Expand Down
23 changes: 23 additions & 0 deletions test/test_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ def test_bar_base(fake_writer):
assert_equal(c.renderer, "canvas")


@patch("pyecharts.render.engine.write_utf8_html_file")
def test_bar_item_base(fake_writer):
x_axis = ["A", "B", "C"]
bar_item_1 = [
opts.BarItem(name=d[0], value=d[1]) for d in list(zip(x_axis, [1, 2, 3]))
]
bar_item_2 = [
opts.BarItem(name=d[0], value=d[1]) for d in list(zip(x_axis, [4, 5, 6]))
]

c = (
Bar()
.add_xaxis(x_axis)
.add_yaxis("series0", bar_item_1)
.add_yaxis("series1", bar_item_2)
)
c.render()
_, content = fake_writer.call_args[0]
assert_greater(len(content), 2000)
assert_equal(c.theme, "white")
assert_equal(c.renderer, "canvas")


@patch("pyecharts.render.engine.write_utf8_html_file")
def test_bar_base_with_animation(fake_writer):
c = (
Expand Down
27 changes: 26 additions & 1 deletion test/test_boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

from nose.tools import assert_equal

from pyecharts import options as opts
from pyecharts.charts import Boxplot


@patch("pyecharts.render.engine.write_utf8_html_file")
def test_boxpolt_base(fake_writer):
def test_boxplot_base(fake_writer):
v1 = [
[850, 740, 900, 1070, 930, 850, 950, 980, 980, 880, 1000, 980],
[960, 940, 960, 940, 880, 800, 850, 880, 900, 840, 830, 790],
Expand All @@ -23,3 +24,27 @@ def test_boxpolt_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_boxplot_item_base(fake_writer):
x_axis = ["expr1", "expr2"]
v1 = [
[850, 740, 900, 1070, 930, 850, 950, 980, 980, 880, 1000, 980],
[960, 940, 960, 940, 880, 800, 850, 880, 900, 840, 830, 790],
]
v2 = [
[890, 810, 810, 820, 800, 770, 760, 740, 750, 760, 910, 920],
[890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870, 870],
]
c = Boxplot()

series_a = [opts.BoxplotItem(name=x_axis[0], value=d) for d in c.prepare_data(v1)]
series_b = [opts.BoxplotItem(name=x_axis[1], value=d) for d in c.prepare_data(v2)]

c.add_xaxis(xaxis_data=x_axis).add_yaxis("A", series_a).add_yaxis(
"B", series_b)
c.render()
_, content = fake_writer.call_args[0]
assert_equal(c.theme, "white")
assert_equal(c.renderer, "canvas")
16 changes: 16 additions & 0 deletions test/test_effectscatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from nose.tools import assert_equal

from pyecharts import options as opts
from pyecharts.charts import EffectScatter
from pyecharts.faker import Faker

Expand All @@ -13,3 +14,18 @@ def test_effectscatter_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_effectscatter_item_base(fake_writer):
x_axis = Faker.choose()
chart_item = [
opts.EffectScatterItem(name=d[0], value=d[1])
for d in list(zip(x_axis, Faker.values()))
]

c = EffectScatter().add_xaxis(x_axis).add_yaxis("", chart_item)
c.render()
_, content = fake_writer.call_args[0]
assert_equal(c.theme, "white")
assert_equal(c.renderer, "canvas")
18 changes: 18 additions & 0 deletions test/test_funnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from nose.tools import assert_equal

from pyecharts import options as opts
from pyecharts.charts import Funnel
from pyecharts.faker import Faker

Expand All @@ -13,3 +14,20 @@ def test_funnel_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_funnel_item_base(fake_writer):
funnel_item = [
opts.FunnelItem(name=d[0], value=d[1])
for d in list(zip(Faker.choose(), Faker.values()))
]
c = (
Funnel()
.add("商品", funnel_item)
.set_global_opts(title_opts=opts.TitleOpts(title="Funnel-基本示例"))
)
c.render()
_, content = fake_writer.call_args[0]
assert_equal(c.theme, "white")
assert_equal(c.renderer, "canvas")
Loading

0 comments on commit 50f8da2

Please sign in to comment.