{"spec_id":"timeline-basic","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\ntimeline-basic: Event Timeline\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-11\n\"\"\"\n\nimport os\nimport time\nfrom pathlib import Path\n\nimport pandas as pd\nfrom bokeh.io import output_file, save\nfrom bokeh.models import ColumnDataSource, Label\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\n\n\n# Save files in the directory where this script is located\nSCRIPT_DIR = Path(__file__).parent\nos.chdir(SCRIPT_DIR)\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# Okabe-Ito palette (first series always #009E73)\nIMPRINT = [\n    \"#009E73\",  # brand green\n    \"#C475FD\",  # vermillion\n    \"#4467A3\",  # blue\n    \"#BD8233\",  # reddish purple\n    \"#AE3030\",  # orange\n]\n\n# Data - Software project milestones\nevents = [\n    (\"2024-01-15\", \"Project Kickoff\", \"Planning\"),\n    (\"2024-02-01\", \"Requirements Complete\", \"Planning\"),\n    (\"2024-03-10\", \"Design Review\", \"Design\"),\n    (\"2024-04-20\", \"Prototype Ready\", \"Development\"),\n    (\"2024-05-15\", \"Alpha Release\", \"Development\"),\n    (\"2024-06-30\", \"Beta Testing\", \"Testing\"),\n    (\"2024-07-25\", \"Bug Fix Sprint\", \"Testing\"),\n    (\"2024-08-15\", \"Performance Audit\", \"Testing\"),\n    (\"2024-09-10\", \"Security Review\", \"Release\"),\n    (\"2024-10-01\", \"v1.0 Launch\", \"Release\"),\n]\n\ndf = pd.DataFrame(events, columns=[\"date\", \"event\", \"category\"])\ndf[\"date\"] = pd.to_datetime(df[\"date\"])\n\n# Assign alternating y positions for label readability (above/below axis)\ndf[\"y_pos\"] = [0.6 if i % 2 == 0 else -0.6 for i in range(len(df))]\n\n# Map categories to Okabe-Ito colors\ncategories = df[\"category\"].unique().tolist()\ncolor_map = {cat: IMPRINT[i % len(IMPRINT)] for i, cat in enumerate(categories)}\ndf[\"color\"] = df[\"category\"].map(color_map)\n\n# Create figure\np = figure(\n    width=4800,\n    height=2700,\n    title=\"timeline-basic · bokeh · anyplot.ai\",\n    x_axis_type=\"datetime\",\n    y_range=(-1.5, 1.5),\n    tools=\"\",\n    toolbar_location=None,\n)\n\n# Draw the central timeline axis (horizontal line)\np.line(\n    x=[df[\"date\"].min() - pd.Timedelta(days=10), df[\"date\"].max() + pd.Timedelta(days=10)],\n    y=[0, 0],\n    line_width=6,\n    line_color=INK_SOFT,\n    line_alpha=0.6,\n)\n\n# Draw vertical connector lines for each event\nfor _, row in df.iterrows():\n    p.line(x=[row[\"date\"], row[\"date\"]], y=[0, row[\"y_pos\"]], line_width=3, line_color=row[\"color\"], line_alpha=0.8)\n\n# Plot event markers with category colors\nfor cat in categories:\n    cat_df = df[df[\"category\"] == cat]\n    cat_source = ColumnDataSource(cat_df)\n    p.scatter(\n        x=\"date\",\n        y=\"y_pos\",\n        source=cat_source,\n        size=35,\n        color=color_map[cat],\n        alpha=0.9,\n        marker=\"circle\",\n        legend_label=cat,\n        line_color=PAGE_BG,\n        line_width=3,\n    )\n\n# Add event labels with proper positioning\nfor _, row in df.iterrows():\n    y_offset = 80 if row[\"y_pos\"] > 0 else -80\n    baseline = \"bottom\" if row[\"y_pos\"] > 0 else \"top\"\n    label = Label(\n        x=row[\"date\"],\n        y=row[\"y_pos\"],\n        text=row[\"event\"],\n        text_font_size=\"20pt\",\n        text_color=INK,\n        text_align=\"center\",\n        text_baseline=baseline,\n        y_offset=y_offset,\n    )\n    p.add_layout(label)\n\n# Style the plot\np.title.text_font_size = \"28pt\"\np.title.text_color = INK\np.title.align = \"center\"\n\np.xaxis.axis_label = \"Date\"\np.xaxis.axis_label_text_font_size = \"22pt\"\np.xaxis.axis_label_text_color = INK\np.xaxis.major_label_text_font_size = \"18pt\"\np.xaxis.major_label_text_color = INK_SOFT\np.xaxis.major_label_orientation = 0.4\np.xaxis.axis_line_color = INK_SOFT\np.xaxis.axis_line_width = 2\np.xaxis.major_tick_line_color = INK_SOFT\np.xaxis.major_tick_line_width = 2\np.xaxis.minor_tick_line_width = 1\n\np.yaxis.visible = False\np.ygrid.visible = False\np.xgrid.grid_line_color = INK\np.xgrid.grid_line_alpha = 0.10\n\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\n\n# Configure legend\np.legend.location = \"top_left\"\np.legend.title = \"Phase\"\np.legend.title_text_font_size = \"22pt\"\np.legend.title_text_color = INK\np.legend.label_text_font_size = \"18pt\"\np.legend.label_text_color = INK_SOFT\np.legend.glyph_height = 30\np.legend.glyph_width = 30\np.legend.border_line_color = INK_SOFT\np.legend.background_fill_color = ELEVATED_BG\np.legend.background_fill_alpha = 0.95\np.legend.padding = 15\np.legend.spacing = 10\n\n# Save HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with headless Chrome via Selenium\nW, H = 4800, 2700\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={W},{H}\",\n    \"--hide-scrollbars\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(W, H)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}