diff options
| author | Randy Morgan (@morgan_randy) <[email protected]> | 2013-11-04 00:24:59 -0800 |
|---|---|---|
| committer | Randy Morgan (@morgan_randy) <[email protected]> | 2013-11-04 00:24:59 -0800 |
| commit | 0c3817c9545ca44ad6bcd0f60c0ea19378257c9c (patch) | |
| tree | 0166f1e9019e116ac4999ff0695a96653191f472 | |
| parent | 3bf46a00d717d5e991166360bf4a2ece41cc3fbc (diff) | |
| parent | 82dbc7b81c28c66d530c4f826544527bf692f08b (diff) | |
| download | caxlsx-0c3817c9545ca44ad6bcd0f60c0ea19378257c9c.tar.gz caxlsx-0c3817c9545ca44ad6bcd0f60c0ea19378257c9c.zip | |
Merge pull request #254 from skateinmars/bubble_chart
Add support for bubble charts
| -rw-r--r-- | lib/axlsx/drawing/bubble_chart.rb | 59 | ||||
| -rw-r--r-- | lib/axlsx/drawing/bubble_series.rb | 63 | ||||
| -rw-r--r-- | lib/axlsx/drawing/drawing.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/drawing/num_data_source.rb | 2 | ||||
| -rw-r--r-- | test/drawing/tc_bubble_chart.rb | 44 | ||||
| -rw-r--r-- | test/drawing/tc_bubble_series.rb | 21 | ||||
| -rw-r--r-- | test/drawing/tc_data_source.rb | 6 | ||||
| -rw-r--r-- | test/tc_package.rb | 7 |
8 files changed, 202 insertions, 2 deletions
diff --git a/lib/axlsx/drawing/bubble_chart.rb b/lib/axlsx/drawing/bubble_chart.rb new file mode 100644 index 00000000..2f2c4595 --- /dev/null +++ b/lib/axlsx/drawing/bubble_chart.rb @@ -0,0 +1,59 @@ +# encoding: UTF-8 +module Axlsx + + # The BubbleChart allows you to insert a bubble chart into your worksheet + # @see Worksheet#add_chart + # @see Chart#add_series + # @see README for an example + class BubbleChart < Chart + + include Axlsx::OptionsParser + + # the x value axis + # @return [ValAxis] + def x_val_axis + axes[:x_val_axis] + end + alias :xValAxis :x_val_axis + + # the y value axis + # @return [ValAxis] + def y_val_axis + axes[:y_val_axis] + end + alias :yValAxis :y_val_axis + + # Creates a new bubble chart + def initialize(frame, options={}) + @vary_colors = 0 + + super(frame, options) + @series_type = BubbleSeries + @d_lbls = nil + parse_options options + end + + # Serializes the object + # @param [String] str + # @return [String] + def to_xml_string(str = '') + super(str) do |str_inner| + str_inner << '<c:bubbleChart>' + str_inner << '<c:varyColors val="' << vary_colors.to_s << '"/>' + @series.each { |ser| ser.to_xml_string(str_inner) } + d_lbls.to_xml_string(str_inner) if @d_lbls + axes.to_xml_string(str_inner, :ids => true) + str_inner << '</c:bubbleChart>' + axes.to_xml_string(str_inner) + end + str + end + + # The axes for the bubble chart. BubbleChart has an x_val_axis and + # a y_val_axis + # @return [Axes] + def axes + @axes ||= Axes.new(:x_val_axis => ValAxis, :y_val_axis => ValAxis) + end + end +end diff --git a/lib/axlsx/drawing/bubble_series.rb b/lib/axlsx/drawing/bubble_series.rb new file mode 100644 index 00000000..6d263774 --- /dev/null +++ b/lib/axlsx/drawing/bubble_series.rb @@ -0,0 +1,63 @@ +# encoding: UTF-8 +module Axlsx + + # A BubbleSeries defines the x/y position and bubble size of data in the chart + # @note The recommended way to manage series is to use Chart#add_series + # @see Worksheet#add_chart + # @see Chart#add_series + # @see examples/example.rb + class BubbleSeries < Series + + # The x data for this series. + # @return [AxDataSource] + attr_reader :xData + + # The y data for this series. + # @return [NumDataSource] + attr_reader :yData + + # The bubble size for this series. + # @return [NumDataSource] + attr_reader :bubbleSize + + # The fill color for this series. + # Red, green, and blue is expressed as sequence of hex digits, RRGGBB. A perceptual gamma of 2.2 is used. + # @return [String] + attr_reader :color + + # Creates a new BubbleSeries + def initialize(chart, options={}) + @xData, @yData, @bubbleSize = nil + super(chart, options) + @xData = AxDataSource.new(:tag_name => :xVal, :data => options[:xData]) unless options[:xData].nil? + @yData = NumDataSource.new({:tag_name => :yVal, :data => options[:yData]}) unless options[:yData].nil? + @bubbleSize = NumDataSource.new({:tag_name => :bubbleSize, :data => options[:bubbleSize]}) unless options[:bubbleSize].nil? + end + + # @see color + def color=(v) + @color = v + end + + # Serializes the object + # @param [String] str + # @return [String] + def to_xml_string(str = '') + super(str) do |inner_str| + # needs to override the super color here to push in ln/and something else! + if color + str << '<c:spPr><a:solidFill>' + str << '<a:srgbClr val="' << color << '"/>' + str << '</a:solidFill>' + str << '<a:ln><a:solidFill>' + str << '<a:srgbClr val="' << color << '"/></a:solidFill></a:ln>' + str << '</c:spPr>' + end + @xData.to_xml_string(inner_str) unless @xData.nil? + @yData.to_xml_string(inner_str) unless @yData.nil? + @bubbleSize.to_xml_string(inner_str) unless @bubbleSize.nil? + end + str + end + end +end diff --git a/lib/axlsx/drawing/drawing.rb b/lib/axlsx/drawing/drawing.rb index b5856909..e7fcba85 100644 --- a/lib/axlsx/drawing/drawing.rb +++ b/lib/axlsx/drawing/drawing.rb @@ -8,6 +8,7 @@ module Axlsx require 'axlsx/drawing/bar_series.rb' require 'axlsx/drawing/line_series.rb' require 'axlsx/drawing/scatter_series.rb' + require 'axlsx/drawing/bubble_series.rb' require 'axlsx/drawing/scaling.rb' require 'axlsx/drawing/axis.rb' @@ -37,6 +38,7 @@ module Axlsx require 'axlsx/drawing/line_chart.rb' require 'axlsx/drawing/line_3D_chart.rb' require 'axlsx/drawing/scatter_chart.rb' + require 'axlsx/drawing/bubble_chart.rb' require 'axlsx/drawing/picture_locking.rb' require 'axlsx/drawing/pic.rb' diff --git a/lib/axlsx/drawing/num_data_source.rb b/lib/axlsx/drawing/num_data_source.rb index 14d53301..c52b0fa8 100644 --- a/lib/axlsx/drawing/num_data_source.rb +++ b/lib/axlsx/drawing/num_data_source.rb @@ -33,7 +33,7 @@ module Axlsx # allowed element tag names # @return [Array] def self.allowed_tag_names - [:yVal, :val] + [:yVal, :val, :bubbleSize] end # sets the tag name for this data source diff --git a/test/drawing/tc_bubble_chart.rb b/test/drawing/tc_bubble_chart.rb new file mode 100644 index 00000000..7adad218 --- /dev/null +++ b/test/drawing/tc_bubble_chart.rb @@ -0,0 +1,44 @@ +require 'tc_helper.rb' + +class TestBubbleChart < Test::Unit::TestCase + def setup + @p = Axlsx::Package.new + @chart = nil + @p.workbook.add_worksheet do |sheet| + sheet.add_row ["First", 1, 5, 7, 9] + sheet.add_row ["", 1, 25, 49, 81] + sheet.add_row ["", 1, 42, 60, 75] + sheet.add_row ["Second", 5, 2, 14, 9] + sheet.add_row ["", 5, 10, 15, 20] + sheet.add_row ["", 5, 28, 92, 13] + sheet.add_chart(Axlsx::BubbleChart, :title => "example: Bubble Chart") do |chart| + chart.start_at 0, 4 + chart.end_at 10, 19 + chart.add_series :xData => sheet["B1:E1"], :yData => sheet["B2:E2"], :bubbleSize => sheet["B3:E3"], :title => sheet["A1"] + chart.add_series :xData => sheet["B4:E4"], :yData => sheet["B5:E5"], :bubbleSize => sheet["B6:E6"], :title => sheet["A3"] + @chart = chart + end + end + end + + def teardown + end + + def test_initialization + assert_equal(@chart.series_type, Axlsx::BubbleSeries, "series type incorrect") + assert(@chart.xValAxis.is_a?(Axlsx::ValAxis), "independant value axis not created") + assert(@chart.yValAxis.is_a?(Axlsx::ValAxis), "dependant value axis not created") + end + + def test_to_xml_string + schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD)) + doc = Nokogiri::XML(@chart.to_xml_string) + errors = [] + schema.validate(doc).each do |error| + errors.push error + puts error.message + end + assert(errors.empty?, "error free validation") + end + +end diff --git a/test/drawing/tc_bubble_series.rb b/test/drawing/tc_bubble_series.rb new file mode 100644 index 00000000..e601912f --- /dev/null +++ b/test/drawing/tc_bubble_series.rb @@ -0,0 +1,21 @@ +require 'tc_helper.rb' + +class TestBubbleSeries < Test::Unit::TestCase + + def setup + p = Axlsx::Package.new + @ws = p.workbook.add_worksheet :name=>"hmmm" + @chart = @ws.add_chart Axlsx::BubbleChart, :title => "Bubble Chart" + @series = @chart.add_series :xData=>[1,2,4], :yData=>[1,3,9], :bubbleSize=>[1,5,7], :title=>"GDP", :color => 'FF0000' + end + + def test_initialize + assert_equal(@series.title.text, "GDP", "series title has been applied") + end + + def test_to_xml_string + doc = Nokogiri::XML(@chart.to_xml_string) + assert_equal(doc.xpath("//a:srgbClr[@val='#{@series.color}']").size,2) + end + +end diff --git a/test/drawing/tc_data_source.rb b/test/drawing/tc_data_source.rb index 6b35e008..e9bed095 100644 --- a/test/drawing/tc_data_source.rb +++ b/test/drawing/tc_data_source.rb @@ -6,6 +6,12 @@ @data_source = Axlsx::NumDataSource.new :data => ["1", "2", "3"] end + def test_tag_name + assert_raise(ArgumentError) { @data_source.tag_name = :zVal } + assert_nothing_raised { @data_source.tag_name = :yVal } + assert_nothing_raised { @data_source.tag_name = :bubbleSize } + end + def test_to_xml_string_strLit str = '<?xml version="1.0" encoding="UTF-8"?>' str << '<c:chartSpace xmlns:c="' << Axlsx::XML_NS_C << '">' diff --git a/test/tc_package.rb b/test/tc_package.rb index 8f0caa46..0a33e248 100644 --- a/test/tc_package.rb +++ b/test/tc_package.rb @@ -53,6 +53,11 @@ class TestPackage < Test::Unit::TestCase chart.d_lbls.show_val = true end + ws.add_chart(Axlsx::BubbleChart, :title => 'bubble chart') do |chart| + chart.add_series :xData => [1,2,3,4], :yData => [4,3,2,1], :yData => [1,3,2,4] + chart.d_lbls.show_val = true + 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| @@ -177,7 +182,7 @@ class TestPackage < Test::Unit::TestCase #no mystery parts - assert_equal(24, p.size) + assert_equal(25, p.size) end |
