【问题标题】:SVG Adding radial gradient to donut chartSVG向圆环图添加径向渐变
【发布时间】:2014-02-17 11:49:22
【问题描述】:

我正在使用 d3.js 绘制图表。
是否可以在圆环图上添加径向渐变,这张图怎么样?

【问题讨论】:

  • This 可能会对您有所帮助。

标签: javascript svg d3.js gradient donut-chart


【解决方案1】:

假设弧形部分是填充的路径元素,您可以使用径向渐变来获得该结果。

this similar question,我们可以重用例子到达:

var dataset = {
  apples: [53245, 28479, 19697, 24037, 40245],
};

var width = 460,
    height = 300,
    radius = Math.min(width, height) / 2;

var color = d3.scale.category20();

var pie = d3.layout.pie()
    .sort(null);

var arc = d3.svg.arc()
    .innerRadius(radius - 100)
    .outerRadius(radius - 50);

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

var grads = svg.append("defs").selectAll("radialGradient").data(pie(dataset.apples))
    .enter().append("radialGradient")
    .attr("gradientUnits", "userSpaceOnUse")
    .attr("cx", 0)
    .attr("cy", 0)
    .attr("r", "100%")
    .attr("id", function(d, i) { return "grad" + i; });
grads.append("stop").attr("offset", "15%").style("stop-color", function(d, i) { return color(i); });
grads.append("stop").attr("offset", "20%").style("stop-color", "white");
grads.append("stop").attr("offset", "27%").style("stop-color", function(d, i) { return color(i); });

var path = svg.selectAll("path")
    .data(pie(dataset.apples))
  .enter().append("path")
    .attr("fill", function(d, i) { return "url(#grad" + i + ")"; })
    .attr("d", arc);

Jsfiddle:http://jsfiddle.net/X8hfm/

【讨论】:

  • 不错的解决方案,我昨天在干净的 svg jsfiddle.net/Gf3w8/5 上做了一些相同的事情
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-22
相关资源
最近更新 更多