summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorZsolt Kozaroczy <[email protected]>2021-04-26 22:32:49 +0200
committerGitHub <[email protected]>2021-04-26 22:32:49 +0200
commit8d55f0d2ad3c5e4f4c6e9be1cf1e7b065fc25e49 (patch)
treeb68b0e90908246a407c450cee146a066ab1ce0ec
parent6e74fb8db66695649ad0726d88dbecea2dad1722 (diff)
downloadcaxlsx-8d55f0d2ad3c5e4f4c6e9be1cf1e7b065fc25e49.tar.gz
caxlsx-8d55f0d2ad3c5e4f4c6e9be1cf1e7b065fc25e49.zip
Add option to define a series color for the BarSeries (#81)
-rw-r--r--examples/README.md1
-rw-r--r--examples/chart_series_color_example.md35
-rw-r--r--examples/images/chart_series_color_example.pngbin0 -> 43955 bytes
-rw-r--r--lib/axlsx/drawing/bar_series.rb19
-rw-r--r--test/drawing/tc_bar_series.rb11
5 files changed, 64 insertions, 2 deletions
diff --git a/examples/README.md b/examples/README.md
index f1ab1d5b..eca725dc 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -48,6 +48,7 @@ Types:
Customizations:
* [Chart colors](chart_colors_example.md)
* [Hide gridlines](hide_gridlines_in_chart_example.md)
+* [Chart series color](chart_series_example.md)
### Columns
diff --git a/examples/chart_series_color_example.md b/examples/chart_series_color_example.md
new file mode 100644
index 00000000..35d1f49e
--- /dev/null
+++ b/examples/chart_series_color_example.md
@@ -0,0 +1,35 @@
+## Description
+
+You could change the colors of the series with providing a series_color. Do not confuse it with defining colors for each datapoint in the series.
+
+## 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']
+
+ sheet.add_row ['', 'Jan.', 'Feb', 'March']
+ sheet.add_row ['Year1', 10, 20, 30]
+ sheet.add_row ['Year2', 15, 25, 35]
+
+ sheet.add_chart(Axlsx::BarChart, start_at: 'A6', end_at: 'F20') do |chart|
+ chart.barDir = :col
+ chart.bg_color = 'FFFFFF'
+ # The first series will be red, but some bars will be overwritten to blue and green
+ chart.add_series data: sheet['B3:D3'], title: sheet['A3'], labels: sheet['B2:D2'], series_color: 'FF0000', colors: ['0000FF', '00FF00']
+ # The second series will be green
+ chart.add_series data: sheet['B4:D4'], title: sheet['A4'], labels: sheet['B2:D2'], series_color: '00FF00'
+ end
+end
+
+p.serialize 'chart_series_color_example.xlsx'
+```
+
+## Output
+
+![Output](images/chart_series_color_example.png "Output")
diff --git a/examples/images/chart_series_color_example.png b/examples/images/chart_series_color_example.png
new file mode 100644
index 00000000..177dc5c2
--- /dev/null
+++ b/examples/images/chart_series_color_example.png
Binary files differ
diff --git a/lib/axlsx/drawing/bar_series.rb b/lib/axlsx/drawing/bar_series.rb
index d266dd09..0e5d1168 100644
--- a/lib/axlsx/drawing/bar_series.rb
+++ b/lib/axlsx/drawing/bar_series.rb
@@ -22,12 +22,18 @@ module Axlsx
# An array of rgb colors to apply to your bar chart.
attr_reader :colors
+ # The fill color for this series.
+ # Red, green, and blue is expressed as sequence of hex digits, RRGGBB.
+ # @return [String]
+ attr_reader :series_color
+
# Creates a new series
# @option options [Array, SimpleTypedList] data
# @option options [Array, SimpleTypedList] labels
# @option options [String] title
# @option options [String] shape
# @option options [String] colors an array of colors to use when rendering each data point
+ # @option options [String] series_color a color to use when rendering series
# @param [Chart] chart
def initialize(chart, options={})
@shape = :box
@@ -40,6 +46,10 @@ module Axlsx
# @see colors
def colors=(v) DataTypeValidator.validate "BarSeries.colors", [Array], v; @colors = v end
+ def series_color=(v)
+ @series_color = v
+ end
+
# @see shape
def shape=(v)
RestrictionValidator.validate "BarSeries.shape", [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax], v
@@ -60,9 +70,16 @@ module Axlsx
str << '</a:solidFill></c:spPr></c:dPt>'
end
+ if series_color
+ str << '<c:spPr><a:solidFill>'
+ str << ('<a:srgbClr val="' << series_color << '"/>')
+ str << '</a:solidFill>'
+ str << '</c:spPr>'
+ end
+
@labels.to_xml_string(str) unless @labels.nil?
@data.to_xml_string(str) unless @data.nil?
- # this is actually only required for shapes other than box
+ # this is actually only required for shapes other than box
str << ('<c:shape val="' << shape.to_s << '"></c:shape>')
end
end
diff --git a/test/drawing/tc_bar_series.rb b/test/drawing/tc_bar_series.rb
index 483e561d..f076fae0 100644
--- a/test/drawing/tc_bar_series.rb
+++ b/test/drawing/tc_bar_series.rb
@@ -6,13 +6,21 @@ class TestBarSeries < Test::Unit::TestCase
p = Axlsx::Package.new
@ws = p.workbook.add_worksheet :name=>"hmmm"
@chart = @ws.add_chart Axlsx::Bar3DChart, :title => "fishery"
- @series = @chart.add_series :data=>[0,1,2], :labels=>["zero", "one", "two"], :title=>"bob", :colors => ['FF0000', '00FF00', '0000FF'], :shape => :cone
+ @series = @chart.add_series(
+ data: [0, 1, 2],
+ labels: ['zero', 'one', 'two'],
+ title: 'bob',
+ colors: ['FF0000', '00FF00', '0000FF'],
+ shape: :cone,
+ series_color: '5A5A5A'
+ )
end
def test_initialize
assert_equal(@series.title.text, "bob", "series title has been applied")
assert_equal(@series.data.class, Axlsx::NumDataSource, "data option applied")
assert_equal(@series.shape, :cone, "series shape has been applied")
+ assert_equal(@series.series_color, '5A5A5A', 'series color has been applied')
assert(@series.data.is_a?(Axlsx::NumDataSource))
assert(@series.labels.is_a?(Axlsx::AxDataSource))
end
@@ -33,5 +41,6 @@ class TestBarSeries < Test::Unit::TestCase
assert_equal(doc.xpath("//c:dPt/c:idx[@val='#{index}']").size,1)
assert_equal(doc.xpath("//c:dPt/c:spPr/a:solidFill/a:srgbClr[@val='#{@series.colors[index]}']").size,1)
end
+ assert_equal(doc.xpath('//c:spPr[not(ancestor::c:dPt)]/a:solidFill/a:srgbClr').first.get_attribute('val'), '5A5A5A', 'series color has been applied')
end
end