class PillChart::SimplePillChart

Public Class Methods

new(height = 10, width = 60, value = 33, max = 100, type = :simple, colors = {}) click to toggle source

constructor method

# File lib/pill_chart/simple_pill_chart.rb, line 19
def initialize(height = 10, width = 60, value = 33,
               max = 100, type = :simple, colors = {})
    @width, @height = width, height
    @paths = []
    @type = type
    @max = max
    @valueWidth = Integer((value * @width) / @max)
    baseConfig = {
        "background" => "#eee",
        "foreground" => "#999",
        "low" => "#AD6D6D",
        "medium" => "#C59663",
        "high" => "#ADC563",
        "full" => "#92C447"
    }
    @config = baseConfig.merge(colors)
end

Public Instance Methods

getHeight() click to toggle source
# File lib/pill_chart/simple_pill_chart.rb, line 52
def getHeight
    @height
end
getWidth() click to toggle source

define accessor methods

# File lib/pill_chart/simple_pill_chart.rb, line 48
def getWidth
    @width
end
pill() click to toggle source
# File lib/pill_chart/simple_pill_chart.rb, line 37
def pill
    self.drawBackgroundPath
    if @type == :simple
        self.drawSimpleForegroundPath
    elsif @type == :state
        self.drawStateForegroundPath
    end
    self.toSvg
end
to_s() click to toggle source

define to_s method

# File lib/pill_chart/simple_pill_chart.rb, line 57
def to_s
    self.pill
end

Protected Instance Methods

drawBackgroundPath() click to toggle source
# File lib/pill_chart/simple_pill_chart.rb, line 63
def drawBackgroundPath
    render = []
    render << "<rect"
    render << "id=\"background\" x=\"0\" cy=\"0\" height=\"#{@height}\" width=\"#{@width}\""
    render << "style=\"fill: #{@config['background']}; mask: url(#pill-base)\""
    render << "/>"
    @paths.push([:background], render.join(" "))
end
drawSimpleForegroundPath() click to toggle source

<rect id=“background” x=“0” cy=“0” height=“20” width=“60” style=“fill: red; mask: url(pill-base)”/> <rect id=“progress” x=“0” cy=“0” height=“20” width=“30” style=“fill: green; mask: url(pill-base)”/>

# File lib/pill_chart/simple_pill_chart.rb, line 76
def drawSimpleForegroundPath
    render = []
    render << "<rect"
    render << "id=\"foreground\" x=\"0\" cy=\"0\" height=\"#{@height}\" width=\"#{@valueWidth}\""
    render << "style=\"fill: #{@config['foreground']}; mask: url(#pill-base)\""
    render << "/>"
    @paths.push([:foreground], render.join(" "))
end
drawStateForegroundPath() click to toggle source
# File lib/pill_chart/simple_pill_chart.rb, line 85
def drawStateForegroundPath
    if @valueWidth < ((20 * @max) / 100)
        barColor = @config["low"]
    elsif @valueWidth < ((40 * @max) / 100)
        barColor = @config["medium"]
    elsif @valueWidth < ((70 * @max) / 100)
        barColor = @config["high"]
    else
        barColor = @config["full"]
    end
    render = []
    render << "<rect"
    render << "id=\"foreground\" x=\"0\" cy=\"0\" height=\"#{@height}\" width=\"#{@valueWidth}\""
    render << "style=\"fill: #{barColor}; mask: url(#pill-base)\""
    render << "/>"
    @paths.push([:foreground], render.join(" "))
end
generateMask() click to toggle source

<mask id=“pill-base” x=“0” y=“0” width=“60” height=“20” style=“fill: ffffff” >

<circle cx="10" cy="10" r="10"/>
<circle cx="50" cy="10" r="10"/>
<rect x="10" cy="0" height="20" width="40"/>

</mask>

# File lib/pill_chart/simple_pill_chart.rb, line 110
def generateMask
    ray = @height / 2
    generated = []
    generated << "<mask"
    generated << "id=\"pill-base\""
    generated << "x=\"0\" y=\"0\" width=\"#{@width}\" height=\"#{@height}\" style=\"fill: #ffffff\""
    generated << ">"
    generated << "<circle cx=\"#{ray}\" cy=\"#{ray}\" r=\"#{ray}\"/>"
    generated << "<circle cx=\"#{@width - ray}\" cy=\"#{ray}\" r=\"#{ray}\"/>"
    generated << "<rect"
    generated << "x=\"#{ray}\" y=\"0\" width=\"#{@width - (2 * ray)}\" height=\"#{@height}\"/>"
    generated << "</mask>"
    "<defs>" + generated.join(" ") + "</defs>"
end
getSvgFooter() click to toggle source
# File lib/pill_chart/simple_pill_chart.rb, line 129
def getSvgFooter
    '</svg>'
end
getSvgHeader() click to toggle source
# File lib/pill_chart/simple_pill_chart.rb, line 125
def getSvgHeader
    "<svg width=\"#{@width}\" height=\"#{@height}\">"
end
toSvg() click to toggle source
# File lib/pill_chart/simple_pill_chart.rb, line 133
def toSvg
    finalSvg = []
    finalSvg.push self.getSvgHeader
    finalSvg.push self.generateMask
    finalSvg.push @paths.join("\n")
    finalSvg.push self.getSvgFooter
    finalSvg.join
end