Creates a D3.js layer on a given element with specified attributes.
el
d3.Selection parent D3 selection.tag
string HTML/SVG tag to create.cls
string class to apply to new elements.data
Function data-binding function. (optional, default(d)=>[d]
)
// Avoid using `.append()` on D3 selections! Use `layer()` instead.
// AVOID THIS
el.selectAll("g.nodes").append("g").attr("class", "nodes");
// USE THIS INSTEAD
import { layer } from "https://cdn.jsdelivr.net/npm/@gramex/chartbase@1";
layer(el, "g", "layer");
// To specify the data against the appended nodes:
// AVOID THIS
el.selectAll("g.nodes").data(data).append("g").attr("class", "nodes");
// USE THIS INSTEAD
import { layer } from "https://cdn.jsdelivr.net/npm/@gramex/chartbase@1";
layer(el, "g", "layer", data);
Returns d3.Selection layer of <${tag} class="${cls}">
.
Retrieves the closest SVG element, along with its width and height
el
(string | HTMLElement) The selector or HTML element.width
number? The width of the SVG. Optional, fallback to the closest SVG parent.height
number? The height of the SVG. Optional, fallback to the closest SVG parent.
// Set the default width and height of a chart based on the closest SVG element:
const { width, height } = getSVG("#chart-svg");
// If your function accepts an optional width or height parameter, pass it as defaults:
{ width, height } = getSVG("#chart-svg", width, height);
// If the element is not an SVG (e.g. `g#nodes`), you can access the closest SVG container by:
{ container } = getSVG("g#nodes");
// el may be a selector or a DOM element. To get the DOM element, use:
{ el } = getSVG("g#nodes");
Returns SVGSize { el, container, width, height }
.
Type: Object
el
HTMLElement The DOM element passed. If a string selector was passed, this is the result ofdocument.querySelector
.container
HTMLElement The closest SVG element.width
number The width of the SVG.height
number The height of the SVG.