axlsx 1.1.3 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/CHANGELOG.md +21 -0
  2. data/LICENSE +1 -1
  3. data/README.md +38 -415
  4. data/examples/#extractive.csv# +0 -0
  5. data/examples/#extractive.rb# +45 -0
  6. data/examples/chart_colors.rb +73 -0
  7. data/examples/chart_colors.rb~ +0 -0
  8. data/examples/chart_colors.xlsx +0 -0
  9. data/examples/colored_series_data.xlsx +0 -0
  10. data/examples/conditional_formatting/getting_barred.xlsx +0 -0
  11. data/examples/example.rb +174 -113
  12. data/examples/example.xlsx +0 -0
  13. data/examples/example_streamed.xlsx +0 -0
  14. data/examples/extractive.csv +1 -0
  15. data/examples/extractive.csv~ +1 -0
  16. data/examples/extractive.rb +42 -0
  17. data/examples/extractive.rb~ +3 -0
  18. data/examples/extractive.xlsx +0 -0
  19. data/examples/no-use_autowidth.xlsx +0 -0
  20. data/examples/shared_strings_example.xlsx +0 -0
  21. data/examples/stack.rb +21 -0
  22. data/examples/stack.rb~ +27 -0
  23. data/examples/stack.xlsx +0 -0
  24. data/examples/~$chart_colors.xlsx +0 -0
  25. data/examples/~$extractive.xlsx +0 -0
  26. data/lib/axlsx/drawing/ax_data_source.rb +25 -0
  27. data/lib/axlsx/drawing/ax_data_source.rb~ +55 -0
  28. data/lib/axlsx/drawing/axis.rb +12 -3
  29. data/lib/axlsx/drawing/bar_series.rb +23 -6
  30. data/lib/axlsx/drawing/cat_axis.rb +24 -0
  31. data/lib/axlsx/drawing/chart.rb +7 -5
  32. data/lib/axlsx/drawing/data_source.rb~ +51 -0
  33. data/lib/axlsx/drawing/drawing.rb +8 -4
  34. data/lib/axlsx/drawing/line_series.rb +20 -4
  35. data/lib/axlsx/drawing/num_data.rb +52 -0
  36. data/lib/axlsx/drawing/num_data.rb~ +51 -0
  37. data/lib/axlsx/drawing/num_data_source.rb +58 -0
  38. data/lib/axlsx/drawing/num_data_source.rb~ +54 -0
  39. data/lib/axlsx/drawing/num_val.rb +32 -0
  40. data/lib/axlsx/drawing/num_val.rb~ +40 -0
  41. data/lib/axlsx/drawing/pie_series.rb +18 -4
  42. data/lib/axlsx/drawing/ref.rb~ +41 -0
  43. data/lib/axlsx/drawing/scatter_series.rb +28 -2
  44. data/lib/axlsx/drawing/ser_axis.rb +1 -1
  45. data/lib/axlsx/drawing/series.rb +1 -1
  46. data/lib/axlsx/drawing/str_data.rb +42 -0
  47. data/lib/axlsx/drawing/str_data.rb~ +58 -0
  48. data/lib/axlsx/drawing/str_val.rb +33 -0
  49. data/lib/axlsx/drawing/str_val.rb~ +35 -0
  50. data/lib/axlsx/drawing/view_3D.rb +6 -3
  51. data/lib/axlsx/util/validators.rb +1 -1
  52. data/lib/axlsx/version.rb +1 -1
  53. data/lib/axlsx/workbook/worksheet/col.rb +2 -1
  54. data/lib/axlsx/workbook/worksheet/row.rb +44 -5
  55. data/lib/axlsx/workbook/worksheet/table.rb +1 -1
  56. data/test/drawing/tc_bar_series.rb +18 -13
  57. data/test/drawing/tc_cat_axis_data.rb +23 -13
  58. data/test/drawing/tc_chart.rb +0 -2
  59. data/test/drawing/tc_data_source.rb +17 -0
  60. data/test/drawing/tc_data_source.rb~ +30 -0
  61. data/test/drawing/tc_line_series.rb +3 -9
  62. data/test/drawing/tc_named_axis_data.rb +27 -0
  63. data/test/drawing/tc_num_data.rb +27 -0
  64. data/test/drawing/tc_num_data.rb~ +35 -0
  65. data/test/drawing/tc_num_val.rb +29 -0
  66. data/test/drawing/tc_num_val.rb~ +29 -0
  67. data/test/drawing/tc_pie_series.rb +4 -11
  68. data/test/drawing/tc_scatter_series.rb +6 -5
  69. data/test/drawing/tc_str_data.rb +18 -0
  70. data/test/drawing/tc_str_data.rb~ +30 -0
  71. data/test/drawing/tc_str_val.rb +21 -0
  72. data/test/drawing/tc_str_val.rb~ +26 -0
  73. data/test/drawing/tc_two_cell_anchor.rb +1 -1
  74. data/test/workbook/worksheet/tc_row.rb +6 -0
  75. metadata +65 -13
  76. data/lib/axlsx/drawing/cat_axis_data.rb +0 -34
  77. data/lib/axlsx/drawing/named_axis_data.rb +0 -36
  78. data/lib/axlsx/drawing/val_axis_data.rb +0 -27
  79. data/test/drawing/tc_val_axis_data.rb +0 -17
