Class: Uniword::Builder::ChartBuilder

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

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_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

#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

#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

Instance Method Details

#build_drawing(document) ⇒ Wordprocessingml::Drawing

Register chart on a document and create the Drawing element

Parameters:

Returns:



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_xmlString

Build the chart XML string

Returns:

  • (String)

    Chart XML



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

Parameters:

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

    Category labels (setter if provided)

Returns:

  • (Array<String>, self)

    Categories array (getter) or self (setter)



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

Parameters:

  • width (Integer)

    Width in EMU (914400 = 1 inch)

  • height (Integer)

    Height in EMU

Returns:

  • (self)


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

Parameters:

  • show (Boolean) (defaults to: true)

    Show legend (default true)

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

    Position: ‘t’, ‘b’, ‘l’, ‘r’, ‘tr’

Returns:

  • (self)


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

Parameters:

  • name (String)

    Series name

  • data (Array<Numeric>)

    Data values

Returns:

  • (self)


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