|
| 1 | +import {ascending} from "d3-array"; |
| 2 | +import {create} from "d3-selection"; |
| 3 | +import {arc as shapeArc} from "d3-shape"; |
| 4 | +import {filter} from "../defined.js"; |
| 5 | +import {Mark, number, maybeColor, maybeNumber, title} from "../mark.js"; |
| 6 | +import {Style, applyDirectStyles, applyIndirectStyles, applyTransform} from "../style.js"; |
| 7 | + |
| 8 | +const constant = x => () => x; |
| 9 | + |
| 10 | +export class Arc extends Mark { |
| 11 | + constructor( |
| 12 | + data, |
| 13 | + { |
| 14 | + startAngle = d => d.startAngle, |
| 15 | + endAngle = d => d.endAngle, |
| 16 | + x = constant(0), |
| 17 | + y = constant(0), |
| 18 | + innerRadius = 0, |
| 19 | + outerRadius = 140, |
| 20 | + z, |
| 21 | + title, |
| 22 | + fill, |
| 23 | + stroke, |
| 24 | + insetTop = 0, |
| 25 | + insetRight = 0, |
| 26 | + insetBottom = 0, |
| 27 | + insetLeft = 0, |
| 28 | + transform, |
| 29 | + ...style |
| 30 | + } = {} |
| 31 | + ) { |
| 32 | + const [vfill, cfill] = maybeColor(fill); |
| 33 | + const [vstroke, cstroke] = maybeColor(stroke); |
| 34 | + const [vx, cx = vx == null ? 0 : undefined] = maybeNumber(x); |
| 35 | + const [vy, cy = vy == null ? 0 : undefined] = maybeNumber(y); |
| 36 | + const [vri, cri = vri == null ? 0 : undefined] = maybeNumber(innerRadius); |
| 37 | + const [vro, cro = vro == null ? 0 : undefined] = maybeNumber(outerRadius); |
| 38 | + |
| 39 | + super( |
| 40 | + data, |
| 41 | + [ |
| 42 | + {name: "x", value: vx, scale: "x"}, |
| 43 | + {name: "y", value: vy, scale: "y"}, |
| 44 | + {name: "startAngle", value: startAngle}, |
| 45 | + {name: "endAngle", value: endAngle}, |
| 46 | + {name: "innerRadius", value: innerRadius, optional: true}, |
| 47 | + {name: "outerRadius", value: outerRadius, optional: true}, |
| 48 | + {name: "z", value: z, optional: true}, |
| 49 | + {name: "title", value: title, optional: true}, |
| 50 | + {name: "fill", value: vfill, scale: "color", optional: true}, |
| 51 | + {name: "stroke", value: vstroke, scale: "color", optional: true} |
| 52 | + ], |
| 53 | + transform |
| 54 | + ); |
| 55 | + Style(this, {fill: cfill, stroke: cstroke, ...style}); |
| 56 | + this.x = cx; |
| 57 | + this.y = cy; |
| 58 | + this.ri = cri; |
| 59 | + this.ro = cro; |
| 60 | + this.insetTop = number(insetTop); |
| 61 | + this.insetRight = number(insetRight); |
| 62 | + this.insetBottom = number(insetBottom); |
| 63 | + this.insetLeft = number(insetLeft); |
| 64 | + } |
| 65 | + render( |
| 66 | + I, |
| 67 | + {x, y, color}, |
| 68 | + {startAngle: A, endAngle: B, innerRadius: RI, outerRadius: RO, x: X, y: Y, z: Z, title: L, fill: F, stroke: S} |
| 69 | + ) { |
| 70 | + const index = filter(I, A, B, F, S); |
| 71 | + if (Z) index.sort((i, j) => ascending(Z[i], Z[j])); |
| 72 | + |
| 73 | + const arc = shapeArc() |
| 74 | + .startAngle(i => A[i]) |
| 75 | + .endAngle(i => B[i]) |
| 76 | + .innerRadius(i => RI[i] || this.ri) |
| 77 | + .outerRadius(i => RO[i] || this.ro); |
| 78 | + |
| 79 | + return create("svg:g") |
| 80 | + .call(applyIndirectStyles, this) |
| 81 | + .call(applyTransform, x, y) |
| 82 | + .call(g => g.selectAll() |
| 83 | + .data(index) |
| 84 | + .join("path") |
| 85 | + .call(applyDirectStyles, this) |
| 86 | + .attr("d", arc) |
| 87 | + .attr("transform", i => `translate(${x(X[i])},${y(Y[i])})`) |
| 88 | + .attr("fill", F && (i => color(F[i]))) |
| 89 | + .attr("stroke", S && (i => color(S[i]))) |
| 90 | + .call(title(L))) |
| 91 | + .node(); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +export function arc(data, options) { |
| 96 | + return new Arc(data, options); |
| 97 | +} |
0 commit comments