javascript - donut chart 中的过渡颜色

标签 javascript d3.js charts

我有一个圆环图,显示评分(满分 10 分)。如果评分 > 5,则它将显示为红色,否则显示为绿色。我还在图表中添加了动画,以便评分逐渐增加。我需要做的唯一更改是,如果评级超过 5,则评级弧应该以绿色开始,然后在超过 5 时变为红色。

这是代码笔网址:https://codepen.io/javacodenet/pen/pKGmRx

var duration = 1500,
  transition = 200,
  percent = 7;
var width = 260;
var height = 260;
var thickness = 30;
var anglesRange = 0.75 * Math.PI
var dataset = {
    lower: calcPercent(0),
    upper: calcPercent(percent)
  },
  radius = Math.min(width, height) / 2;
var colors = ["#fb4f5d", "#efefef"]
var arc = d3.arc()
  .innerRadius(radius - thickness)
  .outerRadius(radius);

var pie = d3.pie()
  .value(function(d) {
    return d;
  })
  .sort(null)
  .startAngle(anglesRange * -1)
  .endAngle(anglesRange);

var svg = d3.select("#chart").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('fill', '#efefef')
  .attr("d", arc)
  .each(function(d) {
    this._current = d;
  });

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

var progress = 0;
format = d3.format("0");
var timeout = setTimeout(function() {
  clearTimeout(timeout);
  path = path.data(pie(dataset.upper))
    .attr('fill', (d, i) => {
      return i == 0 ? (d.value > 5 ? '#fb4f5d' : '#00FF00') : '#efefef';
    });
  path.transition().duration(duration).attrTween("d", function(a) {
    var i = d3.interpolate(this._current, a);
    var i2 = d3.interpolateNumber(progress, percent)
    console.log(i2);
    this._current = i(0);
    return function(t) {
      text.text(Math.round(i2(t)));
      return arc(i(t));
    };
  });
}, 200);

function calcPercent(percent) {
  return [percent, 10 - percent];
};
body {
  width: 100%;
  height: 100%;
  font-family: Lora, "Helvetica Neue", Helvetica, Arial, sans-serif;
  color: #fff;
  background-color: #000;
}

path.color0 {
  fill: #fff;
}

path.color1 {
  fill: rgba(255, 255, 255, .3);
}

text {
  font-size: 7em;
  font-weight: 400;
  line-height: 16em;
  fill: #fff;
}
<html>
<head>
  <script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
  <div id="chart"></div>
</body>
</html>

最佳答案

您想要的结果并不十分清楚:您想要一个 <path> 吗?元素超过 5 时变为红色,或者你想要两个 <path>元素,一个从 0 到 5 涂成绿色,另一个从 5 到 7 涂成红色?

如果前者是正确的,只需移动"fill"attrTween里面方法。这是您进行更改后的代码:

var duration = 1500,
  transition = 200,
  percent = 7;
var width = 260;
var height = 260;
var thickness = 30;
var anglesRange = 0.75 * Math.PI
var dataset = {
    lower: calcPercent(0),
    upper: calcPercent(percent)
  },
  radius = Math.min(width, height) / 2;
var colors = ["#fb4f5d", "#efefef"]
var arc = d3.arc()
  .innerRadius(radius - thickness)
  .outerRadius(radius);

var pie = d3.pie()
  .value(function(d) {
    return d;
  })
  .sort(null)
  .startAngle(anglesRange * -1)
  .endAngle(anglesRange);

var svg = d3.select("#chart").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('fill', '#efefef')
  .attr("d", arc)
  .each(function(d) {
    this._current = d;
  });

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

var progress = 0;
format = d3.format("0");
var timeout = setTimeout(function() {
  clearTimeout(timeout);
  path = path.data(pie(dataset.upper));
  path.transition().duration(duration).attrTween("d", function(a, index) {
    var self = this;
    var i = d3.interpolate(this._current, a);
    var i2 = d3.interpolateNumber(progress, percent)
    this._current = i(0);
    return function(t) {
      d3.select(self).attr('fill', index !== 0 ? '#efefef' : i2(t) > 5 ? '#fb4f5d' : '#00FF00');
      text.text(Math.round(i2(t)));
      return arc(i(t));
    };
  });
}, 200);

function calcPercent(percent) {
  return [percent, 10 - percent];
};
body {
  width: 100%;
  height: 100%;
  font-family: Lora, "Helvetica Neue", Helvetica, Arial, sans-serif;
  color: #fff;
  background-color: #000;
}

path.color0 {
  fill: #fff;
}

path.color1 {
  fill: rgba(255, 255, 255, .3);
}

text {
  font-size: 7em;
  font-weight: 400;
  line-height: 16em;
  fill: #fff;
}
<html>

<head>
  <script src="https://d3js.org/d3.v5.min.js"></script>
</head>

<body>
  <div id="chart"></div>
</body>

</html>

关于javascript - donut chart 中的过渡颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51149323/

相关文章:

javascript - 如何根据节点名称设置节点链接颜色?

javascript - D3 实现 : custom topology image behind D3 map

javascript - 如何从 x 轴获取刻度的 X 值?

javascript - Chromium 错误 :- 6 Charts. js/Chartnew.js

javascript - 如果不是 Internet Explorer,则仅运行脚本

javascript - 如何使用 CSS :hover? 修改自身和其他类

javascript - 为什么我的 Promise 数组在调用 Promise.all() 之前运行?

javascript - 错误率: jquery file upload on submit form button

jquery - Chart.js - 悬停后回调

用于大型数据集的 javascript/html5 图表库