pyecharts源码解读(18)HTML组件包components之图像组件Image

当前pyecharts的版本为1.9.0。

components包概述

components包位于pyecharts包顶级目录中,用于定义pyecharts的HTML组件。包结构如下:

├─components # HTML组件包
│  │  image.py # 定义图像组件类Image
│  │  table.py # 定义表格组件类Table
│  │  __init__.py # 重构命名空间,将组件类命名空间提升至components包命名空间

目前HTML组件类TableImage 与复合图表类Page不兼容。

Image

pyecharts/components/image.py模块只定义了图像组件类Image

Image类继承自基类ChartMixin,作用为绘制图像。

Image类的签名为class Image(page_title: str = CurrentConfig.PAGE_TITLE, js_host: str = "")

Image类的构造方法参数如下:

  • js_host:JavaScript库的URL。字符串,默认值为""
  • page_title:HTML页面标题。字符串,默认值为全局变量CurrentConfig.PAGE_TITLE

Image类的属性如下:

  • js_host:JavaScript库的URL。字符串,默认值为全局变量CurrentConfig.ONLINE_HOST。属性值为构造方法参数js_host与全局变量CurrentConfig.ONLINE_HOST进行或操作的结果。
  • page_title:HTML页面标题。字符串。值为构造方法参数page_title值。
  • js_functions:自定义JavaScript语句。类型为OrderedSet对象。默认值为OrderedSet()
  • js_dependencies:定义JavaScript依赖库。类型为OrderedSet对象。默认值为OrderedSet("echarts")
  • title_opts:图像组件标题配置。类型为ComponentTitleOpts对象,默认值为ComponentTitleOpts()。标题配置包括titlesubtitletitle_stylesubtitle_style等4个配置项。
  • html_content<img>标签中插入的HTML。字符串,默认值为""注意!模板中的渲染方式<img {{ chart.html_content }}/>
  • _component_type:组件类型。字符串,默认值为"image"
  • chart_id:组件id。字符串,默认值为uuid.uuid4().hex

Image类的方法如下:

  • add(self, src: str, style_opts: Optional[dict] = None):添加图像信息。
    • src:图像的源地址。类型为字符串。
    • style_opts:图像样式,即<img> 标签 CSS 样式。类型为字典。默认值为None
  • set_global_opts(title_opts: Union[ComponentTitleOpts, dict, None] = None):将title_opts参数值设置为图像对象的title_opts属性值。
  • render:调用renderengine模块中的render函数渲染HTML文档。默认渲染模板为components.html
  • render_embed:调用renderengine模块中的render_embed函数输出HTML字符串。默认渲染模板为components.html
  • render_notebook:调用renderengine模块中的render_notebook函数将输出嵌入到notebook中。默认渲染模板为 nb_components.html

图像组件模板

macro部分源码:

{% elif chart._component_type == "image" %}
    <div id="{{ chart.chart_id }}" class="chart-container" style="">
         <p class="title" {{ chart.title_opts.title_style }}> {{ chart.title_opts.title }}</p>
         <p class="subtitle" {{ chart.title_opts.subtitle_style }}> {{ chart.title_opts.subtitle }}</p>
         <img {{ chart.html_content }}/>
    </div>
{% endif %}

components.html源码:

{% import 'macro' as macro %}
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>{{ chart.page_title }}</title>
</head>
<body>

{{ macro.gen_components_content(chart) }}

</body>
</html>

简易图像组件Image案例

from pyecharts.components import Image

image = Image()

img_src = (
    "https://user-images.githubusercontent.com/19553554/"
    "71825144-2d568180-30d6-11ea-8ee0-63c849cfd934.png"
)

image.add(
    src = img_src,
    style_opts = {"width": "200px", "height": "200px", "style": "margin-top: 20px"},
)

image.set_global_opts({"title":"标题", 
                       "subtitle":"副标题",
                       "title_style":"style='color:red'",
                       "subtitle_style":"style='color:green'"
                      })

image.render()

在这里插入图片描述

pyecharts/components/image.py模块源码

import uuid

from jinja2 import Environment

from ..charts.mixins import ChartMixin
from ..commons.utils import OrderedSet
from ..globals import CurrentConfig
from ..options import ComponentTitleOpts
from ..render import engine
from ..types import Optional, Union


class Image(ChartMixin):
    def __init__(self, page_title: str = CurrentConfig.PAGE_TITLE, js_host: str = ""):
        self.page_title = page_title
        self.js_host = js_host or CurrentConfig.ONLINE_HOST
        self.js_dependencies: OrderedSet = OrderedSet()
        self.title_opts: ComponentTitleOpts = ComponentTitleOpts()
        self.html_content: str = ""
        self._component_type: str = "image"
        self.chart_id: str = uuid.uuid4().hex

    def add(self, src: str, style_opts: Optional[dict] = None):
        html_tag_args = ""
        html_tag_args += 'src="{}" '.format(src)
        if style_opts:
            for k, v in style_opts.items():
                html_tag_args += '{}="{}" '.format(k, v)
        self.html_content = html_tag_args
        return self

    def set_global_opts(self, title_opts: Union[ComponentTitleOpts, dict, None] = None):
        self.title_opts = title_opts
        return self

    def render(
        self,
        path: str = "render.html",
        template_name: str = "components.html",
        env: Optional[Environment] = None,
        **kwargs,
    ) -> str:
        return engine.render(self, path, template_name, env, **kwargs)

    def render_embed(
        self,
        template_name: str = "components.html",
        env: Optional[Environment] = None,
        **kwargs,
    ) -> str:
        return engine.render_embed(self, template_name, env, **kwargs)

    def render_notebook(self):
        # only notebook env need to re-generate chart_id
        self.chart_id = uuid.uuid4().hex
        return engine.render_notebook(self, "nb_components.html", "nb_components.html")

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值