|
| 1 | +import {ascending} from "d3-array"; |
| 2 | +import {create} from "d3-selection"; |
| 3 | +import {filter, nonempty, positive} from "../defined.js"; |
| 4 | +import {Mark, indexOf, identity, first, second, maybeColor, maybeNumber} from "../mark.js"; |
| 5 | +import {Style, applyDirectStyles, applyIndirectStyles, applyBandTransform} from "../style.js"; |
| 6 | + |
| 7 | +export class Symbolic extends Mark { |
| 8 | + constructor( |
| 9 | + data, |
| 10 | + { |
| 11 | + x = first, |
| 12 | + y = second, |
| 13 | + z, |
| 14 | + size, |
| 15 | + title, |
| 16 | + fill, |
| 17 | + stroke, |
| 18 | + symbol, |
| 19 | + transform, |
| 20 | + ...style |
| 21 | + } = {} |
| 22 | + ) { |
| 23 | + const [vsize, csize = vsize == null ? 27 : undefined] = maybeNumber(size); |
| 24 | + const [vfill, cfill = vfill == null ? "none" : undefined] = maybeColor(fill); |
| 25 | + const [vstroke, cstroke = vstroke == null && cfill === "none" ? "currentColor" : undefined] = maybeColor(stroke); |
| 26 | + super( |
| 27 | + data, |
| 28 | + [ |
| 29 | + {name: "x", value: x, scale: "x"}, |
| 30 | + {name: "y", value: y, scale: "y"}, |
| 31 | + {name: "z", value: z, optional: true}, |
| 32 | + {name: "size", value: vsize, scale: "size", optional: true}, |
| 33 | + {name: "title", value: title, optional: true}, |
| 34 | + {name: "fill", value: vfill, scale: "color", optional: true}, |
| 35 | + {name: "stroke", value: vstroke, scale: "color", optional: true}, |
| 36 | + {name: "symbol", value: symbol, optional: true} |
| 37 | + ], |
| 38 | + transform |
| 39 | + ); |
| 40 | + this.size = csize; |
| 41 | + Style(this, { |
| 42 | + fill: cfill, |
| 43 | + stroke: cstroke, |
| 44 | + strokeWidth: cstroke != null || vstroke != null ? 1.5 : undefined, |
| 45 | + ...style |
| 46 | + }); |
| 47 | + } |
| 48 | + render( |
| 49 | + I, |
| 50 | + {x, y, size, color}, |
| 51 | + {x: X, y: Y, z: Z, size: A, title: L, fill: F, stroke: S, symbol: K} |
| 52 | + ) { |
| 53 | + let index = filter(I, X, Y, F, S); |
| 54 | + if (A) index = index.filter(i => positive(A[i])); |
| 55 | + if (Z) index.sort((i, j) => ascending(Z[i], Z[j])); |
| 56 | + const table = new symbolTable(); |
| 57 | + return create("svg:g") |
| 58 | + .call(applyIndirectStyles, this) |
| 59 | + .call(applyBandTransform, x, y) |
| 60 | + .call(g => g.selectAll() |
| 61 | + .data(index) |
| 62 | + .join("use") |
| 63 | + .call(applyDirectStyles, this) |
| 64 | + .attr("transform", i => `translate(${x(X[i])},${y(Y[i])})scale(${scale(A ? size(A[i]) : this.size)})`) |
| 65 | + .attr("fill", F && (i => color(F[i]))) |
| 66 | + .attr("stroke", S && (i => color(S[i]))) |
| 67 | + .attr("href", K ? (i => table.get(K[i])) : table.get("rect")) |
| 68 | + .call(L ? text => text |
| 69 | + .filter(i => nonempty(L[i])) |
| 70 | + .append("title") |
| 71 | + .text(i => L[i]) : () => {}) |
| 72 | + ) |
| 73 | + .call(g => g.append("defs") |
| 74 | + .selectAll() |
| 75 | + .data(table.symbols()) |
| 76 | + .join("g") |
| 77 | + .attr("id", ({id}) => id) |
| 78 | + .html(({src}) => src) |
| 79 | + .selectAll("*") |
| 80 | + .attr("vector-effect", "non-scaling-stroke") |
| 81 | + ) |
| 82 | + .node(); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +class symbolTable { |
| 87 | + constructor() { |
| 88 | + this.s = new Map(); |
| 89 | + } |
| 90 | + symbols() { |
| 91 | + return this.s.values(); |
| 92 | + } |
| 93 | + add(name, src) { |
| 94 | + this.s.set(name, {src, id: "toto" + name}); |
| 95 | + } |
| 96 | + get(name) { |
| 97 | + if (!this.s.has(name)) { |
| 98 | + switch (name) { |
| 99 | + case "rect": |
| 100 | + this.add(name, "<rect width=2 height=2 x=-1 y=-1>"); |
| 101 | + break; |
| 102 | + case "dot": |
| 103 | + default: |
| 104 | + this.add(name, "<circle r=1>"); |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + return `#${this.s.get(name).id}`; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +function scale(area) { |
| 113 | + return Math.sqrt(area / 3); |
| 114 | +} |
| 115 | + |
| 116 | +export function symbol(data, options) { |
| 117 | + return new Symbolic(data, options); |
| 118 | +} |
| 119 | + |
| 120 | +export function symbolX(data, {x = identity, ...options} = {}) { |
| 121 | + return new Symbolic(data, {...options, x, y: indexOf}); |
| 122 | +} |
| 123 | + |
| 124 | +export function symbolY(data, {y = identity, ...options} = {}) { |
| 125 | + return new Symbolic(data, {...options, x: indexOf, y}); |
| 126 | +} |
0 commit comments