|
| 1 | +""" |
| 2 | +Chart 7: Age Group Distribution - Interactions |
| 3 | +Type of Chart: Grouped vertical bar chart |
| 4 | +Data Points: |
| 5 | + - demographic.csv: demo_age_5_17, demo_age_17_ |
| 6 | + - biometric.csv: bio_age_5_17, bio_age_17_ |
| 7 | + - 4 bars: 5-17 (Demo), 18+ (Demo), 5-17 (Bio), 18+ (Bio) |
| 8 | +""" |
| 9 | +import matplotlib.pyplot as plt |
| 10 | +import numpy as np |
| 11 | + |
| 12 | +from src.charts import register_chart |
| 13 | +from src.charts.base import BaseChart |
| 14 | +from src.processors import AgeGroupAggregator |
| 15 | +import config |
| 16 | + |
| 17 | + |
| 18 | +@register_chart |
| 19 | +class Chart07AgeGroupInteractions(BaseChart): |
| 20 | + |
| 21 | + @property |
| 22 | + def chart_id(self) -> str: |
| 23 | + return "07" |
| 24 | + |
| 25 | + @property |
| 26 | + def title(self) -> str: |
| 27 | + return "Age Group Distribution - Interactions" |
| 28 | + |
| 29 | + def generate(self) -> plt.Figure: |
| 30 | + data = self.data_loader.get_all_data() |
| 31 | + processor = AgeGroupAggregator() |
| 32 | + age_data = processor.process_interactions(data) |
| 33 | + |
| 34 | + fig, ax = plt.subplots(figsize=(12, 8)) |
| 35 | + |
| 36 | + # Set up bar positions |
| 37 | + x = np.arange(len(age_data)) |
| 38 | + width = 0.6 |
| 39 | + |
| 40 | + # Create bars with different colors for demo vs bio |
| 41 | + colors = [ |
| 42 | + config.COLORS["demographic"], # 5-17 (Demo) |
| 43 | + config.COLORS["demographic"], # 18+ (Demo) |
| 44 | + config.COLORS["biometric"], # 5-17 (Bio) |
| 45 | + config.COLORS["biometric"], # 18+ (Bio) |
| 46 | + ] |
| 47 | + |
| 48 | + bars = ax.bar( |
| 49 | + x, |
| 50 | + age_data["total"], |
| 51 | + width, |
| 52 | + color=colors, |
| 53 | + edgecolor="white", |
| 54 | + linewidth=1.5, |
| 55 | + alpha=0.8 |
| 56 | + ) |
| 57 | + |
| 58 | + # Add value labels on top of bars |
| 59 | + for i, (bar, row) in enumerate(zip(bars, age_data.itertuples())): |
| 60 | + height = bar.get_height() |
| 61 | + # Format in millions |
| 62 | + value_text = f"{row.total/1e6:.1f}M" |
| 63 | + percentage_text = f"({row.percentage:.1f}%)" |
| 64 | + |
| 65 | + ax.text( |
| 66 | + bar.get_x() + bar.get_width() / 2, |
| 67 | + height, |
| 68 | + f"{value_text}\n{percentage_text}", |
| 69 | + ha="center", |
| 70 | + va="bottom", |
| 71 | + fontsize=10, |
| 72 | + fontweight="bold" |
| 73 | + ) |
| 74 | + |
| 75 | + self._apply_common_style(ax) |
| 76 | + ax.set_xlabel("Age Group by Service Type", fontsize=12) |
| 77 | + ax.set_ylabel("Total Interactions", fontsize=12) |
| 78 | + ax.set_xticks(x) |
| 79 | + ax.set_xticklabels(age_data["category"], fontsize=10) |
| 80 | + ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda x, p: f"{x/1e6:.0f}M")) |
| 81 | + ax.grid(axis="y", alpha=0.3, linestyle="--") |
| 82 | + |
| 83 | + # Add legend |
| 84 | + from matplotlib.patches import Patch |
| 85 | + legend_elements = [ |
| 86 | + Patch(facecolor=config.COLORS["demographic"], label="Demographic", alpha=0.8), |
| 87 | + Patch(facecolor=config.COLORS["biometric"], label="Biometric", alpha=0.8) |
| 88 | + ] |
| 89 | + ax.legend(handles=legend_elements, loc="upper right", fontsize=10) |
| 90 | + |
| 91 | + fig.tight_layout() |
| 92 | + |
| 93 | + return fig |
0 commit comments