D3入门学习1node
const svg = d3 .select('div') // other CSS Selectors~~ .append("svg") .style({ backgound: 'pink'' }) .attr("width", '80vw') .attr("height", '90vh');
效果:app
new D3Svg([opts]);
opts.xx | 说明 |
---|---|
container | 装载svg元素的容器元素,默认在body元素建立一个空的div容器 |
width | svg容器的宽度 |
height | svg容器的高度 |
style | svg容器的样式配置 |
wrapperContainerStyle | 最外层容器的样式配置 |
class D3Svg { private svg: d3.Selection<any>; private wrapperContainer: HTMLElement | HTMLDivElement; constructor( options: ISvgContainerOptions = {}, ) { const container = options.container || DEFAULT_CONTAINER(); const width = options.width || DEFAULT_CONTAINER_WIDTH; const height = options.options || DEFAULT_CONTAINER__HEIGHT; const { style, wrapperContainerStyle } = options; /** * initialize wrapperContainer */ this.wrapperContainer = container; if (wrapperContainerStyle) { this.addWrapperStyle(wrapperContainerStyle); } this.svg = d3 .select(this.wrapperContainer) .append("svg") .style({ ...style }) .attr("width", width) .attr("height", height); } getSize(): IBoxSize { if (this.svg) { // Notice: it's SVGSVGElement const node = this.svg.node() as SVGSVGElement; const box = node.getBoundingClientRect(); return box; } return { width: 0, height: 0, }; } public addWrapperStyle(styles: { [key: string]: string }) { Object.keys(styles).map(key => { this.wrapperContainer.style.setProperty(key, styles[key]); }); } }
下一篇,开始添加咱们的图像~~svg