Class: Writexlsx::Sparkline

Inherits:
Object
  • Object
show all
Includes:
Utility
Defined in:
lib/write_xlsx/sparkline.rb

Overview

Sparkline - A class for handle Excel sparkline

Used in conjunction with WriteXLSX.

Copyright 2000-2012, John McNamara, [email protected] Converted to ruby by Hideo NAKAMURA, [email protected]

Constant Summary

Constants included from Utility

Utility::CHAR_WIDTHS, Utility::COL_MAX, Utility::DEFAULT_COL_PIXELS, Utility::MAX_DIGIT_WIDTH, Utility::PADDING, Utility::PERL_TRUE_VALUES, Utility::ROW_MAX, Utility::SHEETNAME_MAX, Utility::STR_MAX

Instance Method Summary collapse

Methods included from Utility

#absolute_char, #check_dimensions, #check_dimensions_and_update_max_min_values, #check_parameter, #color, #convert_date_time, #convert_font_args, #dash_types, delete_files, #escape_url, #fill_properties, #float_to_str, #get_font_latin_attributes, #get_font_style_attributes, #layout_properties, #legend_properties, #line_fill_properties, #line_properties, #palette_color_from_index, #params_to_font, #pattern_properties, #pixels_to_points, #ptrue?, #put_deprecate_message, #quote_sheetname, #r_id_attributes, #row_col_notation, #shape_style_base, #store_col_max_min_values, #store_row_max_min_values, #substitute_cellref, #underline_attributes, #v_shape_attributes_base, #v_shape_style_base, #value_or_raise, #write_a_body_pr, #write_a_def_rpr, #write_a_end_para_rpr, #write_a_lst_style, #write_a_p_formula, #write_a_p_pr_formula, #write_a_solid_fill, #write_a_srgb_clr, #write_anchor, #write_auto_fill, #write_color, #write_comment_path, #write_def_rpr_r_pr_common, #write_div, #write_fill, #write_font, #write_stroke, #write_tx_pr, #write_xml_declaration, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell, #xl_string_pixel_width, #xml_str

Constructor Details

#initialize(ws, param, sheetname) ⇒ Sparkline

Returns a new instance of Sparkline.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/write_xlsx/sparkline.rb', line 19

def initialize(ws, param, sheetname)
  @color = {}

  # Check for valid input parameters.
  param.each_key do |k|
    raise "Unknown parameter '#{k}' in add_sparkline()" unless valid_sparkline_parameter[k]
  end
  %i[location range].each do |required_key|
    raise "Parameter '#{required_key}' is required in add_sparkline()" unless param[required_key]
  end

  # Handle the sparkline type.
  type = param[:type] || 'line'
  raise "Parameter ':type' must be 'line', 'column' or 'win_loss' in add_sparkline()" unless %w[line column win_loss].include?(type)

  type  = 'stacked' if type == 'win_loss'
  @type = type

  # We handle single location/range values or array refs of values.
  @locations = [param[:location]].flatten
  @ranges    = [param[:range]].flatten

  raise "Must have the same number of location and range parameters in add_sparkline()" if @ranges.size != @locations.size

  # Cleanup the input ranges.
  @ranges.collect! do |range|
    # Remove the absolute reference $ symbols.
    range = range.gsub("$", '')
    # Convert a simple range into a full Sheet1!A1:D1 range.
    range = "#{sheetname}!#{range}" unless range =~ /!/
    range
  end
  # Cleanup the input locations.
  @locations.collect! { |location| location.gsub("$", '') }

  # Map options.
  @high      = param[:high_point]
  @low       = param[:low_point]
  @negative  = param[:negative_points]
  @first     = param[:first_point]
  @last      = param[:last_point]
  @markers   = param[:markers]
  @min       = param[:min]
  @max       = param[:max]
  @axis      = param[:axis]
  @reverse   = param[:reverse]
  @hidden    = param[:show_hidden]
  @weight    = param[:weight]

  # Map empty cells options.
  @empty = case param[:empty_cells] || ''
           when 'zero'
             0
           when 'connect'
             'span'
           else
             'gap'
           end

  # Map the date axis range.
  date_range = param[:date_axis]
  date_range = "#{sheetname}!#{date_range}" if ptrue?(date_range) && !(date_range =~ /!/)
  @date_axis = date_range

  # Set the sparkline styles.
  style = spark_styles[param[:style] || 0]

  @series_color   = style[:series]
  @negative_color = style[:negative]
  @markers_color  = style[:markers]
  @first_color    = style[:first]
  @last_color     = style[:last]
  @high_color     = style[:high]
  @low_color      = style[:low]

  # Override the style colours with user defined colors.
  %i[series_color negative_color markers_color first_color last_color high_color low_color].each do |user_color|
    set_spark_color(user_color, ptrue?(param[user_color]) ? ws.palette_color(param[user_color]) : nil)
  end
end

Instance Method Details

#countObject



100
101
102
# File 'lib/write_xlsx/sparkline.rb', line 100

def count
  @locations.size
end

#group_attributesObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/write_xlsx/sparkline.rb', line 104

def group_attributes
  cust_max = cust_max_min(@max) if @max
  cust_min = cust_max_min(@min) if @min

  a = []
  a << ['manualMax', @max] if @max && @max != 'group'
  a << ['manualMin', @min] if @min && @min != 'group'

  # Ignore the default type attribute (line).
  a << ['type',          @type]        if @type != 'line'

  a << ['lineWeight',    @weight]      if @weight
  a << ['dateAxis',      1]            if @date_axis
  a << ['displayEmptyCellsAs', @empty] if ptrue?(@empty)

  a << ['markers',       1]         if @markers
  a << ['high',          1]         if @high
  a << ['low',           1]         if @low
  a << ['first',         1]         if @first
  a << ['last',          1]         if @last
  a << ['negative',      1]         if @negative
  a << ['displayXAxis',  1]         if @axis
  a << ['displayHidden', 1]         if @hidden
  a << ['minAxisType',   cust_min]  if cust_min
  a << ['maxAxisType',   cust_max]  if cust_max
  a << ['rightToLeft',   1]         if @reverse
  a
end

#write_sparkline_group(writer) ⇒ Object

Write the <x14:sparklineGroup> element.

Example for order.

<x14:sparklineGroup

manualMax="0"
manualMin="0"
lineWeight="2.25"
type="column"
dateAxis="1"
displayEmptyCellsAs="span"
markers="1"
high="1"
low="1"
first="1"
last="1"
negative="1"
displayXAxis="1"
displayHidden="1"
minAxisType="custom"
maxAxisType="custom"
rightToLeft="1">


157
158
159
160
161
162
163
# File 'lib/write_xlsx/sparkline.rb', line 157

def write_sparkline_group(writer)
  @writer = writer

  @writer.tag_elements('x14:sparklineGroup', group_attributes) do
    write
  end
end