Class: Uniword::Builder::ChartBuilder

Inherits:
Object
  • Object
show all
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.

Examples:

Add a bar chart to a document

doc.chart do |c|
  c.title 'Sales Report'
  c.categories ['Q1', 'Q2', 'Q3', 'Q4']
  c.series 'Revenue', data: [100, 200, 150, 300]
  c.series 'Costs', data: [80, 120, 100, 200]
end

Add a pie chart

doc.chart(type: :pie) do |c|
  c.title 'Market Share'
  c.categories ['Product A', 'Product B', 'Product C']
  c.series 'Share', data: [45, 30, 25]
end

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

Instance Method Summary collapse

Methods included from DeterministicId

#deterministic_id

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_typeObject (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

#heightObject (readonly)

Returns the value of attribute height.



39
40
41
# File 'lib/uniword/builder/chart_builder.rb', line 39

def height
  @height
end

#series_listObject (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_legendObject (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_textObject (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

#widthObject (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

Parameters:

Returns:



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_xmlString

Build the chart XML string

Returns:

  • (String)

    Chart XML



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

Parameters:

  • labels (Array<String>, nil) (defaults to: nil)

    Category labels (setter if provided)

Returns:

  • (Array<String>, self)

    Categories array (getter) or self (setter)



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

Parameters:

  • width (Integer)

    Width in EMU (914400 = 1 inch)

  • height (Integer)

    Height in EMU

Returns:

  • (self)


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

Parameters:

  • show (Boolean) (defaults to: true)

    Show legend (default true)

  • position (String) (defaults to: "b")

    Position: 't', 'b', 'l', 'r', 'tr'

Returns:

  • (self)


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

Parameters:

  • name (String)

    Series name

  • data (Array<Numeric>)

    Data values

Returns:

  • (self)


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