D3 Exploration - Step 1
D3入门学习1
创建SVG容器
- svg的相关内容: MDN
基本使用
const svg = d3 .select('div') // other CSS Selectors~~ .append("svg") .style({ backgound: 'pink'' }) .attr("width", '80vw') .attr("height", '90vh');
效果:
封装 D3Svg 类
用法
new D3Svg([opts]);
opts.xx | 说明 |
---|---|
container | 装载svg元素的容器元素,默认在body元素创建一个空的div容器 |
width | svg容器的宽度 |
height | svg容器的高度 |
style | svg容器的样式配置 |
wrapperContainerStyle | 最外层容器的样式配置 |
- Creates and returns a svg element that is attached to elem. elem can be a DOM object or a selector.
- The opts object will set the width and height of the svg if set, otherwise these attributes will remain null.
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]); }); } }
下一篇,开始添加我们的图像~~
相关推荐
mapaler 2020-07-17
MIKUScallion 2020-07-05
Dickzeng 2020-07-05
wallowyou 2020-06-28
hermanncain 2020-06-25
mapaler 2020-06-21
Dickzeng 2020-06-17
程序员俱乐部 2020-06-11
lanzhusiyu 2020-06-09
hermanncain 2020-05-09
Elena0 2020-04-11
Leonwey 2020-04-11
wallowyou 2020-03-05
jinxiutong 2020-02-10
jinxiutong 2020-02-03
mqbeauty 2020-01-08
mapaler 2020-01-17
Elena0 2020-01-12
mapaler 2020-01-05