简体   繁体   English

将 border-radius 属性添加到 D3js 圆环图

[英]Add border-radius property to D3js Donut Chart

I have this donut chart currently working in an AngularJS app:我有这个圆环图目前在一个 AngularJS 应用程序中工作:

在此处输入图片说明

But the design mockup says we would like this, note the border-radius property on the green portion of the arc:但是设计模型说我们想要这个,注意圆弧绿色部分的边界半径属性:

在此处输入图片说明

How do I add a border-radius to the SVG that d3js outputs, the code I'm currently using looks like this:如何向 d3js 输出的 SVG 添加边框半径,我目前使用的代码如下所示:

let data = [
    {
        label: 'Data',
        count: scope.data
    },
    {
        label: 'Fill',
        count: 100 - scope.data
    }
];

let width = 60;
let height = 60;
let radius = Math.min(width, height) / 2;
let color = d3.scale
    .ordinal()
    .range(['#3CC692', '#F3F3F4']);
let selector = '#donut-asset-' + scope.chartId;

d3
    .select(selector)
    .selectAll('*')
    .remove();

let svg = d3
    .selectAll(selector)
    .append('svg')
    .attr('width', width)
    .attr('height', height)
    .append('g')
    .attr(
        'transform',
        'translate(' + width / 2 + ',' + height / 2 + ')'
    );
let arc = d3.svg
    .arc()
    .innerRadius(23)
    .outerRadius(radius);
let pie = d3.layout
    .pie()
    .value(function(d) {
        return d.count;
    })
    .sort(null);
let path = svg
    .selectAll('path')
    .data(pie(data))
    .enter()
    .append('path')
    .attr('d', arc)
    .attr('fill', function(d, i) {
        return color(d.data.label);
    });
let legend = svg
    .selectAll('.legend')
    .data(data)
    .enter()
    .append('g')
    .attr('class', 'legend')
    .attr('transform', function(d, i) {
        return 'translate(' + 0 + ',' + 0 + ')';
    });
legend
    .append('text')
    .attr('x', 1)
    .attr('y', 1)
    .attr('text-anchor', 'middle')
    .attr('dominant-baseline', 'central')
    .text(function(d) {
        return d.count + '%';
    });

}; };

I know to use cornerRadius but when I do it sets a radius for both arcs, it just needs to exist on the colored one.我知道要使用cornerRadius,但是当我这样做时,它会为两个弧设置一个半径,它只需要存在于有颜色的弧上。 Any ideas?有任何想法吗? Thanks in advance for any help!在此先感谢您的帮助!

You can apply a corner radius to a d3 arc which allows rounding on the corners:您可以将拐角半径应用于允许在拐角处倒圆的 d3 弧:

let arc = d3.svg
    .arc()
    .innerRadius(23)
    .outerRadius(radius)
    .cornderRadius(10);

But, the downside is that all arcs' borders are rounded:但是,缺点是所有弧的边界都是圆角的:

在此处输入图片说明

If you apply the cornerRadius to only the darkened arc - the other arc won't fill in the background behind the rounded corners.如果您仅将 cornerRadius 应用于变暗的弧线 - 另一条弧线将不会填充圆角后面的背景。 Instead, we could append a circular arc (full donut) and place the darkened arc on top with rounding (my example doesn't adapt your code, just shows how that it can be done, also with d3v4 which uses d3.arc() rather than d3.svg.arc() ):相反,我们可以附加一个圆弧(完整的甜甜圈)并将变暗的圆弧放在顶部并带有舍入(我的示例不适应您的代码,只是展示了如何做到这一点,还有使用d3.arc()而不是d3.svg.arc() ):

 var backgroundArc = d3.arc() .innerRadius(30) .outerRadius(50) .startAngle(0) .endAngle(Math.PI*2); var mainArc = d3.arc() .innerRadius(30) .outerRadius(50) .cornerRadius(10) .startAngle(0) .endAngle(function(d) { return d/100*Math.PI* 2 }); var data = [10,20,30,40,50] // percents. var svg = d3.select("body").append("svg") .attr("width", 600) .attr("height", 200); var charts = svg.selectAll("g") .data(data) .enter() .append("g") .attr("transform",function(d,i) { return "translate("+(i*100+50)+",100)"; }); charts.append("path") .attr("d", backgroundArc) .attr("fill","#ccc") charts.append("path") .attr("d", mainArc) .attr("fill","orange")
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script>

Try playing with stroke attributes like:尝试使用笔画属性,例如:

  • stroke中风
  • stroke-dasharray笔画-dasharray
  • stroke-dashoffset中风-dashoffset
  • stroke-linecap笔画线帽
  • stroke-linejoin笔划线连接
  • stroke-miterlimit行程限制
  • stroke-opacity中风不透明度
  • stroke-width笔画宽度

And set width of bar to lower values, or 0.并将条形的宽度设置为较低的值,或 0。

Reference: https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute参考: https : //developer.mozilla.org/en-US/docs/Web/SVG/Attribute

But the better way is to make charts on canvas, because you can draw everything you want.但更好的方法是在画布上制作图表,因为你可以画出你想要的一切。 Or to use an library.或者使用图书馆。

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

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