qinglong/repo/dockerbot/jbot/bot/chart.py
2021-06-09 18:47:34 +08:00

135 lines
4.6 KiB
Python

from telethon import events
from .. import jdbot, chat_id, _LogDir, logger
from ..bot.quickchart import QuickChart
from .beandata import get_bean_data
_botimg = _LogDir + '/bot/bean.jpeg'
@jdbot.on(events.NewMessage(chats=chat_id, pattern=r'^/chart'))
async def mybean(event):
try:
await jdbot.send_message(chat_id, '正在查询,请稍后')
if len(event.raw_text.split(' ')) > 1:
text = event.raw_text.replace('/chart ', '')
else:
text = None
if text and int(text):
beanin, beanout, beanstotal, date = get_bean_data(int(text))
if not beanout:
await jdbot.send_message(chat_id, 'something wrong,I\'m sorry\n'+str(beanin))
else:
creat_chart(date, '账号'+str(text),
beanin, beanout, beanstotal[1:])
await jdbot.send_message(chat_id, f'您的账号{text}收支情况', file=_botimg)
else:
await jdbot.send_message(chat_id, '请正确使用命令\n/chart n n为第n个账号')
except Exception as e:
await jdbot.send_message(chat_id, 'something wrong,I\'m sorry\n'+str(e))
logger.error('something wrong,I\'m sorry'+str(e))
def creat_chart(xdata, title, bardata, bardata2, linedate):
qc = QuickChart()
qc.background_color = '#fff'
qc.width = "1000"
qc.height = "600"
qc.config = {
"type": "bar",
"data": {
"labels": xdata,
"datasets": [
{
"label": "IN",
"backgroundColor": [
"rgb(255, 99, 132)",
"rgb(255, 159, 64)",
"rgb(255, 205, 86)",
"rgb(75, 192, 192)",
"rgb(54, 162, 235)",
"rgb(153, 102, 255)",
"rgb(255, 99, 132)"
],
"yAxisID": "y1",
"data": bardata
},
{
"label": "OUT",
"backgroundColor": [
"rgb(255, 99, 132)",
"rgb(255, 159, 64)",
"rgb(255, 205, 86)",
"rgb(75, 192, 192)",
"rgb(54, 162, 235)",
"rgb(153, 102, 255)",
"rgb(255, 99, 132)"
],
"yAxisID": "y1",
"data": bardata2
},
{
"label": "TOTAL",
"type": "line",
"fill": False,
"backgroundColor": "rgb(201, 203, 207)",
"yAxisID": "y2",
"data": linedate
}
]
},
"options": {
"plugins": {
"datalabels": {
"anchor": 'end',
"align": -100,
"color": '#666',
"font": {
"size": 20,
}
},
},
"legend": {
"labels": {
"fontSize": 20,
"fontStyle": 'bold',
}
},
"title": {
"display": True,
"text": title + " 收支情况",
"fontSize": 24,
},
"scales": {
"xAxes": [{
"ticks": {
"fontSize": 24,
}
}],
"yAxes": [
{
"id": "y1",
"type": "linear",
"display": False,
"position": "left",
"ticks": {
"max": int(int(max([max(bardata), max(bardata2)])+100)*2)
},
"scaleLabel": {
"fontSize": 20,
"fontStyle": 'bold',
}
},
{
"id": "y2",
"type": "linear",
"display": False,
"ticks": {
"min": int(min(linedate)*2-(max(linedate))-100),
"max": int(int(max(linedate)))
},
"position": "right"
}
]
}
}
}
qc.to_file(_botimg)