write_xlsx 0.57.0 → 0.58.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.
@@ -74,6 +74,9 @@ the first worksheet in an Excel XML spreadsheet called ruby.xlsx:
74
74
  workbook.close
75
75
 
76
76
  == Recent change
77
+ 2013-02-10 v0.58.0
78
+ Added chart area and plot area formatting.
79
+
77
80
  2013-02-10 v0.57.0
78
81
  Add major and minor axis chart gridline formatting.
79
82
 
@@ -494,6 +494,8 @@ def initialize(subtype) # :nodoc:
494
494
  @horiz_cat_axis = 0
495
495
  @horiz_val_axis = 1
496
496
  @protection = 0
497
+ @chartarea = {}
498
+ @plotarea = {}
497
499
  @x_axis = {}
498
500
  @y_axis = {}
499
501
  @x2_axis = {}
@@ -531,6 +533,9 @@ def assemble_xml_file # :nodoc:
531
533
  # Write the c:chart element.
532
534
  write_chart
533
535
 
536
+ # Write the c:spPr element for the chartarea formatting.
537
+ write_sp_pr(@chartarea)
538
+
534
539
  # Write the c:printSettings element.
535
540
  write_print_settings if @embedded && @embedded != 0
536
541
  end
@@ -897,9 +902,8 @@ def set_legend(params)
897
902
  # set_style() method.
898
903
  #
899
904
  def set_plotarea(params)
900
-
901
- # TODO. Need to refactor for XLSX format.
902
- return
905
+ # Convert the user defined properties to internal properties.
906
+ @plotarea = get_area_properties(params)
903
907
  end
904
908
 
905
909
  #
@@ -913,8 +917,8 @@ def set_plotarea(params)
913
917
  # set_style() method.
914
918
  #
915
919
  def set_chartarea(params)
916
- # TODO. Need to refactor for XLSX format.
917
- return
920
+ # Convert the user defined properties to internal properties.
921
+ @chartarea = get_area_properties(params)
918
922
  end
919
923
 
920
924
  #
@@ -1201,6 +1205,58 @@ def get_palette_color(index) # :nodoc:
1201
1205
  sprintf("%02X%02X%02X", *rgb)
1202
1206
  end
1203
1207
 
1208
+ #
1209
+ # Get the Spreadsheet::WriteExcel line pattern for backward compatibility.
1210
+ #
1211
+ def get_swe_line_pattern(val)
1212
+ value = val.downcase
1213
+ default = 'solid'
1214
+
1215
+ patterns = {
1216
+ 0 => 'solid',
1217
+ 1 => 'dash',
1218
+ 2 => 'dot',
1219
+ 3 => 'dash_dot',
1220
+ 4 => 'long_dash_dot_dot',
1221
+ 5 => 'none',
1222
+ 6 => 'solid',
1223
+ 7 => 'solid',
1224
+ 8 => 'solid',
1225
+ 'solid' => 'solid',
1226
+ 'dash' => 'dash',
1227
+ 'dot' => 'dot',
1228
+ 'dash-dot' => 'dash_dot',
1229
+ 'dash-dot-dot' => 'long_dash_dot_dot',
1230
+ 'none' => 'none',
1231
+ 'dark-gray' => 'solid',
1232
+ 'medium-gray' => 'solid',
1233
+ 'light-gray' => 'solid'
1234
+ }
1235
+
1236
+ patterns[value] || default
1237
+ end
1238
+
1239
+ #
1240
+ # Get the Spreadsheet::WriteExcel line weight for backward compatibility.
1241
+ #
1242
+ def get_swe_line_weight(val)
1243
+ value = val.downcase
1244
+ default = 1
1245
+
1246
+ weights = {
1247
+ 1 => 0.25,
1248
+ 2 => 1,
1249
+ 3 => 2,
1250
+ 4 => 3,
1251
+ 'hairline' => 0.25,
1252
+ 'narrow' => 1,
1253
+ 'medium' => 2,
1254
+ 'wide' => 3
1255
+ }
1256
+
1257
+ weights[value] || default
1258
+ end
1259
+
1204
1260
  #
1205
1261
  # Convert user defined line properties to the structure required internally.
1206
1262
  #
@@ -1368,6 +1424,51 @@ def get_labels_properties(labels) # :nodoc:
1368
1424
  labels
