html - 不使用 CSS 或 JS 创建 SVG 饼图的数学方法

标签 html math svg charts geometry

我想用纯 SVG 创建饼图。我不想使用 JS 或 CSS,本网站上的大多数解决方案都使用它们。我看到这篇很棒的文章,它解释了如何在纯 SVG 中创建饼图:https://seesparkbox.com/foundry/how_to_code_an_SVG_pie_chart

问题是这篇文章只描述了如何只制作一片。我正在寻求创建一个最多可包含 360 个元素的饼图(其中每个饼图将占饼图的 0.27%)。

在以下示例中,我试图通过将它旋转到 -89 而不是 -90 来创建另一个楔形,但我没有得到我正在寻找的结果:https://codepen.io/HexylCinnamal/pen/KKwEjpK

<svg xmlns="http://www.w3.org/2000/svg" height="100%" width="100%" viewBox="0 0 20 20">
<circle r="10" cx="10" cy="10" fill="transparent"/>
<circle r="5" cx="10" cy="10" fill="transparent" stroke="tomato" stroke-width="10"
    stroke-dasharray="calc(1 * 31.4 / 100) 31.4" transform="rotate(-90) translate(-20)"/>
<circle r="5" cx="10" cy="10" fill="transparent" stroke="blue" stroke-width="10"
    stroke-dasharray="calc(1 * 31.4 / 100) 31.4" transform="rotate(-89) translate(-20)"/>
</svg>

我想知道是否需要做任何数学运算来计算正确的角度和平移,以使蓝色楔形出现在红色楔形旁边。

最佳答案

不幸的是,用于计算属性 stroke-dasharraycalc() 仅适用于 Chrome

对于跨浏览器的解决方案,需要在stroke-dasharray中进行计算和赋值。

stroke-dasharray ="Circumference * 0.35, Circumference"stroke-dasharray = "31.4 * 0.35, 31.4"stroke-dasharray="10.99 31.4"

<svg height="20%" width="20%" viewBox="0 0 20 20" style="border:1px solid gray; ">
  <circle r="10" cx="10" cy="10" fill="white" />
  <circle r="5" cx="10" cy="10" fill="bisque"
          stroke="tomato"
          stroke-width="10"
          stroke-dasharray="10.99 31.4" />
</svg>

对于两个段:

  1. 红色="35%"
  2. blue="15%"stroke-dasharray = 31.4 * 0.15, 31.4stroke-dasharray ="4.71, 31.4"

<svg height="20%" width="20%" viewBox="0 0 20 20" style="border:1px solid; ">
  <circle r="10" cx="10" cy="10" fill="white" />
  <circle r="5" cx="10" cy="10" fill="bisque"
          stroke="tomato"
          stroke-width="10"
          stroke-dasharray="10.99 31.4" />
  <circle r="5" cx="10" cy="10" fill="bisque"
          stroke="dodgerblue"
          stroke-width="10"
          stroke-dasharray="4.71 31.4" />	  
</svg>

我们看到蓝色部分与红色部分重叠;因此,有必要将蓝色扇区移动等于红色扇区长度的量 10.99

添加移动蓝色扇区 stroke-dashoffset="-10.99"

<svg height="20%" width="20%" viewBox="0 0 20 20" style="border:1px solid; ">
   <circle r="5" cx="10" cy="10" fill="bisque" /> 
  <circle r="5" cx="10" cy="10" fill="transparent"
          stroke="tomato"
          stroke-width="10"
          stroke-dasharray="10.99 31.4" />
  <circle r="5" cx="10" cy="10" fill="transparent"
          stroke="dodgerblue"
          stroke-width="10"
          stroke-dasharray="4.71 31.4"
		  stroke-dashoffset="-10.99"
		  />	  
 
</svg>

四个部门

该解决方案适用于所有现代浏览器,包括 MS Edge

<!-- https://seesparkbox.com/foundry/how_to_code_an_SVG_pie_chart -->
<svg height="20%" width="20%" viewBox="0 0 20 20" style="border:1px solid; ">
   <circle r="5" cx="10" cy="10" fill="bisque" /> 
  <circle r="5" cx="10" cy="10" fill="transparent"
          stroke="tomato"
          stroke-width="10"
          stroke-dasharray="10.99 31.4" />
		  
  <circle r="5" cx="10" cy="10" fill="transparent"
          stroke="dodgerblue"
          stroke-width="10"
          stroke-dasharray="4.71 31.4"
		  stroke-dashoffset="-10.99"
		  />	  
  <circle r="5" cx="10" cy="10" fill="transparent"
          stroke="gold"
          stroke-width="10"
          stroke-dasharray="9.42 31.4"
		  stroke-dashoffset="-15.7"
		  />	  		  
   <circle r="5" cx="10" cy="10" fill="transparent"
          stroke="yellowgreen"
          stroke-width="10"
          stroke-dasharray="6.28 31.4"
		  stroke-dashoffset="-25.12"
		  />	  
  <text x="10" y="15" font-size="3px" fill="black" >35%</text>	
    <text x="1" y="14" font-size="3px" fill="black" >15%</text> 
     <text x="4" y="6" font-size="3px" fill="black" >30%</text>	
	   <text x="12" y="8" font-size="3px" fill="black" >20%</text>	
</svg>

关于html - 不使用 CSS 或 JS 创建 SVG 饼图的数学方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59921845/

相关文章:

html - 了解 CSS 字母间距 : is it valid to replace the default value of normal with 0?

html - 将轮播高度调整为图像高度(响应式)

javascript - 用一个类计算 div 的数量

python - 用 Pygame 画抛物线

javascript - react .js : Non-CSS animations

javascript - 未捕获的类型错误 : Cannot read property 'classList' of null at HTMLImageElement

javascript - 如何在刻度值中创建具有可变刻度间隔且刻度值之间具有相同距离的左轴

c - 如何打印 float 的精确值?

css - 如何使用 SVG 图像作为背景?

javascript - Angular uiGrid : D3 CellTemplate not sorting