axlsx 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ module Axlsx
2
+
3
+ # The Line3DChart is a three dimentional line chart (who would have guessed?) that you can add to your worksheet.
4
+ # @example Creating a chart
5
+ # # This example creates a line in a single sheet.
6
+ # require "rubygems" # if that is your preferred way to manage gems!
7
+ # require "axlsx"
8
+ #
9
+ # p = Axlsx::Package.new
10
+ # ws = p.workbook.add_worksheet
11
+ # ws.add_row :values => ["This is a chart with no data in the sheet"]
12
+ #
13
+ # chart = ws.add_chart(Axlsx::Line3DChart, :start_at=> [0,1], :end_at=>[0,6], :title=>"Most Popular Pets")
14
+ # chart.add_series :data => [1, 9, 10], :labels => ["Slimy Reptiles", "Fuzzy Bunnies", "Rottweiler"]
15
+ #
16
+ # @see Worksheet#add_chart
17
+ # @see Worksheet#add_row
18
+ # @see Chart#add_series
19
+ # @see Series
20
+ # @see Package#serialize
21
+ class Line3DChart < Chart
22
+
23
+ # the category axis
24
+ # @return [CatAxis]
25
+ attr_reader :catAxis
26
+
27
+ # the category axis
28
+ # @return [ValAxis]
29
+ attr_reader :valAxis
30
+
31
+ # the category axis
32
+ # @return [Axis]
33
+ attr_reader :serAxis
34
+
35
+ # space between bar or column clusters, as a percentage of the bar or column width.
36
+ # @return [String]
37
+ attr_accessor :gapDepth
38
+
39
+ #grouping for a column, line, or area chart.
40
+ # must be one of [:percentStacked, :clustered, :standard, :stacked]
41
+ # @return [Symbol]
42
+ attr_accessor :grouping
43
+
44
+ # validation regex for gap amount percent
45
+ GAP_AMOUNT_PERCENT = /0*(([0-9])|([1-9][0-9])|([1-4][0-9][0-9])|500)%/
46
+
47
+ # Creates a new line chart object
48
+ # @param [GraphicFrame] frame The workbook that owns this chart.
49
+ # @option options [Symbol] grouping
50
+ # @option options [String] gapDepth
51
+ def initialize(frame, options={})
52
+ @grouping = :standard
53
+ @catAxId = rand(8 ** 8)
54
+ @valAxId = rand(8 ** 8)
55
+ @serAxId = rand(8 ** 8)
56
+ @catAxis = CatAxis.new(@catAxId, @valAxId)
57
+ @valAxis = ValAxis.new(@valAxId, @catAxId)
58
+ @serAxis = SerAxis.new(@serAxId, @valAxId)
59
+ @view3D = View3D.new(:perspective=>30)
60
+ super(frame, options)
61
+ @series_type = LineSeries
62
+ end
63
+
64
+ def grouping=(v)
65
+ RestrictionValidator.validate "Bar3DChart.grouping", [:percentStacked, :standard, :stacked], v
66
+ @grouping = v
67
+ end
68
+
69
+ def gapDepth=(v)
70
+ RegexValidator.validate "Bar3DChart.gapWidth", GAP_AMOUNT_PERCENT, v
71
+ @gapDepth=(v)
72
+ end
73
+
74
+ # Serializes the bar chart
75
+ # @return [String]
76
+ def to_xml
77
+ super() do |xml|
78
+ xml.send('c:line3DChart') {
79
+ xml.send('c:grouping', :val=>grouping)
80
+ xml.send('c:varyColors', :val=>1)
81
+ @series.each { |ser| ser.to_xml(xml) }
82
+ xml.send('c:gapDepth', :val=>@gapDepth) unless @gapDepth.nil?
83
+ xml.send('c:axId', :val=>@catAxId)
84
+ xml.send('c:axId', :val=>@valAxId)
85
+ xml.send('c:axId', :val=>@serAxId)
86
+ }
87
+ @catAxis.to_xml(xml)
88
+ @valAxis.to_xml(xml)
89
+ @serAxis.to_xml(xml)
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,138 @@
1
+ module Axlsx
2
+
3
+ # The Line3DChart is a three dimentional barchart (who would have guessed?) that you can add to your worksheet.
4
+ # @example Creating a chart
5
+ # # This example creates two charts in a single sheet.
6
+ # # The first uses data directly fed to the sheet, while the second references cells withing the worksheet for data.
7
+ #
8
+ # require "rubygems" # if that is your preferred way to manage gems!
9
+ # require "axlsx"
10
+ #
11
+ # p = Axlsx::Package.new
12
+ # ws = p.workbook.add_worksheet
13
+ # ws.add_row :values => ["This is a chart with no data in the sheet"]
14
+ #
15
+ # chart = ws.add_chart(Axlsx::Line3DChart, :start_at=> [0,1], :end_at=>[0,6], :title=>"Most Popular Pets")
16
+ # chart.add_series :data => [1, 9, 10], :labels => ["Slimy Reptiles", "Fuzzy Bunnies", "Rottweiler"]
17
+ #
18
+ # ws.add_row :values => ["This chart uses the data below"]
19
+ # title_row = ws.add_row :values => ["Least Popular Pets"]
20
+ # label_row = ws.add_row :values => ["", "Dry Skinned Reptiles", "Bald Cats", "Violent Parrots"]
21
+ # data_row = ws.add_row :values => ["Votes", 6, 4, 1]
22
+ #
23
+ # chart = ws.add_chart(Axlsx::Pie3DChart, :start_at => [0,11], :end_at =>[0,16], :title => title_row.cells.last)
24
+ # chart.add_series :data => data_row.cells[(1..-1)], :labels => label_row.cells
25
+ #
26
+ # f = File.open('example_pie_3d_chart.xlsx', 'w')
27
+ # p.serialize(f)
28
+ #
29
+ # @see Worksheet#add_chart
30
+ # @see Worksheet#add_row
31
+ # @see Chart#add_series
32
+ # @see Series
33
+ # @see Package#serialize
34
+ class Bar3DChart < Chart
35
+
36
+ # the category axis
37
+ # @return [CatAxis]
38
+ attr_reader :catAxis
39
+
40
+ # the category axis
41
+ # @return [ValAxis]
42
+ attr_reader :valAxis
43
+
44
+ # The direction of the bars in the chart
45
+ # must be one of [:bar, :col]
46
+ # @return [Symbol]
47
+ attr_accessor :barDir
48
+
49
+ # space between bar or column clusters, as a percentage of the bar or column width.
50
+ # @return [String]
51
+ attr_accessor :gapDepth
52
+
53
+ # space between bar or column clusters, as a percentage of the bar or column width.
54
+ # @return [String]
55
+ attr_accessor :gapWidth
56
+
57
+ #grouping for a column, line, or area chart.
58
+ # must be one of [:percentStacked, :clustered, :standard, :stacked]
59
+ # @return [Symbol]
60
+ attr_accessor :grouping
61
+
62
+ # The shabe of the bars or columns
63
+ # must be one of [:percentStacked, :clustered, :standard, :stacked]
64
+ # @return [Symbol]
65
+ attr_accessor :shape
66
+
67
+ # validation regex for gap amount percent
68
+ GAP_AMOUNT_PERCENT = /0*(([0-9])|([1-9][0-9])|([1-4][0-9][0-9])|500)%/
69
+
70
+ # Creates a new bar chart object
71
+ # @param [GraphicFrame] frame The workbook that owns this chart.
72
+ # @option options [Cell, String] title
73
+ # @option options [Boolean] show_legend
74
+ # @option options [Symbol] barDir
75
+ # @option options [Symbol] grouping
76
+ # @option options [String] gapWidth
77
+ # @option options [String] gapDepth
78
+ # @option options [Symbol] shape
79
+ def initialize(frame, options={})
80
+ super(frame, options)
81
+ @series_type = BarSeries
82
+ @barDir = :bar
83
+ @grouping = :clustered
84
+ @catAxId = rand(8 ** 8)
85
+ @valAxId = rand(8 ** 8)
86
+ @catAxis = CatAxis.new(@catAxId, @valAxId)
87
+ @valAxis = ValAxis.new(@valAxId, @catAxId)
88
+ @view3D = View3D.new(:rAngAx=>1)
89
+ end
90
+
91
+ def barDir=(v)
92
+ RestrictionValidator.validate "Bar3DChart.barDir", [:bar, :col], v
93
+ @barDir = v
94
+ end
95
+
96
+
97
+ def grouping=(v)
98
+ RestrictionValidator.validate "Bar3DChart.grouping", [:percentStacked, :clustered, :standard, :stacked], v
99
+ @grouping = v
100
+ end
101
+
102
+ def gapWidth=(v)
103
+ RegexValidator.validate "Bar3DChart.gapWidth", GAP_AMOUNT_PERCENT, v
104
+ @gapWidth=(v)
105
+ end
106
+
107
+ def gapDepth=(v)
108
+ RegexValidator.validate "Bar3DChart.gapWidth", GAP_AMOUNT_PERCENT, v
109
+ @gapDepth=(v)
110
+ end
111
+
112
+ def shape=(v)
113
+ RestrictionValidator.validate "Bar3DChart.shape", [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax], v
114
+ @shape = v
115
+ end
116
+
117
+ # Serializes the bar chart
118
+ # @return [String]
119
+ def to_xml
120
+ super() do |xml|
121
+ xml.send('c:bar3DChart') {
122
+ xml.send('c:barDir', :val => barDir)
123
+ xml.send('c:grouping', :val=>grouping)
124
+ xml.send('c:varyColors', :val=>1)
125
+ @series.each { |ser| ser.to_xml(xml) }
126
+ xml.send('c:gapWidth', :val=>@gapWidth) unless @gapWidth.nil?
127
+ xml.send('c:gapDepth', :val=>@gapDepth) unless @gapDepth.nil?
128
+ xml.send('c:shape', :val=>@shape) unless @shape.nil?
129
+ xml.send('c:axId', :val=>@catAxId)
130
+ xml.send('c:axId', :val=>@valAxId)
131
+ xml.send('c:axId', :val=>0)
132
+ }
133
+ @catAxis.to_xml(xml)
134
+ @valAxis.to_xml(xml)
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,76 @@
1
+ module Axlsx
2
+ # A LineSeries defines the title, data and labels for line charts
3
+ # @note The recommended way to manage series is to use Chart#add_series
4
+ # @see Worksheet#add_chart
5
+ # @see Chart#add_series
6
+ class LineSeries < Series
7
+
8
+ # The data for this series.
9
+ # @return [Array, SimpleTypedList]
10
+ attr_reader :data
11
+
12
+ # The labels for this series.
13
+ # @return [Array, SimpleTypedList]
14
+ attr_reader :labels
15
+
16
+ # Creates a new series
17
+ # @option options [Array, SimpleTypedList] data
18
+ # @option options [Array, SimpleTypedList] labels
19
+ # @param [Chart] chart
20
+ def initialize(chart, options={})
21
+ super(chart, options)
22
+ self.data = options[:data] || []
23
+ self.labels = options[:labels] || []
24
+ end
25
+
26
+ # Serializes the series
27
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
28
+ # @return [String]
29
+ def to_xml(xml)
30
+ super(xml) do |xml|
31
+ if !labels.empty?
32
+ xml.send('c:cat') {
33
+ xml.send('c:strRef') {
34
+ xml.send('c:f', Axlsx::cell_range(labels))
35
+ xml.send('c:strCache') {
36
+ xml.send('c:ptCount', :val=>labels.size)
37
+ labels.each_with_index do |cell, index|
38
+ v = cell.is_a?(Cell) ? cell.value : cell
39
+ xml.send('c:pt', :idx=>index) {
40
+ xml.send('c:v', v)
41
+ }
42
+ end
43
+ }
44
+ }
45
+ }
46
+ end
47
+ xml.send('c:val') {
48
+ xml.send('c:numRef') {
49
+ xml.send('c:f', Axlsx::cell_range(data))
50
+ xml.send('c:numCache') {
51
+ xml.send('c:formatCode', 'General')
52
+ xml.send('c:ptCount', :val=>data.size)
53
+ data.each_with_index do |cell, index|
54
+ v = cell.is_a?(Cell) ? cell.value : cell
55
+ xml.send('c:pt', :idx=>index) {
56
+ xml.send('c:v', v)
57
+ }
58
+ end
59
+ }
60
+ }
61
+ }
62
+ end
63
+ end
64
+
65
+ private
66
+
67
+
68
+ # assigns the data for this series
69
+ def data=(v) DataTypeValidator.validate "Series.data", [Array, SimpleTypedList], v; @data = v; end
70
+
71
+ # assigns the labels for this series
72
+ def labels=(v) DataTypeValidator.validate "Series.labels", [Array, SimpleTypedList], v; @labels = v; end
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,91 @@
1
+ module Axlsx
2
+ # A BarSeries defines the title, data and labels for bar charts
3
+ # @note The recommended way to manage series is to use Chart#add_series
4
+ # @see Worksheet#add_chart
5
+ # @see Chart#add_series
6
+ class BarSeries < Series
7
+
8
+
9
+ # The data for this series.
10
+ # @return [Array, SimpleTypedList]
11
+ attr_reader :data
12
+
13
+ # The labels for this series.
14
+ # @return [Array, SimpleTypedList]
15
+ attr_reader :labels
16
+
17
+ # The shabe of the bars or columns
18
+ # must be one of [:percentStacked, :clustered, :standard, :stacked]
19
+ # @return [Symbol]
20
+ attr_accessor :shape
21
+
22
+ # Creates a new series
23
+ # @option options [Array, SimpleTypedList] data
24
+ # @option options [Array, SimpleTypedList] labels
25
+ # @option options [String] title
26
+ # @option options [String] shape
27
+ # @param [Chart] chart
28
+ def initialize(chart, options={})
29
+ @shape = :box
30
+ super(chart, options)
31
+ self.data = options[:data] || []
32
+ self.labels = options[:labels] || []
33
+ end
34
+
35
+ def shape=(v)
36
+ RestrictionValidator.validate "BarSeries.shape", [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax], v
37
+ @shape = v
38
+ end
39
+
40
+ # Serializes the series
41
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
42
+ # @return [String]
43
+ def to_xml(xml)
44
+ super(xml) do |xml|
45
+ if !labels.empty?
46
+ xml.send('c:cat') {
47
+ xml.send('c:strRef') {
48
+ xml.send('c:f', Axlsx::cell_range(labels))
49
+ xml.send('c:strCache') {
50
+ xml.send('c:ptCount', :val=>labels.size)
51
+ labels.each_with_index do |cell, index|
52
+ v = cell.is_a?(Cell) ? cell.value : cell
53
+ xml.send('c:pt', :idx=>index) {
54
+ xml.send('c:v', v)
55
+ }
56
+ end
57
+ }
58
+ }
59
+ }
60
+ end
61
+ xml.send('c:val') {
62
+ xml.send('c:numRef') {
63
+ xml.send('c:f', Axlsx::cell_range(data))
64
+ xml.send('c:numCache') {
65
+ xml.send('c:formatCode', 'General')
66
+ xml.send('c:ptCount', :val=>data.size)
67
+ data.each_with_index do |cell, index|
68
+ v = cell.is_a?(Cell) ? cell.value : cell
69
+ xml.send('c:pt', :idx=>index) {
70
+ xml.send('c:v', v)
71
+ }
72
+ end
73
+ }
74
+ }
75
+ }
76
+ end
77
+ end
78
+
79
+
80
+ private
81
+
82
+
83
+ # assigns the data for this series
84
+ def data=(v) DataTypeValidator.validate "Series.data", [Array, SimpleTypedList], v; @data = v; end
85
+
86
+ # assigns the labels for this series
87
+ def labels=(v) DataTypeValidator.validate "Series.labels", [Array, SimpleTypedList], v; @labels = v; end
88
+
89
+ end
90
+
91
+ end
@@ -39,8 +39,8 @@ module Axlsx
39
39
  # @param [Workbook] workbook The workbook that owns this chart.
