0.0.12 • Published 4 years ago
@plotdb/layout v0.0.12
@plotdb/layout
Compute chart layout by HTML/CSS.
Usage
We will need 3 elements:
- root node: containing layout and render node. should set with
pdl-layoutclass. - layout node: provide layout information with
div.pdl-cellforg.pdl-cellwith the samedata-nameattr. - render node: containing
g.pdl-cellelement corresponding topdl-cellin layout node.
A sample DOM structure in Pug:
#my-chart.pdl-layout
div(data-type="layout")
.pdl-cell(data-name="yaxis")
.pdl-cell(data-name="view")
.pdl-cell
.pdl-cell(data-name="xaxis")
div(data-type="render")
svg
g.pdl-cell(data-name="yaxis")
g.pdl-cell(data-name="view"): rect(ld-each="data")
g
g.pdl-cell(data-name="xaxis")then, init with JS:
mylayout = new layout({root: '#my-chart'})
mylayout.on \render, ->
d3.select('g.pdl-cell[data-name=view]').call ->
/* get corresponding node and related size (box{x,y,width,height}) information */
@layout{node, box}
@get-box('name') # get bounding box with given name
@get-node('name') # get DOM node with given name
@get-group('name') # get `g` (group) with given name
mylayout.init -> ... /* initializing ... */when the layout object is no longer needed, one should destroy it:
mylayout.destroy!Configuration
autoSvg: true if automatically create correspondingsvgandgelement. default true- even with
autoSvgenabled, user can still prepare partial svg / g elements.@plotdb/layoutwill fill the missing parts automatically.
- even with
watchResize: true if automatically callsupdatewhen container resized. default true.- by disabling this you will have to manually call
updatewhen you want to update layout.
- by disabling this you will have to manually call
round: set true to automatically round dimensions. default true.
API
init(cb): initialize layout. optionalcbcallback function.cb(): a function called beforeupdatebut after groups are prepared.- run with the context of the inited layout object.
- if it returns a promise,
@plotdb/layoutwaits until it resolves before callingupdate
root(): get root nodegetNode(name): get layout node bynamegetNodes: get all layout nodes in an object, hashed by namesgetGroup(name): get rendergbynamegetGroups: get all rendergin an object, hashed by namesgetBox(name,cached): -> get bounding client rect or node byname.cached: return cached version if true. default false ( computed in realtime )
update(opt): update layout. ( update position of all groups )- fire rendering event if
optistrue. default false.
- fire rendering event if
destroy(): destroy functionon(n,cb): handlenevent withcbcallback function.fire(n, ...v): firenevent with parameters in v.
SVG styling
when rendering text manually with SVG, one should be aware of following settings:
line-heightshould be1emin HTML layout.dominant-baselineshould behangingin SVG text.
License
MIT