{"spec_id":"bar-spine","library":"highcharts","language":"javascript","code":"// anyplot.ai\n// bar-spine: Spine Plot for Two-Variable Proportions\n// Library: highcharts 13.1.1 | JavaScript 22.23.4\n// Quality: 86/100 | Updated: 2026-11-08\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\n// Retained (good) keeps the brand-green first slot and Churned (bad) the\n// semantic-red anchor; any other status takes the Imprint colour of its position.\nconst STATUS_COLORS = { Retained: t.palette[0], Churned: t.palette[4] };\nconst MIN_LABEL_SHARE = 3; // segments below this share (%) stay unlabelled\n\n// Columns: set these to your data's column names\nconst TIER = \"Subscription tier\"; // role: x_category\nconst STATUS = \"Customer status\"; // role: fill_category\nconst CUSTOMERS = \"Customer count\"; // role: count\n\n// Data: replace this block with your own records, one object per row\nconst data = [\n  { [TIER]: \"Free\", [STATUS]: \"Retained\", [CUSTOMERS]: 2800 },\n  { [TIER]: \"Free\", [STATUS]: \"Churned\", [CUSTOMERS]: 1200 },\n  { [TIER]: \"Basic\", [STATUS]: \"Retained\", [CUSTOMERS]: 2000 },\n  { [TIER]: \"Basic\", [STATUS]: \"Churned\", [CUSTOMERS]: 500 },\n  { [TIER]: \"Pro\", [STATUS]: \"Retained\", [CUSTOMERS]: 1080 },\n  { [TIER]: \"Pro\", [STATUS]: \"Churned\", [CUSTOMERS]: 120 },\n  { [TIER]: \"Enterprise\", [STATUS]: \"Retained\", [CUSTOMERS]: 288 },\n  { [TIER]: \"Enterprise\", [STATUS]: \"Churned\", [CUSTOMERS]: 12 },\n];\n\n// Plot: reads only data and the constants above\n// Highcharts' core bundle has no variable-width column series (that lives in\n// the variwide module, which isn't loaded). The spine plot is a percent-\n// stacked, fully opaque area chart on a linear x-axis instead: each tier spans\n// its cumulative customer count, with one point at each end holding the same\n// count, so every segment is flat across the tier and steps at the boundary.\n// A tiny epsilon keeps boundary x-values strictly increasing (duplicates\n// would double-count in Highcharts' per-x stacking sum) while staying far\n// below one screen pixel.\nconst tiers = [...new Set(data.map((d) => d[TIER]))];\nconst statuses = [...new Set(data.map((d) => d[STATUS]))];\nconst totalWidth = data.reduce((sum, d) => sum + (d[CUSTOMERS] ?? 0), 0);\nconst eps = totalWidth * 1e-6;\n\nlet cursor = 0;\nconst bars = tiers.map((tier) => {\n  const rows = data.filter((d) => d[TIER] === tier);\n  const total = rows.reduce((sum, d) => sum + (d[CUSTOMERS] ?? 0), 0);\n  const bar = { tier, rows, total, x0: cursor, x1: cursor + total, center: cursor + total / 2 };\n  cursor += total;\n  return bar;\n});\n\n// Highcharts stacks the first series on top, so the last status sits on the\n// baseline.\nconst areaSeries = statuses.map((status, i) => {\n  const color = STATUS_COLORS[status] ?? t.palette[i];\n  return {\n    type: \"area\",\n    name: status,\n    color,\n    data: bars.flatMap((bar, j) => {\n      const y = bar.rows.find((d) => d[STATUS] === status)?.[CUSTOMERS] ?? null;\n      return [\n        { x: j === 0 ? bar.x0 : bar.x0 + eps, y },\n        { x: j === bars.length - 1 ? bar.x1 : bar.x1 - eps, y },\n      ];\n    }),\n  };\n});\n\n// Percentage labels sit at the vertical middle of their own segment: a\n// label-only series places them in data units, centred on each band.\nconst labelPoints = bars.flatMap((bar) => {\n  let below = 0;\n  return [...statuses].reverse().flatMap((status) => {\n    const value = bar.rows.find((d) => d[STATUS] === status)?.[CUSTOMERS] ?? 0;\n    const share = (100 * value) / bar.total;\n    const point = { x: bar.center, y: below + share / 2, label: `${Math.round(share)}%` };\n    below += share;\n    return share >= MIN_LABEL_SHARE ? [point] : [];\n  });\n});\n\nconst titleText =\n  \"SaaS Subscription Churn by Tier · bar-spine · javascript · highcharts · anyplot.ai\";\nconst titleFontSize = Math.max(18, Math.round(30 * Math.min(1, 67 / titleText.length)));\n\nHighcharts.chart(\"container\", {\n  palette: { colorScheme: t.theme },\n  chart: {\n    backgroundColor: \"transparent\",\n    animation: false,\n    spacingRight: 28, // room for the last tier's label under a narrow bar\n    style: { fontFamily: \"inherit\" },\n  },\n  credits: { enabled: false },\n  colors: t.palette,\n  title: {\n    text: titleText,\n    style: { color: t.ink, fontSize: `${titleFontSize}px`, fontWeight: \"600\" },\n  },\n  xAxis: {\n    min: 0,\n    max: totalWidth,\n    title: {\n      text: `${TIER} — width ∝ ${CUSTOMERS.toLowerCase()}`,\n      style: { color: t.inkSoft, fontSize: \"16px\" },\n    },\n    tickPositions: bars.map((bar) => bar.center),\n    gridLineWidth: 0,\n    lineColor: t.inkSoft,\n    tickColor: t.inkSoft,\n    tickLength: 6,\n    // A page-coloured hairline marks where two adjacent tiers meet, also\n    // where their segments share a height.\n    plotLines: bars.slice(1).map((bar) => ({\n      value: bar.x0,\n      color: t.pageBg,\n      width: 2,\n      zIndex: 4,\n    })),\n    labels: {\n      formatter: function () {\n        return bars.find((bar) => bar.center === this.value)?.tier ?? \"\";\n      },\n      style: { color: t.inkSoft, fontSize: \"14px\" },\n    },\n  },\n  yAxis: {\n    tickInterval: 20,\n    title: {\n      text: `Share within ${TIER.toLowerCase()} (%)`,\n      style: { color: t.inkSoft, fontSize: \"16px\" },\n    },\n    gridLineColor: t.grid,\n    labels: {\n      format: \"{value}%\",\n      style: { color: t.inkSoft, fontSize: \"14px\" },\n    },\n  },\n  legend: {\n    itemStyle: { color: t.inkSoft, fontSize: \"14px\" },\n    itemHoverStyle: { color: t.ink },\n    symbolRadius: 2,\n    itemDistance: 24,\n    margin: 20,\n    backgroundColor: t.elevatedBg,\n    borderColor: t.grid,\n    borderWidth: 1,\n    borderRadius: 6,\n    padding: 12,\n  },\n  tooltip: {\n    shared: true,\n    backgroundColor: t.elevatedBg,\n    borderColor: t.grid,\n    style: { color: t.ink, fontSize: \"13px\" },\n    formatter: function () {\n      const bar = bars.find((b) => this.x >= b.x0 && this.x <= b.x1);\n      const lines = this.points.map(\n        (p) => `${p.series.name}: ${p.percentage.toFixed(1)}% (${p.y.toLocaleString(\"en-US\")})`\n      );\n      return [`<b>${bar.tier}</b>`, ...lines].join(\"<br/>\");\n    },\n  },\n  plotOptions: {\n    series: { animation: false },\n    area: {\n      stacking: \"percent\",\n      fillOpacity: 1,\n      lineWidth: 1,\n      lineColor: t.pageBg,\n      marker: { enabled: false },\n      states: { hover: { enabled: false } },\n    },\n  },\n  series: [\n    ...areaSeries,\n    {\n      type: \"scatter\",\n      name: \"Share labels\",\n      data: labelPoints,\n      showInLegend: false,\n      enableMouseTracking: false,\n      marker: { enabled: false },\n      dataLabels: {\n        enabled: true,\n        align: \"center\",\n        verticalAlign: \"middle\",\n        format: \"{point.label}\",\n        style: {\n          color: \"#FFFFFF\",\n          fontSize: \"14px\",\n          fontWeight: \"600\",\n          textOutline: \"1px rgba(26, 26, 23, 0.55)\",\n        },\n      },\n    },\n  ],\n});\n"}