1369
1425
  end
1370
1426
 
1427
+ #
1428
+ # Convert user defined area properties to the structure required internally.
1429
+ #
1430
+ def get_area_properties(arg) # :nodoc:
1431
+ area = {}
1432
+
1433
+ # Map deprecated Spreadsheet::WriteExcel fill colour.
1434
+ arg[:fill] = { :color => arg[:color] } if arg[:color]
1435
+
1436
+ # Map deprecated Spreadsheet::WriteExcel line_weight.
1437
+ if arg[:line_weight]
1438
+ width = get_swe_line_weight(arg[:line_weight])
1439
+ arg[:border] = { :width => width }
1440
+ end
1441
+
1442
+ # Map deprecated Spreadsheet::WriteExcel line_pattern.
1443
+ if arg[:line_pattern]
1444
+ pattern = get_swe_line_pattern(arg[:line_pattern])
1445
+ if pattern == 'none'
1446
+ arg[:border] = { :none => 1 }
1447
+ else
1448
+ arg[:border][:dash_type] = pattern
1449
+ end
1450
+ end
1451
+
1452
+ # Map deprecated Spreadsheet::WriteExcel line colour.
1453
+ arg[:border][:color] = arg[:line_color] if arg[:line_color]
1454
+
1455
+ # Handle Excel::Writer::XLSX style properties.
1456
+
1457
+ # Set the line properties for the chartarea.
1458
+ line = get_line_properties(arg[:line])
1459
+
1460
+ # Allow 'border' as a synonym for 'line'.
1461
+ line = get_line_properties(arg[:border]) if (arg[:border])
1462
+
1463
+ # Set the fill properties for the chartarea.
1464
+ fill = get_fill_properties(arg[:fill])
1465
+
1466
+ area[:_line] = line
1467
+ area[:_fill] = fill
1468
+
1469
+ return area
1470
+ end
1471
+
1371
1472
  def value_or_raise(hash, key, msg)
1372
1473
  raise "Unknown #{msg} '#{key}'" unless hash[key.to_sym]
1373
1474
  hash[key.to_sym]
@@ -1631,6 +1732,9 @@ def write_plot_area_base(type = nil) # :nodoc:
1631
1732
  }
1632
1733
  write_val_axis(params)
1633
1734
  write_cat_or_date_axis(params, type)
1735
+
1736
+ # Write the c:spPr element for the plotarea formatting.
1737
+ write_sp_pr(@plotarea)
1634
1738
  end
1635
1739
  end
1636
1740
 
@@ -2935,7 +3039,7 @@ def write_sp_pr(series) # :nodoc:
2935
3039
  end
2936
3040
  end
2937
3041
  # Write the a:ln element.
2938
- write_a_ln(series[:_line]) if series[:_line] && series[:_line][:_defined] != 0
3042
+ write_a_ln(series[:_line]) if series[:_line] && ptrue?(series[:_line][:_defined])
2939
3043
  end
2940
3044
  end
2941
3045
 
@@ -169,6 +169,9 @@ def write_plot_area
169
169
  :axis_ids => @axis2_ids,
170
170
  :position => 'l'
171
171
  )
172
+
173
+ # Write the c:spPr element for the plotarea formatting.
174
+ write_sp_pr(@plotarea)
172
175
  end
173
176
  end
174
177
 
@@ -1,5 +1,5 @@
1
1
  require 'write_xlsx/workbook'
2
2
 
3
3
  class WriteXLSX < Writexlsx::Workbook
4
- VERSION = "0.57.0"
4
+ VERSION = "0.58.0"
5
5
  end
