本文目录导读:

- 方法一:使用 Python + Matplotlib/Seaborn(静态看板)
- 方法二:使用 Python + Plotly(交互式看板)
- 方法三:使用 Bash + CSV + 文本报告(极简方案)
- 方法四:使用 HTML + JavaScript 单页看板(前端方案)
- 推荐选择
使用 Python + Matplotlib/Seaborn(静态看板)
适合生成简单的静态图表,如折线图、柱状图、饼图。
import matplotlib.pyplot as plt
import numpy as np
# 生成示例数据
months = ['一月', '二月', '三月', '四月', '五月']
sales = [120, 150, 180, 140, 200]
plt.figure(figsize=(10, 5))
# 1. 主折线图
plt.subplot(1, 2, 1)
plt.plot(months, sales, marker='o', color='#2E86AB', linewidth=2)'月度销售趋势')
plt.xlabel('月份')
plt.ylabel('销售额(万元)')
plt.grid(alpha=0.3)
# 2. 辅助柱状图
plt.subplot(1, 2, 2)
colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7']
plt.bar(months, sales, color=colors)'各月销售对比')
plt.xlabel('月份')
plt.ylabel('销售额(万元)')
plt.tight_layout()
plt.savefig('simple_dashboard.png', dpi=150)
plt.show()
优点:简单、快速、无需额外依赖
缺点:静态、交互性差
使用 Python + Plotly(交互式看板)
Plotly 可以生成 HTML 格式的交互式图表,支持悬浮、缩放。
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
# 模拟数据
df = pd.DataFrame({
'月份': ['一月', '二月', '三月', '四月', '五月', '六月'],
'销售额': [120, 150, 180, 140, 200, 170],
'成本': [80, 100, 120, 90, 130, 110],
'利润': [40, 50, 60, 50, 70, 60]
})
# 创建子图看板
fig = make_subplots(
rows=2, cols=2,
subplot_titles=('销售额趋势', '成本与利润', '利润饼图', '利润分布'),
specs=[[{'type': 'scatter'}, {'type': 'bar'}],
[{'type': 'pie'}, {'type': 'box'}]]
)
# 添加折线图
fig.add_trace(go.Scatter(x=df['月份'], y=df['销售额'],
mode='lines+markers', name='销售额',
line=dict(color='#2E86AB', width=2)),
row=1, col=1)
# 添加柱状图
fig.add_trace(go.Bar(x=df['月份'], y=df['成本'],
name='成本', marker_color='#FF6B6B'),
row=1, col=2)
fig.add_trace(go.Bar(x=df['月份'], y=df['利润'],
name='利润', marker_color='#4ECDC4'),
row=1, col=2)
# 添加饼图
fig.add_trace(go.Pie(labels=df['月份'], values=df['利润'],
name='利润分布', hole=0.3),
row=2, col=1)
# 添加箱线图
fig.add_trace(go.Box(y=df['利润'], name='利润',
marker_color='#96CEB4'),
row=2, col=2)
fig.update_layout(height=700, width=1000,
title_text="简单销售看板",
showlegend=True)
fig.write_html("interactive_dashboard.html")
print("看板已生成:interactive_dashboard.html")
优点:交互性强、支持多种图表类型
缺点:文件较大、需浏览器打开
使用 Bash + CSV + 文本报告(极简方案)
适合纯文本终端环境,不依赖图形界面。
#!/bin/bash
# 模拟数据
cat > data.csv << EOF
日期,销售额,用户数
2024-01-01,1200,150
2024-01-02,1350,180
2024-01-03,1100,140
2024-01-04,1500,200
2024-01-05,1400,190
EOF
echo "========== 销售看板 =========="
echo "日期 销售额 用户数"
echo "-----------------------------"
# 计算汇总
total_sales=0
total_users=0
while IFS=, read -r date sales users; do
if [[ "$date" != "日期" ]]; then
printf "%-10s %-8s %-6s\n" "$date" "$sales" "$users"
((total_sales += sales))
((total_users += users))
fi
done < data.csv
echo "-----------------------------"
echo "总计: $total_sales $total_users"
echo "平均: $((total_sales/5)) $((total_users/5))"
echo "=============================="
优点:零依赖、极快
缺点:仅文本、无图表
使用 HTML + JavaScript 单页看板(前端方案)
将数据硬编码在 HTML 中,使用 Chart.js 或 ECharts。
<!DOCTYPE html>
<html>
<head>简单看板</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
.dashboard {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
max-width: 800px;
margin: 20px auto;
padding: 20px;
}
.card {
background: #f5f5f5;
border-radius: 8px;
padding: 15px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
h1 { text-align: center; color: #333; }
</style>
</head>
<body>
<h1>📊 月度看板</h1>
<div class="dashboard">
<div class="card">
<h3>销售额趋势</h3>
<canvas id="lineChart"></canvas>
</div>
<div class="card">
<h3>各部门占比</h3>
<canvas id="pieChart"></canvas>
</div>
</div>
<script>
const ctx1 = document.getElementById('lineChart').getContext('2d');
new Chart(ctx1, {
type: 'line',
data: {
labels: ['一月','二月','三月','四月','五月'],
datasets: [{
label: '销售额(万元)',
data: [120, 150, 180, 140, 200],
borderColor: '#2E86AB',
tension: 0.3
}]
}
});
const ctx2 = document.getElementById('pieChart').getContext('2d');
new Chart(ctx2, {
type: 'pie',
data: {
labels: ['销售部','技术部','市场部','运营部'],
datasets: [{
data: [35, 25, 20, 20],
backgroundColor: ['#FF6B6B','#4ECDC4','#45B7D1','#96CEB4']
}]
}
});
</script>
</body>
</html>
优点:美观、轻松部署到任意静态服务器
缺点:需手动更新数据
推荐选择
| 场景 | 推荐方法 |
|---|---|
| 快速生成报表图片 | 方法一(Matplotlib) |
| 需要交互式网页 | 方法二(Plotly) |
| 纯终端环境 / SSH | 方法三(Bash) |
| 前端开发 / 静态部署 | 方法四(HTML+JS) |
如需自动化数据更新,可在脚本中添加数据源(数据库、API、CSV)读取逻辑。