writeexcel 0.3.5 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitattributes +1 -0
- data/README.rdoc +12 -6
- data/VERSION +1 -1
- data/charts/chartex.rb +316 -315
- data/charts/demo1.rb +1 -0
- data/charts/demo2.rb +1 -0
- data/charts/demo3.rb +117 -116
- data/charts/demo4.rb +119 -118
- data/charts/demo5.rb +48 -47
- data/examples/a_simple.rb +1 -0
- data/examples/autofilter.rb +1 -0
- data/examples/bigfile.rb +30 -29
- data/examples/chart_area.rb +121 -120
- data/examples/chart_bar.rb +120 -119
- data/examples/chart_column.rb +120 -119
- data/examples/chart_line.rb +120 -119
- data/examples/chart_pie.rb +108 -107
- data/examples/chart_scatter.rb +121 -120
- data/examples/chart_stock.rb +148 -147
- data/examples/chess.rb +1 -0
- data/examples/colors.rb +1 -0
- data/examples/comments1.rb +1 -0
- data/examples/comments2.rb +3 -2
- data/examples/copyformat.rb +1 -0
- data/examples/data_validate.rb +1 -0
- data/examples/date_time.rb +1 -0
- data/examples/defined_name.rb +1 -0
- data/examples/demo.rb +1 -0
- data/examples/diag_border.rb +1 -0
- data/examples/formats.rb +1 -0
- data/examples/formula_result.rb +1 -0
- data/examples/header.rb +1 -0
- data/examples/hide_sheet.rb +1 -0
- data/examples/hyperlink.rb +1 -0
- data/examples/images.rb +1 -0
- data/examples/indent.rb +1 -0
- data/examples/merge1.rb +1 -0
- data/examples/merge2.rb +1 -0
- data/examples/merge3.rb +1 -0
- data/examples/merge4.rb +1 -0
- data/examples/merge5.rb +1 -0
- data/examples/merge6.rb +67 -66
- data/examples/outline.rb +1 -0
- data/examples/outline_collapsed.rb +1 -0
- data/examples/panes.rb +1 -0
- data/examples/properties.rb +1 -0
- data/examples/properties_jp.rb +1 -0
- data/examples/protection.rb +1 -0
- data/examples/regions.rb +1 -0
- data/examples/repeat.rb +1 -0
- data/examples/right_to_left.rb +1 -0
- data/examples/row_wrap.rb +1 -0
- data/examples/stats.rb +1 -0
- data/examples/stocks.rb +1 -0
- data/examples/tab_colors.rb +1 -0
- data/examples/write_arrays.rb +1 -0
- data/lib/writeexcel.rb +6 -1
- data/lib/writeexcel/biffwriter.rb +21 -20
- data/lib/writeexcel/chart.rb +25 -12
- data/lib/writeexcel/charts/area.rb +153 -152
- data/lib/writeexcel/charts/bar.rb +178 -177
- data/lib/writeexcel/charts/column.rb +157 -156
- data/lib/writeexcel/charts/external.rb +62 -61
- data/lib/writeexcel/charts/line.rb +153 -152
- data/lib/writeexcel/charts/pie.rb +170 -169
- data/lib/writeexcel/charts/scatter.rb +4 -3
- data/lib/writeexcel/charts/stock.rb +212 -211
- data/lib/writeexcel/compatibility.rb +320 -0
- data/lib/writeexcel/excelformulaparser.rb +587 -586
- data/lib/writeexcel/format.rb +12 -13
- data/lib/writeexcel/formula.rb +30 -28
- data/lib/writeexcel/helper.rb +23 -0
- data/lib/writeexcel/olewriter.rb +5 -16
- data/lib/writeexcel/properties.rb +43 -54
- data/lib/writeexcel/storage_lite.rb +981 -968
- data/lib/writeexcel/workbook.rb +94 -73
- data/lib/writeexcel/worksheet.rb +230 -210
- data/test/helper.rb +19 -0
- data/test/test_00_IEEE_double.rb +1 -0
- data/test/test_01_add_worksheet.rb +1 -0
- data/test/test_02_merge_formats.rb +3 -5
- data/test/test_04_dimensions.rb +3 -5
- data/test/test_05_rows.rb +6 -6
- data/test/test_06_extsst.rb +8 -8
- data/test/test_11_date_time.rb +3 -5
- data/test/test_12_date_only.rb +3 -5
- data/test/test_13_date_seconds.rb +4 -6
- data/test/test_21_escher.rb +3 -5
- data/test/test_22_mso_drawing_group.rb +20 -22
- data/test/test_23_note.rb +5 -7
- data/test/test_24_txo.rb +3 -5
- data/test/test_25_position_object.rb +84 -79
- data/test/test_26_autofilter.rb +3 -13
- data/test/test_27_autofilter.rb +3 -13
- data/test/test_28_autofilter.rb +3 -13
- data/test/test_29_process_jpg.rb +5 -0
- data/test/test_30_validation_dval.rb +3 -5
- data/test/test_31_validation_dv_strings.rb +3 -5
- data/test/test_32_validation_dv_formula.rb +3 -5
- data/test/test_40_property_types.rb +10 -9
- data/test/test_41_properties.rb +1 -0
- data/test/test_42_set_properties.rb +14 -15
- data/test/test_50_name_stored.rb +299 -302
- data/test/test_51_name_print_area.rb +357 -360
- data/test/test_52_name_print_titles.rb +454 -457
- data/test/test_53_autofilter.rb +203 -206
- data/test/test_60_chart_generic.rb +5 -0
- data/test/test_61_chart_subclasses.rb +95 -94
- data/test/test_62_chart_formats.rb +272 -267
- data/test/test_63_chart_area_formats.rb +649 -644
- data/test/test_biff.rb +12 -38
- data/test/test_compatibility.rb +627 -0
- data/test/test_example_match.rb +3 -18
- data/test/test_format.rb +46 -105
- data/test/test_formula.rb +1 -0
- data/test/test_ole.rb +3 -4
- data/test/test_storage_lite.rb +125 -146
- data/test/test_workbook.rb +2 -23
- data/test/test_worksheet.rb +4 -5
- data/utils/add_magic_comment.rb +80 -0
- data/writeexcel.gemspec +8 -2
- metadata +10 -4
@@ -1,169 +1,170 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
# chart.
|
34
|
-
#
|
35
|
-
# :
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
# data
|
40
|
-
#
|
41
|
-
# [ '
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
# chart.
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
#
|
73
|
-
# chart.
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
78
|
-
#
|
79
|
-
#
|
80
|
-
#
|
81
|
-
#
|
82
|
-
#
|
83
|
-
#
|
84
|
-
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
# [
|
93
|
-
# ]
|
94
|
-
#
|
95
|
-
#
|
96
|
-
# worksheet.write('
|
97
|
-
#
|
98
|
-
#
|
99
|
-
# chart
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
103
|
-
#
|
104
|
-
# :
|
105
|
-
# :
|
106
|
-
#
|
107
|
-
#
|
108
|
-
#
|
109
|
-
#
|
110
|
-
#
|
111
|
-
#
|
112
|
-
#
|
113
|
-
# worksheet
|
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
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
data
|
146
|
-
data += [
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
#
|
154
|
-
#
|
155
|
-
#
|
156
|
-
#
|
157
|
-
#
|
158
|
-
#
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
end
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
###############################################################################
|
3
|
+
#
|
4
|
+
# Pie - A writer class for Excel Pie charts.
|
5
|
+
#
|
6
|
+
# Used in conjunction with Spreadsheet::WriteExcel::Chart.
|
7
|
+
#
|
8
|
+
# See formatting note in Spreadsheet::WriteExcel::Chart.
|
9
|
+
#
|
10
|
+
# Copyright 2000-2010, John McNamara, jmcnamara@cpan.org
|
11
|
+
#
|
12
|
+
# original written in Perl by John McNamara
|
13
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
14
|
+
#
|
15
|
+
|
16
|
+
require 'writeexcel'
|
17
|
+
|
18
|
+
class Chart
|
19
|
+
|
20
|
+
# ==SYNOPSIS
|
21
|
+
#
|
22
|
+
# To create a simple Excel file with a Pie chart using WriteExcel:
|
23
|
+
#
|
24
|
+
# #!/usr/bin/ruby -w
|
25
|
+
#
|
26
|
+
# require 'writeexcel'
|
27
|
+
#
|
28
|
+
# workbook = Spreadsheet::WriteExcel.new('chart.xls')
|
29
|
+
# worksheet = workbook.add_worksheet
|
30
|
+
#
|
31
|
+
# chart = workbook.add_chart(:type => Chart::Pie)
|
32
|
+
#
|
33
|
+
# # Configure the chart.
|
34
|
+
# chart.add_series(
|
35
|
+
# :categories => '=Sheet1!$A$2:$A$7',
|
36
|
+
# :values => '=Sheet1!$B$2:$B$7'
|
37
|
+
# )
|
38
|
+
#
|
39
|
+
# # Add the worksheet data the chart refers to.
|
40
|
+
# data = [
|
41
|
+
# [ 'Category', 2, 3, 4, 5, 6, 7 ],
|
42
|
+
# [ 'Value', 1, 4, 5, 2, 1, 5 ]
|
43
|
+
# ]
|
44
|
+
#
|
45
|
+
# worksheet.write('A1', data)
|
46
|
+
#
|
47
|
+
# workbook.close
|
48
|
+
#
|
49
|
+
# ==DESCRIPTION
|
50
|
+
#
|
51
|
+
# This module implements Pie charts for Spreadsheet::WriteExcel. The chart
|
52
|
+
# object is created via the Workbook add_chart() method:
|
53
|
+
#
|
54
|
+
# chart = workbook.add_chart(:type => Chart::Pie)
|
55
|
+
#
|
56
|
+
# Once the object is created it can be configured via the following methods
|
57
|
+
# that are common to all chart classes:
|
58
|
+
#
|
59
|
+
# chart.add_series
|
60
|
+
# chart.set_title
|
61
|
+
#
|
62
|
+
# These methods are explained in detail in Chart section of WriteExcel.
|
63
|
+
# Class specific methods or settings, if any, are explained below.
|
64
|
+
#
|
65
|
+
# ==Pie Chart Methods
|
66
|
+
#
|
67
|
+
# There aren't currently any pie chart specific methods. See the TODO
|
68
|
+
# section of Chart section in WriteExcel.
|
69
|
+
#
|
70
|
+
# A Pie chart doesn't have an X or Y axis so the following common chart
|
71
|
+
# methods are ignored.
|
72
|
+
#
|
73
|
+
# chart.set_x_axis
|
74
|
+
# chart.set_y_axis
|
75
|
+
#
|
76
|
+
# ==EXAMPLE
|
77
|
+
#
|
78
|
+
# Here is a complete example that demonstrates most of the available
|
79
|
+
# features when creating a chart.
|
80
|
+
#
|
81
|
+
# #!/usr/bin/ruby -w
|
82
|
+
#
|
83
|
+
# require 'writeexcel'
|
84
|
+
#
|
85
|
+
# workbook = Spreadsheet::WriteExcel.new('chart_pie.xls')
|
86
|
+
# worksheet = workbook.add_worksheet
|
87
|
+
# bold = workbook.add_format(:bold => 1)
|
88
|
+
#
|
89
|
+
# # Add the worksheet data that the charts will refer to.
|
90
|
+
# headings = [ 'Category', 'Values' ]
|
91
|
+
# data = [
|
92
|
+
# [ 'Apple', 'Cherry', 'Pecan' ],
|
93
|
+
# [ 60, 30, 10 ],
|
94
|
+
# ]
|
95
|
+
#
|
96
|
+
# worksheet.write('A1', headings, bold)
|
97
|
+
# worksheet.write('A2', data)
|
98
|
+
#
|
99
|
+
# # Create a new chart object. In this case an embedded chart.
|
100
|
+
# chart = workbook.add_chart(:type => Chart::Pie, :embedded => 1)
|
101
|
+
#
|
102
|
+
# # Configure the series.
|
103
|
+
# chart.add_series(
|
104
|
+
# :name => 'Pie sales data',
|
105
|
+
# :categories => '=Sheet1!$A$2:$A$4',
|
106
|
+
# :values => '=Sheet1!$B$2:$B$4',
|
107
|
+
# )
|
108
|
+
#
|
109
|
+
# # Add a title.
|
110
|
+
# chart.set_title(:name => 'Popular Pie Types')
|
111
|
+
#
|
112
|
+
#
|
113
|
+
# # Insert the chart into the worksheet (with an offset).
|
114
|
+
# worksheet.insert_chart('C2', chart, 25, 10)
|
115
|
+
#
|
116
|
+
# workbook.close
|
117
|
+
#
|
118
|
+
class Pie < Chart
|
119
|
+
###############################################################################
|
120
|
+
#
|
121
|
+
# new()
|
122
|
+
#
|
123
|
+
#
|
124
|
+
def initialize(*args) # :nodoc:
|
125
|
+
super
|
126
|
+
@vary_data_color = 1
|
127
|
+
end
|
128
|
+
|
129
|
+
###############################################################################
|
130
|
+
#
|
131
|
+
# _store_chart_type()
|
132
|
+
#
|
133
|
+
# Implementation of the abstract method from the specific chart class.
|
134
|
+
#
|
135
|
+
# Write the Pie chart BIFF record.
|
136
|
+
#
|
137
|
+
def store_chart_type # :nodoc:
|
138
|
+
record = 0x1019 # Record identifier.
|
139
|
+
length = 0x0006 # Number of bytes to follow.
|
140
|
+
angle = 0x0000 # Angle.
|
141
|
+
donut = 0x0000 # Donut hole size.
|
142
|
+
grbit = 0x0002 # Option flags.
|
143
|
+
|
144
|
+
header = [record, length].pack('vv')
|
145
|
+
data = [angle].pack('v')
|
146
|
+
data += [donut].pack('v')
|
147
|
+
data += [grbit].pack('v')
|
148
|
+
|
149
|
+
append(header, data)
|
150
|
+
end
|
151
|
+
|
152
|
+
###############################################################################
|
153
|
+
#
|
154
|
+
# _store_axisparent_stream(). Overridden.
|
155
|
+
#
|
156
|
+
# Write the AXISPARENT chart substream.
|
157
|
+
#
|
158
|
+
# A Pie chart has no X or Y axis so we override this method to remove them.
|
159
|
+
#
|
160
|
+
def store_axisparent_stream # :nodoc:
|
161
|
+
store_axisparent(*@config[:axisparent])
|
162
|
+
|
163
|
+
store_begin
|
164
|
+
store_pos(*@config[:axisparent_pos])
|
165
|
+
|
166
|
+
store_chartformat_stream
|
167
|
+
store_end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
1
2
|
###############################################################################
|
2
3
|
#
|
3
4
|
# Scatter - A writer class for Excel Scatter charts.
|
@@ -110,9 +111,9 @@ class Chart
|
|
110
111
|
# )
|
111
112
|
#
|
112
113
|
# # Add a chart title and some axis labels.
|
113
|
-
# chart.set_title (name => 'Results of sample analysis')
|
114
|
-
# chart.set_x_axis(name => 'Test number')
|
115
|
-
# chart.set_y_axis(name => 'Sample length (cm)')
|
114
|
+
# chart.set_title (:name => 'Results of sample analysis')
|
115
|
+
# chart.set_x_axis(:name => 'Test number')
|
116
|
+
# chart.set_y_axis(:name => 'Sample length (cm)')
|
116
117
|
#
|
117
118
|
# # Insert the chart into the worksheet (with an offset).
|
118
119
|
# worksheet.insert_chart('D2', chart, 25, 10)
|
@@ -1,211 +1,212 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
# chart.add_series(:categories => '=Sheet1!$A$2:$A$6', :values => '=Sheet1!$
|
35
|
-
# chart.add_series(:categories => '=Sheet1!$A$2:$A$6', :values => '=Sheet1!$
|
36
|
-
# chart.add_series(:categories => '=Sheet1!$A$2:$A$6', :values => '=Sheet1!$
|
37
|
-
#
|
38
|
-
#
|
39
|
-
# #
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
# chart.
|
55
|
-
# chart.
|
56
|
-
# chart.
|
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
|
-
# [ '2009-08-
|
91
|
-
# [ '2009-08-
|
92
|
-
# [ '2009-08-
|
93
|
-
# [ '2009-08-
|
94
|
-
#
|
95
|
-
#
|
96
|
-
#
|
97
|
-
#
|
98
|
-
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
# worksheet.write(row,
|
102
|
-
# worksheet.write(row,
|
103
|
-
# worksheet.write(row,
|
104
|
-
# worksheet.write(row,
|
105
|
-
# row
|
106
|
-
#
|
107
|
-
#
|
108
|
-
#
|
109
|
-
# chart
|
110
|
-
#
|
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
|
-
# chart
|
138
|
-
# chart.
|
139
|
-
# chart.
|
140
|
-
#
|
141
|
-
#
|
142
|
-
# worksheet
|
143
|
-
#
|
144
|
-
#
|
145
|
-
#
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
#
|
150
|
-
#
|
151
|
-
#
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
#
|
159
|
-
#
|
160
|
-
#
|
161
|
-
#
|
162
|
-
#
|
163
|
-
#
|
164
|
-
#
|
165
|
-
#
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
#
|
180
|
-
#
|
181
|
-
#
|
182
|
-
#
|
183
|
-
#
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
end
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
###############################################################################
|
3
|
+
#
|
4
|
+
# Stock - A writer class for Excel Stock charts.
|
5
|
+
#
|
6
|
+
# Used in conjunction with Spreadsheet::WriteExcel::Chart.
|
7
|
+
#
|
8
|
+
# See formatting note in Spreadsheet::WriteExcel::Chart.
|
9
|
+
#
|
10
|
+
# Copyright 2000-2010, John McNamara, jmcnamara@cpan.org
|
11
|
+
#
|
12
|
+
# original written in Perl by John McNamara
|
13
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
14
|
+
#
|
15
|
+
|
16
|
+
require 'writeexcel'
|
17
|
+
|
18
|
+
class Chart
|
19
|
+
|
20
|
+
# ==SYNOPSIS
|
21
|
+
#
|
22
|
+
# To create a simple Excel file with a Stock chart using WriteExcel:
|
23
|
+
#
|
24
|
+
# #!/usr/bin/ruby -w
|
25
|
+
#
|
26
|
+
# require 'writeexcel'
|
27
|
+
#
|
28
|
+
# workbook = Spreadsheet::WriteExcel.new('chart.xls')
|
29
|
+
# worksheet = workbook.add_worksheet
|
30
|
+
#
|
31
|
+
# chart = workbook.add_chart(:type => Chart::Stock)
|
32
|
+
#
|
33
|
+
# # Add a series for each Open-High-Low-Close.
|
34
|
+
# chart.add_series(:categories => '=Sheet1!$A$2:$A$6', :values => '=Sheet1!$B$2:$B$6')
|
35
|
+
# chart.add_series(:categories => '=Sheet1!$A$2:$A$6', :values => '=Sheet1!$C$2:$C$6')
|
36
|
+
# chart.add_series(:categories => '=Sheet1!$A$2:$A$6', :values => '=Sheet1!$D$2:$D$6')
|
37
|
+
# chart.add_series(:categories => '=Sheet1!$A$2:$A$6', :values => '=Sheet1!$E$2:$E$6')
|
38
|
+
#
|
39
|
+
# # Add the worksheet data the chart refers to.
|
40
|
+
# # ... See the full example below.
|
41
|
+
#
|
42
|
+
# workbook.close
|
43
|
+
#
|
44
|
+
# ==DESCRIPTION
|
45
|
+
#
|
46
|
+
# This module implements Stock charts for WriteExcel. The chart object
|
47
|
+
# is created via the Workbook add_chart() method:
|
48
|
+
#
|
49
|
+
# chart = workbook.add_chart(:type => Chart::Stock)
|
50
|
+
#
|
51
|
+
# Once the object is created it can be configured via the following methods
|
52
|
+
# that are common to all chart classes:
|
53
|
+
#
|
54
|
+
# chart.add_series
|
55
|
+
# chart.set_x_axis
|
56
|
+
# chart.set_y_axis
|
57
|
+
# chart.set_title
|
58
|
+
#
|
59
|
+
# These methods are explained in detail in Chart section of WriteExcel.
|
60
|
+
# Class specific methods or settings, if any, are explained below.
|
61
|
+
#
|
62
|
+
# ==Stock Chart Methods
|
63
|
+
#
|
64
|
+
# There aren't currently any stock chart specific methods.
|
65
|
+
# See the TODO section of Chart section in WriteExcel.
|
66
|
+
#
|
67
|
+
# The default Stock chart is an Open-High-Low-Close chart.
|
68
|
+
# A series must be added for each of these data sources.
|
69
|
+
#
|
70
|
+
# The default Stock chart is in black and white. User defined colours
|
71
|
+
# will be added at a later stage.
|
72
|
+
#
|
73
|
+
# ==EXAMPLE
|
74
|
+
#
|
75
|
+
# Here is a complete example that demonstrates most of the available features
|
76
|
+
# when creating a Stock chart.
|
77
|
+
#
|
78
|
+
# #!/usr/bin/ruby -w
|
79
|
+
#
|
80
|
+
# require 'writeexcel'
|
81
|
+
#
|
82
|
+
# workbook = Spreadsheet::WriteExcel.new('chart_stock_ex.xls')
|
83
|
+
# worksheet = workbook.add_worksheet
|
84
|
+
# bold = workbook.add_format(:bold => 1)
|
85
|
+
# date_format = workbook.add_format(:num_format => 'dd/mm/yyyy')
|
86
|
+
#
|
87
|
+
# # Add the worksheet data that the charts will refer to.
|
88
|
+
# headings = [ 'Date', 'Open', 'High', 'Low', 'Close' ]
|
89
|
+
# data = [
|
90
|
+
# [ '2009-08-23', 110.75, 113.48, 109.05, 109.40 ],
|
91
|
+
# [ '2009-08-24', 111.24, 111.60, 103.57, 104.87 ],
|
92
|
+
# [ '2009-08-25', 104.96, 108.00, 103.88, 106.00 ],
|
93
|
+
# [ '2009-08-26', 104.95, 107.95, 104.66, 107.91 ],
|
94
|
+
# [ '2009-08-27', 108.10, 108.62, 105.69, 106.15 ]
|
95
|
+
# ]
|
96
|
+
#
|
97
|
+
# worksheet.write('A1', headings, bold)
|
98
|
+
#
|
99
|
+
# row = 1
|
100
|
+
# data.each do |d|
|
101
|
+
# worksheet.write(row, 0, d[0], date_format)
|
102
|
+
# worksheet.write(row, 1, d[1])
|
103
|
+
# worksheet.write(row, 2, d[2])
|
104
|
+
# worksheet.write(row, 3, d[3])
|
105
|
+
# worksheet.write(row, 4, d[4])
|
106
|
+
# row += 1
|
107
|
+
# end
|
108
|
+
#
|
109
|
+
# # Create a new chart object. In this case an embedded chart.
|
110
|
+
# chart = workbook.add_chart(:type => Chart::Stock, ::embedded => 1)
|
111
|
+
#
|
112
|
+
# # Add a series for each of the Open-High-Low-Close columns.
|
113
|
+
# chart.add_series(
|
114
|
+
# :categories => '=Sheet1!$A$2:$A$6',
|
115
|
+
# :values => '=Sheet1!$B$2:$B$6',
|
116
|
+
# :name => 'Open'
|
117
|
+
# )
|
118
|
+
#
|
119
|
+
# chart.add_series(
|
120
|
+
# :categories => '=Sheet1!$A$2:$A$6',
|
121
|
+
# :values => '=Sheet1!$C$2:$C$6',
|
122
|
+
# :name => 'High'
|
123
|
+
# )
|
124
|
+
#
|
125
|
+
# chart.add_series(
|
126
|
+
# :categories => '=Sheet1!$A$2:$A$6',
|
127
|
+
# :values => '=Sheet1!$D$2:$D$6',
|
128
|
+
# :name => 'Low'
|
129
|
+
# )
|
130
|
+
#
|
131
|
+
# chart.add_series(
|
132
|
+
# :categories => '=Sheet1!$A$2:$A$6',
|
133
|
+
# :values => '=Sheet1!$E$2:$E$6',
|
134
|
+
# :name => 'Close'
|
135
|
+
# )
|
136
|
+
#
|
137
|
+
# # Add a chart title and some axis labels.
|
138
|
+
# chart.set_title(:name => 'Open-High-Low-Close')
|
139
|
+
# chart.set_x_axis(:name => 'Date')
|
140
|
+
# chart.set_y_axis(:name => 'Share price')
|
141
|
+
#
|
142
|
+
# # Insert the chart into the worksheet (with an offset).
|
143
|
+
# worksheet.insert_chart('F2', chart, 25, 10)
|
144
|
+
#
|
145
|
+
# workbook.close
|
146
|
+
#
|
147
|
+
class Stock < Chart
|
148
|
+
###############################################################################
|
149
|
+
#
|
150
|
+
# new()
|
151
|
+
#
|
152
|
+
#
|
153
|
+
def initialize(*args) # :nodoc:
|
154
|
+
super
|
155
|
+
end
|
156
|
+
|
157
|
+
###############################################################################
|
158
|
+
#
|
159
|
+
# _store_chart_type()
|
160
|
+
#
|
161
|
+
# Implementation of the abstract method from the specific chart class.
|
162
|
+
#
|
163
|
+
# Write the LINE chart BIFF record. A stock chart uses the same LINE record
|
164
|
+
# as a line chart but with additional DROPBAR and CHARTLINE records to define
|
165
|
+
# the stock style.
|
166
|
+
#
|
167
|
+
def store_chart_type # :nodoc:
|
168
|
+
record = 0x1018 # Record identifier.
|
169
|
+
length = 0x0002 # Number of bytes to follow.
|
170
|
+
grbit = 0x0000 # Option flags.
|
171
|
+
|
172
|
+
header = [record, length].pack('vv')
|
173
|
+
data = [grbit].pack('v')
|
174
|
+
|
175
|
+
append(header, data)
|
176
|
+
end
|
177
|
+
|
178
|
+
###############################################################################
|
179
|
+
#
|
180
|
+
# _store_marker_dataformat_stream(). Overridden.
|
181
|
+
#
|
182
|
+
# This is an implementation of the parent abstract method to define
|
183
|
+
# properties of markers, linetypes, pie formats and other.
|
184
|
+
#
|
185
|
+
def store_marker_dataformat_stream # :nodoc:
|
186
|
+
store_dropbar
|
187
|
+
store_begin
|
188
|
+
store_lineformat(0x00000000, 0x0000, 0xFFFF, 0x0001, 0x004F)
|
189
|
+
store_areaformat(0x00FFFFFF, 0x0000, 0x01, 0x01, 0x09, 0x08)
|
190
|
+
store_end
|
191
|
+
|
192
|
+
store_dropbar
|
193
|
+
store_begin
|
194
|
+
store_lineformat(0x00000000, 0x0000, 0xFFFF, 0x0001, 0x004F)
|
195
|
+
store_areaformat(0x0000, 0x00FFFFFF, 0x01, 0x01, 0x08, 0x09)
|
196
|
+
store_end
|
197
|
+
|
198
|
+
store_chartline
|
199
|
+
store_lineformat(0x00000000, 0x0000, 0xFFFF, 0x0000, 0x004F)
|
200
|
+
|
201
|
+
|
202
|
+
store_dataformat(0x0000, 0xFFFD, 0x0000)
|
203
|
+
store_begin
|
204
|
+
store_3dbarshape
|
205
|
+
store_lineformat(0x00000000, 0x0005, 0xFFFF, 0x0000, 0x004F)
|
206
|
+
store_areaformat(0x00000000, 0x0000, 0x00, 0x01, 0x4D, 0x4D)
|
207
|
+
store_pieformat
|
208
|
+
store_markerformat(0x00, 0x00, 0x00, 0x00, 0x4D, 0x4D, 0x3C)
|
209
|
+
store_end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|