Feynman Diagram for Particle Interactions in Apache ECharts (JavaScript)

The same plot in 14 other libraries — JavaScript: Chart.js, D3.js, Highcharts, MUI X Charts; Python: Altair, Bokeh, lets-plot, Matplotlib, Plotly, plotnine, Pygal, Seaborn; R: ggplot2; Julia: Makie.jl. Compare all 15 side by side: Feynman Diagram for Particle Interactions in Python, R, Julia and JavaScript.

A Feynman diagram visualizes interactions between subatomic particles in quantum field theory. Different line styles represent different particle types: straight lines for fermions (electrons, quarks), wavy lines for photons, curly/looped lines for gluons, and dashed lines for scalar bosons (e.g., Higgs). Lines meet at vertices representing interaction points. Invented by Richard Feynman, these diagrams are both a computational tool and a cultural icon of modern physics.

Feynman Diagram for Particle Interactions rendered with Apache ECharts

Renders

JavaScript source (Apache ECharts)

// anyplot.ai
// feynman-basic: Feynman Diagram for Particle Interactions
// Library: echarts 6.1.0 | JavaScript 22.23.5
// Quality: 85/100 | Updated: 2026-12-01

//# anyplot-orientation: landscape
const t = window.ANYPLOT_TOKENS;

// Imprint palette: one colour per particle type
const C_FERMION = t.palette[0]; // #009E73 brand green
const C_GLUON = t.palette[1]; // #C475FD lavender
const C_PHOTON = t.palette[2]; // #4467A3 blue
const C_BOSON = t.palette[3]; // #BD8233 ochre

const LW = 5;
const W = 1600, H = 900; // CSS mount size; the graph series uses these as its coordinate range

// Columns
const FROM = "from"; // role: start point of a propagator
const TO = "to"; // role: end point of a propagator
const KIND = "type"; // role: particle type (fermion | photon | gluon | boson)
const LABEL = "label"; // role: particle symbol
const LABEL_OFFSET = "labelOffset"; // role: label position relative to the line midpoint

// Data: gg -> H -> gamma gamma, two top-quark triangles joined by a Higgs line.
// Positions are schematic; only which lines meet at which vertex carries meaning.
const points = {
  g1: [140, 200], g2: [140, 740], // incoming gluons
  A1: [430, 330], A2: [430, 610], A3: [650, 470], // first top-quark triangle
  B1: [950, 470], B2: [1170, 330], B3: [1170, 610], // second top-quark triangle
  p1: [1460, 200], p2: [1460, 740], // outgoing photons
};
const vertices = ["A1", "A2", "A3", "B1", "B2", "B3"];
const data = [
  { [FROM]: "g1", [TO]: "A1", [KIND]: "gluon", [LABEL]: "g", [LABEL_OFFSET]: [14, -44] },
  { [FROM]: "g2", [TO]: "A2", [KIND]: "gluon", [LABEL]: "g", [LABEL_OFFSET]: [-20, -40] },
  { [FROM]: "A1", [TO]: "A3", [KIND]: "fermion", [LABEL]: "t", [LABEL_OFFSET]: [22, -30] },
  { [FROM]: "A3", [TO]: "A2", [KIND]: "fermion", [LABEL]: "t", [LABEL_OFFSET]: [22, 30] },
  { [FROM]: "A2", [TO]: "A1", [KIND]: "fermion", [LABEL]: "t", [LABEL_OFFSET]: [-34, 0] },
  { [FROM]: "A3", [TO]: "B1", [KIND]: "boson", [LABEL]: "H", [LABEL_OFFSET]: [0, -34] },
  { [FROM]: "B1", [TO]: "B2", [KIND]: "fermion", [LABEL]: "t", [LABEL_OFFSET]: [-22, -30] },
  { [FROM]: "B2", [TO]: "B3", [KIND]: "fermion", [LABEL]: "t", [LABEL_OFFSET]: [34, 0] },
  { [FROM]: "B3", [TO]: "B1", [KIND]: "fermion", [LABEL]: "t", [LABEL_OFFSET]: [-22, 30] },
  { [FROM]: "B2", [TO]: "p1", [KIND]: "photon", [LABEL]: "γ", [LABEL_OFFSET]: [14, -44] },
  { [FROM]: "B3", [TO]: "p2", [KIND]: "photon", [LABEL]: "γ", [LABEL_OFFSET]: [14, 44] },
];

