Class: Uniword::Builder::ChartBuilder
- Inherits:
-
Object
- Object
- Uniword::Builder::ChartBuilder
- Defined in:
- lib/uniword/builder/chart_builder.rb
Overview
Builds chart elements for embedding in documents.
Creates chart XML (ChartSpace) and the Drawing wrapper that references it via relationship ID.
Supports bar, line, and pie charts with literal data.
Constant Summary collapse
- CHART_NS =
"http://schemas.openxmlformats.org/drawingml/2006/chart"- CHART_REL_TYPE =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart"- CHART_CONTENT_TYPE =
"application/vnd.openxmlformats-officedocument." \ "drawingml.chart+xml"
Instance Attribute Summary collapse
-
#chart_type ⇒ Object
readonly
Returns the value of attribute chart_type.
-
#series_list ⇒ Object
readonly
Returns the value of attribute series_list.
-
#title_text ⇒ Object
readonly
Returns the value of attribute title_text.
Instance Method Summary collapse
-
#build_drawing(document) ⇒ Wordprocessingml::Drawing
Register chart on a document and create the Drawing element.
-
#build_xml ⇒ String
Build the chart XML string.
-
#categories(labels = nil) ⇒ Array<String>, self
Get or set category labels.
-
#dimensions(width:, height:) ⇒ self
Set chart dimensions.
-
#initialize(chart_type: :bar) ⇒ ChartBuilder
constructor
A new instance of ChartBuilder.
-
#legend(show: true, position: "b") ⇒ self
Configure legend.
-
#series(name, data:) ⇒ self
Add a data series.
- #title(text) ⇒ Object
Constructor Details
#initialize(chart_type: :bar) ⇒ ChartBuilder
Returns a new instance of ChartBuilder.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/uniword/builder/chart_builder.rb', line 39 def initialize(chart_type: :bar) @chart_type = chart_type @title_text = nil @categories = [] @series_list = [] @show_legend = true @legend_position = "b" @width = 5_486_400 # default 6 inches in EMU @height = 3_200_400 # default ~3.5 inches in EMU end |
Instance Attribute Details
#chart_type ⇒ Object (readonly)
Returns the value of attribute chart_type.
37 38 39 |
# File 'lib/uniword/builder/chart_builder.rb', line 37 def chart_type @chart_type end |
#series_list ⇒ Object (readonly)
Returns the value of attribute series_list.
37 38 39 |
# File 'lib/uniword/builder/chart_builder.rb', line 37 def series_list @series_list end |
#title_text ⇒ Object (readonly)
Returns the value of attribute title_text.
37 38 39 |
# File 'lib/uniword/builder/chart_builder.rb', line 37 def title_text @title_text end |
Instance Method Details
#build_drawing(document) ⇒ Wordprocessingml::Drawing
Register chart on a document and create the Drawing element
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/uniword/builder/chart_builder.rb', line 120 def build_drawing(document) root = document.respond_to?(:model) ? document.model : document root.chart_parts ||= {} r_id = "rIdChart#{root.chart_parts.size + 1}" target = "charts/chart#{root.chart_parts.size + 1}.xml" root.chart_parts[r_id] = { xml: build_xml, target: target, } create_drawing(r_id) end |
#build_xml ⇒ String
Build the chart XML string
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/uniword/builder/chart_builder.rb', line 103 def build_xml builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml| xml["c"].chartSpace("xmlns:c" => CHART_NS, "xmlns:a" => "http://schemas.openxmlformats.org/drawingml/2006/main", "xmlns:r" => "http://schemas.openxmlformats.org/officeDocument/2006/relationships") do build_chart_xml(xml) end end builder.to_xml end |
#categories(labels = nil) ⇒ Array<String>, self
Get or set category labels
54 55 56 57 58 59 60 61 |
# File 'lib/uniword/builder/chart_builder.rb', line 54 def categories(labels = nil) if labels @categories = labels self else @categories end end |
#dimensions(width:, height:) ⇒ self
Set chart dimensions
94 95 96 97 98 |
# File 'lib/uniword/builder/chart_builder.rb', line 94 def dimensions(width:, height:) @width = width @height = height self end |
#legend(show: true, position: "b") ⇒ self
Configure legend
83 84 85 86 87 |
# File 'lib/uniword/builder/chart_builder.rb', line 83 def legend(show: true, position: "b") @show_legend = show @legend_position = position self end |
#series(name, data:) ⇒ self
Add a data series
73 74 75 76 |
# File 'lib/uniword/builder/chart_builder.rb', line 73 def series(name, data:) @series_list << { name: name, data: data } self end |
#title(text) ⇒ Object
63 64 65 66 |
# File 'lib/uniword/builder/chart_builder.rb', line 63 def title(text) @title_text = text self end |