diff options
| author | Randy Morgan <[email protected]> | 2012-07-23 09:13:44 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-07-23 09:13:44 +0900 |
| commit | c1d047bc2f4f913fef1ebdaa956a358942bc90c6 (patch) | |
| tree | 0a7e0ce144fa9a6447573d60cd5e2d49ce250e50 /lib/axlsx/drawing/d_lbls.rb | |
| parent | e75a5215c465ad70e5ea82bd48035531517a3b45 (diff) | |
| download | caxlsx-c1d047bc2f4f913fef1ebdaa956a358942bc90c6.tar.gz caxlsx-c1d047bc2f4f913fef1ebdaa956a358942bc90c6.zip | |
more cleanup for optional data label attributes
Diffstat (limited to 'lib/axlsx/drawing/d_lbls.rb')
| -rw-r--r-- | lib/axlsx/drawing/d_lbls.rb | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/lib/axlsx/drawing/d_lbls.rb b/lib/axlsx/drawing/d_lbls.rb index 13882056..f7126a67 100644 --- a/lib/axlsx/drawing/d_lbls.rb +++ b/lib/axlsx/drawing/d_lbls.rb @@ -24,17 +24,17 @@ module Axlsx # Bar3DChart and Line3DChart and ScatterChart do not support d_lbl_pos or show_leader_lines # BOOLEAN_ATTRIBUTES = [:show_legend_key, :show_val, :show_cat_name, :show_ser_name, :show_percent, :show_bubble_size, :show_leader_lines] - + # creates a new DLbls object - def initialize(options={}) - @d_lbl_pos = :bestFit + def initialize(chart_type, options={}) + raise ArgumentError, 'chart_type must inherit from Chart' unless chart_type.superclass == Chart + @chart_type = chart_type initialize_defaults options.each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end - end - + # Initialize all the values to false as Excel requires them to # explicitly be disabled or all will show. def initialize_defaults @@ -43,22 +43,31 @@ module Axlsx end end + # The chart type that is using this data lables instance. + # This affects the xml output as not all chart types support the + # same data label attributes. + attr_reader :chart_type + # The position of the data labels in the chart # @see d_lbl_pos= for a list of allowed values # @return [Symbol] - attr_reader :d_lbl_pos + def d_lbl_pos + return unless @chart_type == Pie3DChart + @d_lbl_pos ||= :bestFit + end # @see DLbls#d_lbl_pos # Assigns the label postion for this data labels on this chart. # Allowed positions are :bestFilt, :b, :ctr, :inBase, :inEnd, :l, # :outEnd, :r and :t - # The default is :best_fit + # The default is :bestFit # @param [Symbol] label_postion the postion you want to use. def d_lbl_pos=(label_position) + return unless @chart_type == Pie3DChart Axlsx::RestrictionValidator.validate 'DLbls#d_lbl_pos', [:bestFit, :b, :ctr, :inBase, :inEnd, :l, :outEnd, :r, :t], label_position @d_lbl_pos = label_position end - + # Dynamically create accessors for boolean attriubtes BOOLEAN_ATTRIBUTES.each do |attr| class_eval %{ @@ -80,10 +89,19 @@ module Axlsx # serializes the data labels # @return [String] def to_xml_string(str = '') + validate_attributes_for_chart_type str << '<c:dLbls>' instance_values.each { |name, value| str << "<c:#{Axlsx::camel(name, false)} val='#{value}' />" } str << '</c:dLbls>' end + + # nills out d_lbl_pos and show_leader_lines as these attributes, while valid in the spec actually chrash excel for any chart type other than pie charts. + def validate_attributes_for_chart_type + return if @chart_type == Pie3DChart + @d_lbl_pos = nil + @show_leader_lines = nil + end + + end end - |