@@ -10,14 +10,18 @@ module Axlsx
10
10
 
11
11
  require 'axlsx/drawing/scaling.rb'
12
12
  require 'axlsx/drawing/axis.rb'
13
+
14
+ require 'axlsx/drawing/str_val.rb'
15
+ require 'axlsx/drawing/num_val.rb'
16
+ require 'axlsx/drawing/str_data.rb'
17
+ require 'axlsx/drawing/num_data.rb'
18
+ require 'axlsx/drawing/num_data_source.rb'
19
+ require 'axlsx/drawing/ax_data_source.rb'
20
+
13
21
  require 'axlsx/drawing/ser_axis.rb'
14
22
  require 'axlsx/drawing/cat_axis.rb'
15
23
  require 'axlsx/drawing/val_axis.rb'
16
24
 
17
- require 'axlsx/drawing/cat_axis_data.rb'
18
- require 'axlsx/drawing/val_axis_data.rb'
19
- require 'axlsx/drawing/named_axis_data.rb'
20
-
21
25
  require 'axlsx/drawing/marker.rb'
22
26
 
23
27
  require 'axlsx/drawing/one_cell_anchor.rb'
@@ -14,6 +14,11 @@ module Axlsx
14
14
  # @return [CatAxisData]
15
15
  attr_reader :labels
16
16
 
17
+ # The fill color for this series.
18
+ # Red, green, and blue is expressed as sequence of hex digits, RRGGBB. A perceptual gamma of 2.2 is used.
19
+ # @return [String]
20
+ attr_reader :color
21
+
17
22
  # Creates a new series
18
23
  # @option options [Array, SimpleTypedList] data
19
24
  # @option options [Array, SimpleTypedList] labels
@@ -21,8 +26,13 @@ module Axlsx
21
26
  def initialize(chart, options={})
22
27
  @labels, @data = nil, nil
23
28
  super(chart, options)
24
- @labels = CatAxisData.new(options[:labels]) unless options[:labels].nil?
25
- @data = ValAxisData.new(options[:data]) unless options[:data].nil?
29
+ @labels = AxDataSource.new(:data => options[:labels]) unless options[:labels].nil?
30
+ @data = NumDataSource.new(options) unless options[:data].nil?
31
+ end
32
+
33
+ # @see color
34
+ def color=(v)
35
+ @color = v
26
36
  end
27
37
 
28
38
  # Serializes the object
@@ -30,6 +40,12 @@ module Axlsx
30
40
  # @return [String]
31
41
  def to_xml_string(str = '')
32
42
  super(str) do
43
+ if color
44
+ str << '<c:spPr><a:solidFill>'
45
+ str << '<a:srgbClr val="' << color << '"/>'
46
+ str << '</a:solidFill></c:spPr>'
47
+ end
48
+
33
49
  @labels.to_xml_string(str) unless @labels.nil?
34
50
  @data.to_xml_string(str) unless @data.nil?
35
51
  end
@@ -38,10 +54,10 @@ module Axlsx
38
54
  private
39
55
 
40
56
  # assigns the data for this series
41
- def data=(v) DataTypeValidator.validate "Series.data", [SimpleTypedList], v; @data = v; end
57
+ def data=(v) DataTypeValidator.validate "Series.data", [NumDataSource], v; @data = v; end
42
58
 
43
59
  # assigns the labels for this series
44
- def labels=(v) DataTypeValidator.validate "Series.labels", [SimpleTypedList], v; @labels = v; end
60
+ def labels=(v) DataTypeValidator.validate "Series.labels", [AxDataSource], v; @labels = v; end
45
61
 
46
62
  end
47
63
  end
