summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-04-27 19:18:05 +0900
committerRandy Morgan <[email protected]>2012-04-27 19:18:05 +0900
commit956117947416cba4ce21737ae0c2a431e4865b8e (patch)
tree045b5993105166eedb9be3760e94716461e1d2f4
parent3748326fe3c09515077ab97f4d2a2b0638e823fd (diff)
downloadcaxlsx-956117947416cba4ce21737ae0c2a431e4865b8e.tar.gz
caxlsx-956117947416cba4ce21737ae0c2a431e4865b8e.zip
colored chart series and examples. Still need to workout scatter as it uses line and shape
-rw-r--r--examples/chart_colors.rb63
-rw-r--r--lib/axlsx/drawing/bar_series.rb17
-rw-r--r--lib/axlsx/drawing/pie_series.rb14
-rw-r--r--lib/axlsx/drawing/series.rb16
4 files changed, 108 insertions, 2 deletions
diff --git a/examples/chart_colors.rb b/examples/chart_colors.rb
new file mode 100644
index 00000000..14725626
--- /dev/null
+++ b/examples/chart_colors.rb
@@ -0,0 +1,63 @@
+#!/usr/bin/env ruby -w -s
+# -*- coding: utf-8 -*-
+$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
+require 'axlsx'
+
+p = Axlsx::Package.new
+wb = p.workbook
+
+##Generating A Bar Chart
+
+wb.add_worksheet(:name => "Bar Chart") do |sheet|
+ sheet.add_row ["A Simple Bar Chart"]
+ sheet.add_row ["First", "Second", "Third"]
+ sheet.add_row [1, 2, 3]
+ sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A4", :end_at => "F17", :title => sheet["A1"]) do |chart|
+ chart.add_series :data => sheet["A3:C3"], :labels => sheet["A2:C2"], :colors => ['FF0000', '00FF00', '0000FF'], :color => "000000"
+ chart.valAxis.label_rotation = -45
+ chart.catAxis.label_rotation = 45
+ end
+end
+
+
+##Generating A Pie Chart
+
+wb.add_worksheet(:name => "Pie Chart") do |sheet|
+ sheet.add_row ["First", "Second", "Third", "Fourth"]
+ sheet.add_row [1, 2, 3, "=PRODUCT(A2:C2)"]
+ sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,2], :end_at => [5, 15], :title => "example 3: Pie Chart") do |chart|
+ chart.add_series :data => sheet["A2:D2"], :labels => sheet["A1:D1"], :colors => ['FF0000', '00FF00', '0000FF']
+ end
+end
+
+# Line Chart
+
+wb.add_worksheet(:name => "Line Chart") do |sheet|
+ sheet.add_row ["First", 1, 5, 7, 9]
+ sheet.add_row ["Second", 5, 2, 14, 9]
+ sheet.add_chart(Axlsx::Line3DChart, :title => "example 6: Line Chart", :rotX => 30, :rotY => 20) do |chart|
+ chart.start_at 0, 2
+ chart.end_at 10, 15
+ chart.add_series :data => sheet["B1:E1"], :title => sheet["A1"], :color => "FF0000"
+ chart.add_series :data => sheet["B2:E2"], :title => sheet["A2"], :color => "00FF00"
+ end
+end
+
+##Generating A Scatter Chart
+
+wb.add_worksheet(:name => "Scatter Chart") do |sheet|
+ sheet.add_row ["First", 1, 5, 7, 9]
+ sheet.add_row ["", 1, 25, 49, 81]
+ sheet.add_row ["Second", 5, 2, 14, 9]
+ sheet.add_row ["", 5, 10, 15, 20]
+ sheet.add_chart(Axlsx::ScatterChart, :title => "example 7: Scatter Chart") do |chart|
+ chart.start_at 0, 4
+ chart.end_at 10, 19
+ chart.add_series :xData => sheet["B1:E1"], :yData => sheet["B2:E2"], :title => sheet["A1"], :color => '0000FF'
+ chart.add_series :xData => sheet["B3:E3"], :yData => sheet["B4:E4"], :title => sheet["A3"], :color => 'FF0000'
+ end
+end
+
+
+p.validate.each { |e| puts e.message }
+p.serialize 'chart_colors.xlsx'
diff --git a/lib/axlsx/drawing/bar_series.rb b/lib/axlsx/drawing/bar_series.rb
index 86ae6367..63c20a31 100644
--- a/lib/axlsx/drawing/bar_series.rb
+++ b/lib/axlsx/drawing/bar_series.rb
@@ -20,19 +20,27 @@ module Axlsx
# @return [Symbol]
attr_reader :shape
+ # An array of rgb colors to apply to your bar chart.
+ attr_reader :colors
+
# 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
# @param [Chart] chart
def initialize(chart, options={})
@shape = :box
+ @colors = []
super(chart, options)
self.labels = CatAxisData.new(options[:labels]) unless options[:labels].nil?
self.data = ValAxisData.new(options[:data]) unless options[:data].nil?
end
+ # @see colors
+ def colors=(v) DataTypeValidator.validate "BarSeries.colors", [Array], v; @colors = v end
+
# The shabe of the bars or columns
# must be one of [:percentStacked, :clustered, :standard, :stacked]
def shape=(v)
@@ -45,9 +53,16 @@ module Axlsx
# @return [String]
def to_xml_string(str = '')
super(str) do |str_inner|
+ colors.each_with_index do |c, index|
+ str << '<c:dPt>'
+ str << '<c:idx val="' << index.to_s << '"/>'
+ str << '<c:spPr><a:solidFill>'
+ str << '<a:srgbClr val="' << c << '"/>'
+ str << '</a:solidFill></c:spPr></c:dPt>'
+ end
@labels.to_xml_string(str_inner) unless @labels.nil?
@data.to_xml_string(str_inner) unless @data.nil?
- str_inner << '<shape val="' << @shape.to_s << '"/>'
+ str_inner << '<c:shape val="' << @shape.to_s << '"/>'
end
end
diff --git a/lib/axlsx/drawing/pie_series.rb b/lib/axlsx/drawing/pie_series.rb
index 0deac1be..7e518886 100644
--- a/lib/axlsx/drawing/pie_series.rb
+++ b/lib/axlsx/drawing/pie_series.rb
@@ -19,6 +19,9 @@ module Axlsx
# @return [Integert]
attr_reader :explosion
+ # An array of rgb colors to apply to your bar chart.
+ attr_reader :colors
+
# Creates a new series
# @option options [Array, SimpleTypedList] data
# @option options [Array, SimpleTypedList] labels
@@ -27,11 +30,15 @@ module Axlsx
# @param [Chart] chart
def initialize(chart, options={})
@explosion = nil
+ @colors = []
super(chart, options)
self.labels = CatAxisData.new(options[:labels]) unless options[:labels].nil?
self.data = ValAxisData.new(options[:data]) unless options[:data].nil?
end
+ # @see colors
+ def colors=(v) DataTypeValidator.validate "BarSeries.colors", [Array], v; @colors = v end
+
# @see explosion
def explosion=(v) Axlsx::validate_unsigned_int(v); @explosion = v; end
@@ -41,6 +48,13 @@ module Axlsx
def to_xml_string(str = '')
super(str) do |str_inner|
str_inner << '<c:explosion val="' << @explosion << '"/>' unless @explosion.nil?
+ colors.each_with_index do |c, index|
+ str << '<c:dPt>'
+ str << '<c:idx val="' << index.to_s << '"/>'
+ str << '<c:spPr><a:solidFill>'
+ str << '<a:srgbClr val="' << c << '"/>'
+ str << '</a:solidFill></c:spPr></c:dPt>'
+ end
@labels.to_xml_string str_inner unless @labels.nil?
@data.to_xml_string str_inner unless @data.nil?
end
diff --git a/lib/axlsx/drawing/series.rb b/lib/axlsx/drawing/series.rb
index 798919b5..44e82ae1 100644
--- a/lib/axlsx/drawing/series.rb
+++ b/lib/axlsx/drawing/series.rb
@@ -10,6 +10,11 @@ module Axlsx
# @return [Chart]
attr_reader :chart
+ # 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
+
# The title of the series
# @return [SeriesTitle]
attr_reader :title
@@ -27,6 +32,11 @@ module Axlsx
end
end
+ # @see color
+ def color=(v)
+ @color = v
+ end
+
# The index of this series in the chart's series.
# @return [Integer]
def index
@@ -63,10 +73,14 @@ module Axlsx
str << '<c:idx val="' << index.to_s << '"/>'
str << '<c:order val="' << (order || index).to_s << '"/>'
title.to_xml_string(str) unless title.nil?
+ if color
+ str << '<c:spPr><a:solidFill>'
+ str << '<a:srgbClr val="' << color << '"/>'
+ str << '</a:solidFill></c:spPr>'
+ end
yield str if block_given?
str << '</c:ser>'
end
-
end
end