d5b4e96 ←260dd9bantonApr 23, 2026 api

create _run.py via macOS

A /docs/undefined +55 -0
A /docs/undefined +55 -0
1"""
2JSON to chart: reads .json data files and generates PNG charts.
3"""
4import json
5import os
6import matplotlib
7matplotlib.use("Agg")
8import matplotlib.pyplot as plt
9
10json_files = sorted(f for f in os.listdir(".") if f.endswith(".json"))
11if not json_files:
12 print("No JSON files found")
13 exit(0)
14
15charts_made = 0
16
17for fname in json_files:
18 with open(fname) as f:
19 doc = json.load(f)
20
21 if "data" not in doc or not isinstance(doc["data"], list):
22 continue
23
24 data = doc["data"]
25 title = doc.get("title", fname)
26
27 # auto-detect x and y columns (first string column = x, first number column = y)
28 if not data:
29 continue
30
31 sample = data[0]
32 x_col = next((k for k, v in sample.items() if isinstance(v, str)), None)
33 y_col = next((k for k, v in sample.items() if isinstance(v, (int, float))), None)
34
35 if not x_col or not y_col:
36 continue
37
38 x_vals = [row[x_col] for row in data]
39 y_vals = [row[y_col] for row in data]
40
41 fig, ax = plt.subplots(figsize=(10, 6))
42 ax.bar(x_vals, y_vals, color="#4a90d9", edgecolor="white", linewidth=0.5)
43 ax.set_title(title, fontsize=16, fontweight="bold", pad=15)
44 ax.set_xlabel(x_col, fontsize=12)
45 ax.set_ylabel(y_col, fontsize=12)
46 ax.spines["top"].set_visible(False)
47 ax.spines["right"].set_visible(False)
48 ax.grid(axis="y", alpha=0.3)
49
50 chart_name = os.path.splitext(fname)[0] + "_chart.png"
51 fig.savefig(chart_name, dpi=150, bbox_inches="tight", facecolor="white")
52 plt.close(fig)
53 charts_made += 1
54
55print(f"Done: generated {charts_made} charts from {len(json_files)} JSON files")