pyecharts源码解读(13)图表类包charts之组合图表:直角坐标系(网格布局)Grid

当前pyecharts的版本为1.9.0。

概述

pyecharts/charts/composite_charts/包中的四个模块分别定义了组合图表类,其中pyecharts/charts/composite_charts/grid.py模块只定义了网格布局类Grid

Grid类继承自图表基类Base,作用为在一个echarts实例中利用2×2网格布局同时绘制4个数据系列。即实现ECharts中的直角坐标系grid功能。
注意!网格上仅支持绘制折线图,柱状图,散点图(气泡图)。

Grid类的签名为class Grid( init_opts: opts.InitOpts = opts.InitOpts())

Grid类的属性和方法大多继承自Base类,主要添加了add方法。

def add(
    # 图表实例,支持Chart类或者其子类,但是只有Bar、Line、Scatter才能完全实现Grid的特性
    chart: Chart,
    # 直角坐标系网格配置项,参见 options.GridOpts类
    grid_opts: Union[opts.GridOpts, dict],
    # 直角坐标系网格索引
    grid_index: int = 0,
    # 是否由自己控制 Axis 索引
    is_control_axis_index: bool = False,
)

案例:在2×2网格绘制图表

from pyecharts.charts import Bar, Line, Grid

a = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
b1 = [5, 20, 36, 10, 75, 90]
b2 = [7, 25, 31, 14, 65, 100]

line1 = Line()
line1.add_xaxis(a)
line1.add_yaxis("", b1)

line2 = Line()
line2.add_xaxis(a)
line2.add_yaxis("", b2)

x = ['一月', '二月', '三月', '四月', '五月', '六月']
y1 = [800, 600, 901, 1234, 1290, 1330, 1620]
y2 = [700, 660, 920, 1204, 1250, 1000, 1420]

bar1 = Bar()
bar1.add_xaxis(x)
bar1.add_yaxis("",y1)

bar2 = Bar()
bar2.add_xaxis(x)
bar2.add_yaxis("",y2)

grid=Grid()
grid.add(line1,{"left":"7%","top":"7%","width": '38%', "height": '38%'})
grid.add(line2,{"right":"7%","top":"7%","width": '38%', "height": '38%'})
grid.add(bar1,{"left":"7%","bottom":"7%","width": '38%', "height": '38%'})
grid.add(bar2,{"right":"7%","bottom":"7%","width": '38%', "height": '38%'})

grid.render()

输出为:
在这里插入图片描述

Grid类源码

class Grid(Base):

    def __init__(self, init_opts: types.Init = opts.InitOpts()):
        super().__init__(init_opts=init_opts)
        self.options: types.Optional[dict] = None
        self._axis_index: int = 0
        self._grow_grid_index: int = 0
        self.bg_color = init_opts.opts.get("bg_color")

    def add(
        self,
        chart: Chart,
        grid_opts: types.Union[opts.GridOpts, dict],
        *,
        grid_index: int = 0,
        is_control_axis_index: bool = False,
    ):
        if self.options is None:
            self.options = copy.deepcopy(chart.options)
            self.chart_id = chart.chart_id
            self.options.update(grid=[], title=[])
            if self.theme != ThemeType.WHITE:
                self.options.update(color=[])

            # Priority Order: Grid > Other Chart
            self.options.update(backgroundColor=self.bg_color)

            if not is_control_axis_index:
                for s in self.options.get("series"):
                    s.update(xAxisIndex=self._axis_index, yAxisIndex=self._axis_index)

        title = chart.options.get("title", opts.TitleOpts().opts)
        if isinstance(title, opts.TitleOpts):
            title = title.opts
        if not isinstance(title, types.Sequence):
            title = (title,)
        self.options.get("title").extend(title)

        if not is_control_axis_index:
            for s in chart.options.get("series"):
                s.update(xAxisIndex=self._axis_index, yAxisIndex=self._axis_index)

        for dep in chart.js_dependencies.items:
            self.js_dependencies.add(dep)

        if chart.options.get("geo") is not None:
            self.options.update(geo=chart.options.get("geo"))

        if isinstance(chart, RectChart):
            if grid_index == 0:
                grid_index = self._grow_grid_index

            for x in chart.options.get("xAxis"):
                x.update(gridIndex=grid_index)
            for y in chart.options.get("yAxis"):
                y.update(gridIndex=grid_index)
            self._grow_grid_index += 1

        if self._axis_index > 0:
            self.options.get("series").extend(chart.options.get("series"))
            self.options.get("legend").extend(chart.options.get("legend"))
            if isinstance(chart, RectChart):
                self.options.get("xAxis").extend(chart.options.get("xAxis"))
                self.options.get("yAxis").extend(chart.options.get("yAxis"))

        self.options.get("grid").append(grid_opts)
        self._axis_index += 1
        return self
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值