{"spec_id":"bar-stacked-labeled","library":"pygal","language":"python","code":"\"\"\" anyplot.ai\nbar-stacked-labeled: Stacked Bar Chart with Total Labels\nLibrary: pygal 3.1.0 | Python 3.13.13\nQuality: 88/100 | Updated: 2026-05-18\n\"\"\"\n\nimport os\nimport sys\nfrom pathlib import Path\n\n\n# Remove the script's directory from sys.path to avoid conflict with pygal.py filename\nscript_dir = str(Path(__file__).parent)\nsys.path = [p for p in sys.path if p != script_dir]\n\nimport pygal\nfrom pygal.style import Style\n\n\n# Theme tokens (Okabe-Ito palette, theme-adaptive chrome)\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Okabe-Ito palette (first series always #009E73)\nIMPRINT = (\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\")\n\n# Data - Quarterly revenue by product category (in thousands)\ncategories = [\"Q1\", \"Q2\", \"Q3\", \"Q4\"]\nproducts = {\n    \"Electronics\": [180, 95, 140, 220],\n    \"Furniture\": [45, 130, 85, 75],\n    \"Clothing\": [55, 60, 145, 90],\n    \"Accessories\": [32, 45, 38, 115],\n}\n\n# Calculate totals for labels\ntotals = [sum(products[product][i] for product in products) for i in range(len(categories))]\n\n# Custom style for large canvas with theme-adaptive tokens\ncustom_style = Style(\n    background=PAGE_BG,\n    plot_background=PAGE_BG,\n    foreground=INK,\n    foreground_strong=INK,\n    foreground_subtle=INK_MUTED,\n    colors=IMPRINT,\n    title_font_size=28,\n    label_font_size=22,\n    major_label_font_size=18,\n    legend_font_size=16,\n    value_font_size=16,\n    stroke_width=3,\n)\n\n# Create stacked bar chart with proper legend positioning\nchart = pygal.StackedBar(\n    width=4800,\n    height=2700,\n    style=custom_style,\n    title=\"bar-stacked-labeled · Python · pygal · anyplot.ai\",\n    x_title=\"Quarter\",\n    y_title=\"Revenue ($K)\",\n    show_y_guides=True,\n    show_x_guides=False,\n    legend_at_bottom=False,  # Move legend to side to avoid truncation\n    legend_at_bottom_columns=None,\n    print_values=True,\n    print_values_position=\"top\",\n    value_formatter=lambda x: \"\",  # Hide default values\n    margin=80,\n    margin_right=150,  # Extra space for legend on right\n    margin_bottom=200,\n    spacing=80,\n)\n\n# Set x-axis labels\nchart.x_labels = categories\n\n# Add data series with total labels on top\nproduct_list = list(products.items())\nfor idx, (product, values) in enumerate(product_list):\n    is_top = idx == len(product_list) - 1\n    if is_top:\n        # Top series: show total labels above each bar\n        data = [{\"value\": v, \"formatter\": lambda x, i=i: f\"${totals[i]}K\"} for i, v in enumerate(values)]\n    else:\n        # Other series: no labels\n        data = values\n    chart.add(product, data)\n\n# Save as PNG and HTML\nchart.render_to_png(f\"plot-{THEME}.png\")\nchart.render_to_file(f\"plot-{THEME}.html\")\n"}