【问题标题】:How to make indents between sectors in donut chart?如何在圆环图中的扇区之间进行缩进?
【发布时间】:2021-02-22 13:19:48
【问题描述】:

这是我的代码:

const innerRadius = 40;
const colorSmooth = 300; //smooth transition of colors (resolution)
const innerText = "Chart";

// values in perсents
var percentsGreat = 50;
var percentsGood = 20;
var percentsNormal = 20;
var percentsBad = 10;

// "start", "end" arcs colors pair per value
var greatStartColor = "#FFE39C";
var greatEndColor = "#FFBA9C";
var goodStartColor = "#6FCF97";
var goodEndColor = "#66D2EA";
var normalStartColor = "#BC9CFF";
var normalEndColor = "#8BA4F9";
var badStartColor = "#919191";
var badEndColor = "#3D4975";


// define the workspace and set the origin to its center
var svg = d3.select("svg"),
  width = svg.attr("width"),
  height = svg.attr("height"),
  g = svg.append("g").attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

var radius = d3.min([width, height]) / 2;

var arc = d3.arc()
  .innerRadius(radius - innerRadius)
  .outerRadius(radius)
  .startAngle(function(d) {
    return d;
  })
  .endAngle(function(d) {
    return d + dblpi / colorSmooth * 1.2;
  }); // 1.2 to block artifacts on the transitions

var great = colorSmooth / 100 * percentsGreat;
var good = colorSmooth / 100 * percentsGood + great;
var normal = colorSmooth / 100 * percentsNormal + good;
var bad = colorSmooth / 100 * percentsBad + normal;

var dblpi = 2 * Math.PI;

var color = d3.scaleLinear()
  .domain([0, great,
    great, good,
    good, normal,
    normal, bad
  ])
  .range([greatStartColor, greatEndColor,
    goodStartColor, goodEndColor,
    normalStartColor, normalEndColor,
    badStartColor, badEndColor
  ])
  .interpolate(d3.interpolateRgb)

g.selectAll("path")
  .data(d3.range(0, dblpi, dblpi / colorSmooth))
  .enter()
  .append("path")
  .attr("d", arc)
  .style("fill", function(d, i) {
    return color(i);
  });

g.append("text")
  .attr("class", "percent-score")
  .attr("dy", ".35em") // align the text to the center of the chart
  .attr("text-anchor", "middle")
  .text(innerText);

// svg.attr("stroke", "white")
//  .style("stroke-width", "2px");
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg width="500" height="400"></svg>

如何在扇区之间进行缩进?就像这张照片:

我找到了一个无渐变图表的解决方案(最后两行),但它不适用于渐变。

【问题讨论】:

    标签: javascript svg d3.js donut-chart


    【解决方案1】:

    惯用的 D3 使用 padAngle:

    pad 角度转换为分隔相邻圆弧的固定线性距离,定义为 padRadius * padAngle。该距离从弧的起点和终点平均减去。

    但是,由于您创建渐变的方式很奇怪(使用多条路径),这里我使用阈值比例来比较索引:

    .padAngle((_, i) => color2(i) === color2(i + 1) ? 0 : 1)
    

    即使这样,您也会发现以弧度表示的角度不正确。也就是说,我建议您改变方法(每个类别只使用 一个 路径)。

    这是您的更改代码:

    const innerRadius = 40;
    const colorSmooth = 300; //smooth transition of colors (resolution)
    const innerText = "Chart";
    
    // values in perсents
    var percentsGreat = 50;
    var percentsGood = 20;
    var percentsNormal = 20;
    var percentsBad = 10;
    
    // "start", "end" arcs colors pair per value
    var greatStartColor = "#FFE39C";
    var greatEndColor = "#FFBA9C";
    var goodStartColor = "#6FCF97";
    var goodEndColor = "#66D2EA";
    var normalStartColor = "#BC9CFF";
    var normalEndColor = "#8BA4F9";
    var badStartColor = "#919191";
    var badEndColor = "#3D4975";
    
    
    // define the workspace and set the origin to its center
    var svg = d3.select("svg"),
      width = svg.attr("width"),
      height = svg.attr("height"),
      g = svg.append("g").attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
    
    var radius = d3.min([width, height]) / 2;
    
    var arc = d3.arc()
      .innerRadius(radius - innerRadius)
      .outerRadius(radius)
      .padAngle((_, i) => color2(i) === color2(i + 1) ? 0 : 1)
      .startAngle(function(d) {
        return d;
      })
      .endAngle(function(d) {
        return d + dblpi / colorSmooth * 1.2;
      }); // 1.2 to block artifacts on the transitions
    
    var great = colorSmooth / 100 * percentsGreat;
    var good = colorSmooth / 100 * percentsGood + great;
    var normal = colorSmooth / 100 * percentsNormal + good;
    var bad = colorSmooth / 100 * percentsBad + normal;
    
    var dblpi = 2 * Math.PI;
    
    var color = d3.scaleLinear()
      .domain([0, great,
        great, good,
        good, normal,
        normal, bad
      ])
      .range([greatStartColor, greatEndColor,
        goodStartColor, goodEndColor,
        normalStartColor, normalEndColor,
        badStartColor, badEndColor
      ])
      .interpolate(d3.interpolateRgb);
    
    const color2 = d3.scaleThreshold()
      .domain([great, good, normal, normal, bad])
      .range(d3.range(6));
    
    g.selectAll("path")
      .data(d3.range(0, dblpi, dblpi / colorSmooth))
      .enter()
      .append("path")
      .attr("d", arc)
      .style("fill", function(d, i) {
        return color(i);
      });
    
    g.append("text")
      .attr("class", "percent-score")
      .attr("dy", ".35em") // align the text to the center of the chart
      .attr("text-anchor", "middle")
      .text(innerText);
    
    // svg.attr("stroke", "white")
    //  .style("stroke-width", "2px");
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
    <svg width="500" height="400"></svg>

    【讨论】:

      【解决方案2】:

      .attr("stroke", "white").attr("stoke-width", "5px") 添加到您的弧定义将在弧之间添加缩进扇区(空白)。

      g.selectAll("path")
            .data(d3.range(0, dblpi, dblpi / colorSmooth))
            .enter()
            .append("path")
            .attr("d", arc)
            .attr("stroke", "white")
            .attr("stoke-width", "5px")
            .style("fill", function(d, i) {
              return color(i);
            });
      

      注意:您正在使用多个颜色范围来创建渐变效果,这将导致为每个颜色值生成多个弧线,从而导致https://codepen.io/mont8593/pen/mdOBLdd

      您可以参考Adding gradient to 3rd party solid color donut chart 在您的圆环图中实现渐变。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-20
        • 2014-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多