SVG饼图与ruby

我试图生成一小段代码,为我构建一个SVG饼图,如下所示:

# piechart-svg.rb: BEGIN { @colors = [] both = %w(coral grey green salmon blue seagreen slategrey cyan) darks = %w(magenta red olivegreen orange turquoise goldenrod khaki orchid slateblue violet) lights = %w(steelblue yellow skyblue pink goldenrodyellow) @colors << both.map {|c| [c, "light#{c}", "dark#{c}"] } @colors << darks.map {|c| [c, "dark#{c}"] } @colors << lights.map {|c| [c, "light#{c}"] } @colors.flatten! @radius = 100.0 @prevcoord = "#{@radius},0" puts <<-EOF     .wedge { stroke: darkgrey; stroke-linejoin: round; fill-opacity: .2; }   EOF } ARGV.each_with_index do |percent,index| print " \n" @prevcoord = "#{x},#{y}" end END { puts "  " } 

问题是,如果我喂它,比如50%( ruby piechart-svg.rb 50 ),产生的楔形不到圆的一半。 我不明白为什么。

你的度数会向后转换弧度。 应该:

 x = Math::cos(angle_as_degrees * Math::PI / 180) * @radius y = Math::sin(angle_as_degrees * Math::PI / 180) * @radius 

请注意,如果任何切片大于50%,您仍然会遇到问题,但这与此问题是分开的。 🙂