@@ -0,0 +1,52 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class TestRegressionChartChartArea01 < Test::Unit::TestCase
5
+ def setup
6
+ setup_dir_var
7
+ end
8
+
9
+ def teardown
10
+ File.delete(@xlsx) if File.exist?(@xlsx)
11
+ end
12
+
13
+ def test_chart_chartarea01
14
+ @xlsx = 'chart_chartarea01.xlsx'
15
+ workbook = WriteXLSX.new(@xlsx)
16
+ worksheet = workbook.add_worksheet
17
+ chart = workbook.add_chart(:type => 'column', :embedded => 1)
18
+
19
+ # For testing, copy the randomly generated axis ids in the target xlsx file.
20
+ chart.instance_variable_set(:@axis_ids, [82933248, 82952960])
21
+
22
+ data = [
23
+ [1, 2, 3, 4, 5],
24
+ [2, 4, 6, 8, 10],
25
+ [3, 6, 9, 12, 15]
26
+ ]
27
+
28
+ worksheet.write('A1', data)
29
+
30
+ chart.add_series(:values => '=Sheet1!$A$1:$A$5')
31
+ chart.add_series(:values => '=Sheet1!$B$1:$B$5')
32
+ chart.add_series(:values => '=Sheet1!$C$1:$C$5')
33
+
34
+ chart.set_chartarea(
35
+ :border => { :none => 1 },
36
+ :fill => { :color => 'red' }
37
+ )
38
+ chart.set_plotarea(
39
+ :border => {
40
+ :color => 'yellow',
41
+ :width => 1,
42
+ :dash_type => 'dash'
43
+ },
44
+ :fill => { :color => '#92D050' }
45
+ )
46
+
47
+ worksheet.insert_chart('E9', chart)
48
+
49
+ workbook.close
50
+ compare_xlsx_for_regression(File.join(@regression_output, @xlsx), @xlsx)
51
+ end
52
+ end
@@ -0,0 +1,52 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class TestRegressionChartChartArea02 < Test::Unit::TestCase
5
+ def setup
6
+ setup_dir_var
7
+ end
8
+
9
+ def teardown
10
+ File.delete(@xlsx) if File.exist?(@xlsx)
11
+ end
12
+
13
+ def test_chart_chartarea02
14
+ @xlsx = 'chart_chartarea01.xlsx'
15
+ workbook = WriteXLSX.new(@xlsx)
16
+ worksheet = workbook.add_worksheet
17
+ chart = workbook.add_chart(:type => 'column', :embedded => 1)
18
+
19
+ # For testing, copy the randomly generated axis ids in the target xlsx file.
20
+ chart.instance_variable_set(:@axis_ids, [82933248, 82952960])
21
+
22
+ data = [
23
+ [1, 2, 3, 4, 5],
24
+ [2, 4, 6, 8, 10],
25
+ [3, 6, 9, 12, 15]
26
+ ]
27
+
28
+ worksheet.write('A1', data)
29
+
30
+ chart.add_series(:values => '=Sheet1!$A$1:$A$5')
31
+ chart.add_series(:values => '=Sheet1!$B$1:$B$5')
32
+ chart.add_series(:values => '=Sheet1!$C$1:$C$5')
33
+
34
+ # Test the deprecated writeexcel interface.
35
+ chart.set_chartarea(
36
+ :line_pattern => 'none',
37
+ :color => 'red'
38
+ )
39
+
40
+ chart.set_plotarea(
41
+ :line_pattern => 'dash',
42
+ :line_weight => 'narrow',
43
+ :line_color => 'yellow',
44
+ :color => '#92D050'
45
+ )
46
+
47
+ worksheet.insert_chart('E9', chart)
48
+
49
+ workbook.close
50
+ compare_xlsx_for_regression(File.join(@regression_output, @xlsx), @xlsx)
51
+ end
52
+ end
@@ -0,0 +1,55 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class TestRegressionChartChartArea03 < Test::Unit::TestCase
5
+ def setup
6
+ setup_dir_var
7
+ end
8
+
9
+ def teardown
10
+ File.delete(@xlsx) if File.exist?(@xlsx)
11
+ end
12
+
13
+ def test_chart_chartarea03
14
+ @xlsx = 'chart_chartarea03.xlsx'
15
+ workbook = WriteXLSX.new(@xlsx)
16
+ worksheet = workbook.add_worksheet
17
+ chart = workbook.add_chart(:type => 'scatter', :embedded => 1)
18
+
19
+ # For testing, copy the randomly generated axis ids in the target xlsx file.
20
+ chart.instance_variable_set(:@axis_ids, [46210048, 46208512])
21
+
22
+ data = [
23
+ [1, 2, 3, 4, 5],
24
+ [2, 4, 6, 8, 10],
25
+ [3, 6, 9, 12, 15]
26
+ ]
27
+
28
+ worksheet.write('A1', data)
29
+
30
+ chart.add_series(
31
+ :categories => '=Sheet1!$A$1:$A$5',
32
+ :values => '=Sheet1!$B$1:$B$5'
33
+ )
34
+ chart.add_series(
35
+ :categories => '=Sheet1!$A$1:$A$5',
36
+ :values => '=Sheet1!$C$1:$C$5'
37
+ )
38
+
39
+ # Test the deprecated writeexcel interface.
40
+ chart.set_chartarea(
41
+ :border => { :dash_type => 'round_dot' },
42
+ :fill => { :color => '#9999FF' }
43
+ )
44
+
45
+ chart.set_plotarea(
46
+ :border => { :dash_type => 'square_dot' },
47
+ :fill => { :color => '#FFC000' }
48
+ )
49
+
50
+ worksheet.insert_chart('E9', chart)
51
+
52
+ workbook.close
53
+ compare_xlsx_for_regression(File.join(@regression_output, @xlsx), @xlsx)
54
+ end
55
+ end
@@ -0,0 +1,73 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class TestRegressionChartChartArea04 < Test::Unit::TestCase
5
+ def setup
6
+ setup_dir_var
7
+ end
8
+
9
+ def teardown
10
+ File.delete(@xlsx) if File.exist?(@xlsx)
11
+ end
12
+
13
+ def test_chart_chartarea04
14
+ @xlsx = 'chart_chartarea04.xlsx'
15
+ workbook = WriteXLSX.new(@xlsx)
16
+ worksheet = workbook.add_worksheet
17
+ chart = workbook.add_chart(:type => 'stock', :embedded => 1)
18
+ date_format = workbook.add_format(:num_format => 14)
19
+
20
+ # For testing, copy the randomly generated axis ids in the target xlsx file.
21
+ chart.instance_variable_set(:@axis_ids, [82954112, 82956288])
22
+
23
+ data = [
24
+ [ '2007-01-01T', '2007-01-02T', '2007-01-03T', '2007-01-04T', '2007-01-05T' ],
25
+ [ 27.2, 25.03, 19.05, 20.34, 18.5 ],
26
+ [ 23.49, 19.55, 15.12, 17.84, 16.34 ],
27
+ [ 25.45, 23.05, 17.32, 20.45, 17.34 ]
28
+ ]
29
+
30
+ (0..4).each do |row|
31
+ worksheet.write_date_time(row, 0, data[0][row], date_format)
32
+ worksheet.write(row, 1, data[1][row])
33
+ worksheet.write(row, 2, data[2][row])
34
+ worksheet.write(row, 3, data[3][row])
35
+ end
36
+
37
+ worksheet.set_column('A:D', 11)
38
+
39
+ chart.add_series(
40
+ :categories => '=Sheet1!$A$1:$A$5',
41
+ :values => '=Sheet1!$B$1:$B$5'
42
+ )
43
+ chart.add_series(
44
+ :categories => '=Sheet1!$A$1:$A$5',
45
+ :values => '=Sheet1!$C$1:$C$5'
46
+ )
47
+ chart.add_series(
48
+ :categories => '=Sheet1!$A$1:$A$5',
49
+ :values => '=Sheet1!$D$1:$D$5'
50
+ )
51
+
52
+ # Test the deprecated writeexcel interface.
53
+ chart.set_chartarea(
54
+ :border => { :color => '#FF0000' },
55
+ :fill => { :color => '#00B050' }
56
+ )
57
+
58
+ chart.set_plotarea(
59
+ :border => { :dash_type => 'dash_dot' },
60
+ :fill => { :color => '#FFC000' }
61
+ )
62
+
63
+ worksheet.insert_chart('E9', chart)
64
+
65
+ workbook.close
66
+ compare_xlsx_for_regression(
67
+ File.join(@regression_output, @xlsx),
68
+ @xlsx,
69
+ [],
70
+ {'xl/charts/chart1.xml' => ['<c:formatCode']}
71
+ )
72
+ end
73
+ end
@@ -0,0 +1,50 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class TestRegressionChartChartArea05 < Test::Unit::TestCase
5
+ def setup
6
+ setup_dir_var
7
+ end
8
+
9
+ def teardown
10
+ File.delete(@xlsx) if File.exist?(@xlsx)
11
+ end
12
+
13
+ def test_chart_chartarea05
14
+ @xlsx = 'chart_chartarea05.xlsx'
15
+ workbook = WriteXLSX.new(@xlsx)
16
+ worksheet = workbook.add_worksheet
17
+ chart = workbook.add_chart(:type => 'pie', :embedded => 1)
18
+
19
+ data = [
20
+ [2, 4, 6],
21
+ [60, 30, 10]
22
+ ]
23
+
24
+ worksheet.write('A1', data)
25
+
26
+ chart.add_series(
27
+ :categories => '=Sheet1!$A$1:$A$3',
28
+ :values => '=Sheet1!$B$1:$B$3'
29
+ )
30
+
31
+ chart.set_chartarea(
32
+ :border => {
33
+ :color => '#FFFF00',
34
+ :dash_type => 'long_dash'
35
+ },
36
+ :fill => { :color => '#92D050' }
37
+ )
38
+
39
+ # This should be ignored for a pie chart.
40
+ chart.set_plotarea(
41
+ :border => { :dash_type => 'dash_dot' },
42
+ :fill => { :color => '#FFC000' }
43
+ )
44
+
45
+ worksheet.insert_chart('E9', chart)
46
+
47
+ workbook.close
48
+ compare_xlsx_for_regression(File.join(@regression_output, @xlsx), @xlsx)
49
+ end
50
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: write_xlsx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.57.0
4
+ version: 0.58.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -387,6 +387,11 @@ files:
387
387
  - test/regression/test_chart_blank04.rb
