From f0c2414d657c4f82821afe4aa331cb91b38b9eb9 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Thu, 15 Nov 2012 09:15:01 +0900 Subject: Updated DataBar conditional formatting to accept cfvo hashes in the initializer to bring the API inline with ColorScale --- lib/axlsx/workbook/worksheet/data_bar.rb | 64 +++++++++++++++++++++----------- test/workbook/worksheet/tc_data_bar.rb | 7 ++++ 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/lib/axlsx/workbook/worksheet/data_bar.rb b/lib/axlsx/workbook/worksheet/data_bar.rb index 7b4c492c..ad636248 100644 --- a/lib/axlsx/workbook/worksheet/data_bar.rb +++ b/lib/axlsx/workbook/worksheet/data_bar.rb @@ -10,20 +10,27 @@ module Axlsx include Axlsx::OptionsParser include Axlsx::SerializedAttributes + class << self + # This differs from ColorScale. There must be exactly two cfvos one color + def default_cfvos + [{:type => :min, :val => "0"}, + {:type => :max, :val => "0"}] + end + end + # Creates a new data bar conditional formatting object + # @param [Hash] options # @option options [Integer] minLength # @option options [Integer] maxLength # @option options [Boolean] showValue # @option options [String] color - the rbg value used to color the bars - def initialize(options = {}) + # @param [Array] cfvos hashes defining the gradient interpolation points for this formatting. + def initialize(options = {}, *cfvos) @min_length = 10 @max_length = 90 @show_value = true - # TODO initialize using the same pattern as color_scale so consumers can override these values - # as they like. - value_objects << Cfvo.new(:type => :min, :val => 0) - value_objects << Cfvo.new(:type => :max, :val => 0) parse_options options + initialize_cfvos(cfvos) yield self if block_given? end @@ -74,27 +81,27 @@ module Axlsx end alias :minLength= :min_length= - # @see maxLength - def max_length=(v) - Axlsx.validate_unsigned_int(v) - @max_length = v - end + # @see maxLength + def max_length=(v) + Axlsx.validate_unsigned_int(v) + @max_length = v + end alias :maxLength= :max_length= - # @see showValue - def show_value=(v) - Axlsx.validate_boolean(v) - @show_value = v - end + # @see showValue + def show_value=(v) + Axlsx.validate_boolean(v) + @show_value = v + end alias :showValue= :show_value= - # Sets the color for the data bars. - # @param [Color|String] v The color object, or rgb string value to apply - def color=(v) - @color = v if v.is_a? Color - self.color.rgb = v if v.is_a? String - @color - end + # Sets the color for the data bars. + # @param [Color|String] v The color object, or rgb string value to apply + def color=(v) + @color = v if v.is_a? Color + self.color.rgb = v if v.is_a? String + @color + end # Serialize this object to an xml string # @param [String] str @@ -107,5 +114,18 @@ module Axlsx self.color.to_xml_string(str) str << '' end + + private + + def initialize_cfvos(cfvos) + self.class.default_cfvos.map.with_index do |default, index| + if index < cfvos.size + value_objects << Cfvo.new(default.merge(cfvos[index])) + else + value_objects << Cfvo.new(default) + end + end + end + end end diff --git a/test/workbook/worksheet/tc_data_bar.rb b/test/workbook/worksheet/tc_data_bar.rb index af803f23..a6194de9 100644 --- a/test/workbook/worksheet/tc_data_bar.rb +++ b/test/workbook/worksheet/tc_data_bar.rb @@ -11,6 +11,13 @@ class TestDataBar < Test::Unit::TestCase assert_equal @data_bar.showValue, true end + def test_override_default_cfvos + data_bar = Axlsx::DataBar.new({:color => 'FF00FF00'}, {:type => :min, :val => "20"}) + assert_equal("20", data_bar.value_objects.first.val) + assert_equal("0", data_bar.value_objects.last.val) + end + + def test_minLength assert_raise(ArgumentError) { @data_bar.minLength = :invalid_type } assert_nothing_raised { @data_bar.minLength = 0} -- cgit v1.2.3