我正在尝试构建一个非常简单的甜甜圈图。
这是工作代码:
const countries = [
{'percent': 2,colour: 'red'},
{'percent': 28,colour: 'blue'},
{'percent': 36,colour: 'yellow'},
{'percent': 34,colour: 'orange'}
];
const donutData = countries.map((country, index) => {
return {
stroke: country.colour,
dashoffset: 25 - countries.slice(0, index).reduce((a, b) => a + b.percent, 0),
dashArray: [country.percent, 100 - country.percent]
}
});
const div = document.createElement('div');
div.innerHTML = '<svg id="donut" width="100%" height="100%" viewBox="3 3 36 36"></svg>';
document.body.appendChild(div);
document.querySelector('#donut').innerHTML= donutData.reduce((a, item) => {
return a +
`<circle
cx="21"
cy="21"
fill="transparent"
r="15.91549430918954"
stroke-width="2.3"
stroke="${item.stroke}"
stroke-dasharray="${item.dashArray[0]} ${item.dashArray[1]}"
stroke-dashoffset="${item.dashoffset}"></circle>
`;
}, '')
https://jsfiddle.net/miladhi/1dxnkjht/1/
上面的方法工作正常,但是尝试将stroke-linecap="round"
添加到<circle>
上,它会变成奇特的形状,笔触彼此重叠。
如您在这里看到的https://jsfiddle.net/miladhi/x8w4kgdv/。
我可以理解问题,但不知道如何在笔划之间增加一点边距,以免造成难看的堆积。
我很感谢任何建议。
答案 0 :(得分:0)
这是您想要的吗?
只需从仪表板长度中减去圆形端盖的半径(每个端部一个)。
只要圆半径不是很小,圆帽就应该整齐地相互接触。
const countries = [
{'percent': 10,colour: 'red'},
{'percent': 20,colour: 'blue'},
{'percent': 36,colour: 'yellow'},
{'percent': 34,colour: 'orange'}
];
const STROKE_WIDTH = 2.3;
const donutData = countries.map((country, index) => {
// Subtract the radius of the round cap, twice.
const dashLength = country.percent - STROKE_WIDTH;
return {
stroke: country.colour,
dashoffset: 25 - countries.slice(0, index).reduce((a, b) => a + b.percent, 0),
dashArray: [dashLength, 100 - dashLength]
}
});
const div = document.createElement('div');
div.innerHTML = '<svg id="donut" width="100%" height="100%" viewBox="3 3 36 36"></svg>';
document.body.appendChild(div);
document.querySelector('#donut').innerHTML= donutData.reduce((a, item) => {
return a +
`<circle
cx="21"
cy="21"
fill="transparent"
r="15.91549430918954"
stroke-width="${STROKE_WIDTH}"
stroke-linecap="round"
stroke="${item.stroke}"
stroke-dasharray="${item.dashArray[0]} ${item.dashArray[1]}"
stroke-dashoffset="${item.dashoffset}"></circle>
`;
}, '')
更新
一个可以轻松处理短行长度的版本。
const countries = [
{'percent': 10, colour: 'red'},
{'percent': 20, colour: 'blue'},
{'percent': 36, colour: 'yellow'},
{'percent': 33, colour: 'orange'},
{'percent': 1, colour: 'green'},
];
const STROKE_WIDTH = 2.3;
const donutData = countries.map((country, index) => {
let dashLength, offsetAdjust, caps;
if (country.percent >= STROKE_WIDTH) {
// Subtract the radius of the round cap, twice.
dashLength = country.percent - STROKE_WIDTH;
offsetAdjust = STROKE_WIDTH / 2;
caps = "round";
} else {
dashLength = country.percent;
offsetAdjust = 0;
caps = "butt";
}
return {
stroke: country.colour,
dashoffset: 25 - countries.slice(0, index).reduce((a, b) => a + b.percent, 0) - offsetAdjust,
dashArray: [dashLength, 100 - dashLength],
caps: caps
}
});
const div = document.createElement('div');
div.innerHTML = '<svg id="donut" width="100%" height="100%" viewBox="3 3 36 36"></svg>';
document.body.appendChild(div);
document.querySelector('#donut').innerHTML= donutData.reduce((a, item) => {
return a +
`<circle
cx="21"
cy="21"
fill="transparent"
r="15.91549430918954"
stroke-width="${STROKE_WIDTH}"
stroke-linecap="${item.caps}"
stroke="${item.stroke}"
stroke-dasharray="${item.dashArray[0]} ${item.dashArray[1]}"
stroke-dashoffset="${item.dashoffset}"></circle>
`;
}, '')
答案 1 :(得分:0)
这是使用路径和标记进行操作的方法。诀窍是使用标记开始/标记结束组合。前一行的看似重叠实际上是作为标记的起点粘贴在当前行上。
<svg width="600px" height="400px">
<defs>
<marker id="round-cap-blue" viewBox="0 0 1 1"
markerWidth="1" markerHeight="1"
orient="auto" refX="0.5" refY="0.5">
<circle cx="0.5" cy="0.5" r="0.5" fill="blue"/>
</marker>
<marker id="round-cap-red" viewBox="0 0 1 1"
markerWidth="1" markerHeight="1"
orient="auto" refX="0.5" refY="0.5">
<circle cx="0.5" cy="0.5" r="0.5" fill="red"/>
</marker>
<marker id="round-cap-green" viewBox="0 0 1 1"
markerWidth="1" markerHeight="1"
orient="auto" refX="0.5" refY="0.5">
<circle cx="0.5" cy="0.5" r="0.5" fill="green"/>
</marker>
</defs>
<g transform="translate(100,0)">
<path fill="none" stroke="blue" stroke-width="30" d="M 150 150
A 100 100 0 0 0 50 50" marker-end="url(#round-cap-blue)"/>
<path fill="none" stroke="red" stroke-width="30" d="M 50 250
A 100 100 0 0 0 150 150" marker-end="url(#round-cap-red)"/>
<path fill="none" stroke="green" stroke-width="30" d="M 50 50
A 100 100 0 0 0 50 250" marker-start="url(#round-cap-blue)" marker-end="url(#round-cap-green)"/>
</g>
</svg>