Class: Uniword::Builder::ChartBuilder
- Inherits:
-
Object
- Object
- Uniword::Builder::ChartBuilder
- Includes:
- DeterministicId
- 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.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#series_list ⇒ Object
readonly
Returns the value of attribute series_list.
-
#show_legend ⇒ Object
readonly
Returns the value of attribute show_legend.
-
#title_text ⇒ Object
readonly
Returns the value of attribute title_text.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
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
Methods included from DeterministicId
Constructor Details
#initialize(chart_type: :bar) ⇒ ChartBuilder
Returns a new instance of ChartBuilder.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/uniword/builder/chart_builder.rb', line 41 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 |
#height ⇒ Object (readonly)
Returns the value of attribute height.
39 40 41 |
# File 'lib/uniword/builder/chart_builder.rb', line 39 def height @height 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 |
#show_legend ⇒ Object (readonly)
Returns the value of attribute show_legend.
39 40 41 |
# File 'lib/uniword/builder/chart_builder.rb', line 39 def show_legend @show_legend 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 |
#width ⇒ Object (readonly)
Returns the value of attribute width.
39 40 41 |
# File 'lib/uniword/builder/chart_builder.rb', line 39 def width @width end |
Instance Method Details
#build_drawing(document) ⇒ Wordprocessingml::Drawing
Register chart on a document and create the Drawing element
122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/uniword/builder/chart_builder.rb', line 122 def build_drawing(document) root = document.is_a?(Uniword::Builder::DocumentBuilder) ? 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
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/uniword/builder/chart_builder.rb', line 105 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
56 57 58 59 60 61 62 63 |
# File 'lib/uniword/builder/chart_builder.rb', line 56 def categories(labels = nil) if labels @categories = labels self else @categories end end |
#dimensions(width:, height:) ⇒ self
Set chart dimensions
96 97 98 99 100 |
# File 'lib/uniword/builder/chart_builder.rb', line 96 def dimensions(width:, height:) @width = width @height = height self end |
#legend(show: true, position: "b") ⇒ self
Configure legend
85 86 87 88 89 |
# File 'lib/uniword/builder/chart_builder.rb', line 85 def legend(show: true, position: "b") @show_legend = show @legend_position = position self end |
#series(name, data:) ⇒ self
Add a data series
75 76 77 78 |
# File 'lib/uniword/builder/chart_builder.rb', line 75 def series(name, data:) @series_list << { name: name, data: data } self end |
#title(text) ⇒ Object
65 66 67 68 |
# File 'lib/uniword/builder/chart_builder.rb', line 65 def title(text) @title_text = text self end |