From a7e9549a48f3ea61ff8a596307dc3a0e4d5876f8 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Sat, 28 Apr 2012 11:54:58 +0900 Subject: support for specifying the colors to use for data series in charts. --- lib/axlsx/drawing/bar_series.rb | 16 +++++++++------- lib/axlsx/drawing/line_series.rb | 18 +++++++++++++++++- lib/axlsx/drawing/pie_series.rb | 2 +- lib/axlsx/drawing/scatter_series.rb | 28 +++++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/lib/axlsx/drawing/bar_series.rb b/lib/axlsx/drawing/bar_series.rb index 63c20a31..25523410 100644 --- a/lib/axlsx/drawing/bar_series.rb +++ b/lib/axlsx/drawing/bar_series.rb @@ -35,7 +35,7 @@ module Axlsx @colors = [] super(chart, options) self.labels = CatAxisData.new(options[:labels]) unless options[:labels].nil? - self.data = ValAxisData.new(options[:data]) unless options[:data].nil? + self.data = NamedAxisData.new(:val, options[:data]) unless options[:data].nil? end # @see colors @@ -53,16 +53,18 @@ module Axlsx # @return [String] def to_xml_string(str = '') super(str) do |str_inner| + colors.each_with_index do |c, index| - str << '' - str << '' - str << '' - str << '' - str << '' + str_inner << '' + str_inner << '' + str_inner << '' + str_inner << '' + str_inner << '' end + @labels.to_xml_string(str_inner) unless @labels.nil? @data.to_xml_string(str_inner) unless @data.nil? - str_inner << '' + str_inner << '' end end diff --git a/lib/axlsx/drawing/line_series.rb b/lib/axlsx/drawing/line_series.rb index ac3840f1..1d785a23 100644 --- a/lib/axlsx/drawing/line_series.rb +++ b/lib/axlsx/drawing/line_series.rb @@ -14,6 +14,11 @@ module Axlsx # @return [CatAxisData] attr_reader :labels + # 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 series # @option options [Array, SimpleTypedList] data # @option options [Array, SimpleTypedList] labels @@ -22,7 +27,12 @@ module Axlsx @labels, @data = nil, nil super(chart, options) @labels = CatAxisData.new(options[:labels]) unless options[:labels].nil? - @data = ValAxisData.new(options[:data]) unless options[:data].nil? + @data = NamedAxisData.new('val', options[:data]) unless options[:data].nil? + end + + # @see color + def color=(v) + @color = v end # Serializes the object @@ -30,6 +40,12 @@ module Axlsx # @return [String] def to_xml_string(str = '') super(str) do + if color + str << '' + str << '' + str << '' + end + @labels.to_xml_string(str) unless @labels.nil? @data.to_xml_string(str) unless @data.nil? end diff --git a/lib/axlsx/drawing/pie_series.rb b/lib/axlsx/drawing/pie_series.rb index 7e518886..13f3cd5d 100644 --- a/lib/axlsx/drawing/pie_series.rb +++ b/lib/axlsx/drawing/pie_series.rb @@ -33,7 +33,7 @@ module Axlsx @colors = [] super(chart, options) self.labels = CatAxisData.new(options[:labels]) unless options[:labels].nil? - self.data = ValAxisData.new(options[:data]) unless options[:data].nil? + self.data = NamedAxisData.new(:val, options[:data]) unless options[:data].nil? end # @see colors diff --git a/lib/axlsx/drawing/scatter_series.rb b/lib/axlsx/drawing/scatter_series.rb index 2469e0bf..3f4bf289 100644 --- a/lib/axlsx/drawing/scatter_series.rb +++ b/lib/axlsx/drawing/scatter_series.rb @@ -16,20 +16,46 @@ module Axlsx # @return [NamedAxisData] attr_reader :yData + # 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 ScatterSeries def initialize(chart, options={}) @xData, @yData = nil super(chart, options) - @xData = NamedAxisData.new("xVal", options[:xData]) unless options[:xData].nil? @yData = NamedAxisData.new("yVal", options[:yData]) unless options[:yData].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 << '' + str << '' + str << '' + str << '' + str << '' + str << '' + str << '' + str << '' + str << '' + str << '' + str << '' + str << '' + str << '' + str << '' + end @xData.to_xml_string(inner_str) unless @xData.nil? @yData.to_xml_string(inner_str) unless @yData.nil? end -- cgit v1.2.3