|
| 1 | +import {group} from "d3-array"; |
| 2 | +import {create} from "d3-selection"; |
| 3 | +import {Mark, indexOf} from "../mark.js"; |
| 4 | +import {Channels} from "../plot.js"; |
| 5 | +import {Scales, autoScaleRange} from "../scales.js"; |
| 6 | + |
| 7 | +export class FacetY extends Mark { |
| 8 | + constructor( |
| 9 | + data, |
| 10 | + { |
| 11 | + x, |
| 12 | + y |
| 13 | + } = {}, |
| 14 | + options = {} |
| 15 | + ) { |
| 16 | + super( |
| 17 | + data, |
| 18 | + { |
| 19 | + x: {value: x, scale: "x"}, |
| 20 | + y: {value: y, scale: "y", type: "band"} |
| 21 | + } |
| 22 | + ); |
| 23 | + this.options = options; |
| 24 | + } |
| 25 | + render(I, {y: {scale: y}, ...scales}, dimensions) { |
| 26 | + const {data, options, channels: {y: {value: Y}}} = this; |
| 27 | + const {marks: submarks = []} = options; |
| 28 | + const subchannels = Channels(submarks); |
| 29 | + const subscales = {...Scales(subchannels, options.scales), ...scales}; |
| 30 | + const subdimensions = {...dimensions, marginTop: 0, marginBottom: 0, height: y.bandwidth()}; |
| 31 | + |
| 32 | + autoScaleRange(subscales, subdimensions); |
| 33 | + |
| 34 | + return create("svg:g") |
| 35 | + .call(g => g.selectAll() |
| 36 | + .data(group(I, i => Y[i])) |
| 37 | + .join("g") |
| 38 | + .attr("transform", ([key]) => `translate(0,${y(key)})`) |
| 39 | + .each(function([, I]) { |
| 40 | + for (const mark of submarks) { |
| 41 | + const index = mark.data === data ? I |
| 42 | + : mark.data === undefined ? undefined |
| 43 | + : Array.from(mark.data, indexOf); |
| 44 | + const node = mark.render(index, subscales, subdimensions); |
| 45 | + if (node != null) this.appendChild(node); |
| 46 | + } |
| 47 | + })) |
| 48 | + .node(); |
| 49 | + } |
| 50 | +} |
0 commit comments