{"spec_id":"parallel-categories-basic","library":"echarts","language":"javascript","code":"// anyplot.ai\n// parallel-categories-basic: Basic Parallel Categories Plot\n// Library: echarts 6.1.0 | JavaScript 22.23.4\n// Quality: 89/100 | Updated: 2026-11-09\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\n// Outcome colors by meaning, with the Imprint palette as fallback for other outcomes\nconst OUTCOME_COLORS = { Purchased: t.palette[0], Abandoned: t.palette[4] };\n\n// Columns: set these to your data's column names, the dimensions in axis order\nconst DIMENSIONS = [\"Acquisition channel\", \"Product category\", \"Device\", \"Purchase outcome\"]; // role: dimension\nconst VISITORS = \"Visitors\"; // role: count\n\n// Data: replace this block with your own records, one object per combination of categories\n// Visitors of an online shop: visits per channel and product category, each category's\n// desktop share, and a purchase rate per device that each channel shifts up or down.\nconst visits = {\n  \"Organic Search\": { Electronics: 420, Apparel: 280, \"Home & Garden\": 150 },\n  \"Paid Ads\": { Electronics: 380, Apparel: 210, \"Home & Garden\": 90 },\n  \"Social Media\": { Electronics: 160, Apparel: 340, \"Home & Garden\": 120 },\n  \"Email Campaign\": { Electronics: 90, Apparel: 130, \"Home & Garden\": 60 },\n};\nconst desktopShare = { Electronics: 0.6, Apparel: 0.55, \"Home & Garden\": 0.5 };\nconst purchaseRate = { Desktop: 0.72, Mobile: 0.44 };\nconst channelLift = { \"Organic Search\": 0.04, \"Paid Ads\": -0.02, \"Social Media\": -0.12, \"Email Campaign\": 0.12 };\nconst [CHANNEL, CATEGORY, DEVICE, OUTCOME] = DIMENSIONS;\nconst data = [];\nfor (const [channel, byCategory] of Object.entries(visits)) {\n  for (const [category, total] of Object.entries(byCategory)) {\n    const desktop = Math.round(total * desktopShare[category]);\n    for (const [device, visitors] of [\n      [\"Desktop\", desktop],\n      [\"Mobile\", total - desktop],\n    ]) {\n      const purchased = Math.round(visitors * (purchaseRate[device] + channelLift[channel]));\n      for (const [outcome, count] of [\n        [\"Purchased\", purchased],\n        [\"Abandoned\", visitors - purchased],\n      ]) {\n        data.push({ [CHANNEL]: channel, [CATEGORY]: category, [DEVICE]: device, [OUTCOME]: outcome, [VISITORS]: count });\n      }\n    }\n  }\n}\n\n// Plot: reads only data and the constants above\n// ECharts has no parallel-categories series; a Sankey with one node column per\n// dimension, one node per category and one ribbon per path segment draws it natively.\n// Ribbons take the color of the last dimension, the outcome.\nconst last = DIMENSIONS.at(-1);\nconst outcomes = [...new Set(data.map((d) => d[last]))];\nconst outcomeColor = Object.fromEntries(\n  outcomes.map((outcome, i) => [outcome, OUTCOME_COLORS[outcome] ?? t.palette[i]]),\n);\n\n// One node per category of each dimension, keyed by its dimension so that equal\n// category names on two axes stay apart; only the outcome segments carry color.\nconst nodes = DIMENSIONS.flatMap((dim) =>\n  [...new Set(data.map((d) => d[dim]))].map((value) => ({\n    name: `${dim}: ${value}`,\n    label: { formatter: value },\n    itemStyle: { color: dim === last ? outcomeColor[value] : t.inkSoft },\n  })),\n);\n\n// Ribbons: every path segment between neighbouring axes, split by the path's outcome\n// so each ribbon keeps its outcome color from the first axis to the last.\nconst linkTotals = new Map();\ndata.forEach((d) => {\n  DIMENSIONS.slice(0, -1).forEach((dim, k) => {\n    const source = `${dim}: ${d[dim]}`;\n    const target = `${DIMENSIONS[k + 1]}: ${d[DIMENSIONS[k + 1]]}`;\n    const key = `${source}\\u0000${target}\\u0000${d[last]}`;\n    const link = linkTotals.get(key) ?? { source, target, outcome: d[last], value: 0 };\n    link.value += d[VISITORS];\n    linkTotals.set(key, link);\n  });\n});\nconst links = [...linkTotals.values()].map(({ source, target, outcome, value }) => ({\n  source,\n  target,\n  value,\n  lineStyle: { color: outcomeColor[outcome] },\n}));\n\nconst totalVisitors = data.reduce((sum, d) => sum + d[VISITORS], 0);\n\n// Axis names above the node columns: justified columns sit at equal steps across the\n// series' width, and each name starts at its column's left edge like the node labels.\nconst layout = { left: 40, right: 170, top: 150, bottom: 50, nodeWidth: 22 };\nconst columnStep =\n  (window.ANYPLOT_SIZE.width - layout.left - layout.right - layout.nodeWidth) / (DIMENSIONS.length - 1);\nconst axisNames = DIMENSIONS.map((dim, k) => ({\n  type: \"text\",\n  left: layout.left + k * columnStep,\n  top: layout.top - 38,\n  style: { text: dim, fill: t.ink, fontSize: 17, fontWeight: 600 },\n}));\n\nconst chart = echarts.init(document.getElementById(\"container\"));\n\nchart.setOption({\n  animation: false,\n  backgroundColor: \"transparent\",\n  title: {\n    text: \"parallel-categories-basic · javascript · echarts · anyplot.ai\",\n    subtext: `${totalVisitors.toLocaleString(\"en-US\")} ${VISITORS.toLowerCase()} · ribbon width shows ${VISITORS.toLowerCase()}, color shows ${last.toLowerCase()}`,\n    left: \"center\",\n    textStyle: { color: t.ink, fontSize: 22, fontWeight: 600 },\n    subtextStyle: { color: t.inkSoft, fontSize: 16 },\n  },\n  tooltip: { trigger: \"item\" },\n  graphic: axisNames,\n  series: [\n    {\n      type: \"sankey\",\n      left: layout.left,\n      right: layout.right,\n      top: layout.top,\n      bottom: layout.bottom,\n      nodeWidth: layout.nodeWidth,\n      nodeGap: 22,\n      nodeAlign: \"justify\",\n      // More relaxation passes than the default 32 let the layout settle on an\n      // order of categories within each axis with fewer ribbon crossings.\n      layoutIterations: 100,\n      orient: \"horizontal\",\n      draggable: false,\n      emphasis: { focus: \"trajectory\" },\n      lineStyle: { opacity: 0.6, curveness: 0.5 },\n      label: {\n        color: t.ink,\n        fontSize: 15,\n        fontWeight: 500,\n        backgroundColor: t.pageBg,\n        padding: [3, 6],\n        borderRadius: 3,\n      },\n      data: nodes,\n      links,\n    },\n  ],\n});\n"}