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::COL_MAX, 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, #convert_date_time, #dash_types, delete_files, #fill_properties, #float_to_str, #layout_properties, #line_fill_properties, #line_properties, #palette_color, #pixels_to_points, #process_workbook_options, #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_anchor, #write_auto_fill, #write_color, #write_comment_path, #write_div, #write_fill, #write_font, #write_stroke, #write_xml_declaration, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell, #xml_str

Constructor Details

#initialize(ws, param, sheetname) ⇒ Sparkline


17
18
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
99
100
101
102
103
104
105
# File 'lib/write_xlsx/sparkline.rb', line 17

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

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

  # Handle the sparkline type.
  type = param[:type] || 'line'
  unless ['line', 'column', 'win_loss'].include?(type)
    raise "Parameter ':type' must be 'line', 'column' or 'win_loss' in add_sparkline()"
  end
  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

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

  # 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]
  if ptrue?(date_range) && !(date_range =~ /!/)
    date_range = "#{sheetname}!#{date_range}"
  end
  @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.
  [: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


107
108
109
# File 'lib/write_xlsx/sparkline.rb', line 107

def count
  @locations.size
end

#group_attributesObject


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/write_xlsx/sparkline.rb', line 111

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

164
165
166
167
168
169
170
# File 'lib/write_xlsx/sparkline.rb', line 164

def write_sparkline_group(writer)
  @writer = writer

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