Skip to content

Commit

Permalink
add support with Graphic Component (pyecharts#1162)
Browse files Browse the repository at this point in the history
* add support with Graphic Component

* Update: clean code

* Fix: example
  • Loading branch information
sunhailin-Leo authored and chenjiandongx committed Jun 1, 2019
1 parent dc7617f commit 4c43f9d
Show file tree
Hide file tree
Showing 5 changed files with 346 additions and 23 deletions.
57 changes: 54 additions & 3 deletions example/bar_example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from example.commons import Collector, Faker
from pyecharts import options as opts
from pyecharts.charts import Bar, Page
from pyecharts.commons.utils import JsCode

C = Collector()

Expand Down Expand Up @@ -232,7 +233,7 @@ def bar_datazoom_slider() -> Bar:
.add_yaxis("商家A", Faker.days_values)
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-DataZoom(slider-水平)"),
datazoom_opts=[opts.DataZoomOpts()],
datazoom_opts=opts.DataZoomOpts(),
)
)
return c
Expand All @@ -246,7 +247,7 @@ def bar_datazoom_slider_vertical() -> Bar:
.add_yaxis("商家A", Faker.days_values, color=Faker.rand_color())
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-DataZoom(slider-垂直)"),
datazoom_opts=[opts.DataZoomOpts(orient="vertical")],
datazoom_opts=opts.DataZoomOpts(orient="vertical"),
)
)
return c
Expand All @@ -260,7 +261,7 @@ def bar_datazoom_inside() -> Bar:
.add_yaxis("商家A", Faker.days_values, color=Faker.rand_color())
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-DataZoom(inside)"),
datazoom_opts=[opts.DataZoomOpts(type_="inside")],
datazoom_opts=opts.DataZoomOpts(type_="inside"),
)
)
return c
Expand Down Expand Up @@ -347,4 +348,54 @@ def bar_rorate_xaxis_label() -> Bar:
return c


@C.funcs
def bar_graphic_component() -> Bar:
c = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar-Graphic 组件示例"),
graphic_opts=[
opts.GraphicGroup(
graphic_item=opts.GraphicItem(
rotation=JsCode("Math.PI / 4"),
bounding="raw",
right=110,
bottom=110,
z=100,
),
children=[
opts.GraphicRect(
graphic_item=opts.GraphicItem(
left="center", top="center", z=100
),
graphic_shape_opts=opts.GraphicShapeOpts(
width=400, height=50
),
graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
fill="rgba(0,0,0,0.3)"
),
),
opts.GraphicText(
graphic_item=opts.GraphicItem(
left="center", top="center", z=100
),
graphic_textstyle_opts=opts.GraphicTextStyleOpts(
text="pyecharts bar chart",
font="bold 26px Microsoft YaHei",
graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
fill="#fff"
),
),
),
],
)
],
)
)
return c


Page().add(*[fn() for fn, _ in C.charts]).render()
30 changes: 11 additions & 19 deletions pyecharts/charts/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
from ..charts.base import Base
from ..commons.types import Optional, Sequence, Union
from ..globals import RenderType, ThemeType
from ..options.charts_options import BaseGraphic

VisualMapType = Union[opts.VisualMapOpts, dict]
DataZoomType = Union[opts.DataZoomOpts, dict]
GraphicType = Union[BaseGraphic, dict]


class Chart(Base):
Expand Down Expand Up @@ -112,23 +117,17 @@ def set_global_opts(
toolbox_opts: Union[opts.ToolboxOpts, dict] = None,
xaxis_opts: Union[opts.AxisOpts, dict, None] = None,
yaxis_opts: Union[opts.AxisOpts, dict, None] = None,
visualmap_opts: Union[
opts.VisualMapOpts, Sequence[Union[opts.VisualMapOpts, dict]], dict, None
] = None,
datazoom_opts: Sequence[Union[opts.DataZoomOpts, dict, None]] = None,
visualmap_opts: Union[VisualMapType, Sequence[VisualMapType], None] = None,
datazoom_opts: Union[DataZoomType, Sequence[DataZoomType], None] = None,
graphic_opts: Union[GraphicType, Sequence[GraphicType], None] = None,
):
vs = []
if isinstance(visualmap_opts, Sequence):
for vo in visualmap_opts:
vs.append(vo)

_visualmap_opts = vs if vs else visualmap_opts

self.options.update(
title=title_opts,
toolbox=toolbox_opts,
tooltip=tooltip_opts,
visualMap=_visualmap_opts,
visualMap=visualmap_opts,
dataZoom=datazoom_opts,
graphic=graphic_opts,
)

if isinstance(legend_opts, opts.LegendOpts):
Expand All @@ -146,13 +145,6 @@ def set_global_opts(
yaxis_opts = yaxis_opts.opts
self.options["yAxis"][0].update(yaxis_opts)

if datazoom_opts:
dzs = []
for dz in datazoom_opts:
if not dz:
continue
dzs.append(dz)
self.options.update(dataZoom=dzs)
return self


Expand Down
9 changes: 9 additions & 0 deletions pyecharts/options/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
BMapTypeControl,
ComponentTitleOpts,
GraphCategory,
GraphicBasicStyleOpts,
GraphicGroup,
GraphicImage,
GraphicImageStyleOpts,
GraphicItem,
GraphicRect,
GraphicShapeOpts,
GraphicText,
GraphicTextStyleOpts,
GraphLink,
GraphNode,
SunburstItem,
Expand Down
Loading

0 comments on commit 4c43f9d

Please sign in to comment.