ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Python | Dashboard制作

Python | Dashboard制作 运行环境jupyter notebook (python 3.12.7) Pyecharts1.安装pyecharts!pip install pyecharts验证安装是否成功from pyecharts import __version__ print(Pyecharts版本:, __version__) # 应显示1.x以上版本2.运行基础版代码生成深/浅色双模式HTMLfrom pyecharts.charts import Line, Bar, Pie, Scatter, Page from pyecharts import options as opts import random # 生成示例数据 categories [Mon, Tue, Wed, Thu, Fri, Sat, Sun] data1 [random.randint(20, 100) for _ in range(7)] data2 [random.randint(30, 150) for _ in range(7)] pie_data [(A, 45), (B, 30), (C, 25)] scatter_data [(i, random.randint(10, 50)) for i in range(20)] # 创建 Page 实例 page Page(layoutPage.SimplePageLayout) # 折线图添加固定ID line ( Line(init_optsopts.InitOpts(chart_idchart_line)) .add_xaxis(categories) .add_yaxis(Series 1, data1, is_smoothTrue) .add_yaxis(Series 2, data2, is_smoothTrue) .set_global_opts( title_optsopts.TitleOpts(titleWeekly Trend), toolbox_optsopts.ToolboxOpts(), tooltip_optsopts.TooltipOpts(triggeraxis) ) ) # 柱状图添加固定ID bar ( Bar(init_optsopts.InitOpts(chart_idchart_bar)) .add_xaxis(categories) .add_yaxis(Sales, data1, color#5793f3) .add_yaxis(Profit, data2, color#d14a61) .set_global_opts( title_optsopts.TitleOpts(titleSales vs Profit), datazoom_optsopts.DataZoomOpts(), toolbox_optsopts.ToolboxOpts() ) ) # 饼图添加固定ID pie ( Pie(init_optsopts.InitOpts(chart_idchart_pie)) .add(, pie_data, radius[30%, 55%]) .set_global_opts( title_optsopts.TitleOpts(titleMarket Share), legend_optsopts.LegendOpts(orientvertical, pos_top15%, pos_left2%) ) .set_series_opts(label_optsopts.LabelOpts(formatter{b}: {d}%)) ) # 散点图添加固定ID scatter ( Scatter(init_optsopts.InitOpts(chart_idchart_scatter)) .add_xaxis([x[0] for x in scatter_data]) .add_yaxis(Value, [y[1] for y in scatter_data]) .set_global_opts( title_optsopts.TitleOpts(titleScatter Distribution), toolbox_optsopts.ToolboxOpts(), visualmap_optsopts.VisualMapOpts(max_50) ) ) # 将图表添加到 Page page.add(line, bar, pie, scatter) # 生成初始 HTML page.render(dashboard.html) # ------------- 手动添加交互功能 -------------- def insert_theme_switcher(): 向生成的 HTML 中插入主题切换组件 with open(dashboard.html, r, encodingutf-8) as f: html f.read() insert_code f !-- 主题切换按钮 -- div styleposition: fixed; top: 20px; right: 20px; z-index: 9999; background: white; padding: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); button onclickchangeTheme(light)☀️ 明亮模式/button button onclickchangeTheme(dark) 暗黑模式/button /div script // 存储图表配置 var chartConfigs {{ chart_line: {line.dump_options()}, chart_bar: {bar.dump_options()}, chart_pie: {pie.dump_options()}, chart_scatter: {scatter.dump_options()} }}; // 主题切换函数 function changeTheme(theme) {{ [chart_line, chart_bar, chart_pie, chart_scatter].forEach(chartId {{ let chart echarts.getInstanceByDom(document.getElementById(chartId)); if (chart) chart.dispose(); let newChart echarts.init( document.getElementById(chartId), theme, {{ renderer: canvas }} ); newChart.setOption(chartConfigs[chartId]); }}); }} /script # 在 /body 前插入代码 new_html html.replace(/body, insert_code \n/body) with open(dashboard.html, w, encodingutf-8) as f: f.write(new_html) insert_theme_switcher() print(仪表盘生成成功打开 dashboard.html 查看效果)HTML截图
返回列表