@@ -0,0 +1,52 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Axlsx
3
+
4
+ #This class specifies data for a particular data point. It is used for both numCache and numLit object
5
+ class NumData
6
+
7
+ # A string representing the format code to apply. For more information see see the SpreadsheetML numFmt element's (§18.8.30) formatCode attribute.
8
+ # @return [String]
9
+ attr_reader :format_code
10
+
11
+ # creates a new NumVal object
12
+ # @option options [String] formatCode
13
+ # @option options [Array] :data
14
+ # @see StrData
15
+ def initialize(options={})
16
+ @format_code = "General"
17
+ @pt = SimpleTypedList.new NumVal
18
+ options.each do |o|
19
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
20
+ end
21
+ end
22
+
23
+ # Creates the val objects for this data set. I am not overly confident this is going to play nicely with time and data types.
24
+ # @param [Array] values An array of cells or values.
25
+ def data=(values=[])
26
+ @tag_name = values.first.is_a?(Cell) ? :numCache : :numLit
27
+ values.each do |value|
28
+ v = value.is_a?(Cell) ? value.value : value
29
+ @pt << NumVal.new(:v => v)
30
+ end
31
+ end
32
+
33
+ # @see format_code
34
+ def format_code=(v='General')
35
+ Axlsx::validate_string(v)
36
+ @format_code = v
37
+ end
38
+
39
+ # serialize the object
40
+ def to_xml_string(str = "")
41
+ str << '<c:' << @tag_name.to_s << '>'
42
+ str << '<c:formatCode>' << format_code.to_s << '</c:formatCode>'
43
+ str << '<c:ptCount val="' << @pt.size.to_s << '"/>'
44
+ @pt.each_with_index do |num_val, index|
45
+ num_val.to_xml_string index, str
46
+ end
47
+ str << '</c:' << @tag_name.to_s << '>'
48
+ end
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,51 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Axlsx
3
+
4
+ #This class specifies data for a particular data point. It is used for both numCache and numLit object
5
+ class NumData < StrData
6
+
7
+ def self.allowed_tag_names
8
+ [:numCache, :numLit]
9
+ end
10
+
11
+ # A string representing the format code to apply. For more information see see the SpreadsheetML numFmt element's (§18.8.30) formatCode attribute.
12
+ # @return [String]
13
+ attr_reader :format_code
14
+
15
+ # creates a new NumVal object
16
+ # @option options [String] formatCode
17
+ # @option options [Array] :data
18
+ # @option options [Array] :data
19
+ # @option options [String] :tag_name
20
+ # @see StrData
21
+ def initialize(options={})
22
+ @tag_prefix = :num
23
+ @type = NumVal
24
+ @format_code = "General"
25
+ @pt = SimpleTypedList.new @type
26
+ options.each do |o|
27
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
28
+ end
29
+
30
+ end
31
+
32
+ # @see format_code
33
+ def format_code=(v='General')
34
+ Axlsx::validate_string(v)
35
+ @format_code = v
36
+ end
37
+
38
+ # serialize the object
39
+ def to_xml_string(idx, str = "")
40
+ str << '<c:' << tag_name.to_s << '>'
41
+ str << '<c:formatCode>' << format_code.to_s << '</c:formatCode>'
42
+ str << '<c:ptCount val="' << @pt.size.to_s << '"/>'
43
+ pt.each_with_index do |num_val, index|
44
+ num_val.to_xml_string index, str
45
+ end
46
+ str << '</c:' << tag_name.to_s << '>'
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,58 @@
1
+ module Axlsx
2
+ # A numeric data source for use by charts.
3
+ class NumDataSource
4
+
5
+ # The tag name to use when serializing this data source.
6
+ # Only items defined in allowed_tag_names are allowed
7
+ # @return [Symbol]
8
+ attr_reader :tag_name
9
+
10
+ attr_reader :data
11
+
12
+ # allowed element tag names
13
+ # @return [Array]
14
+ def self.allowed_tag_names
15
+ [:yVal, :val]
16
+ end
17
+
18
+ # creates a new NumDataSource object
19
+ # @option options [Array] data An array of Cells or Numeric objects
20
+ # @option options [Symbol] tag_name see tag_name
21
+ def initialize(options={})
22
+ # override these three in child classes
23
+ @data_type ||= NumData
24
+ @tag_name ||= :val
25
+ @ref_tag_name ||= :numRef
26
+
27
+ @f = nil
28
+ @data = @data_type.new(options)
29
+ if options[:data] && options[:data].first.is_a?(Cell)
30
+ @f = Axlsx::cell_range(options[:data])
31
+ end
32
+ options.each do |o|
33
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
34
+ end
35
+ end
36
+
37
+ # sets the tag name for this data source
38
+ # @param [Symbol] One of the allowed_tag_names
39
+ def tag_name=(v)
40
+ Axlsx::RestrictionValidator.validate "#{self.class.name}.tag_name", self.class.allowed_tag_names, v
41
+ @tag_name = v
42
+ end
43
+
44
+ def to_xml_string(str="")
45
+ str << '<c:' << tag_name.to_s << '>'
46
+ if @f
47
+ str << '<c:' << @ref_tag_name.to_s << '>'
48
+ str << '<c:f>' << @f.to_s << '</c:f>'
49
+ end
50
+ @data.to_xml_string str
51
+ if @f
52
+ str << '</c:' << @ref_tag_name.to_s << '>'
53
+ end
54
+ str << '</c:' << tag_name.to_s << '>'
55
+ end
56
+ end
57
+ end
58
+
@@ -0,0 +1,54 @@
1
+ module Axlsx
2
+ # A numeric data source for use by charts.
3
+ class NumDataSource
4
+
5
+ # The tag name to use when serializing this data source.
6
+ # Only items defined in allowed_tag_names are allowed
7
+ # @return [Symbol]
8
+ attr_reader :tag_name
9
+
10
+ attr_reader :data
11
+
12
+ # allowed element tag names
13
+ # @return [Array]
14
+ def self.allowed_tag_names
15
+ [:yVal, :val]
16
+ end
17
+
18
+ # creates a new NumDataSource object
19
+ # @option options [Array] data An array of Cells or Numeric objects
20
+ # @option options [Symbol] tag_name see tag_name
21
+ def initialize(options={})
22
+ @f = nil
23
+ @tag_name = :val
24
+ @data = NumData.new(options)
25
+ if options[:data] && options[:data].first.is_a?(Cell)
26
+ @f = Axlsx::cell_range(options[:data])
27
+ end
28
+ options.each do |o|
29
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
30
+ end
31
+ end
32
+
33
+ # sets the tag name for this data source
34
+ # @param [Symbol] One of the allowed_tag_names
35
+ def tag_name=(v)
36
+ Axlsx::RestrictionValidator.validate "#{self.class.name}.tag_name", self.class.allowed_tag_names, v
37
+ @tag_name = v
38
+ end
39
+
40
+ def to_xml_string(str="")
41
+ str << '<c:' << tag_name.to_s << '>'
42
+ if @f
43
+ str << '<c:numRef>'
44
+ str << '<c:f>' << @f.to_s << '</c:f>'
45
+ end
46
+ @data.to_xml_string str
47
+ if @f
48
+ str << '</c:numRef>'
49
+ end
50
+ str << '</c:' << tag_name.to_s << '>'
51
+ end
52
+ end
53
+ end
54
+
@@ -0,0 +1,32 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Axlsx
3
+
4
+ #This class specifies data for a particular data point.
5
+ class NumVal < StrVal
6
+
7
+ # A string representing the format code to apply.
8
+ # For more information see see the SpreadsheetML numFmt element's (§18.8.30) formatCode attribute.
9
+ # @return [String]
10
+ attr_reader :format_code
11
+
12
+ # creates a new NumVal object
13
+ # @option options [String] formatCode
14
+ # @option options [Integer] v
15
+ def initialize(options={})
16
+ @format_code = "General"
17
+ super(options)
18
+ end
19
+
20
+ # @see format_code
21
+ def format_code=(v)
22
+ Axlsx::validate_string(v)
23
+ @format_code = v
24
+ end
25
+
26
+ # serialize the object
27
+ def to_xml_string(idx, str = "")
28
+ Axlsx::validate_unsigned_int(idx)
29
+ str << '<c:pt idx="' << idx.to_s << '" formatCode="' << format_code << '"><c:v>' << v.to_s << '</c:v></c:pt>'
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,40 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Axlsx
3
+
4
+ #This class specifies data for a particular data point.
5
+ class NumVal < StrVal
6
+
7
+ # A string representing the format code to apply. For more information see see the SpreadsheetML numFmt element's (§18.8.30) formatCode attribute.
8
+ # @return [String]
9
+ attr_reader :format_code
10
+
11
+ # creates a new NumVal object
12
+ # @option options [String] formatCode
13
+ # @option options [Integer] v
14
+ def initialize(options={})
15
+ @format_code = "General"
16
+ @v = @idx = 0
17
+ options.each do |o|
18
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
19
+ end
20
+ end
21
+
22
+ # @see format_code
23
+ def format_code=(v='General')
24
+ Axlsx::validate_string(v)
25
+ @format_code = v
26
+ end
27
+
28
+ # @see v
29
+ def v=(v)
30
+ Axlsx::validate_int(v)
31
+ @v = v
32
+ end
33
+
34
+ # serialize the object
35
+ def to_xml_string(idx, str = "")
36
+ Axlsx::validate_unsigned_int(idx)
37
+ str << '<c:pt idx="' << idx.to_s << '" formatCode="' << format_code << '"><c:v>' << v.to_s << '</c:v></c:pt>'
38
+ end
39
+ end
40
+ end
@@ -19,6 +19,9 @@ module Axlsx
19
19
  # @return [Integert]
