lib/write_xlsx/chart.rb in write_xlsx-0.90.0 vs lib/write_xlsx/chart.rb in write_xlsx-0.97.0
- old
+ new
@@ -8,26 +8,34 @@
module Writexlsx
class Table
include Writexlsx::Utility
- attr_reader :horizontal, :vertical, :outline, :show_keys
+ attr_reader :horizontal, :vertical, :outline, :show_keys, :font
def initialize(params = {})
@horizontal, @vertical, @outline, @show_keys = true, true, true, false
@horizontal = params[:horizontal] if params.has_key?(:horizontal)
@vertical = params[:vertical] if params.has_key?(:vertical)
@outline = params[:outline] if params.has_key?(:outline)
@show_keys = params[:show_keys] if params.has_key?(:show_keys)
+ @font = convert_font_args(params[:font])
end
+ def palette=(palette)
+ @palette = palette
+ end
+
def write_d_table(writer)
- writer.tag_elements('c:dTable') do
- writer.empty_tag('c:showHorzBorder', attributes) if ptrue?(horizontal)
- writer.empty_tag('c:showVertBorder', attributes) if ptrue?(vertical)
- writer.empty_tag('c:showOutline', attributes) if ptrue?(outline)
- writer.empty_tag('c:showKeys', attributes) if ptrue?(show_keys)
+ @writer = writer
+ @writer.tag_elements('c:dTable') do
+ @writer.empty_tag('c:showHorzBorder', attributes) if ptrue?(horizontal)
+ @writer.empty_tag('c:showVertBorder', attributes) if ptrue?(vertical)
+ @writer.empty_tag('c:showOutline', attributes) if ptrue?(outline)
+ @writer.empty_tag('c:showKeys', attributes) if ptrue?(show_keys)
+ # Write the table font.
+ write_tx_pr(nil, font) if ptrue?(font)
end
end
private
@@ -262,10 +270,14 @@
if @requires_category != 0 && !params.has_key?(:categories)
raise "Must specify ':categories' in add_series for this chart type"
end
+ if @series.size == 255
+ raise "The maximum number of series that can be added to an Excel Chart is 255."
+ end
+
@series << Series.new(self, params)
# Set the secondary axis properties.
x2_axis = params[:x2_axis]
y2_axis = params[:y2_axis]
@@ -415,10 +427,11 @@
# The set_table method adds a data table below the horizontal axis with the
# data used to plot the chart.
#
def set_table(params = {})
@table = Table.new(params)
+ @table.palette = @palette
end
#
# Set properties for the chart up-down bars.
#
@@ -516,43 +529,10 @@
write_axis_ids(params)
end
end
#
- # Convert user defined font values into private hash values.
- #
- def convert_font_args(params)
- return unless params
- font = params_to_font(params)
-
- # Convert font size units.
- font[:_size] *= 100 if font[:_size] && font[:_size] != 0
-
- # Convert rotation into 60,000ths of a degree.
- if ptrue?(font[:_rotation])
- font[:_rotation] = 60_000 * font[:_rotation].to_i
- end
-
- font
- end
-
- def params_to_font(params)
- {
- :_name => params[:name],
- :_color => params[:color],
- :_size => params[:size],
- :_bold => params[:bold],
- :_italic => params[:italic],
- :_underline => params[:underline],
- :_pitch_family => params[:pitch_family],
- :_charset => params[:charset],
- :_baseline => params[:baseline] || 0,
- :_rotation => params[:rotation]
- }
- end
-
- #
# Switch name and name_formula parameters if required.
#
def process_names(name = nil, name_formula = nil) # :nodoc:
# Name looks like a formula, use it to set name_formula.
if name.respond_to?(:to_ary)
@@ -671,24 +651,10 @@
# The series data was all numeric.
'num'
end
#
- # Convert the user specified colour index or string to a rgb colour.
- #
- def color(color_code) # :nodoc:
- if color_code and color_code =~ /^#[0-9a-fA-F]{6}$/
- # Convert a HTML style #RRGGBB color.
- color_code.sub(/^#/, '').upcase
- else
- index = Format.color(color_code)
- raise "Unknown color '#{color_code}' used in chart formatting." unless index
- palette_color(index)
- end
- end
-
- #
# Returns series which use the primary axes.
#
def get_primary_axes_series
@series.reject {|s| s.y2_axis}
end
@@ -722,42 +688,10 @@
[id1, id2]
end
#
- # Get the font style attributes from a font hash.
- #
- def get_font_style_attributes(font)
- return [] unless font
-
- attributes = []
- attributes << ['sz', font[:_size]] if ptrue?(font[:_size])
- attributes << ['b', font[:_bold]] if font[:_bold]
- attributes << ['i', font[:_italic]] if font[:_italic]
- attributes << ['u', 'sng'] if font[:_underline]
-
- # Turn off baseline when testing fonts that don't have it.
- if font[:_baseline] != -1
- attributes << ['baseline', font[:_baseline]]
- end
- attributes
- end
-
- #
- # Get the font latin attributes from a font hash.
- #
- def get_font_latin_attributes(font)
- return [] unless font
-
- attributes = []
- attributes << ['typeface', font[:_name]] if ptrue?(font[:_name])
- attributes << ['pitchFamily', font[:_pitch_family]] if font[:_pitch_family]
- attributes << ['charset', font[:_charset]] if font[:_charset]
-
- attributes
- end
- #
# Setup the default properties for a chart.
#
def set_default_properties # :nodoc:
display_setup
axis_setup
@@ -1936,31 +1870,11 @@
write_a_lst_style
# Write the a:p element.
write_a_p_rich(title)
end
end
-
#
- # Write the <a:bodyPr> element.
- #
- def write_a_body_pr(rot, horiz = nil) # :nodoc:
- rot = -5400000 if !rot && ptrue?(horiz)
- attributes = []
- attributes << ['rot', rot] if rot
- attributes << ['vert', 'horz'] if ptrue?(horiz)
-
- @writer.empty_tag('a:bodyPr', attributes)
- end
-
- #
- # Write the <a:lstStyle> element.
- #
- def write_a_lst_style # :nodoc:
- @writer.empty_tag('a:lstStyle')
- end
-
- #
# Write the <a:p> element for rich string titles.
#
def write_a_p_rich(title) # :nodoc:
@writer.tag_elements('a:p') do
# Write the a:pPr element.
@@ -1969,53 +1883,17 @@
write_a_r(title)
end
end
#
- # Write the <a:p> element for formula titles.
- #
- def write_a_p_formula(font = nil) # :nodoc:
- @writer.tag_elements('a:p') do
- # Write the a:pPr element.
- write_a_p_pr_formula(font)
- # Write the a:endParaRPr element.
- write_a_end_para_rpr
- end
- end
-
- #
# Write the <a:pPr> element for rich string titles.
#
def write_a_p_pr_rich(font) # :nodoc:
@writer.tag_elements('a:pPr') { write_a_def_rpr(font) }
end
#
- # Write the <a:pPr> element for formula titles.
- #
- def write_a_p_pr_formula(font) # :nodoc:
- @writer.tag_elements('a:pPr') { write_a_def_rpr(font) }
- end
-
- #
- # Write the <a:defRPr> element.
- #
- def write_a_def_rpr(font = nil) # :nodoc:
- write_def_rpr_r_pr_common(
- font,
- get_font_style_attributes(font),
- 'a:defRPr')
- end
-
- #
- # Write the <a:endParaRPr> element.
- #
- def write_a_end_para_rpr # :nodoc:
- @writer.empty_tag('a:endParaRPr', [ ['lang', 'en-US'] ])
- end
-
- #
# Write the <a:r> element.
#
def write_a_r(title) # :nodoc:
@writer.tag_elements('a:r') do
# Write the a:rPr element.
@@ -2034,54 +1912,18 @@
attributes += attr_font unless attr_font.empty?
write_def_rpr_r_pr_common(font, attributes, 'a:rPr')
end
- def write_def_rpr_r_pr_common(font, style_attributes, tag) # :nodoc:
- latin_attributes = get_font_latin_attributes(font)
- has_color = ptrue?(font) && ptrue?(font[:_color])
-
- if !latin_attributes.empty? || has_color
- @writer.tag_elements(tag, style_attributes) do
- if has_color
- write_a_solid_fill(:color => font[:_color])
- end
- if !latin_attributes.empty?
- write_a_latin(latin_attributes)
- end
- end
- else
- @writer.empty_tag(tag, style_attributes)
- end
- end
-
#
# Write the <a:t> element.
#
def write_a_t(title) # :nodoc:
@writer.data_element('a:t', title)
end
#
- # Write the <c:txPr> element.
- #
- def write_tx_pr(horiz, font) # :nodoc:
- rotation = nil
- if font && font[:_rotation]
- rotation = font[:_rotation]
- end
- @writer.tag_elements('c:txPr') do
- # Write the a:bodyPr element.
- write_a_body_pr(rotation, horiz)
- # Write the a:lstStyle element.
- write_a_lst_style
- # Write the a:p element.
- write_a_p_formula(font)
- end
- end
-
- #
# Write the <c:marker> element.
#
def write_marker(marker = nil) # :nodoc:
marker ||= @default_marker
@@ -2198,38 +2040,10 @@
def write_a_no_fill # :nodoc:
@writer.empty_tag('a:noFill')
end
#
- # Write the <a:solidFill> element.
- #
- def write_a_solid_fill(fill) # :nodoc:
- @writer.tag_elements('a:solidFill') do
- if fill[:color]
- # Write the a:srgbClr element.
- write_a_srgb_clr(color(fill[:color]), fill[:transparency])
- end
- end
- end
-
- #
- # Write the <a:srgbClr> element.
- #
- def write_a_srgb_clr(color, transparency = nil) # :nodoc:
- tag = 'a:srgbClr'
- attributes = [ ['val', color] ]
-
- if ptrue?(transparency)
- @writer.tag_elements(tag, attributes) do
- write_a_alpha(transparency)
- end
- else
- @writer.empty_tag(tag, attributes)
- end
- end
-
- #
# Write the <a:alpha> element.
#
def write_a_alpha(val)
val = (100 - val.to_i) * 1000
@@ -2262,10 +2076,24 @@
write_period(trendline.period) if trendline.type == 'movingAvg'
# Write the c:forward element.
write_forward(trendline.forward)
# Write the c:backward element.
write_backward(trendline.backward)
+ if trendline.intercept
+ # Write the c:intercept element.
+ write_intercept(trendline.intercept)
+ end
+ if trendline.display_r_squared
+ # Write the c:dispRSqr element.
+ write_disp_rsqr
+ end
+ if trendline.display_equation
+ # Write the c:dispEq element.
+ write_disp_eq
+ # Write the c:trendlineLbl element.
+ write_trendline_lbl
+ end
end
end
#
# Write the <c:trendlineType> element.
@@ -2311,9 +2139,57 @@
#
def write_backward(val) # :nodoc:
return unless val
@writer.empty_tag('c:backward', [ ['val', val] ])
+ end
+
+ #
+ # Write the <c:intercept> element.
+ #
+ def write_intercept(val)
+ @writer.empty_tag('c:intercept', [ ['val', val] ])
+ end
+
+ #
+ # Write the <c:dispEq> element.
+ #
+ def write_disp_eq
+ @writer.empty_tag('c:dispEq', [ ['val', 1] ])
+ end
+
+ #
+ # Write the <c:dispRSqr> element.
+ #
+ def write_disp_rsqr
+ @writer.empty_tag('c:dispRSqr', [ ['val', 1] ])
+ end
+
+ #
+ # Write the <c:trendlineLbl> element.
+ #
+ def write_trendline_lbl
+ @writer.tag_elements('c:trendlineLbl') do
+ # Write the c:layout element.
+ write_layout
+ # Write the c:numFmt element.
+ write_trendline_num_fmt
+ end
+ end
+
+ #
+ # Write the <c:numFmt> element.
+ #
+ def write_trendline_num_fmt
+ format_code = 'General'
+ source_linked = 0
+
+ attributes = [
+ ['formatCode', format_code],
+ ['sourceLinked', source_linked]
+ ]
+
+ @writer.empty_tag('c:numFmt', attributes)
end
#
# Write the <c:hiLowLines> element.
#