-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |