d3常用API及使用心得


1、记得用svg标签包裹元素,否则查看元素属性都正确,显示却是一片空白。

2、应该使用第一种写法,第二种写法会导致不居中显示,整个图在左上角。

let svg = d3.select("#basic-topo");
let clientRect = svg.node().getBoundingClientRect(),
    width = +clientRect.width,
    height = +clientRect.height;
let svg = d3.select("#basic-topo"),  
    width = +svg.attr("width"),  // 第二种写法
    height = +svg.attr("height");

第三种写法,用类来封装

let TopologyGraphLayout = function(vm, selector) {
  var self = this;
  this.vm = vm;

  this.width = $(selector).width();
  this.height = $(selector).height();
    
  this.zoom = d3.zoom().on("zoom", this.zoomed.bind(this)); // 监听缩放事件
  this.svg = d3
    .select(selector)
    .append("svg")
    .attr("width", this.width)
    .attr("height", this.height)
    .call(this.zoom)
    .on("dblclick.zoom", null);
}

3、ID选择器不要使用".",否则在使用 selection.select 时会报错


CSS的命名规范中,名称不能以数字开始,只能以字母、连字符、下划线开始。之后可以是字母、连字符、下划线或数字。


4、将对象转为数组的方法

  • d3.keys - 列举关联数组的键。
  • d3.values - 列举关联数组的值。
  • d3.entries - 列举关联数组的键值对实体。


5、映射


6、集合


7、选择元素

d3选择,插入, 删除元素


8、修改元素


9、数据绑定

理解update,enter,exit的使用



猜你喜欢

转载自blog.csdn.net/u010238381/article/details/80527812
D3
今日推荐