Class: Axlsx::DLbls
- Inherits:
-
Object
- Object
- Axlsx::DLbls
- Includes:
- Accessors, OptionsParser
- Defined in:
- lib/axlsx/drawing/d_lbls.rb
Overview
There are more elements in the dLbls spec that allow for customizations and formatting. For now, I am just implementing the basics. The DLbls class manages serialization of data labels showLeaderLines and leaderLines are not currently implemented
Instance Attribute Summary collapse
-
#chart_type ⇒ Object
readonly
The chart type that is using this data lables instance.
Instance Method Summary collapse
-
#d_lbl_pos ⇒ Symbol
The position of the data labels in the chart.
-
#d_lbl_pos=(label_position) ⇒ Object
Assigns the label postion for this data labels on this chart.
-
#initialize(chart_type, options = {}) ⇒ DLbls
constructor
creates a new DLbls object.
-
#initialize_defaults ⇒ Object
Initialize all the values to false as Excel requires them to explicitly be disabled or all will show.
-
#to_xml_string(str = +'')) ⇒ String
serializes the data labels.
-
#validate_attributes_for_chart_type ⇒ Object
nills out d_lbl_pos and show_leader_lines as these attributes, while valid in the spec actually crash Excel for any chart type other than pie charts.
Methods included from OptionsParser
Constructor Details
#initialize(chart_type, options = {}) ⇒ DLbls
creates a new DLbls object
13 14 15 16 17 18 19 |
# File 'lib/axlsx/drawing/d_lbls.rb', line 13 def initialize(chart_type, = {}) raise ArgumentError, 'chart_type must inherit from Chart' unless [Chart, LineChart].include?(chart_type.superclass) @chart_type = chart_type initialize_defaults end |
Instance Attribute Details
#chart_type ⇒ Object (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.
48 49 50 |
# File 'lib/axlsx/drawing/d_lbls.rb', line 48 def chart_type @chart_type end |
Instance Method Details
#d_lbl_pos ⇒ Symbol
The position of the data labels in the chart
53 54 55 56 57 |
# File 'lib/axlsx/drawing/d_lbls.rb', line 53 def d_lbl_pos return unless [PieChart, Pie3DChart, LineChart].include? @chart_type @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 :bestFit, :b, :ctr, :inBase, :inEnd, :l, :outEnd, :r and :t The default is :bestFit
65 66 67 68 69 70 |
# File 'lib/axlsx/drawing/d_lbls.rb', line 65 def d_lbl_pos=(label_position) return unless [PieChart, Pie3DChart, LineChart].include? @chart_type 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_defaults ⇒ Object
Initialize all the values to false as Excel requires them to explicitly be disabled or all will show.
37 38 39 40 41 42 43 |
# File 'lib/axlsx/drawing/d_lbls.rb', line 37 def initialize_defaults [:show_legend_key, :show_val, :show_cat_name, :show_ser_name, :show_percent, :show_bubble_size, :show_leader_lines].each do |attr| send("#{attr}=", false) end end |
#to_xml_string(str = +'')) ⇒ String
serializes the data labels
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/axlsx/drawing/d_lbls.rb', line 74 def to_xml_string(str = +'') validate_attributes_for_chart_type str << '<c:dLbls>' instance_vals = Axlsx.instance_values_for(self) %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_vals.key?(key) && !instance_vals[key].nil? str << "<c:#{Axlsx.camel(key, false)} val='#{instance_vals[key]}' />" end str << '</c:dLbls>' end |
#validate_attributes_for_chart_type ⇒ Object
nills out d_lbl_pos and show_leader_lines as these attributes, while valid in the spec actually crash Excel for any chart type other than pie charts.
87 88 89 90 91 92 |
# File 'lib/axlsx/drawing/d_lbls.rb', line 87 def validate_attributes_for_chart_type return if [PieChart, Pie3DChart, LineChart].include? @chart_type @d_lbl_pos = nil @show_leader_lines = nil end |