当前pyecharts
的版本为1.9.0。
components
包概述
components
包位于pyecharts
包顶级目录中,用于定义pyecharts
的HTML组件。包结构如下:
├─components # HTML组件包
│ │ image.py # 定义图像组件类Image
│ │ table.py # 定义表格组件类Table
│ │ __init__.py # 重构命名空间,将组件类命名空间提升至components包命名空间
目前HTML组件类Table
和Image
与复合图表类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()
。标题配置包括title
、subtitle
、title_style
、subtitle_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
:调用render
包engine
模块中的render
函数渲染HTML文档。默认渲染模板为components.html
。render_embed
:调用render
包engine
模块中的render_embed
函数输出HTML字符串。默认渲染模板为components.html
。render_notebook
:调用render
包engine
模块中的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")