mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
✨ 功能调用统计使用html截图
This commit is contained in:
parent
1236a938c9
commit
910542f2b5
7
resources/template/bar_chart/main.css
Normal file
7
resources/template/bar_chart/main.css
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
body {
|
||||||
|
position: absolute;
|
||||||
|
left: -8px;
|
||||||
|
top: -8px;
|
||||||
|
}
|
||||||
|
|
||||||
50
resources/template/bar_chart/main.html
Normal file
50
resources/template/bar_chart/main.html
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>ECharts Example</title>
|
||||||
|
<!-- 引入ECharts -->
|
||||||
|
<script src="https://cdn.bootcss.com/echarts/4.2.1/echarts.min.js"></script>
|
||||||
|
<link rel="stylesheet" href="main.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- 图表容器 -->
|
||||||
|
<div id="main" style="width: 1000px;height:500px;"></div>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
// 基于准备好的dom,初始化echarts实例
|
||||||
|
var myChart = echarts.init(document.getElementById('main'));
|
||||||
|
|
||||||
|
// 指定图表的配置项和数据
|
||||||
|
var option = {
|
||||||
|
xAxis: {
|
||||||
|
type: 'value',
|
||||||
|
boundaryGap: [0, 0.01]
|
||||||
|
},
|
||||||
|
yAxis: {
|
||||||
|
type: 'category',
|
||||||
|
data: {{data.category_data|tojson}}
|
||||||
|
},
|
||||||
|
series: [
|
||||||
|
{
|
||||||
|
data: {{data.data|tojson}},
|
||||||
|
type: 'bar',
|
||||||
|
itemStyle: {
|
||||||
|
// 为每个柱子设置随机颜色
|
||||||
|
color: function () {
|
||||||
|
// 生成一个随机颜色
|
||||||
|
return 'rgb(' +
|
||||||
|
Math.round(Math.random() * 255) + ',' +
|
||||||
|
Math.round(Math.random() * 255) + ',' +
|
||||||
|
Math.round(Math.random() * 255) + ')';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
// 使用刚指定的配置项和数据显示图表。
|
||||||
|
myChart.setOption(option);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -6,6 +6,8 @@ from zhenxun.models.group_console import GroupConsole
|
|||||||
from zhenxun.models.group_member_info import GroupInfoUser
|
from zhenxun.models.group_member_info import GroupInfoUser
|
||||||
from zhenxun.models.plugin_info import PluginInfo
|
from zhenxun.models.plugin_info import PluginInfo
|
||||||
from zhenxun.models.statistics import Statistics
|
from zhenxun.models.statistics import Statistics
|
||||||
|
from zhenxun.utils.echart_utils import ChartUtils
|
||||||
|
from zhenxun.utils.echart_utils.models import Barh
|
||||||
from zhenxun.utils.enum import PluginType
|
from zhenxun.utils.enum import PluginType
|
||||||
from zhenxun.utils.image_utils import BuildImage, BuildMat, MatType
|
from zhenxun.utils.image_utils import BuildImage, BuildMat, MatType
|
||||||
|
|
||||||
@ -114,7 +116,6 @@ class StatisticsManage:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def __build_image(cls, data_list: list[tuple[str, int]], title: str):
|
async def __build_image(cls, data_list: list[tuple[str, int]], title: str):
|
||||||
mat = BuildMat(MatType.BARH)
|
|
||||||
module2count = {x[0]: x[1] for x in data_list}
|
module2count = {x[0]: x[1] for x in data_list}
|
||||||
plugin_info = await PluginInfo.filter(
|
plugin_info = await PluginInfo.filter(
|
||||||
module__in=module2count.keys(),
|
module__in=module2count.keys(),
|
||||||
@ -125,7 +126,5 @@ class StatisticsManage:
|
|||||||
for plugin in plugin_info:
|
for plugin in plugin_info:
|
||||||
x_index.append(plugin.name)
|
x_index.append(plugin.name)
|
||||||
data.append(module2count.get(plugin.module, 0))
|
data.append(module2count.get(plugin.module, 0))
|
||||||
mat.x_index = x_index
|
barh = Barh(data=data, category_data=x_index)
|
||||||
mat.data = data
|
return await ChartUtils.barh(barh)
|
||||||
mat.title = title
|
|
||||||
return await mat.build()
|
|
||||||
|
|||||||
24
zhenxun/utils/echart_utils/__init__.py
Normal file
24
zhenxun/utils/echart_utils/__init__.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
from nonebot_plugin_htmlrender import template_to_pic
|
||||||
|
|
||||||
|
from zhenxun.configs.path_config import TEMPLATE_PATH
|
||||||
|
from zhenxun.utils._build_image import BuildImage
|
||||||
|
|
||||||
|
from .models import Barh
|
||||||
|
|
||||||
|
|
||||||
|
class ChartUtils:
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def barh(cls, data: Barh) -> BuildImage:
|
||||||
|
"""横向统计图"""
|
||||||
|
pic = await template_to_pic(
|
||||||
|
template_path=str((TEMPLATE_PATH / "bar_chart").absolute()),
|
||||||
|
template_name="main.html",
|
||||||
|
templates={"data": data},
|
||||||
|
pages={
|
||||||
|
"viewport": {"width": 1000, "height": 500},
|
||||||
|
"base_url": f"file://{TEMPLATE_PATH}",
|
||||||
|
},
|
||||||
|
wait=2,
|
||||||
|
)
|
||||||
|
return BuildImage.open(pic)
|
||||||
9
zhenxun/utils/echart_utils/models.py
Normal file
9
zhenxun/utils/echart_utils/models.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class Barh(BaseModel):
|
||||||
|
|
||||||
|
category_data: list[str]
|
||||||
|
"""坐标轴数据"""
|
||||||
|
data: list[int | float]
|
||||||
|
"""实际数据"""
|
||||||
Loading…
Reference in New Issue
Block a user