// ── line-style generators (wavy and curly lines have no series type) ─────────
function wavyBeziers(x1, y1, x2, y2, nWaves, amp, color, lw) {
  const dx = x2 - x1, dy = y2 - y1, len = Math.hypot(dx, dy);
  const nx = -dy / len, ny = dx / len, n = nWaves * 2, out = [];
  for (let i = 0; i < n; i++) {
    const t0 = i / n, t1 = (i + 1) / n, tm = (t0 + t1) / 2, sign = i % 2 === 0 ? 1 : -1;
    out.push({ type: "bezierCurve", shape: {
      x1: x1 + dx * t0, y1: y1 + dy * t0,
      cpx1: x1 + dx * tm + nx * amp * sign, cpy1: y1 + dy * tm + ny * amp * sign,
      x2: x1 + dx * t1, y2: y1 + dy * t1,
    }, style: { stroke: color, lineWidth: lw, fill: "rgba(0,0,0,0)" } });
  }
  return out;
}

// Curly gluon: a trochoid, so each turn is a closed coil that loops back over itself
function curlyLine(x1, y1, x2, y2, nLoops, amp, color, lw) {
  const dx = x2 - x1, dy = y2 - y1, len = Math.hypot(dx, dy);
  const ux = dx / len, uy = dy / len, nx = -uy, ny = ux;
  const pitch = len / (2 * Math.PI * nLoops), steps = nLoops * 48, pts = [];
  for (let i = 0; i <= steps; i++) {
    const phi = (2 * Math.PI * nLoops * i) / steps;
    const along = pitch * phi - amp * Math.sin(phi), across = amp * (1 - Math.cos(phi));
    pts.push([x1 + ux * along + nx * across, y1 + uy * along + ny * across]);
  }
  return { type: "polyline", shape: { points: pts },
    style: { stroke: color, lineWidth: lw, fill: "rgba(0,0,0,0)", lineJoin: "round" } };
}

function arrow(x1, y1, x2, y2, sz) {
  const dx = x2 - x1, dy = y2 - y1, len = Math.hypot(dx, dy);
  const ux = dx / len, uy = dy / len, px = -uy, py = ux;
  const bx = x2 - ux * sz, by = y2 - uy * sz, w = sz * 0.42;
  return [[x2, y2], [bx + px * w, by + py * w], [bx - px * w, by - py * w]];
}

function solidLine(x1, y1, x2, y2, color, lw, dash) {
  return { type: "polyline", shape: { points: [[x1, y1], [x2, y2]] },
    style: { stroke: color, lineWidth: lw, fill: "rgba(0,0,0,0)", lineDash: dash } };
}

// ── Plot ─────────────────────────────────────────────────────────────────────
const elems = []; // graphic layer: wavy/curly lines, fermion arrows, labels, key
const links = []; // graph series: straight fermion lines and the dashed Higgs line

for (const d of data) {
  const [x1, y1] = points[d[FROM]], [x2, y2] = points[d[TO]];
  if (d[KIND] === "gluon") elems.push(curlyLine(x1, y1, x2, y2, 5, 18, C_GLUON, LW));
  else if (d[KIND] === "photon") elems.push(...wavyBeziers(x1, y1, x2, y2, 6, 22, C_PHOTON, LW));
  else if (d[KIND] === "boson") {
    links.push({ source: d[FROM], target: d[TO], lineStyle: { color: C_BOSON, width: LW, opacity: 1, type: [16, 10] } });
  } else {
    // fermion: series line, arrow at the midpoint along the direction of flow
    links.push({ source: d[FROM], target: d[TO], lineStyle: { color: C_FERMION, width: LW, opacity: 1 } });
    const mx = (x1 + x2) / 2, my = (y1 + y2) / 2, ux = (x2 - x1) * 0.03, uy = (y2 - y1) * 0.03;
    elems.push({ type: "polygon", shape: { points: arrow(mx - ux, my - uy, mx + ux, my + uy, 26) },
      style: { fill: C_FERMION, stroke: "none" } });
  }
  const color = { gluon: C_GLUON, photon: C_PHOTON, boson: C_BOSON, fermion: C_FERMION }[d[KIND]];
  elems.push({ type: "text", style: {
    text: d[LABEL], x: (x1 + x2) / 2 + d[LABEL_OFFSET][0], y: (y1 + y2) / 2 + d[LABEL_OFFSET][1],
    textAlign: "center", textVerticalAlign: "middle", font: "italic bold 30px serif", fill: color } });
}