388
388
  - test/regression/test_chart_blank05.rb
389
389
  - test/regression/test_chart_blank06.rb
390
+ - test/regression/test_chart_chartarea01.rb
391
+ - test/regression/test_chart_chartarea02.rb
392
+ - test/regression/test_chart_chartarea03.rb
393
+ - test/regression/test_chart_chartarea04.rb
394
+ - test/regression/test_chart_chartarea05.rb
390
395
  - test/regression/test_chart_column01.rb
391
396
  - test/regression/test_chart_column02.rb
392
397
  - test/regression/test_chart_column03.rb
@@ -682,6 +687,10 @@ files:
682
687
  - test/regression/xlsx_files/chart_blank04.xlsx
683
688
  - test/regression/xlsx_files/chart_blank05.xlsx
684
689
  - test/regression/xlsx_files/chart_blank06.xlsx
690
+ - test/regression/xlsx_files/chart_chartarea01.xlsx
691
+ - test/regression/xlsx_files/chart_chartarea03.xlsx
692
+ - test/regression/xlsx_files/chart_chartarea04.xlsx
693
+ - test/regression/xlsx_files/chart_chartarea05.xlsx
685
694
  - test/regression/xlsx_files/chart_column01.xlsx
686
695
  - test/regression/xlsx_files/chart_column02.xlsx
