Skip to content
import matplotlib.pyplot as plt
import numpy as np
import plotly.graph_objects as go

# Define the goals and actual results
goals = [
    {"goal": "Reading", "low": 12, "target": 15, "stretch": 20, "actual": 23},
    {"goal": "Writing", "low": 3, "target": 6, "stretch": 12, "actual": 5},
    {"goal": "Language", "low": 25000, "target": 50000, "stretch": 75000, "actual": 46323},
    {"goal": "Coding", "low": 50000, "target": 75000, "stretch": 100000, "actual": 44177}
]

# Create subplots for each goal in separate columns
fig, axes = plt.subplots(1, len(goals), figsize=(24, 6), sharey=False)

# Plot each goal in a separate subplot
for i, goal in enumerate(goals):
    ax = axes[i]
    
    # Plot the low to stretch range as a filled area
    ax.fill_between([0, 1], goal["low"], goal["stretch"], color='gray', alpha=0.3)
    
    # Plot the low to stretch range as a line
    ax.plot([0.5, 0.5], [goal["low"], goal["stretch"]], color='black', linestyle='--', zorder=4)
    
    # Plot the actual, low, target, and stretch points
    ax.scatter(0.5, goal["actual"], color='red', zorder=5, label='Actual')
    ax.scatter(0.5, goal["low"], color='blue', zorder=5, label='Low')
    ax.scatter(0.5, goal["target"], color='green', zorder=5, label='Target')
    ax.scatter(0.5, goal["stretch"], color='orange', zorder=5, label='Stretch')
    
    # Set the y-axis label
    ax.set_ylabel('Values')
    
    # Set the title for each subplot
    ax.set_title(f'KPI Plot for 2024 Goal: {goal["goal"]}')
    
    # Remove x-axis ticks
    ax.set_xticks([])
    
    # Add a legend
    ax.legend(loc='upper left')

# Adjust layout
plt.tight_layout()

# Show the plot
plt.show()


def create_kpi_gauge(goal, low, target, stretch, actual):
    fig = go.Figure(go.Indicator(
        mode = "gauge+number+delta",
        value = actual,
        title = {'text': goal},
        delta = {'reference': target},
        gauge = {
            'axis': {
                'range': [None, stretch * 1.2],  # Extend range to accommodate dark green
                'tickvals': [low, target, stretch],
                'ticktext': [f'Low ({low})', f'Target ({target})', f'Stretch ({stretch})']
            },
            'steps' : [
                {'range': [0, low], 'color': "red"},
                {'range': [low, target], 'color': "yellow"},
                {'range': [target, stretch], 'color': "lightgreen"},
                {'range': [stretch, stretch * 1.2], 'color': "darkgreen"}],
            'threshold' : {
                'line': {'color': "black", 'width': 4},
                'thickness': 0.75,
                'value': target},
            'bar': {'color': "black", 'thickness': 0.2}}))
    return fig
# Generate KPI gauge charts for each goal
for goal in goals:
    fig = create_kpi_gauge(goal['goal'], goal['low'], goal['target'], goal['stretch'], goal['actual'])
    fig.show()
import os

# Create a directory to save the images if it doesn't exist
output_dir = 'kpi_gauge_charts'
os.makedirs(output_dir, exist_ok=True)

# Generate and save KPI gauge charts for each goal
for goal in goals:
    fig = create_kpi_gauge(goal['goal'], goal['low'], goal['target'], goal['stretch'], goal['actual'])
    file_name = f"{output_dir}/{goal['goal'].replace(' ', '_').lower()}.png"
    fig.write_image(file_name)