Refactoring2-10

派生値の自動生成.

変数はプログラム自身が決めるべき.

>|ruby| require 'svg1.5.0.rb'

class Sparkline

 def initialize(y_values)
   @height_above_x_axis = y_values.max
   @height_below_x_axis = y_values.min
   @final_value = y_values[-1]
   @y_values = reflect_top_and_bottom(y_values)
 end
 def reflect_top_and_bottom(y_values)
   y_values.map{|y| -y}
 end
 def to_svg
   %Q{<svg xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink" >
 #{x_axis}
 #{sparkline}
 #{spark}
  </svg>}
 end
 private
 SQUARE_SIDE=4
 SPARK_COLOR='red'
 def sparkline
   points = []
   @y_values.each_index { |i| points << "#{i},#{200-@y_values[i]}" }
   "<!-- sparkline -->
 #{SVG.polyline(points,"none","#333","1")}"
 end

 def spark
   final_value = @y_values[-1]
   centre_x=@y_values.length-1
   centre_y=200-final_value
   "  <!-- spark -->
 #{SVG.rect(centre_x-(SQUARE_SIDE/2),centre_y-(SQUARE_SIDE/2),
            SQUARE_SIDE,SQUARE_SIDE,'red','none',0)}
 <!-- final value -->
 #{SVG.text(centre_x+6, centre_y+4, final_value,"Verdana","9",SPARK_COLOR)}}"
 end

 def x_axis
   "  <!-- x-axis -->
 #{SVG.line(0,200,@y_values.length,200,"#999","1")}"
 end

end

<

>|ruby| require 'svg1.5.0.rb'

class Sparkline

 def initialize(y_values)
   @height_above_x_axis = y_values.max
   @final_value = y_values[-1]
   @y_values = reflect_top_and_bottom(y_values)
 end
 def reflect_top_and_bottom(y_values)
   y_values.map{|y| -y}
 end
 def to_svg
   %Q{<svg xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink" >
 <g transform="translate(0,#{@height_above_x_axis})">
 #{x_axis}
 #{sparkline}
 #{spark}
 </g>
  </svg>}
 end
 private
 SQUARE_SIDE=4
 SPARK_COLOR='red'
 def sparkline
   points = []
   @y_values.each_index { |i| points << "#{i},#{@y_values[i]}" }
   "<!-- sparkline -->
 #{SVG.polyline(points,"none","#333","1")}"
 end

 def spark
   final_value = @y_values[-1]
   centre_x=@y_values.length-1
   centre_y=final_value
   "  <!-- spark -->
 #{SVG.rect(centre_x-(SQUARE_SIDE/2),centre_y-(SQUARE_SIDE/2),
            SQUARE_SIDE,SQUARE_SIDE,'red','none',0)}
 <!-- final value -->
 #{SVG.text(centre_x+6, centre_y+4, final_value,"Verdana","9",SPARK_COLOR)}}"
 end

 def x_axis
   "  <!-- x-axis -->
 #{SVG.line(0,0,@y_values.length,0,"#999","1")}"
 end

end

<
Last modified:2024/04/19 07:15:48
Keyword(s):
References:[Refactoring2] [RubyPrimary]