20
20
  attr_reader :explosion
21
21
 
22
+ # An array of rgb colors to apply to your bar chart.
23
+ attr_reader :colors
24
+
22
25
  # Creates a new series
23
26
  # @option options [Array, SimpleTypedList] data
24
27
  # @option options [Array, SimpleTypedList] labels
@@ -27,11 +30,15 @@ module Axlsx
27
30
  # @param [Chart] chart
28
31
  def initialize(chart, options={})
29
32
  @explosion = nil
33
+ @colors = []
30
34
  super(chart, options)
31
- self.labels = CatAxisData.new(options[:labels]) unless options[:labels].nil?
32
- self.data = ValAxisData.new(options[:data]) unless options[:data].nil?
35
+ self.labels = AxDataSource.new(:data => options[:labels]) unless options[:labels].nil?
36
+ self.data = NumDataSource.new(options) unless options[:data].nil?
33
37
  end
34
38
 
39
+ # @see colors
40
+ def colors=(v) DataTypeValidator.validate "BarSeries.colors", [Array], v; @colors = v end
41
+
35
42
  # @see explosion
36
43
  def explosion=(v) Axlsx::validate_unsigned_int(v); @explosion = v; end
37
44
 
@@ -41,6 +48,13 @@ module Axlsx
41
48
  def to_xml_string(str = '')
