Skip to content

Commit e88f1ee

Browse files
committed
feat: add top 15 states with the most demographics interactions
1 parent 834a53a commit e88f1ee

13 files changed

Lines changed: 121 additions & 1 deletion
3.11 KB
Loading
90.8 KB
Loading
243 Bytes
Binary file not shown.

src/charts/base.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ def save(self, fig: Optional[plt.Figure] = None) -> Path:
4545
if fig is None:
4646
fig = self.generate()
4747

48+
fig.text(
49+
0.99, 0.01,
50+
"github.com/BMOit",
51+
transform=fig.transFigure,
52+
fontsize=9,
53+
color="gray",
54+
alpha=0.7,
55+
ha="right",
56+
va="bottom"
57+
)
58+
4859
config.CHARTS_OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
4960
fig.savefig(
5061
self.output_path,
-1 Bytes
Binary file not shown.

src/charts/top_states/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Top states charts."""
250 Bytes
Binary file not shown.
3.29 KB
Binary file not shown.

src/charts/top_states/chart_02.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Chart 2: Top 15 States - Demographic Interactions
3+
Type of Chart: Horizontal bar chart
4+
Data Points:
5+
- demographic.csv: state, demo_age_5_17, demo_age_17_
6+
- Calculation: SUM(demo_age_5_17 + demo_age_17_) GROUP BY state, TOP 15
7+
"""
8+
import matplotlib.pyplot as plt
9+
10+
from src.charts import register_chart
11+
from src.charts.base import BaseChart
12+
from src.processors import StateAggregator
13+
import config
14+
15+
16+
@register_chart
17+
class Chart02TopStatesDemographic(BaseChart):
18+
19+
@property
20+
def chart_id(self) -> str:
21+
return "02"
22+
23+
@property
24+
def title(self) -> str:
25+
return "Top 15 States - Demographic Interactions"
26+
27+
def generate(self) -> plt.Figure:
28+
data = self.data_loader.get_all_data()
29+
processor = StateAggregator()
30+
state_data = processor.process(data, dataset="demographic", top_n=15)
31+
32+
fig, ax = plt.subplots(figsize=(12, 8))
33+
34+
bars = ax.barh(
35+
state_data["state"],
36+
state_data["total"],
37+
color=config.COLORS["demographic"],
38+
edgecolor="white",
39+
linewidth=0.5
40+
)
41+
42+
for bar, value in zip(bars, state_data["total"]):
43+
ax.text(
44+
value + state_data["total"].max() * 0.01,
45+
bar.get_y() + bar.get_height() / 2,
46+
f"{value:,.0f}",
47+
va="center",
48+
fontsize=9
49+
)
50+
51+
self._apply_common_style(ax)
52+
ax.set_xlabel("Total Demographic Interactions", fontsize=12)
53+
ax.set_ylabel("State", fontsize=12)
54+
ax.xaxis.set_major_formatter(plt.FuncFormatter(lambda x, p: f"{x/1e6:.1f}M"))
55+
ax.set_xlim(0, state_data["total"].max() * 1.15)
56+
fig.tight_layout()
57+
58+
return fig

src/processors/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Data processors for transforming raw data into chart-ready formats."""
22
from .base import BaseProcessor
33
from .daily_aggregator import DailyAggregator
4+
from .state_aggregator import StateAggregator
45

5-
__all__ = ["BaseProcessor", "DailyAggregator"]
6+
__all__ = ["BaseProcessor", "DailyAggregator", "StateAggregator"]

0 commit comments

Comments
 (0)