diff --git a/AI/Text2Sql/Util/SaveToExcel.py b/AI/Text2Sql/Util/SaveToExcel.py index 1e42c8da..31c497ad 100644 --- a/AI/Text2Sql/Util/SaveToExcel.py +++ b/AI/Text2Sql/Util/SaveToExcel.py @@ -1,7 +1,6 @@ -import pandas as pd from openpyxl.styles import Font, PatternFill, Alignment, Border, Side from openpyxl.utils import get_column_letter - +import pandas as pd def save_to_excel(data, filename): """ @@ -51,7 +50,15 @@ def save_to_excel(data, filename): cell.font = data_font cell.alignment = Alignment(vertical='center', wrap_text=True) - # 设置列宽(所有列固定50) - for idx, column in enumerate(worksheet.columns): + # 动态设置列宽 + for idx, column in enumerate(df.columns): + # 获取列的最大长度 + max_length = max( + df[column].astype(str).map(len).max(), # 数据列的最大长度 + len(str(column)) # 列名的长度 + ) + # 计算列宽,确保在 10 到 120 之间 + column_width = min(max(max_length + 2, 10)*2, 120) # 加 2 是为了留出一些空白 + # 设置列宽 column_letter = get_column_letter(idx + 1) - worksheet.column_dimensions[column_letter].width = 50 # 所有列固定50 + worksheet.column_dimensions[column_letter].width = column_width \ No newline at end of file diff --git a/AI/Text2Sql/Util/__pycache__/SaveToExcel.cpython-310.pyc b/AI/Text2Sql/Util/__pycache__/SaveToExcel.cpython-310.pyc index 42bd1064..589d8762 100644 Binary files a/AI/Text2Sql/Util/__pycache__/SaveToExcel.cpython-310.pyc and b/AI/Text2Sql/Util/__pycache__/SaveToExcel.cpython-310.pyc differ diff --git a/AI/Text2Sql/Util/__pycache__/Text2SqlUtil.cpython-310.pyc b/AI/Text2Sql/Util/__pycache__/Text2SqlUtil.cpython-310.pyc index 415e80e1..595b310a 100644 Binary files a/AI/Text2Sql/Util/__pycache__/Text2SqlUtil.cpython-310.pyc and b/AI/Text2Sql/Util/__pycache__/Text2SqlUtil.cpython-310.pyc differ diff --git a/AI/Text2Sql/YunXiao.py b/AI/Text2Sql/YunXiao.py index 01e4c90f..ea3d8533 100644 --- a/AI/Text2Sql/YunXiao.py +++ b/AI/Text2Sql/YunXiao.py @@ -1,100 +1,14 @@ import os -import platform -from Text2Sql.Util.Text2SqlUtil import * + from Text2Sql.Util.PostgreSQLUtil import PostgreSQLUtil from Text2Sql.Util.SaveToExcel import save_to_excel -from pyecharts.charts import Bar -from pyecharts import options as opts -from pyecharts.commons.utils import JsCode -import pandas as pd -import webbrowser - - -def process_data(raw_data): - """ - 处理原始数据: - 1. 过滤无效行政区 - 2. 按上传数量降序排序 - 3. 取前10名 - """ - df = pd.DataFrame(raw_data) - df = df[df['行政区划名称'].notna() & (df['行政区划名称'] != '')] - df = df.sort_values('上传课程数量', ascending=False).head(10) - return df - - -def create_top10_chart(data): - # 准备数据 - schools = data['学校名称'].tolist() - counts = data['上传课程数量'].tolist() - districts = data['行政区划名称'].tolist() - - # 生成颜色渐变(从深到浅) - colors = [f"rgb({75 + i * 15}, {115 - i * 10}, {220 - i * 20})" for i in range(10)] - - # 创建图表 - bar = Bar(init_opts=opts.InitOpts(width='1200px', height='600px')) - bar.add_xaxis(schools) - bar.add_yaxis( - series_name="上传数量", - y_axis=counts, - itemstyle_opts=opts.ItemStyleOpts(color=JsCode( - "function(params){" - f" return {colors}[params.dataIndex];" - "}" - )), - label_opts=opts.LabelOpts( - position="right", - formatter=JsCode( - "function(params){" - " return params.value + ' (' + params.name.split('').join('\\n') + ')';" - "}" - ) - ) - ) - - # 全局配置 - bar.set_global_opts( - title_opts=opts.TitleOpts( - title="上传资源数量TOP10学校排名", - subtitle="数据来源:课程资源管理系统" - ), - tooltip_opts=opts.TooltipOpts( - formatter=JsCode( - """function(params){ - return params.name + '
' - + '所属行政区:' + %s[params.dataIndex] + '
' - + '上传数量:' + params.value + '' - }""" % districts - ) - ), - xaxis_opts=opts.AxisOpts( - axislabel_opts=opts.LabelOpts( - rotate=30, - formatter=JsCode( - "function(value){" - " return value.length > 6 ? value.substring(0,6)+'...' : value;" - "}" - ) - ) - ), - yaxis_opts=opts.AxisOpts(name="上传数量(件)"), - datazoom_opts=[opts.DataZoomOpts(type_="inside")], - visualmap_opts=opts.VisualMapOpts( - min_=min(counts), - max_=max(counts), - orient="horizontal", - pos_left="center", - range_color=["#91CC75", "#5470C6"] - ) - ) - - # 反转Y轴使降序排列 - bar.reversal_axis() - - return bar - - +from Text2Sql.Util.Text2SqlUtil import * +''' +经验: +1、尽量使用宽表,少用关联,越少越好 +2、应该有一些固定的组合用法预置出来,给出范例,让用户可以简单修改后就能使用 +3、应该有类似于 保存为用例,查询历史等功能,让用户方便利旧。 +''' if __name__ == "__main__": vn = DeepSeekVanna()