diff options
| author | Randy Morgan <[email protected]> | 2012-06-11 08:45:37 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-06-11 08:45:37 +0900 |
| commit | 68f0b530e2260ceacf2ed916e216eaec5da60945 (patch) | |
| tree | 63045ae99fc3abd33e4fde341f73e86af6655969 | |
| parent | e693d2b1933797e18cc1cdd3cba793630e8ecb25 (diff) | |
| download | caxlsx-68f0b530e2260ceacf2ed916e216eaec5da60945.tar.gz caxlsx-68f0b530e2260ceacf2ed916e216eaec5da60945.zip | |
add title object to axis.
| -rwxr-xr-x | examples/example.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/drawing/axis.rb | 18 | ||||
| -rw-r--r-- | test/drawing/tc_axis.rb | 6 | ||||
| -rw-r--r-- | test/tc_package.rb | 12 |
4 files changed, 33 insertions, 5 deletions
diff --git a/examples/example.rb b/examples/example.rb index 23fc30d0..4287dff9 100755 --- a/examples/example.rb +++ b/examples/example.rb @@ -276,6 +276,8 @@ wb.add_worksheet(:name => "Line Chart") do |sheet| chart.end_at 10, 15 chart.add_series :data => sheet["B1:E1"], :title => sheet["A1"] chart.add_series :data => sheet["B2:E2"], :title => sheet["A2"] + chart.catAxis.title = 'Y Axis' + chart.valAxis.title = 'X Axis' end end #``` diff --git a/lib/axlsx/drawing/axis.rb b/lib/axlsx/drawing/axis.rb index 84713943..1da3e430 100644 --- a/lib/axlsx/drawing/axis.rb +++ b/lib/axlsx/drawing/axis.rb @@ -52,6 +52,9 @@ module Axlsx # @return [Boolean] attr_reader :delete + # the title for the axis. This can be a cell or a fixed string. + attr_reader :title + # Creates an Axis object # @param [Integer] ax_id the id of this axis # @param [Integer] cross_ax the id of the perpendicular axis @@ -67,6 +70,7 @@ module Axlsx @format_code = "General" @delete = @label_rotation = 0 @scaling = Scaling.new(:orientation=>:minMax) + @title = nil self.ax_pos = :b self.tick_lbl_pos = :nextTo self.format_code = "General" @@ -113,6 +117,19 @@ module Axlsx @label_rotation = adjusted end + + # The title object for the chart. + # @param [String, Cell] v + # @return [Title] + def title=(v) + DataTypeValidator.validate "#{self.class}.title", [String, Cell], v + @title ||= Title.new + if v.is_a?(String) + @title.text = v + elsif v.is_a?(Cell) + @title.cell = v + end + end # Serializes the object # @param [String] str @@ -131,6 +148,7 @@ module Axlsx str << '</c:spPr>' end str << '</c:majorGridlines>' + @title.to_xml_string(str) unless @title == nil str << '<c:numFmt formatCode="' << @format_code << '" sourceLinked="1"/>' str << '<c:majorTickMark val="none"/>' str << '<c:minorTickMark val="none"/>' diff --git a/test/drawing/tc_axis.rb b/test/drawing/tc_axis.rb index 77eb61a1..46c8c956 100644 --- a/test/drawing/tc_axis.rb +++ b/test/drawing/tc_axis.rb @@ -2,8 +2,9 @@ require 'tc_helper.rb' class TestAxis < Test::Unit::TestCase def setup - @axis = Axlsx::Axis.new 12345, 54321, :gridlines => false + @axis = Axlsx::Axis.new 12345, 54321, :gridlines => false, :title => 'Foo' end + def teardown end @@ -14,6 +15,7 @@ class TestAxis < Test::Unit::TestCase assert_equal(@axis.crosses, :autoZero, "tick label position default incorrect") assert(@axis.scaling.is_a?(Axlsx::Scaling) && @axis.scaling.orientation == :minMax, "scaling default incorrect") assert_raise(ArgumentError) { Axlsx::Axis.new( -1234, 'abcd') } + assert_equal('Foo', @axis.title.text) end def test_axis_position @@ -57,6 +59,6 @@ class TestAxis < Test::Unit::TestCase assert(doc.xpath("//c:crosses[@val='#{@axis.crosses.to_s}']")) assert(doc.xpath("//c:crossAx[@val='#{@axis.crossAx.to_s}']")) assert(doc.xpath("//a:bodyPr[@rot='#{@axis.label_rotation.to_s}']")) - + assert(doc.xpath("//a:t[text()='Foo']")) end end diff --git a/test/tc_package.rb b/test/tc_package.rb index 3f214d6b..eedd2375 100644 --- a/test/tc_package.rb +++ b/test/tc_package.rb @@ -24,8 +24,14 @@ class TestPackage < Test::Unit::TestCase vs.add_selection(:bottom_right, { :active_cell => 'I57', :sqref => 'I57' }) end - chart = ws.add_chart Axlsx::Pie3DChart, :title => "これは?", :start_at => [0,3] - chart.add_series :data=>[1,2,3], :labels=>["a", "b", "c"] + ws.add_chart(Axlsx::Pie3DChart, :title => "これは?", :start_at => [0,3]) do |chart| + chart.add_series :data=>[1,2,3], :labels=>["a", "b", "c"] + end + + ws.add_chart(Axlsx::Line3DChart, :title => "axis labels") do |chart| + chart.valAxis.title = 'bob' + end + @fname = 'axlsx_test_serialization.xlsx' img = File.expand_path('../../examples/image1.jpeg', __FILE__) ws.add_image(:image_src => img, :noSelect => true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com") do |image| @@ -114,7 +120,7 @@ class TestPackage < Test::Unit::TestCase #no mystery parts - assert_equal(p.size, 18) + assert_equal(p.size, 19) end |
