格式化我的圆环图的图例

时间:2017-07-18 11:13:33

标签: d3.js

我尝试使用D3js创建此图表:

donut chart

我不知道如何制作我的&#34;%&#34;像<sup>标签(图片)一样小。我试图从format()删除后缀......它给我一个浮动。

我该怎么做?我是否需要从当前文本中进行子串并添加新文本?什么是更好的方式?

代码:

&#13;
&#13;
makeDonut(1, 45);

  function makeDonut(id, percent) {
    var duration = 2000,
      transition = 200,
      width = 180,
      height = 180;

    var dataset = {
        lower: calcPercent(0),
        upper: calcPercent(percent)
      },
      radius = Math.min(width, height) / 3,
      pie = d3.pie().sort(null),
      format = d3.format(".0%");

    var arc = d3.arc()
      .innerRadius(radius * .8)
      .outerRadius(radius / .7);

    var svg = d3.select("#graph" + id).append("svg")
      .attr("width", width)
      .attr("height", height)
      .append("g")
      .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

    var path = svg.selectAll("path")
      .data(pie(dataset.lower))
      .enter().append("path")
      .attr("class", function(d, i) {
        return "g" + id + "-color" + i
      })
      .attr("d", arc)
      .each(function(d) {
        this._current = d;
      });

    var text = svg.append("text")
      .attr("class", "g" + id + "-text")
      .attr("text-anchor", "middle")
      .attr("dy", ".3em");

    var progress = 0;

    var timeout = setTimeout(function() {
      clearTimeout(timeout);
      path = path.data(pie(dataset.upper));
      path.transition().duration(duration).attrTween("d", function(a) {
        var i = d3.interpolate(this._current, a);
        var i2 = d3.interpolate(progress, percent)
        this._current = i(0);
        return function(t) {
          text.text(format(i2(t) / 100));
          return arc(i(t));
        };
      });
    }, 200);
  };

function calcPercent(percent) {
  return [percent, 100 - percent];
};
&#13;
.g1-color0 {
  fill: #5ca747;
}

.g1-color1 {
  fill: #dcdcdc;
}

.g1-text {
  font-family: 'Roboto Condensed', sans-serif;
  fill: #5ca747;
  font-size: 50px;
  font-weight: bold;
}

.graph1 span {
  font-family: 'Roboto Condensed', sans-serif;
  text-transform: uppercase;
  font-size: 26px;
  color: #5ca747;
}
&#13;
<script src="https://d3js.org/d3.v4.min.js"></script>
<div class="graph1 col-sm-4 text-center">
  <div id="graph1"></div>
  <span>3 projecten klaar</span>
</div>
&#13;
&#13;
&#13;

我创建了JSFiffle

1 个答案:

答案 0 :(得分:1)

一个简单(懒惰)的解决方案就是为%符号创建另一个文本选择:

var percentText = svg.append("text")
    .attr("class", "g" + id + "-percent")
    .attr("text-anchor", "middle")
    .attr("dx", "1.2em")
    .attr("dy", "-0.3em")
    .text("%");

以下是演示:

&#13;
&#13;
makeDonut(1, 45);

  function makeDonut(id, percent) {
    var duration = 2000,
      transition = 200,
      width = 180,
      height = 180;

    var dataset = {
        lower: calcPercent(0),
        upper: calcPercent(percent)
      },
      radius = Math.min(width, height) / 3,
      pie = d3.pie().sort(null);

    var arc = d3.arc()
      .innerRadius(radius * .8)
      .outerRadius(radius / .7);

    var svg = d3.select("#graph" + id).append("svg")
      .attr("width", width)
      .attr("height", height)
      .append("g")
      .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

    var path = svg.selectAll("path")
      .data(pie(dataset.lower))
      .enter().append("path")
      .attr("class", function(d, i) {
        return "g" + id + "-color" + i
      })
      .attr("d", arc)
      .each(function(d) {
        this._current = d;
      });

    var text = svg.append("text")
      .attr("class", "g" + id + "-text")
      .attr("text-anchor", "middle")
      .attr("dy", ".3em");
      
    var percentText = svg.append("text")
      .attr("class", "g" + id + "-percent")
      .attr("text-anchor", "middle")
      .attr("dx", "1.2em")
      .attr("dy", "-0.3em")
      .text("%");

    var progress = 0;

    var timeout = setTimeout(function() {
      clearTimeout(timeout);
      path = path.data(pie(dataset.upper));
      path.transition().duration(duration).attrTween("d", function(a) {
        var i = d3.interpolate(this._current, a);
        var i2 = d3.interpolate(progress, percent)
        this._current = i(0);
        return function(t) {
        if(~~(i2(t))>=10){percentText.attr("dx", "1.8em")}
          text.text(~~(i2(t)));
          return arc(i(t));
        };
      });
    }, 200);
  };

function calcPercent(percent) {
  return [percent, 100 - percent];
};
&#13;
.g1-color0 {
  fill: #5ca747;
}

.g1-color1 {
  fill: #dcdcdc;
}

.g1-text {
  font-family: 'Roboto Condensed', sans-serif;
  fill: #5ca747;
  font-size: 50px;
  font-weight: bold;
}

.g1-percent {
  font-family: 'Roboto Condensed', sans-serif;
  fill: #5ca747;
  font-size: 20px;
  font-weight: bold;
}

.graph1 span {
  font-family: 'Roboto Condensed', sans-serif;
  text-transform: uppercase;
  font-size: 26px;
  color: #5ca747;
}
&#13;
<script src="https://d3js.org/d3.v4.min.js"></script>
<div class="graph1 col-sm-4 text-center">
  <div id="graph1"></div>
  <span>3 projecten klaar</span>
</div>
&#13;
&#13;
&#13;