40
40
  # @option options [Cell, String] title
41
41
  def initialize(workbook, options={})
42
- super(workbook, options)
43
42
  # this charts series type
43
+ super(workbook, options)
44
44
  @series_type = PieSeries
45
45
  @view3D = View3D.new(:rotX => 30, :perspective => 30)
46
46
  end
@@ -0,0 +1,42 @@
1
+ module Axlsx
2
+ #A SarAxis object defines a series axis
3
+ class SerAxis < Axis
4
+
5
+ # @return [Boolean]
6
+ attr_accessor :tickLblSkip
7
+
8
+ # @return [Boolean]
9
+ attr_accessor :tickMarkSkip
10
+
11
+ # Creates a new SerAxis object
12
+ # @param [Integer] axId the id of this axis
13
+ # @param [Integer] crossAx the id of the perpendicular axis
14
+ # @option options [Symbol] axPos
15
+ # @option options [Symbol] tickLblPos
16
+ # @option options [Symbol] crosses
17
+ # @option options [Boolean] tickLblSkip
18
+ # @option options [Symbol] tickMarkSkip
19
+ def initialize(axId, crossAx, options={})
20
+ super(axId, crossAx, options)
21
+ options.each do |o|
22
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
23
+ end
24
+ end
25
+
26
+ def tickLblSkip=(v) Axlsx::validate_boolean(v); @tickLblSkip = v; end
27
+ def tickMarkSkip=(v) Axlsx::validate_boolean(v); @tickMarkSkip = v; end
28
+
29
+ # Serializes the series axis
30
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
31
+ # @return [String]
32
+ def to_xml(xml)
33
+ xml.send('c:serAx') {
34
+ super(xml)
35
+ xml.send('c:tickLblSkip', :val=>@tickLblSkip) unless @tickLblSkip.nil?
36
+ xml.send('c:tickMarkSkip', :val=>@tickMarkSkip) unless @tickMarkSkip.nil?
37
+ }
38
+ end
39
+ end
40
+
41
+
42
+ end