diff options
| author | David N. Robinson <[email protected]> | 2017-11-07 16:51:30 +0200 |
|---|---|---|
| committer | David N. Robinson <[email protected]> | 2017-11-17 06:14:33 +0200 |
| commit | ef396a558b35644c79b74e009acb44b52db36ed1 (patch) | |
| tree | ebbaa0f951b9d99fae372bc319fbacdfc5b7cbe2 | |
| parent | e855b75cd13e5a7e16c755ab648594c5a85e6653 (diff) | |
| download | caxlsx-ef396a558b35644c79b74e009acb44b52db36ed1.tar.gz caxlsx-ef396a558b35644c79b74e009acb44b52db36ed1.zip | |
Add support for non-3D bar charts
| -rw-r--r-- | lib/axlsx/drawing/area_chart.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/drawing/bar_chart.rb | 143 | ||||
| -rw-r--r-- | lib/axlsx/drawing/drawing.rb | 1 | ||||
| -rw-r--r-- | test/drawing/.tc_area_series.rb.swp | bin | 12288 -> 0 bytes | |||
| -rw-r--r-- | test/drawing/tc_area_chart.rb | 2 | ||||
| -rw-r--r-- | test/drawing/tc_bar_chart.rb | 71 |
6 files changed, 217 insertions, 2 deletions
diff --git a/lib/axlsx/drawing/area_chart.rb b/lib/axlsx/drawing/area_chart.rb index e58b2cf1..2002840c 100644 --- a/lib/axlsx/drawing/area_chart.rb +++ b/lib/axlsx/drawing/area_chart.rb @@ -49,7 +49,7 @@ module Axlsx @vary_colors = false @grouping = :standard super(frame, options) - @series_type = LineSeries + @series_type = AreaSeries @d_lbls = nil end diff --git a/lib/axlsx/drawing/bar_chart.rb b/lib/axlsx/drawing/bar_chart.rb new file mode 100644 index 00000000..7efd0ec5 --- /dev/null +++ b/lib/axlsx/drawing/bar_chart.rb @@ -0,0 +1,143 @@ +# encoding: UTF-8 +module Axlsx + + # The BarChart is a three dimentional barchart (who would have guessed?) that you can add to your worksheet. + # @see Worksheet#add_chart + # @see Chart#add_series + # @see Package#serialize + # @see README for an example + class BarChart < Chart + + # the category axis + # @return [CatAxis] + def cat_axis + axes[:cat_axis] + end + alias :catAxis :cat_axis + + # the value axis + # @return [ValAxis] + def val_axis + axes[:val_axis] + end + alias :valAxis :val_axis + + # The direction of the bars in the chart + # must be one of [:bar, :col] + # @return [Symbol] + def bar_dir + @bar_dir ||= :bar + end + alias :barDir :bar_dir + + # space between bar or column clusters, as a percentage of the bar or column width. + # @return [String] + attr_reader :gap_depth + alias :gapDepth :gap_depth + + # space between bar or column clusters, as a percentage of the bar or column width. + # @return [String] + def gap_width + @gap_width ||= 150 + end + alias :gapWidth :gap_width + + #grouping for a column, line, or area chart. + # must be one of [:percentStacked, :clustered, :standard, :stacked] + # @return [Symbol] + def grouping + @grouping ||= :clustered + end + + # The shabe of the bars or columns + # must be one of [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax] + # @return [Symbol] + def shape + @shape ||= :box + end + + # validation regex for gap amount percent + GAP_AMOUNT_PERCENT = /0*(([0-9])|([1-9][0-9])|([1-4][0-9][0-9])|500)%/ + + # Creates a new bar chart object + # @param [GraphicFrame] frame The workbook that owns this chart. + # @option options [Cell, String] title + # @option options [Boolean] show_legend + # @option options [Symbol] bar_dir + # @option options [Symbol] grouping + # @option options [String] gap_width + # @option options [String] gap_depth + # @option options [Symbol] shape + # @see Chart + def initialize(frame, options={}) + @vary_colors = true + @gap_width, @gap_depth, @shape = nil, nil, nil + super(frame, options) + @series_type = BarSeries + @d_lbls = nil + end + + # The direction of the bars in the chart + # must be one of [:bar, :col] + def bar_dir=(v) + RestrictionValidator.validate "BarChart.bar_dir", [:bar, :col], v + @bar_dir = v + end + alias :barDir= :bar_dir= + + #grouping for a column, line, or area chart. + # must be one of [:percentStacked, :clustered, :standard, :stacked] + def grouping=(v) + RestrictionValidator.validate "BarChart.grouping", [:percentStacked, :clustered, :standard, :stacked], v + @grouping = v + end + + # space between bar or column clusters, as a percentage of the bar or column width. + def gap_width=(v) + RegexValidator.validate "BarChart.gap_width", GAP_AMOUNT_PERCENT, v + @gap_width=(v) + end + alias :gapWidth= :gap_width= + + # space between bar or column clusters, as a percentage of the bar or column width. + def gap_depth=(v) + RegexValidator.validate "BarChart.gap_didth", GAP_AMOUNT_PERCENT, v + @gap_depth=(v) + end + alias :gapDepth= :gap_depth= + + # The shabe of the bars or columns + # must be one of [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax] + def shape=(v) + RestrictionValidator.validate "BarChart.shape", [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax], v + @shape = v + end + + # Serializes the object + # @param [String] str + # @return [String] + def to_xml_string(str = '') + super(str) do + str << '<c:barChart>' + str << ('<c:barDir val="' << bar_dir.to_s << '"/>') + str << ('<c:grouping val="' << grouping.to_s << '"/>') + str << ('<c:varyColors val="' << vary_colors.to_s << '"/>') + @series.each { |ser| ser.to_xml_string(str) } + @d_lbls.to_xml_string(str) if @d_lbls + str << ('<c:gapWidth val="' << @gap_width.to_s << '"/>') unless @gap_width.nil? + str << ('<c:gapDepth val="' << @gap_depth.to_s << '"/>') unless @gap_depth.nil? + str << ('<c:shape val="' << @shape.to_s << '"/>') unless @shape.nil? + axes.to_xml_string(str, :ids => true) + str << '</c:barChart>' + axes.to_xml_string(str) + end + end + + # A hash of axes used by this chart. Bar charts have a value and + # category axes specified via axes[:val_axes] and axes[:cat_axis] + # @return [Axes] + def axes + @axes ||= Axes.new(:cat_axis => CatAxis, :val_axis => ValAxis) + end + end +end diff --git a/lib/axlsx/drawing/drawing.rb b/lib/axlsx/drawing/drawing.rb index d9b46ff8..e9da37a5 100644 --- a/lib/axlsx/drawing/drawing.rb +++ b/lib/axlsx/drawing/drawing.rb @@ -36,6 +36,7 @@ module Axlsx require 'axlsx/drawing/chart.rb' require 'axlsx/drawing/pie_3D_chart.rb' require 'axlsx/drawing/bar_3D_chart.rb' + require 'axlsx/drawing/bar_chart.rb' require 'axlsx/drawing/line_chart.rb' require 'axlsx/drawing/line_3D_chart.rb' require 'axlsx/drawing/scatter_chart.rb' diff --git a/test/drawing/.tc_area_series.rb.swp b/test/drawing/.tc_area_series.rb.swp Binary files differdeleted file mode 100644 index 3a76d8d5..00000000 --- a/test/drawing/.tc_area_series.rb.swp +++ /dev/null diff --git a/test/drawing/tc_area_chart.rb b/test/drawing/tc_area_chart.rb index f081b357..b3013c12 100644 --- a/test/drawing/tc_area_chart.rb +++ b/test/drawing/tc_area_chart.rb @@ -14,7 +14,7 @@ class TestAreaChart < Test::Unit::TestCase def test_initialization assert_equal(@chart.grouping, :standard, "grouping defualt incorrect") - assert_equal(@chart.series_type, Axlsx::LineSeries, "series type incorrect") + assert_equal(@chart.series_type, Axlsx::AreaSeries, "series type incorrect") assert(@chart.cat_axis.is_a?(Axlsx::CatAxis), "category axis not created") assert(@chart.val_axis.is_a?(Axlsx::ValAxis), "value access not created") end diff --git a/test/drawing/tc_bar_chart.rb b/test/drawing/tc_bar_chart.rb new file mode 100644 index 00000000..ca1ca016 --- /dev/null +++ b/test/drawing/tc_bar_chart.rb @@ -0,0 +1,71 @@ +require 'tc_helper.rb' + +class TestBarChart < Test::Unit::TestCase + + def setup + @p = Axlsx::Package.new + ws = @p.workbook.add_worksheet + @row = ws.add_row ["one", 1, Time.now] + @chart = ws.add_chart Axlsx::BarChart, :title => "fishery" + end + + def teardown + end + + def test_initialization + assert_equal(@chart.grouping, :clustered, "grouping defualt incorrect") + assert_equal(@chart.series_type, Axlsx::BarSeries, "series type incorrect") + assert_equal(@chart.bar_dir, :bar, " bar direction incorrect") + assert(@chart.cat_axis.is_a?(Axlsx::CatAxis), "category axis not created") + assert(@chart.val_axis.is_a?(Axlsx::ValAxis), "value access not created") + end + + def test_bar_direction + assert_raise(ArgumentError, "require valid bar direction") { @chart.bar_dir = :left } + assert_nothing_raised("allow valid bar direction") { @chart.bar_dir = :col } + assert(@chart.bar_dir == :col) + end + + def test_grouping + assert_raise(ArgumentError, "require valid grouping") { @chart.grouping = :inverted } + assert_nothing_raised("allow valid grouping") { @chart.grouping = :standard } + assert(@chart.grouping == :standard) + end + + + def test_gapWidth + assert_raise(ArgumentError, "require valid gap width") { @chart.gap_width = 200 } + assert_nothing_raised("allow valid gapWidth") { @chart.gap_width = "200%" } + assert(@chart.gap_width == "200%") + end + + def test_gapDepth + assert_raise(ArgumentError, "require valid gap_depth") { @chart.gap_depth = 200 } + assert_nothing_raised("allow valid gap_depth") { @chart.gap_depth = "200%" } + assert(@chart.gap_depth == "200%") + end + + def test_shape + assert_raise(ArgumentError, "require valid shape") { @chart.shape = :star } + assert_nothing_raised("allow valid shape") { @chart.shape = :cone } + assert(@chart.shape == :cone) + 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 + + def test_to_xml_string_has_axes_in_correct_order + str = @chart.to_xml_string + cat_axis_position = str.index(@chart.axes[:cat_axis].id.to_s) + val_axis_position = str.index(@chart.axes[:val_axis].id.to_s) + assert(cat_axis_position < val_axis_position, "cat_axis must occur earlier than val_axis in the XML") + end +end |