// Nodes: interaction vertices are dots with a faint halo, external legs end invisibly
const nodes = Object.entries(points).map(([name, [x, y]]) => vertices.includes(name)
  ? { name, value: [x, y], symbol: "circle", symbolSize: 22,
      itemStyle: { color: t.ink, borderColor: "rgba(0,158,115,0.15)", borderWidth: 9 } }
  : { name, value: [x, y], symbol: "none" });

// Legend: one sample per line style
const LY = 850;
elems.push(solidLine(300, LY, 370, LY, C_FERMION, LW));
elems.push({ type: "polygon", shape: { points: arrow(327, LY, 343, LY, 20) }, style: { fill: C_FERMION, stroke: "none" } });
elems.push({ type: "text", style: { text: "fermion", x: 382, y: LY, textVerticalAlign: "middle", font: "22px sans-serif", fill: t.inkSoft } });
elems.push(...wavyBeziers(560, LY, 630, LY, 3, 14, C_PHOTON, LW));
elems.push({ type: "text", style: { text: "photon", x: 642, y: LY, textVerticalAlign: "middle", font: "22px sans-serif", fill: t.inkSoft } });
elems.push(curlyLine(780, LY - 9, 880, LY - 9, 4, 11, C_GLUON, LW));
elems.push({ type: "text", style: { text: "gluon", x: 900, y: LY, textVerticalAlign: "middle", font: "22px sans-serif", fill: t.inkSoft } });
elems.push(solidLine(1030, LY, 1090, LY, C_BOSON, LW, [16, 10]));
elems.push({ type: "text", style: { text: "scalar boson", x: 1102, y: LY, textVerticalAlign: "middle", font: "22px sans-serif", fill: t.inkSoft } });

// Time axis
elems.push(solidLine(1340, LY, 1480, LY, t.inkSoft, 2));
elems.push({ type: "polygon", shape: { points: arrow(1458, LY, 1480, LY, 14) }, style: { fill: t.inkSoft, stroke: "none" } });
elems.push({ type: "text", style: { text: "time", x: 1326, y: LY, textAlign: "right", textVerticalAlign: "middle", font: "22px sans-serif", fill: t.inkSoft } });

const chart = echarts.init(document.getElementById("container"));
chart.setOption({
  animation: false,
  backgroundColor: t.pageBg,
  title: {
    text: "feynman-basic · javascript · echarts · anyplot.ai",
    left: "center", top: 22,
    textStyle: { color: t.ink, fontSize: 28, fontWeight: "normal" },
  },
  // Hidden axes span the mount in CSS pixels, so graph coordinates equal graphic coordinates
  grid: { left: 0, top: 0, right: 0, bottom: 0 },
  xAxis: { type: "value", min: 0, max: W, show: false },
  yAxis: { type: "value", min: 0, max: H, inverse: true, show: false },
  series: [{
    type: "graph", coordinateSystem: "cartesian2d", layout: "none", z: 6,
    silent: true, data: nodes, links,
  }],
  graphic: elems.map((e) => ({ ...e, z: 5 })),
});

Retrieve this implementation

Runnable source as JSON, for any HTTP client: https://api.anyplot.ai/specs/feynman-basic/echarts/code. Any spec id and library id listed in llms-full.txt fit the same URL shape; every URL below is complete and callable.

{
  "spec_id": "feynman-basic",
  "language": "javascript",
  "library": "echarts",
  "page": "https://anyplot.ai/feynman-basic/javascript/echarts",
  "hub": "https://anyplot.ai/feynman-basic",
  "code_json": "https://api.anyplot.ai/specs/feynman-basic/echarts/code",
  "spec_json": "https://api.anyplot.ai/specs/feynman-basic",
  "render_light_png": "https://storage.googleapis.com/anyplot-images/plots/feynman-basic/javascript/echarts/plot-light.png",
  "render_dark_png": "https://storage.googleapis.com/anyplot-images/plots/feynman-basic/javascript/echarts/plot-dark.png",
  "interactive_light_html": "https://storage.googleapis.com/anyplot-images/plots/feynman-basic/javascript/echarts/plot-light.html",
  "interactive_dark_html": "https://storage.googleapis.com/anyplot-images/plots/feynman-basic/javascript/echarts/plot-dark.html",
  "quality_score": 85.0,
  "license": "MIT",
  "guide": "https://anyplot.ai/llms.txt"
}

Part of Feynman Diagram for Particle Interactions on anyplot.ai.

Other implementations