简体   繁体   English

如何使用d3.js将填充的部分添加到SVG圈

[英]How to add filled sections to SVG circles using d3.js

I am generating some SVG circles using d3.js. 我正在使用d3.js生成一些SVG圈子。 I'm able to generate them, but I can't figure how to divide them into 4 equal sections and color fill each section. 我能够生成它们,但是我无法想象如何将它们分成4个相等的部分并填充每个部分的颜色。 I'm using version 4 of d3.js. 我正在使用d3.js的第4版。

Here is a snippet of my javascript from my fiddle: 这是我的小提琴中的一段javascript:

var nodes = [

  {"type":'family',"id":'f1',"name":'', "image":""},
  {"type":'person',"id":'p1',"name":'fred flintstone',"age": 39, "relationship": "father","sex":' '},
  {"type":'person',"id":'p2',"name":'wilma flintstone',"age": 36, "relationship": "mother","sex":'m'},
  {"type":'person',"id":'p3',"name":'pebbles flintstone',"age": 4 , "relationship": "daughter","sex":'mf'},
  {"type":'person',"id":'p4',"name":'dino',"age": 8 ,"relationship": "pet","sex":'m'},


  {"type":'family',"id":'f3',"name":'', "image":""},
  {"type":'person',"id":'p5',"name":'barney rubble',"age": 43, "relationship": "father","sex":'m'},
  {"type":'person',"id":'p6',"name":'betty rubble',"age": 41, "relationship": "mother","sex":'f'},
  {"type":'person',"id":'p7',"name":'bam bam rubble',"age": 4, "relationship": "son","sex":'m'},


]

//more code in my fiddle

  my.width = function(value) {
    if (!arguments.length) return width;
    width = value;
    return my;
  };

  my.nodes = function(value) {
    if (!arguments.length) return nodes;
    nodes = value;
    return my;
  };

  my.links = function(value) {
    if (!arguments.length) return links;
    links = value;
    return my;
  };

  my.height = function(value) {
    if (!arguments.length) return height;
    height = value;
    return my;
  };

  return my;
}

Thanks much in advance. 非常感谢提前。

https://jsfiddle.net/pqk8y3mb/ https://jsfiddle.net/pqk8y3mb/

I would recommend manually drawing the arcs with SVG's path . 我建议用SVG的路径手动绘制弧线。

First, here is a working example with the additions: https://jsfiddle.net/mztafs0w/ 首先,这是一个增加的工作示例: https//jsfiddle.net/mztafs0w/


Explanation: 说明:

SVG Path has commands such as M for M ove, A for A rc, L for draw L ine to: SVG路径具有诸如M for M ove,A for A rc,L for L L L的命令:

  • Capital letters are absolute pixel movements 大写字母是绝对像素移动
  • Lowercase letters are relative pixel movements 小写字母是相对像素移动

To make a filled pie slice using SVG path, you must perform these actions: 要使用SVG路径创建填充的饼图切片,必须执行以下操作:

如何画弧 image source 图像源


Let's say your radius is 40 and you want a slice for the top-right quadrant. 假设你的半径是40,你想要一个切片用于右上象限。 The entire command for this would be: 整个命令是:

  • Move X(0) Y(-40) -- move to top 移动 X(0)Y(-40) - 移至顶部
  • Arc X-Radius(40) Y-Radius(40) XRot(0) >180deg?(0) Sweep(1) X(40) Y(0) -- arc to right X-半径(40)Y-半径(40)XRot(0)> 180度?(0)扫描(1) X(40)Y(0) - 弧到右
  • Line X(0) Y(0) -- return to center 线 X(0)Y(0) - 返回中心

Compressed into svg path format, this appears as: 压缩成svg路径格式,显示为:

M 0 -40 A 40 40 0 0 1 40 0 L 0 0 (minimally, M0,-40A40,40,0,0,1,40,0L0,0 ) M 0 -40 A 40 40 0 0 1 40 0 L 0 0 (最低, M0,-40A40,40,0,0,1,40,0L0,0

Performing this 4 times to get all 4 quadrants is simple enough, and replacing radius with ${r} allows the size to be easily adjusted. 执行此操作4次以获得所有4个象限非常简单,使用$ {r}替换半径可以轻松调整大小。

The final code added to your JS fiddle: 最终的代码添加到你的JS小提琴:

var slices=[];
  slices[0] = node.append("path")
  .attr("d", function(d) {
    let r = d.type == "family" ? family_radius + 5 : 40;
    return `M 0 -${r} A ${r} ${r} 0 0 1 ${r} 0 L 0 0`; } )
  .attr("fill", "coral");
slices[1] = node.append("path")
    .attr("d", function(d) {
    let r = d.type == "family" ? family_radius + 5 : 40;
    return `M ${r} 0 A ${r} ${r} 0 0 1 0 ${r} L 0 0`; } )
  .attr("fill", "royalblue");
slices[2] = node.append("path")
  .attr("d", function(d) {
    let r = d.type == "family" ? family_radius + 5 : 40;
    return `M 0 ${r} A ${r} ${r} 0 0 1 -${r} 0 L 0 0`; } )
  .attr("fill", "olivedrab");
slices[3] = node.append("path")
  .attr("d", function(d) {
    let r = d.type == "family" ? family_radius + 5 : 40;
    return `M -${r} 0 A ${r} ${r} 0 0 1 0 -${r} L 0 0`; } )
  .attr("fill", "goldenrod");

I recommend you remove the non-working describeArc section and make the code more DRY . 我建议你删除不工作的describeArc部分并使代码更 You may perform more calculations to have the circle slices break at places other than 0/90/180/270. 您可以执行更多计算,以使圆形切片在0/90/180/270以外的位置处断开。 Let me know if you need help with any of those, or you may check the image source for more tips. 如果您需要任何帮助,请告诉我们,或者您可以查看图片来源以获取更多提示。

I also changed the family_radius to family_radius + 5 so you can see the arcs being drawn underneath the white fill of those smaller circles. 我还将family_radius更改为family_radius + 5,以便您可以看到在这些较小圆圈的白色填充下绘制的弧。 If this is not desirable you may either remove the white fill on these circles (line 165 if(d.type == "family"){return "white"} ) or simply not draw these slices at all for those circles. 如果这是不可取的,你可以删除这些圆圈上的白色填充(第165行if(d.type == "family"){return "white"} )或者根本不为这些圆圈绘制这些切片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM