javascript - D3 中的多个省略号不可见且没有错误

标签 javascript d3.js svg charts graphics

我编写的一个简单的多个椭圆程序。程序没有显示错误,但也没有看到椭圆。虽然我尝试为其添加多种随机颜色。我认为某处有一些小错误。有人可以帮忙吗?

片段:

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"></script>

<script> 
$(document).ready(function(){

    //our basic data 
     var customData = [
        { "x": 30, "y": 30, "width": 20, "height" : 10 },
        { "x": 70, "y": 70, "width": 20, "height" : 20},
        { "x": 110, "y": 100, "width": 20, "height" : 30}
     ];

    //Make an SVG Container
    var mySVG = d3.select("svg");

    //create ellipses skeleton by data  
    var ellipses = mySVG.selectAll("ellipse")
            .data(customData)
            .enter()
            .append("ellipse");

    //Draw the Rectangle
    ellipses.append("ellipse")
        .attr("cx", function (d) { return d.x; })
        .attr("cy", function (d) { return d.y; })
        .attr("rx", function (d) { return d.width; })
        .attr("ry", function(d) { return d.height; })
        .attr("fill",function() { return "hsl(" + Math.random() * 360 + ",100%,50%)"; });

});

</script>   
</head>
<body>
    <svg width="500px" height="500px"></svg>
</body>
</html>

最佳答案

您正在追加椭圆元素两次。这是工作片段。

var customData = [{
  "x": 30,
  "y": 30,
  "width": 20,
  "height": 10
}, {
  "x": 70,
  "y": 70,
  "width": 20,
  "height": 20
}, {
  "x": 110,
  "y": 100,
  "width": 20,
  "height": 30
}];

//Make an SVG Container
var mySVG = d3.select("svg");

//create ellipses skeleton by data  
var ellipses = mySVG.selectAll("ellipse")
  .data(customData)
  .enter()
  .append("ellipse");

// Removed second append from here

//Draw the Rectangle
ellipses.attr("cx", function(d) {
    return d.x;
  })
  .attr("cy", function(d) {
    return d.y;
  })
  .attr("rx", function(d) {
    return d.width;
  })
  .attr("ry", function(d) {
    return d.height;
  })
  .attr("fill", function() {
    return "hsl(" + Math.random() * 360 + ",100%,50%)";
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<svg width="500px" height="500px"></svg>

关于javascript - D3 中的多个省略号不可见且没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40521530/

相关文章:

javascript - 如何使用 jQuery 扩展方法为元素或类选择器创建自定义插件

d3.js - d3 墨卡托、地理和路径方法

javascript - D3 转换 : duration doesn't work for line path

android - 如何从位图区域获取多边形形状

java - 检查字符串是否包含 “viewBox”,如果不是,我想将其添加到正确的位置

javascript - 如何将圆附加到 svg 文件?

javascript - 按宽度(百分比)更改div的背景颜色

javascript - cordova.exec() 正在破坏我的应用程序代码

javascript - 使用 JavaScript 添加删除类的最有效方法

javascript - d3.js 根据数据值附加不同的 SVG 元素