Class: Writexlsx::Sparkline

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

Utility::Common::PERL_TRUE_VALUES

Instance Method Summary collapse

Methods included from Utility::Common

#absolute_char, #check_parameter, #float_to_str, #ptrue?, #put_deprecate_message

Constructor Details

#initialize(ws, param, sheetname) ⇒ 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