diff options
| -rw-r--r-- | CHANGELOG.md | 9 | ||||
| -rw-r--r-- | examples/README.md | 1 | ||||
| -rw-r--r-- | examples/images/stacked_bar_chart_example.png | bin | 0 -> 28959 bytes | |||
| -rw-r--r-- | examples/stacked_bar_chart_example.md | 35 | ||||
| -rw-r--r-- | lib/axlsx/drawing/bar_chart.rb | 14 | ||||
| -rw-r--r-- | test/drawing/tc_bar_chart.rb | 14 |
6 files changed, 69 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index c55a2673..d9c3fc45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ CHANGELOG --------- +- **Unreleased** + - [PR #107](https://github.com/caxlsx/caxlsx/pull/107) - Add overlap to bar charts + - **March.27.21**: 3.1.0 - [PR #95](https://github.com/caxlsx/caxlsx/pull/95) - Replace mimemagic with marcel - [PR #87](https://github.com/caxlsx/caxlsx/pull/87) - Implement :offset option for worksheet#add_row @@ -11,7 +14,7 @@ CHANGELOG - **January.5.21**: 3.0.4 - [PR #72](https://github.com/caxlsx/caxlsx/pull/72) - Relax Ruby dependency to allow for Ruby 3. This required Travis to be upgraded from Ubuntu Trusty to Ubuntu Bionic. rbx-3 was dropped. - [PR #71](https://github.com/caxlsx/caxlsx/pull/71) - Adds date type to validator so sheet.add_data_validation works with date type. Addresses [I #26](https://github.com/caxlsx/caxlsx/issues/26) - Date Data Validation not working - - [PR #70](https://github.com/caxlsx/caxlsx/pull/70) - Fix worksheet title length enforcement caused by switching from size to bytesize. Addresses [I #67](https://github.com/caxlsx/caxlsx/issues/67) - character length error in worksheet name when using Japanese, which was introduced by addressing [I #588](https://github.com/randym/axlsx/issues/588) in the old Axlsx repo. + - [PR #70](https://github.com/caxlsx/caxlsx/pull/70) - Fix worksheet title length enforcement caused by switching from size to bytesize. Addresses [I #67](https://github.com/caxlsx/caxlsx/issues/67) - character length error in worksheet name when using Japanese, which was introduced by addressing [I #588](https://github.com/randym/axlsx/issues/588) in the old Axlsx repo. - **December.7.20**: 3.0.3 @@ -19,7 +22,7 @@ CHANGELOG - [PR #56](https://github.com/caxlsx/caxlsx/pull/56) - Add `zip_command` option to `#serialize` for faster serialization of large Excel files by using a zip binary - [PR #54](https://github.com/caxlsx/caxlsx/pull/54) - Fix type detection for floats with out-of-rage exponents - [I #67](https://github.com/caxlsx/caxlsx/issues/67) - Fix regression in worksheet name length enforcement: Some unicode characters were counted incorrectly, so that names that previously worked fine now stopped working. (This was introduced in 3.0.2) - + - **July.16.20**: 3.0.2 - [I #51](https://github.com/caxlsx/caxlsx/issues/51) - Images do not import on Windows. IO read set explicitly to binary mode. - [PR #53](https://github.com/caxlsx/caxlsx/pull/53) - Limit column width to 255. Maximum column width limit in MS Excel is 255 characters, see https://support.microsoft.com/en-us/office/excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3 @@ -152,7 +155,7 @@ CHANGELOG - added in interop requirements so that charts are properly exported to PDF from Libra Office - various readability improvements and work standardizing attribute - names to snake_case. Aliases are provided for backward compatiblity + names to snake_case. Aliases are provided for backward compatiblity - **June.11.12**: 1.1.7 - fix chart rendering issue when label offset is specified as a diff --git a/examples/README.md b/examples/README.md index eca725dc..5f53199e 100644 --- a/examples/README.md +++ b/examples/README.md @@ -44,6 +44,7 @@ Types: * [Line chart (3D)](3d_line_chart_example.md) * [Pie chart (3D)](3d_pie_chart_example.md) * [Scatter chart](scatter_chart_example.md) +* [Stacked bar chart](stacked_bar_chart_example.md) Customizations: * [Chart colors](chart_colors_example.md) diff --git a/examples/images/stacked_bar_chart_example.png b/examples/images/stacked_bar_chart_example.png Binary files differnew file mode 100644 index 00000000..da046983 --- /dev/null +++ b/examples/images/stacked_bar_chart_example.png diff --git a/examples/stacked_bar_chart_example.md b/examples/stacked_bar_chart_example.md new file mode 100644 index 00000000..3fbd2425 --- /dev/null +++ b/examples/stacked_bar_chart_example.md @@ -0,0 +1,35 @@ +## Description + +Stacked bar chart example + +## Code + +```ruby +require 'axlsx' + +p = Axlsx::Package.new +wb = p.workbook + +wb.add_worksheet(name: 'Bar Chart') do |sheet| + sheet.add_row ['A Simple Bar Chart', 'X', 'Y'] + + sheet.add_row ['A', 3, 4] + sheet.add_row ['B', 10, 6] + sheet.add_row ['C', 7, 2] + + sheet.add_chart(Axlsx::BarChart, start_at: 'A6', end_at: 'F20') do |chart| + chart.add_series data: sheet['B2:B4'], labels: sheet['A2:A4'], title: sheet['B1'] + chart.add_series data: sheet['C2:C4'], labels: sheet['A2:A4'], title: sheet['C1'] + + chart.bar_dir = :col + chart.overlap = 100 + chart.grouping = :percentStacked + end +end + +p.serialize 'stacked_bar_chart_example.xlsx' +``` + +## Output + + diff --git a/lib/axlsx/drawing/bar_chart.rb b/lib/axlsx/drawing/bar_chart.rb index e9af9400..cb596fce 100644 --- a/lib/axlsx/drawing/bar_chart.rb +++ b/lib/axlsx/drawing/bar_chart.rb @@ -49,6 +49,12 @@ module Axlsx @grouping ||= :clustered end + # Overlap between series + # @return [Integer] + def overlap + @overlap ||= 0 + end + # The shape of the bars or columns # must be one of [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax] # @return [Symbol] @@ -71,7 +77,7 @@ module Axlsx # @see Chart def initialize(frame, options={}) @vary_colors = true - @gap_width, @gap_depth, @shape = nil, nil, nil + @gap_width, @gap_depth, @overlap, @shape = nil, nil, nil, nil super(frame, options) @series_type = BarSeries @d_lbls = nil @@ -106,6 +112,11 @@ module Axlsx end alias :gapDepth= :gap_depth= + def overlap=(v) + RangeValidator.validate "BarChart.overlap", -100, 100, v + @overlap=(v) + end + # The shape of the bars or columns # must be one of [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax] def shape=(v) @@ -124,6 +135,7 @@ module Axlsx 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:overlap val="' << @overlap.to_s << '"/>') unless @overlap.nil? 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? diff --git a/test/drawing/tc_bar_chart.rb b/test/drawing/tc_bar_chart.rb index ca1ca016..d064e5be 100644 --- a/test/drawing/tc_bar_chart.rb +++ b/test/drawing/tc_bar_chart.rb @@ -45,6 +45,13 @@ class TestBarChart < Test::Unit::TestCase assert(@chart.gap_depth == "200%") end + def test_overlap + assert_raise(ArgumentError, "require valid overlap") { @chart.overlap = -101 } + assert_raise(ArgumentError, "require valid overlap") { @chart.overlap = 101 } + assert_nothing_raised("allow valid overlap") { @chart.overlap = 100 } + assert_equal(@chart.overlap, 100, 'overlap is incorrect') + end + def test_shape assert_raise(ArgumentError, "require valid shape") { @chart.shape = :star } assert_nothing_raised("allow valid shape") { @chart.shape = :cone } @@ -68,4 +75,11 @@ class TestBarChart < Test::Unit::TestCase 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 + + def test_to_xml_string_has_overlap + overlap_value = rand(-100..100) + @chart.overlap = overlap_value + doc = Nokogiri::XML(@chart.to_xml_string) + assert_equal(doc.xpath("//c:barChart/c:overlap").first.attribute('val').value, overlap_value.to_s) + end end |
