我正在尝试使用d3.js v4获取
x
和y
的值来创建圆。使用以下代码,我成功地创建了圆形的图表,但是当我尝试在v4中运行相同的代码时,它不再起作用。我知道在升级到v4时存在一些差异,但我没有找到任何相关信息。所以我想知道是否有人可以帮助我在d3.js v4中运行此代码。
以下是使用v3的代码(在v4中会出错):
var svg = d3.select('body').append('svg')
.attr('width', 250)
.attr('height', 250);
//render the data
function render(data) {
//Bind
var circles = svg.selectAll('circle').data(data);
//Enter
circles.enter().append('circle')
.attr('r', 10);
//Update
circles
.attr('cx', function(d) {
return d.x;
})
.attr('cy', function(d) {
return d.y;
});
//Exit
circles.exit().remove();
}
var myObjects = [{
x: 100,
y: 100
}, {
x: 130,
y: 120
}, {
x: 80,
y: 180
}, {
x: 180,
y: 80
}, {
x: 180,
y: 40
}];
render(myObjects);
<script src='https://d3js.org/d3.v3.min.js'></script>