68 lines
1.5 KiB
Python
68 lines
1.5 KiB
Python
import matplotlib
|
|
matplotlib.use("Agg")
|
|
|
|
from flask import Blueprint, render_template, session, redirect, url_for
|
|
import matplotlib.pyplot as plt
|
|
import io
|
|
import base64
|
|
|
|
dashboard_bp = Blueprint("dashboard", __name__, url_prefix="/dashboard")
|
|
|
|
|
|
def plot_to_base64():
|
|
img = io.BytesIO()
|
|
plt.savefig(img, format="png", bbox_inches="tight")
|
|
plt.close()
|
|
img.seek(0)
|
|
return base64.b64encode(img.getvalue()).decode()
|
|
|
|
|
|
def bar_chart():
|
|
categories = ["Trench", "Manhole", "Pipe Laying", "Restoration"]
|
|
values = [120, 80, 150, 60]
|
|
|
|
plt.figure()
|
|
plt.bar(categories, values)
|
|
plt.title("Work Category Report")
|
|
plt.xlabel("Category")
|
|
plt.ylabel("Count")
|
|
|
|
return plot_to_base64()
|
|
|
|
|
|
def pie_chart():
|
|
labels = ["Completed", "In Progress", "Pending"]
|
|
sizes = [65, 20, 15]
|
|
|
|
plt.figure()
|
|
plt.pie(sizes, labels=labels, autopct="%1.1f%%", startangle=140)
|
|
plt.title("Project Status")
|
|
|
|
return plot_to_base64()
|
|
|
|
|
|
def histogram_chart():
|
|
daily_work = [5, 10, 15, 20, 20, 25, 30, 35, 40, 45, 50]
|
|
|
|
plt.figure()
|
|
plt.hist(daily_work, bins=5)
|
|
plt.title("Daily Work Distribution")
|
|
plt.xlabel("Work Units")
|
|
plt.ylabel("Frequency")
|
|
|
|
return plot_to_base64()
|
|
|
|
|
|
@dashboard_bp.route("/")
|
|
def dashboard():
|
|
if not session.get("user_id"):
|
|
return redirect(url_for("auth.login"))
|
|
|
|
return render_template(
|
|
"dashboard.html",
|
|
title="Dashboard",
|
|
bar_chart=bar_chart(),
|
|
pie_chart=pie_chart(),
|
|
histogram=histogram_chart()
|
|
)
|