687
696
  - test/regression/xlsx_files/chart_column03.xlsx
@@ -1286,6 +1295,11 @@ test_files:
1286
1295
  - test/regression/test_chart_blank04.rb
1287
1296
  - test/regression/test_chart_blank05.rb
1288
1297
  - test/regression/test_chart_blank06.rb
1298
+ - test/regression/test_chart_chartarea01.rb
1299
+ - test/regression/test_chart_chartarea02.rb
1300
+ - test/regression/test_chart_chartarea03.rb
1301
+ - test/regression/test_chart_chartarea04.rb
1302
+ - test/regression/test_chart_chartarea05.rb
1289
1303
  - test/regression/test_chart_column01.rb
1290
1304
  - test/regression/test_chart_column02.rb
1291
1305
  - test/regression/test_chart_column03.rb
@@ -1581,6 +1595,10 @@ test_files:
1581
1595
  - test/regression/xlsx_files/chart_blank04.xlsx
1582
1596
  - test/regression/xlsx_files/chart_blank05.xlsx
1583
1597
  - test/regression/xlsx_files/chart_blank06.xlsx
1598
+ - test/regression/xlsx_files/chart_chartarea01.xlsx
1599
+ - test/regression/xlsx_files/chart_chartarea03.xlsx
1600
+ - test/regression/xlsx_files/chart_chartarea04.xlsx
1601
+ - test/regression/xlsx_files/chart_chartarea05.xlsx
1584
1602
  - test/regression/xlsx_files/chart_column01.xlsx
1585
1603
  - test/regression/xlsx_files/chart_column02.xlsx
1586
1604
  - test/regression/xlsx_files/chart_column03.xlsx