42
49
  super(str) do |str_inner|
43
50
  str_inner << '<c:explosion val="' << @explosion << '"/>' unless @explosion.nil?
51
+ colors.each_with_index do |c, index|
52
+ str << '<c:dPt>'
53
+ str << '<c:idx val="' << index.to_s << '"/>'
54
+ str << '<c:spPr><a:solidFill>'
55
+ str << '<a:srgbClr val="' << c << '"/>'
56
+ str << '</a:solidFill></c:spPr></c:dPt>'
57
+ end
44
58
  @labels.to_xml_string str_inner unless @labels.nil?
45
59
  @data.to_xml_string str_inner unless @data.nil?
46
60
  end
@@ -50,10 +64,10 @@ module Axlsx
50
64
  private
51
65
 
52
66
  # assigns the data for this series
53
- def data=(v) DataTypeValidator.validate "Series.data", [SimpleTypedList], v; @data = v; end
67
+ def data=(v) DataTypeValidator.validate "Series.data", [NumDataSource], v; @data = v; end
54
68
 
55
69
  # assigns the labels for this series
56
- def labels=(v) DataTypeValidator.validate "Series.labels", [SimpleTypedList], v; @labels = v; end
70
+ def labels=(v) DataTypeValidator.validate "Series.labels", [AxDataSource], v; @labels = v; end
57
71
 
58
72
  end
59
73
 
@@ -0,0 +1,41 @@
1
+ module Axlsx
2
+
3
+ # Base class for xVal, yVal and val data sources
4
+ class DataSource
5
+
6
+ def self.allowed_tag_names
7
+ [:yVal, :xVal, :val]
8
+ end
9
+
10
+ def self.allowed_types
11
+ [NumData, StrData]
12
+ end
13
+
14
+ attr_reader :tag_name
15
+
16
+ attr_reader :data
17
+
18
+ def initialize(type, data=[])
19
+ Axlsx::RestrictionValidator.validate "#{self.class.name}", self.class.allowed_types, type
20
+ @data = type.new
21
+ end
22
+
23
+ def f
24
+
25
+ end
26
+
27
+ def is_literal?
28
+
29
+ end
30
+
31
+ def is_reference?
32
+
33
+ end
34
+
35
+ def to_xml_string(str = '')
36
+
37
+ end
38
+
39
+ end
40
+
41
+ end