angularjs - 如何构建这个圆形的两色调 donut chart ?

标签 angularjs svg d3.js charts frontend

我正在尝试制作一个如下所示的 donut chart

Donut Chart

我将使用 angularJS、SVG 和 D3.js

我不知道如何得到那些圆形的末端,请帮助。谢谢。

最佳答案

简单的答案:你使用面具。

我们使用 mask 来绘制挡板的内部。还有一个面具可以在中间切开一个洞。

孔掩码并不是必需的。你可以用粗线形成 donut 。但对我来说,画扇形然后打洞似乎更容易。

这里是 SVG 格式。我将把转换为 D3 留给你。

<svg width="600" height="600">
    <defs>
        <!-- masks out the area outside and inside the inner bevel region -->
        <mask id="innerbevel">
            <rect width="100%" height="100%" fill="black"/>
            <circle cx="0" cy="0" r="235" fill="white"/>
        </mask>
        <!-- cuts hole in centre of graph -->
        <mask id="centrehole">
            <rect x="-100%" y="-100%" width="200%" height="200%" fill="white"/>
            <circle cx="0" cy="0" r="195" fill="black"/>
        </mask>
    </defs>

    <!-- Graph is drawn centred at (0,0). The transform moves it into middle of SVG. -->
    <!-- The mask forms the hole in the centre. -->
    <g transform="translate(300,300)" mask="url(#centrehole)">
        <!-- outer bevel -->
        <g>
            <!-- light blue segment -->
            <path d="M0 0 0 -275 A 275 275 0 0 1 0 275" fill="#89e4d2"/>
            <!-- red segment -->
            <path d="M0 0 0 275 A 275 275 0 0 1 -275 0" fill="#f394a2"/>
            <!-- blue segment -->
            <path d="M0 0 -275 0 A 275 275 0 0 1 0 -275" fill="#a3a4ff"/>

            <!-- light blue rounded end -->
            <circle cx="0" cy="235" r="40" fill="#89e4d2"/>
            <!-- red rounded end -->
            <circle cx="-235" cy="0" r="40" fill="#f394a2"/>
            <!-- blue rounded end -->
            <circle cx="0" cy="-235" r="40" fill="#a3a4ff"/>
        </g>
        <!-- inner bevel - same as above but with different colours and is masked -->
        <g mask="url(#innerbevel)">
            <!-- light blue segment -->
            <path d="M0 0 0 -275 A 275 275 0 0 1 0 275" fill="#5bc8b7"/>
            <!-- red segment -->
            <path d="M0 0 0 275 A 275 275 0 0 1 -275 0" fill="#ef6974"/>
            <!-- blue segment -->
            <path d="M0 0 -275 0 A 275 275 0 0 1 0 -275" fill="#6b5dff"/>

            <!-- light blue rounded end -->
            <circle cx="0" cy="235" r="40" fill="#5bc8b7"/>
            <!-- red rounded end -->
            <circle cx="-235" cy="0" r="40" fill="#ef6974"/>
            <!-- blue rounded end -->
            <circle cx="0" cy="-235" r="40" fill="#6b5dff"/>
        </g>
    </g>

</svg>

关于angularjs - 如何构建这个圆形的两色调 donut chart ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27666584/

相关文章:

javascript - 为什么 AngularJS 资源会返回 promise 和 resolved?

javascript - 如何在单击时打开 Ionic Modal?

css - 鼠标离开时悬停动画会抖动(使用 CSS 过渡和变换)

Javascript D3 不显示可视化

javascript - 添加时如何滚动到 li

javascript - Angular 在 orderBy 之后获取中间索引

javascript - 使用 D3.js 创建矩形图表

javascript - 如何使用 Snap.svg 和 Angular 5 在悬停时缩放不同的 svg 图层

d3.js - d3.nest 将键转为字符串值

javascript - D3 : When I add a transition, 我的鼠标悬停停止工作...为什么?