Skip to content

Commit

Permalink
Added line_styles module
Browse files Browse the repository at this point in the history
  • Loading branch information
jparker committed May 12, 2008
1 parent b20bf20 commit a210024
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/google_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ module GoogleChart
require 'google_chart/data'
require 'google_chart/grid_lines'
require 'google_chart/legends'
require 'google_chart/line_styles'
require 'google_chart/sizes'
require 'google_chart/titles'
30 changes: 30 additions & 0 deletions lib/google_chart/line_styles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module GoogleChart
module LineStyles
@@line_styles = { :solid => [1,1,0], :dash => [1,3,2], :dot => [1,1,2] }
@@default_line_style = :solid
@@default_line_width = 1

def style
@style ||= []
@width ||= []

# Pad @style and @width with defaults until they are equal in length
(@style.size - @width.size).times { @width << @@default_line_width }
(@width.size - @style.size).times { @style << @@default_line_style }

unless @style.empty?
'chls=' + (0...@style.size).collect {|i|
@@line_styles[@style[i]].collect {|n| n * @width[i] }.join(',')
}.join('|')
end
end

def style=(style)
@style = [style].flatten
end

def width=(width)
@width = [width].flatten
end
end
end
45 changes: 45 additions & 0 deletions test/google_chart/test_line_styles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require File.dirname(__FILE__) + '/../test_helper'

FooChart.class_eval { include GoogleChart::LineStyles }

class TestLineStyles < Test::Unit::TestCase
def test_should_not_have_line_styles_by_default
assert_nil(FooChart.new.style)
end

def test_should_be_able_to_provide_solid_line_style
assert_equal('chls=1,1,0', FooChart.new(:style => :solid).style)
end

def test_should_be_able_to_provide_dash_line_style
assert_equal('chls=1,3,2', FooChart.new(:style => :dash).style)
end

def test_should_be_able_to_provide_dot_line_style
assert_equal('chls=1,1,2', FooChart.new(:style => :dot).style)
end

def test_should_be_able_to_provide_multiple_line_styles
assert_equal('chls=1,3,2|1,1,2|1,1,0', FooChart.new(:style => [:dash, :dot, :solid]).style)
end

def test_should_be_able_to_provide_line_width
assert_equal('chls=2,2,0', FooChart.new(:width => 2).style)
end

def test_should_be_able_to_provide_line_width_and_line_style
assert_equal('chls=2,6,4', FooChart.new(:style => :dash, :width => 2).style)
end

def test_should_be_able_to_provide_multiple_line_widths_and_line_styles
assert_equal('chls=2,2,4|3,9,6', FooChart.new(:style => [:dot, :dash], :width => [2, 3]).style)
end

def test_should_use_default_line_width_when_there_are_more_styles_than_widths
assert_equal('chls=2,2,4|1,3,2|1,1,0', FooChart.new(:style => [:dot, :dash, :solid], :width => 2).style)
end

def test_should_use_default_line_style_when_there_are_more_widths_than_styles
assert_equal('chls=2,2,4|3,3,0|2,2,0', FooChart.new(:style => :dot, :width => [2, 3, 2]).style)
end
end

0 comments on commit a210024

Please sign in to comment.