writeexcel 0.1.0 → 0.3.0
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.
- data/README +26 -31
- data/examples/a_simple.rb +42 -42
- data/examples/{autofilters.rb → autofilter.rb} +264 -266
- data/examples/bigfile.rb +29 -0
- data/examples/chart_area.rb +120 -0
- data/examples/chart_bar.rb +119 -0
- data/examples/chart_column.rb +119 -0
- data/examples/chart_line.rb +119 -0
- data/examples/chart_pie.rb +107 -0
- data/examples/chart_scatter.rb +120 -0
- data/examples/chart_stock.rb +147 -0
- data/examples/copyformat.rb +51 -51
- data/examples/data_validate.rb +278 -278
- data/examples/date_time.rb +86 -86
- data/examples/defined_name.rb +31 -0
- data/examples/demo.rb +120 -118
- data/examples/diag_border.rb +35 -35
- data/examples/formats.rb +489 -489
- data/examples/header.rb +136 -136
- data/examples/hidden.rb +28 -28
- data/examples/hyperlink.rb +42 -42
- data/examples/images.rb +52 -52
- data/examples/merge1.rb +39 -39
- data/examples/merge2.rb +44 -44
- data/examples/merge3.rb +65 -65
- data/examples/merge4.rb +82 -82
- data/examples/merge5.rb +79 -79
- data/examples/properties.rb +33 -0
- data/examples/properties_jp.rb +32 -0
- data/examples/protection.rb +46 -46
- data/examples/regions.rb +52 -52
- data/examples/repeat.rb +42 -42
- data/examples/stats.rb +75 -75
- data/examples/stocks.rb +80 -80
- data/examples/tab_colors.rb +30 -30
- data/examples/write_arrays.rb +82 -0
- data/lib/writeexcel.rb +1134 -18
- data/lib/writeexcel/biffwriter.rb +273 -260
- data/lib/writeexcel/chart.rb +2306 -217
- data/lib/writeexcel/charts/area.rb +152 -0
- data/lib/writeexcel/charts/bar.rb +177 -0
- data/lib/writeexcel/charts/column.rb +156 -0
- data/lib/writeexcel/charts/external.rb +61 -0
- data/lib/writeexcel/charts/line.rb +152 -0
- data/lib/writeexcel/charts/pie.rb +169 -0
- data/lib/writeexcel/charts/scatter.rb +192 -0
- data/lib/writeexcel/charts/stock.rb +211 -0
- data/lib/writeexcel/excelformulaparser.rb +208 -195
- data/lib/writeexcel/format.rb +1697 -1108
- data/lib/writeexcel/formula.rb +1050 -986
- data/lib/writeexcel/olewriter.rb +322 -322
- data/lib/writeexcel/properties.rb +251 -250
- data/lib/writeexcel/storage_lite.rb +968 -0
- data/lib/writeexcel/workbook.rb +3294 -2630
- data/lib/writeexcel/worksheet.rb +9012 -6377
- data/test/excelfile/Chart1.xls +0 -0
- data/test/excelfile/Chart2.xls +0 -0
- data/test/excelfile/Chart3.xls +0 -0
- data/test/excelfile/Chart4.xls +0 -0
- data/test/excelfile/Chart5.xls +0 -0
- data/test/perl_output/Chart1.xls.data +0 -0
- data/test/perl_output/Chart2.xls.data +0 -0
- data/test/perl_output/Chart3.xls.data +0 -0
- data/test/perl_output/Chart4.xls.data +0 -0
- data/test/perl_output/Chart5.xls.data +0 -0
- data/test/perl_output/a_simple.xls +0 -0
- data/test/perl_output/autofilter.xls +0 -0
- data/test/perl_output/chart_area.xls +0 -0
- data/test/perl_output/chart_bar.xls +0 -0
- data/test/perl_output/chart_column.xls +0 -0
- data/test/perl_output/chart_line.xls +0 -0
- data/test/perl_output/data_validate.xls +0 -0
- data/test/perl_output/date_time.xls +0 -0
- data/test/perl_output/demo.xls +0 -0
- data/test/perl_output/demo101.bin +0 -0
- data/test/perl_output/demo201.bin +0 -0
- data/test/perl_output/demo301.bin +0 -0
- data/test/perl_output/demo401.bin +0 -0
- data/test/perl_output/demo501.bin +0 -0
- data/test/perl_output/diag_border.xls +0 -0
- data/test/perl_output/headers.xls +0 -0
- data/test/perl_output/hyperlink.xls +0 -0
- data/test/perl_output/images.xls +0 -0
- data/test/perl_output/merge1.xls +0 -0
- data/test/perl_output/merge2.xls +0 -0
- data/test/perl_output/merge3.xls +0 -0
- data/test/perl_output/merge4.xls +0 -0
- data/test/perl_output/merge5.xls +0 -0
- data/test/perl_output/protection.xls +0 -0
- data/test/perl_output/regions.xls +0 -0
- data/test/perl_output/stats.xls +0 -0
- data/test/perl_output/stocks.xls +0 -0
- data/test/perl_output/tab_colors.xls +0 -0
- data/test/perl_output/unicode_cyrillic.xls +0 -0
- data/test/perl_output/workbook1.xls +0 -0
- data/test/perl_output/workbook2.xls +0 -0
- data/test/tc_all.rb +32 -31
- data/test/tc_biff.rb +104 -104
- data/test/tc_chart.rb +22 -22
- data/test/tc_example_match.rb +1944 -1280
- data/test/tc_format.rb +1254 -1267
- data/test/tc_formula.rb +63 -63
- data/test/tc_ole.rb +110 -110
- data/test/tc_storage_lite.rb +149 -0
- data/test/tc_workbook.rb +140 -115
- data/test/tc_worksheet.rb +115 -115
- data/test/test_00_IEEE_double.rb +14 -14
- data/test/test_01_add_worksheet.rb +12 -12
- data/test/test_02_merge_formats.rb +58 -58
- data/test/test_04_dimensions.rb +397 -397
- data/test/test_05_rows.rb +182 -182
- data/test/test_06_extsst.rb +80 -80
- data/test/test_11_date_time.rb +484 -484
- data/test/test_12_date_only.rb +506 -506
- data/test/test_13_date_seconds.rb +486 -486
- data/test/test_21_escher.rb +642 -629
- data/test/test_22_mso_drawing_group.rb +750 -739
- data/test/test_23_note.rb +78 -78
- data/test/test_24_txo.rb +80 -80
- data/test/test_25_position_object.rb +82 -0
- data/test/test_26_autofilter.rb +327 -327
- data/test/test_27_autofilter.rb +144 -144
- data/test/test_28_autofilter.rb +174 -174
- data/test/test_29_process_jpg.rb +681 -131
- data/test/test_30_validation_dval.rb +82 -82
- data/test/test_31_validation_dv_strings.rb +131 -131
- data/test/test_32_validation_dv_formula.rb +211 -211
- data/test/test_40_property_types.rb +191 -191
- data/test/test_41_properties.rb +238 -238
- data/test/test_42_set_properties.rb +442 -419
- data/test/test_50_name_stored.rb +305 -0
- data/test/test_51_name_print_area.rb +363 -0
- data/test/test_52_name_print_titles.rb +460 -0
- data/test/test_53_autofilter.rb +209 -0
- data/test/test_60_chart_generic.rb +576 -0
- data/test/test_61_chart_subclasses.rb +97 -0
- data/test/test_62_chart_formats.rb +270 -0
- data/test/test_63_chart_area_formats.rb +647 -0
- data/test/test_chartex.rb +35 -0
- data/test/ts_all.rb +46 -34
- data/writeexcel.gemspec +18 -0
- data/writeexcel.rdoc +583 -0
- metadata +162 -108
@@ -0,0 +1,61 @@
|
|
1
|
+
###############################################################################
|
2
|
+
#
|
3
|
+
# External - A writer class for Excel external charts.
|
4
|
+
#
|
5
|
+
# Used in conjunction with WriteExcel
|
6
|
+
#
|
7
|
+
# perltidy with options: -mbl=2 -pt=0 -nola
|
8
|
+
#
|
9
|
+
# Copyright 2000-2010, John McNamara, jmcnamara@cpan.org
|
10
|
+
#
|
11
|
+
# original written in Perl by John McNamara
|
12
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
13
|
+
#
|
14
|
+
|
15
|
+
require 'writeexcel/chart'
|
16
|
+
|
17
|
+
class External < Chart # :nodoc:
|
18
|
+
###############################################################################
|
19
|
+
#
|
20
|
+
# new()
|
21
|
+
#
|
22
|
+
def initialize(external_filename, *args)
|
23
|
+
super(*args)
|
24
|
+
|
25
|
+
@filename = external_filename
|
26
|
+
@external_bin = true
|
27
|
+
|
28
|
+
_initialize # Requires overridden initialize().
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
###############################################################################
|
33
|
+
#
|
34
|
+
# _initialize()
|
35
|
+
#
|
36
|
+
# Read all the data into memory for the external binary style chart.
|
37
|
+
#
|
38
|
+
def _initialize
|
39
|
+
filename = @filename
|
40
|
+
filehandle = File.open(filename, 'rb')
|
41
|
+
|
42
|
+
@filehandle = filehandle
|
43
|
+
@datasize = FileTest.size(filename)
|
44
|
+
@using_tmpfile = false
|
45
|
+
|
46
|
+
# Read the entire external chart binary into the the data buffer.
|
47
|
+
# This will be retrieved by _get_data() when the chart is closed().
|
48
|
+
@data = @filehandle.read(@datasize)
|
49
|
+
end
|
50
|
+
|
51
|
+
###############################################################################
|
52
|
+
#
|
53
|
+
# _close()
|
54
|
+
#
|
55
|
+
# We don't need to create or store Chart data structures when using an
|
56
|
+
# external binary, so we have a default close method.
|
57
|
+
#
|
58
|
+
def close
|
59
|
+
return nil
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
###############################################################################
|
2
|
+
#
|
3
|
+
# Line - A writer class for Excel Line charts.
|
4
|
+
#
|
5
|
+
# Used in conjunction with Chart.
|
6
|
+
#
|
7
|
+
# See formatting note in Chart.
|
8
|
+
#
|
9
|
+
# Copyright 2000-2010, John McNamara, jmcnamara@cpan.org
|
10
|
+
#
|
11
|
+
# original written in Perl by John McNamara
|
12
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
13
|
+
#
|
14
|
+
|
15
|
+
require 'writeexcel/chart'
|
16
|
+
|
17
|
+
class Chart
|
18
|
+
|
19
|
+
#
|
20
|
+
# ==SYNOPSIS
|
21
|
+
#
|
22
|
+
# To create a simple Excel file with a Line chart using WriteExcel:
|
23
|
+
#
|
24
|
+
# #!/usr/bin/ruby -w
|
25
|
+
#
|
26
|
+
# require 'writeexcel'
|
27
|
+
#
|
28
|
+
# workbook = WriteExcel.new('chart.xls')
|
29
|
+
# worksheet = workbook.add_worksheet
|
30
|
+
#
|
31
|
+
# chart = workbook.add_chart(:type => Chart::Line)
|
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 Line charts for WriteExcel. The chart object is
|
52
|
+
# created via the Workbook add_chart method:
|
53
|
+
#
|
54
|
+
# chart = workbook.add_chart(:type => Chart::Line)
|
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_x_axis
|
61
|
+
# chart.set_y_axis
|
62
|
+
# chart.set_title
|
63
|
+
#
|
64
|
+
# These methods are explained in detail in Chart section of WriteExcel.
|
65
|
+
# Class specific methods or settings, if any, are explained below.
|
66
|
+
#
|
67
|
+
# ==Line Chart Methods
|
68
|
+
#
|
69
|
+
# There aren't currently any Line chart specific methods. See the TODO
|
70
|
+
# section of Chart of Writeexcel.
|
71
|
+
#
|
72
|
+
# ==EXAMPLE
|
73
|
+
#
|
74
|
+
# Here is a complete example that demonstrates most of the available
|
75
|
+
# features when creating a chart.
|
76
|
+
#
|
77
|
+
# #!/usr/bin/ruby -w
|
78
|
+
#
|
79
|
+
# require 'writeexcel'
|
80
|
+
#
|
81
|
+
# workbook = WriteExcel.new('chart_line.xls')
|
82
|
+
# worksheet = workbook.add_worksheet
|
83
|
+
# bold = workbook.add_format(:bold => 1)
|
84
|
+
#
|
85
|
+
# # Add the worksheet data that the charts will refer to.
|
86
|
+
# headings = [ 'Number', 'Sample 1', 'Sample 2' ]
|
87
|
+
# data = [
|
88
|
+
# [ 2, 3, 4, 5, 6, 7 ],
|
89
|
+
# [ 1, 4, 5, 2, 1, 5 ],
|
90
|
+
# [ 3, 6, 7, 5, 4, 3 ]
|
91
|
+
# ]
|
92
|
+
#
|
93
|
+
# worksheet.write('A1', headings, bold)
|
94
|
+
# worksheet.write('A2', data)
|
95
|
+
#
|
96
|
+
# # Create a new chart object. In this case an embedded chart.
|
97
|
+
# chart = workbook.add_chart(:type => Chart::Line, :embedded => 1)
|
98
|
+
#
|
99
|
+
# # Configure the first series. (Sample 1)
|
100
|
+
# chart.add_series(
|
101
|
+
# :name => 'Sample 1',
|
102
|
+
# :categories => '=Sheet1!$A$2:$A$7',
|
103
|
+
# :values => '=Sheet1!$B$2:$B$7',
|
104
|
+
# )
|
105
|
+
#
|
106
|
+
# # Configure the second series. (Sample 2)
|
107
|
+
# chart.add_series(
|
108
|
+
# :name => 'Sample 2',
|
109
|
+
# :categories => '=Sheet1!$A$2:$A$7',
|
110
|
+
# :values => '=Sheet1!$C$2:$C$7',
|
111
|
+
# )
|
112
|
+
#
|
113
|
+
# # Add a chart title and some axis labels.
|
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)')
|
117
|
+
#
|
118
|
+
# # Insert the chart into the worksheet (with an offset).
|
119
|
+
# worksheet.insert_chart('D2', chart, 25, 10)
|
120
|
+
#
|
121
|
+
# workbook.close
|
122
|
+
#
|
123
|
+
class Line < Chart
|
124
|
+
###############################################################################
|
125
|
+
#
|
126
|
+
# new()
|
127
|
+
#
|
128
|
+
#
|
129
|
+
def initialize(*args) # :nodoc:
|
130
|
+
super(*args)
|
131
|
+
end
|
132
|
+
|
133
|
+
###############################################################################
|
134
|
+
#
|
135
|
+
# _store_chart_type()
|
136
|
+
#
|
137
|
+
# Implementation of the abstract method from the specific chart class.
|
138
|
+
#
|
139
|
+
# Write the LINE chart BIFF record. Defines a line chart type.
|
140
|
+
#
|
141
|
+
def store_chart_type # :nodoc:
|
142
|
+
record = 0x1018 # Record identifier.
|
143
|
+
length = 0x0002 # Number of bytes to follow.
|
144
|
+
grbit = 0x0000 # Option flags.
|
145
|
+
|
146
|
+
header = [record, length].pack('vv')
|
147
|
+
data = [grbit].pack('v')
|
148
|
+
|
149
|
+
append(header, data)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
###############################################################################
|
2
|
+
#
|
3
|
+
# Pie - A writer class for Excel Pie charts.
|
4
|
+
#
|
5
|
+
# Used in conjunction with Spreadsheet::WriteExcel::Chart.
|
6
|
+
#
|
7
|
+
# See formatting note in Spreadsheet::WriteExcel::Chart.
|
8
|
+
#
|
9
|
+
# Copyright 2000-2010, John McNamara, jmcnamara@cpan.org
|
10
|
+
#
|
11
|
+
# original written in Perl by John McNamara
|
12
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
13
|
+
#
|
14
|
+
|
15
|
+
require 'writeexcel'
|
16
|
+
|
17
|
+
class Chart
|
18
|
+
|
19
|
+
# ==SYNOPSIS
|
20
|
+
#
|
21
|
+
# To create a simple Excel file with a Pie chart using WriteExcel:
|
22
|
+
#
|
23
|
+
# #!/usr/bin/ruby -w
|
24
|
+
#
|
25
|
+
# require 'writeexcel'
|
26
|
+
#
|
27
|
+
# workbook = Spreadsheet::WriteExcel.new('chart.xls')
|
28
|
+
# worksheet = workbook.add_worksheet
|
29
|
+
#
|
30
|
+
# chart = workbook.add_chart(:type => Chart::Pie)
|
31
|
+
#
|
32
|
+
# # Configure the chart.
|
33
|
+
# chart.add_series(
|
34
|
+
# :categories => '=Sheet1!$A$2:$A$7',
|
35
|
+
# :values => '=Sheet1!$B$2:$B$7'
|
36
|
+
# )
|
37
|
+
#
|
38
|
+
# # Add the worksheet data the chart refers to.
|
39
|
+
# data = [
|
40
|
+
# [ 'Category', 2, 3, 4, 5, 6, 7 ],
|
41
|
+
# [ 'Value', 1, 4, 5, 2, 1, 5 ]
|
42
|
+
# ]
|
43
|
+
#
|
44
|
+
# worksheet.write('A1', data)
|
45
|
+
#
|
46
|
+
# workbook.close
|
47
|
+
#
|
48
|
+
# ==DESCRIPTION
|
49
|
+
#
|
50
|
+
# This module implements Pie charts for Spreadsheet::WriteExcel. The chart
|
51
|
+
# object is created via the Workbook add_chart() method:
|
52
|
+
#
|
53
|
+
# chart = workbook.add_chart(:type => Chart::Pie)
|
54
|
+
#
|
55
|
+
# Once the object is created it can be configured via the following methods
|
56
|
+
# that are common to all chart classes:
|
57
|
+
#
|
58
|
+
# chart.add_series
|
59
|
+
# chart.set_title
|
60
|
+
#
|
61
|
+
# These methods are explained in detail in Chart section of WriteExcel.
|
62
|
+
# Class specific methods or settings, if any, are explained below.
|
63
|
+
#
|
64
|
+
# ==Pie Chart Methods
|
65
|
+
#
|
66
|
+
# There aren't currently any pie chart specific methods. See the TODO
|
67
|
+
# section of Chart section in WriteExcel.
|
68
|
+
#
|
69
|
+
# A Pie chart doesn't have an X or Y axis so the following common chart
|
70
|
+
# methods are ignored.
|
71
|
+
#
|
72
|
+
# chart.set_x_axis
|
73
|
+
# chart.set_y_axis
|
74
|
+
#
|
75
|
+
# ==EXAMPLE
|
76
|
+
#
|
77
|
+
# Here is a complete example that demonstrates most of the available
|
78
|
+
# features when creating a chart.
|
79
|
+
#
|
80
|
+
# #!/usr/bin/ruby -w
|
81
|
+
#
|
82
|
+
# require 'writeexcel'
|
83
|
+
#
|
84
|
+
# workbook = Spreadsheet::WriteExcel.new('chart_pie.xls')
|
85
|
+
# worksheet = workbook.add_worksheet
|
86
|
+
# bold = workbook.add_format(:bold => 1)
|
87
|
+
#
|
88
|
+
# # Add the worksheet data that the charts will refer to.
|
89
|
+
# headings = [ 'Category', 'Values' ]
|
90
|
+
# data = [
|
91
|
+
# [ 'Apple', 'Cherry', 'Pecan' ],
|
92
|
+
# [ 60, 30, 10 ],
|
93
|
+
# ]
|
94
|
+
#
|
95
|
+
# worksheet.write('A1', headings, bold)
|
96
|
+
# worksheet.write('A2', data)
|
97
|
+
#
|
98
|
+
# # Create a new chart object. In this case an embedded chart.
|
99
|
+
# chart = workbook.add_chart(:type => Chart::Pie, :embedded => 1)
|
100
|
+
#
|
101
|
+
# # Configure the series.
|
102
|
+
# chart.add_series(
|
103
|
+
# :name => 'Pie sales data',
|
104
|
+
# :categories => '=Sheet1!$A$2:$A$4',
|
105
|
+
# :values => '=Sheet1!$B$2:$B$4',
|
106
|
+
# )
|
107
|
+
#
|
108
|
+
# # Add a title.
|
109
|
+
# chart.set_title(name => 'Popular Pie Types')
|
110
|
+
#
|
111
|
+
#
|
112
|
+
# # Insert the chart into the worksheet (with an offset).
|
113
|
+
# worksheet.insert_chart('C2', chart, 25, 10)
|
114
|
+
#
|
115
|
+
# workbook.close
|
116
|
+
#
|
117
|
+
class Pie < Chart
|
118
|
+
###############################################################################
|
119
|
+
#
|
120
|
+
# new()
|
121
|
+
#
|
122
|
+
#
|
123
|
+
def initialize(*args) # :nodoc:
|
124
|
+
super
|
125
|
+
@vary_data_color = 1
|
126
|
+
end
|
127
|
+
|
128
|
+
###############################################################################
|
129
|
+
#
|
130
|
+
# _store_chart_type()
|
131
|
+
#
|
132
|
+
# Implementation of the abstract method from the specific chart class.
|
133
|
+
#
|
134
|
+
# Write the Pie chart BIFF record.
|
135
|
+
#
|
136
|
+
def store_chart_type # :nodoc:
|
137
|
+
record = 0x1019 # Record identifier.
|
138
|
+
length = 0x0006 # Number of bytes to follow.
|
139
|
+
angle = 0x0000 # Angle.
|
140
|
+
donut = 0x0000 # Donut hole size.
|
141
|
+
grbit = 0x0002 # Option flags.
|
142
|
+
|
143
|
+
header = [record, length].pack('vv')
|
144
|
+
data = [angle].pack('v')
|
145
|
+
data += [donut].pack('v')
|
146
|
+
data += [grbit].pack('v')
|
147
|
+
|
148
|
+
append(header, data)
|
149
|
+
end
|
150
|
+
|
151
|
+
###############################################################################
|
152
|
+
#
|
153
|
+
# _store_axisparent_stream(). Overridden.
|
154
|
+
#
|
155
|
+
# Write the AXISPARENT chart substream.
|
156
|
+
#
|
157
|
+
# A Pie chart has no X or Y axis so we override this method to remove them.
|
158
|
+
#
|
159
|
+
def store_axisparent_stream # :nodoc:
|
160
|
+
store_axisparent(*@config[:axisparent])
|
161
|
+
|
162
|
+
store_begin
|
163
|
+
store_pos(*@config[:axisparent_pos])
|
164
|
+
|
165
|
+
store_chartformat_stream
|
166
|
+
store_end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
@@ -0,0 +1,192 @@
|
|
1
|
+
###############################################################################
|
2
|
+
#
|
3
|
+
# Scatter - A writer class for Excel Scatter charts.
|
4
|
+
#
|
5
|
+
# Used in conjunction with Spreadsheet::WriteExcel::Chart.
|
6
|
+
#
|
7
|
+
# See formatting note in Spreadsheet::WriteExcel::Chart.
|
8
|
+
#
|
9
|
+
# Copyright 2000-2010, John McNamara, jmcnamara@cpan.org
|
10
|
+
#
|
11
|
+
# original written in Perl by John McNamara
|
12
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
13
|
+
#
|
14
|
+
|
15
|
+
require 'writeexcel'
|
16
|
+
|
17
|
+
class Chart
|
18
|
+
|
19
|
+
# ==SYNOPSIS
|
20
|
+
#
|
21
|
+
# To create a simple Excel file with a Scatter chart using WriteExcel:
|
22
|
+
#
|
23
|
+
# #!/usr/bin/ruby -w
|
24
|
+
#
|
25
|
+
# require 'writeexcel'
|
26
|
+
#
|
27
|
+
# workbook = Spreadsheet::WriteExcel.new('chart.xls')
|
28
|
+
# worksheet = workbook.add_worksheet
|
29
|
+
#
|
30
|
+
# chart = workbook.add_chart(:type => Chart::Scatter)
|
31
|
+
#
|
32
|
+
# # Configure the chart.
|
33
|
+
# chart.add_series(
|
34
|
+
# :categories => '=Sheet1!$A$2:$A$7',
|
35
|
+
# :values => '=Sheet1!$B$2:$B$7'
|
36
|
+
# )
|
37
|
+
#
|
38
|
+
# # Add the worksheet data the chart refers to.
|
39
|
+
# data = [
|
40
|
+
# [ 'Category', 2, 3, 4, 5, 6, 7 ],
|
41
|
+
# [ 'Value', 1, 4, 5, 2, 1, 5 ]
|
42
|
+
# ]
|
43
|
+
#
|
44
|
+
# worksheet.write('A1', data)
|
45
|
+
#
|
46
|
+
# workbook.close
|
47
|
+
#
|
48
|
+
# ==DESCRIPTION
|
49
|
+
#
|
50
|
+
# This module implements Scatter charts for WriteExcel.
|
51
|
+
# The chart object is created via the Workbook add_chart() method:
|
52
|
+
#
|
53
|
+
# chart = workbook.add_chart(:type => Chart::Scatter)
|
54
|
+
#
|
55
|
+
# Once the object is created it can be configured via the following
|
56
|
+
# methods that are common to all chart classes:
|
57
|
+
#
|
58
|
+
# chart.add_series
|
59
|
+
# chart.set_x_axis
|
60
|
+
# chart.set_y_axis
|
61
|
+
# chart.set_title
|
62
|
+
#
|
63
|
+
# These methods are explained in detail in Chart section of WriteExcel.
|
64
|
+
# Class specific methods or settings, if any, are explained below.
|
65
|
+
#
|
66
|
+
# ==Scatter Chart Methods
|
67
|
+
#
|
68
|
+
# There aren't currently any scatter chart specific methods. See the TODO
|
69
|
+
# section of Chart section in WriteExcel.
|
70
|
+
#
|
71
|
+
# ==EXAMPLE
|
72
|
+
#
|
73
|
+
# Here is a complete example that demonstrates most of the available
|
74
|
+
# features when creating a chart.
|
75
|
+
#
|
76
|
+
# #!/usr/bin/ruby -w
|
77
|
+
#
|
78
|
+
# require 'writeexcel'
|
79
|
+
#
|
80
|
+
# workbook = Spreadsheet::WriteExcel.new('chart_scatter.xls')
|
81
|
+
# worksheet = workbook.add_worksheet
|
82
|
+
# bold = workbook.add_format(:bold => 1)
|
83
|
+
#
|
84
|
+
# # Add the worksheet data that the charts will refer to.
|
85
|
+
# headings = [ 'Number', 'Sample 1', 'Sample 2' ]
|
86
|
+
# data = [
|
87
|
+
# [ 2, 3, 4, 5, 6, 7 ],
|
88
|
+
# [ 1, 4, 5, 2, 1, 5 ],
|
89
|
+
# [ 3, 6, 7, 5, 4, 3 ]
|
90
|
+
# ]
|
91
|
+
#
|
92
|
+
# worksheet.write('A1', headings, bold)
|
93
|
+
# worksheet.write('A2', data)
|
94
|
+
#
|
95
|
+
# # Create a new chart object. In this case an embedded chart.
|
96
|
+
# chart = workbook.add_chart(:type => Chart::Scatter, :embedded => 1)
|
97
|
+
#
|
98
|
+
# # Configure the first series. (Sample 1)
|
99
|
+
# chart.add_series(
|
100
|
+
# :name => 'Sample 1',
|
101
|
+
# :categories => '=Sheet1!$A$2:$A$7',
|
102
|
+
# :values => '=Sheet1!$B$2:$B$7'
|
103
|
+
# )
|
104
|
+
#
|
105
|
+
# # Configure the second series. (Sample 2)
|
106
|
+
# chart.add_series(
|
107
|
+
# :name => 'Sample 2',
|
108
|
+
# :categories => '=Sheet1!$A$2:$A$7',
|
109
|
+
# :values => '=Sheet1!$C$2:$C$7'
|
110
|
+
# )
|
111
|
+
#
|
112
|
+
# # 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)')
|
116
|
+
#
|
117
|
+
# # Insert the chart into the worksheet (with an offset).
|
118
|
+
# worksheet.insert_chart('D2', chart, 25, 10)
|
119
|
+
#
|
120
|
+
# workbook.close
|
121
|
+
#
|
122
|
+
class Scatter < Chart
|
123
|
+
###############################################################################
|
124
|
+
#
|
125
|
+
# new()
|
126
|
+
#
|
127
|
+
#
|
128
|
+
def initialize(*args) # :nodoc:
|
129
|
+
super
|
130
|
+
end
|
131
|
+
|
132
|
+
###############################################################################
|
133
|
+
#
|
134
|
+
# _store_chart_type()
|
135
|
+
#
|
136
|
+
# Implementation of the abstract method from the specific chart class.
|
137
|
+
#
|
138
|
+
# Write the AREA chart BIFF record. Defines a area chart type.
|
139
|
+
#
|
140
|
+
def store_chart_type # :nodoc:
|
141
|
+
record = 0x101B # Record identifier.
|
142
|
+
length = 0x0006 # Number of bytes to follow.
|
143
|
+
bubble_ratio = 0x0064 # Bubble ratio.
|
144
|
+
bubble_type = 0x0001 # Bubble type.
|
145
|
+
grbit = 0x0000 # Option flags.
|
146
|
+
|
147
|
+
header = [record, length].pack('vv')
|
148
|
+
data = [bubble_ratio].pack('v')
|
149
|
+
data += [bubble_type].pack('v')
|
150
|
+
data += [grbit].pack('v')
|
151
|
+
|
152
|
+
append(header, data)
|
153
|
+
end
|
154
|
+
|
155
|
+
###############################################################################
|
156
|
+
#
|
157
|
+
# _store_axis_category_stream(). Overridden.
|
158
|
+
#
|
159
|
+
# Write the AXIS chart substream for the chart category.
|
160
|
+
#
|
161
|
+
# For a Scatter chart the category stream is replace with a values stream. We
|
162
|
+
# override this method and turn it into a values stream.
|
163
|
+
#
|
164
|
+
def store_axis_category_stream # :nodoc:
|
165
|
+
store_axis(0)
|
166
|
+
|
167
|
+
store_begin
|
168
|
+
store_valuerange
|
169
|
+
store_tick
|
170
|
+
store_end
|
171
|
+
end
|
172
|
+
|
173
|
+
###############################################################################
|
174
|
+
#
|
175
|
+
# _store_marker_dataformat_stream(). Overridden.
|
176
|
+
#
|
177
|
+
# This is an implementation of the parent abstract method to define
|
178
|
+
# properties of markers, linetypes, pie formats and other.
|
179
|
+
#
|
180
|
+
def store_marker_dataformat_stream # :nodoc:
|
181
|
+
store_dataformat(0x0000, 0xFFFD, 0x0000)
|
182
|
+
|
183
|
+
store_begin
|
184
|
+
store_3dbarshape
|
185
|
+
store_lineformat(0x00000000, 0x0005, 0xFFFF, 0x0008, 0x004D)
|
186
|
+
store_areaformat(0x00FFFFFF, 0x0000, 0x01, 0x01, 0x4E, 0x4D)
|
187
|
+
store_pieformat
|
188
|
+
store_markerformat(0x00, 0x00, 0x02, 0x01, 0x4D, 0x4D, 0x3C)
|
189
|
+
store_end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|