Class: Axlsx::DLbls

Inherits:
Object
  • Object
show all
Defined in:
lib/axlsx/drawing/d_lbls.rb

Overview

The DLbls class manages serialization of data labels showLeaderLines and leaderLines are not currently implemented

Constant Summary collapse

BOOLEAN_ATTRIBUTES =
Note:

not all charts support all methods! Bar3DChart and Line3DChart and ScatterChart do not support d_lbl_pos or show_leader_lines

These attributes are all boolean so I'm doing a bit of a hand waving magic show to set up the attriubte accessors

[:show_legend_key, :show_val, :show_cat_name, :show_ser_name, :show_percent, :show_bubble_size, :show_leader_lines]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chart_type, options = {}) ⇒ DLbls

creates a new DLbls object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
# File 'lib/axlsx/drawing/d_lbls.rb', line 19

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

Instance Attribute Details

#chart_typeObject (readonly)

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.



39
40
41
# File 'lib/axlsx/drawing/d_lbls.rb', line 39

def chart_type
  @chart_type
end

Instance Method Details

#d_lbl_posSymbol

The position of the data labels in the chart

Returns:

  • (Symbol)

See Also:



44
45
46
47
# File 'lib/axlsx/drawing/d_lbls.rb', line 44

def d_lbl_pos
  return unless @chart_type == Pie3DChart
  @d_lbl_pos ||= :bestFit
end

#d_lbl_pos=(label_position) ⇒ Object

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 :bestFit

Parameters:

  • label_position (Symbol)

    the postion you want to use.

See Also:



55
56
57
58
59
# File 'lib/axlsx/drawing/d_lbls.rb', line 55

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

#initialize_defaultsObject

Initialize all the values to false as Excel requires them to explicitly be disabled or all will show.



30
31
32
33
34
# File 'lib/axlsx/drawing/d_lbls.rb', line 30

def initialize_defaults
  BOOLEAN_ATTRIBUTES.each do |attr|
    self.send("#{attr}=", false)
  end
end

#to_xml_string(str = '') ⇒ String

serializes the data labels

Returns:

  • (String)


81
82
83
84
85
86
87
88
89
# File 'lib/axlsx/drawing/d_lbls.rb', line 81

def to_xml_string(str = '')
  validate_attributes_for_chart_type
  str << '<c:dLbls>'
  %w(d_lbl_pos show_legend_key show_val show_cat_name show_ser_name show_percent show_bubble_size show_leader_lines).each do |key|
    next unless instance_values.keys.include?(key) && instance_values[key] != nil
    str <<  "<c:#{Axlsx::camel(key, false)} val='#{instance_values[key]}' />" 
  end
  str << '</c:dLbls>'
end

#validate_attributes_for_chart_typeObject

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.



92
93
94
95
96
# File 'lib/axlsx/drawing/d_lbls.rb', line 92

def validate_attributes_for_chart_type
  return if @chart_type == Pie3DChart
  @d_lbl_pos = nil
  @show_leader_lines = nil
end