diff options
| author | Randy Morgan <[email protected]> | 2012-05-03 09:10:23 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-05-03 09:10:23 +0900 |
| commit | e622dc54069074198990beb0462da630bad38dd0 (patch) | |
| tree | 4065eb11cf4dcd68440bf8347af4509c130fb11e | |
| parent | 2dfdd1f26b1fc748da3b4edad9aeeaeae842aedb (diff) | |
| download | caxlsx-e622dc54069074198990beb0462da630bad38dd0.tar.gz caxlsx-e622dc54069074198990beb0462da630bad38dd0.zip | |
rebuild series data base objects with full implementation.
Address shape validation error.
27 files changed, 486 insertions, 192 deletions
diff --git a/examples/chart_colors.rb b/examples/chart_colors.rb index 41966d47..5c1109f7 100644 --- a/examples/chart_colors.rb +++ b/examples/chart_colors.rb @@ -11,24 +11,36 @@ wb = p.workbook wb.add_worksheet(:name => "Bar Chart") do |sheet| sheet.add_row ["A Simple Bar Chart"] sheet.add_row ["First", "Second", "Third"] - sheet.add_row [1, 2, 3] + sheet.add_row [1,2,3.5] sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A4", :end_at => "F17", :title => sheet["A1"]) do |chart| chart.add_series :data => sheet["A3:C3"], :labels => sheet["A2:C2"], :colors => ['FF0000', '00FF00', '0000FF'], :color => "000000" chart.valAxis.label_rotation = -45 chart.catAxis.label_rotation = 45 + puts chart.to_xml_string + end +end + +##Generating A Bar Chart without data in the sheet + +wb.add_worksheet(:name => "Hard Bar Chart") do |sheet| + sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A4", :end_at => "F17", :title => "hard code chart") do |chart| + chart.add_series :data => [1,3,5], :labels => ['a', 'b', 'c'], :colors => ['FF0000', '00FF00', '0000FF'] + chart.valAxis.label_rotation = -45 + chart.catAxis.label_rotation = 45 + puts chart.to_xml_string end end ##Generating A Pie Chart -wb.add_worksheet(:name => "Pie Chart") do |sheet| - sheet.add_row ["First", "Second", "Third", "Fourth"] - sheet.add_row [1, 2, 3, "=PRODUCT(A2:C2)"] + wb.add_worksheet(:name => "Pie Chart") do |sheet| + sheet.add_row ["First", "Second", "Third", "Fourth"] + sheet.add_row [1, 2, 3, "=PRODUCT(A2:C2)"] sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,2], :end_at => [5, 15], :title => "example 3: Pie Chart") do |chart| - chart.add_series :data => sheet["A2:D2"], :labels => sheet["A1:D1"], :colors => ['FF0000', '00FF00', '0000FF'] - end -end + chart.add_series :data => sheet["A2:D2"], :labels => sheet["A1:D1"], :colors => ['FF0000', '00FF00', '0000FF'] + end + end # Line Chart @@ -45,18 +57,18 @@ end ##Generating A Scatter Chart -wb.add_worksheet(:name => "Scatter Chart") do |sheet| - sheet.add_row ["First", 1, 5, 7, 9] - sheet.add_row ["", 1, 25, 49, 81] - sheet.add_row ["Second", 5, 2, 14, 9] - sheet.add_row ["", 5, 10, 15, 20] - sheet.add_chart(Axlsx::ScatterChart, :title => "example 7: Scatter Chart") do |chart| - chart.start_at 0, 4 - chart.end_at 10, 19 - chart.add_series :xData => sheet["B1:E1"], :yData => sheet["B2:E2"], :title => sheet["A1"], :color => '00FF00' - chart.add_series :xData => sheet["B3:E3"], :yData => sheet["B4:E4"], :title => sheet["A3"], :color => 'FF0000' - end -end + wb.add_worksheet(:name => "Scatter Chart") do |sheet| + sheet.add_row ["First", 1, 5, 7, 9] + sheet.add_row ["", 1, 25, 49, 81] + sheet.add_row ["Second", 5, 2, 14, 9] + sheet.add_row ["", 5, 10, 15, 20] + sheet.add_chart(Axlsx::ScatterChart, :title => "example 7: Scatter Chart") do |chart| + chart.start_at 0, 4 + chart.end_at 10, 19 + chart.add_series :xData => sheet["B1:E1"], :yData => sheet["B2:E2"], :title => sheet["A1"], :color => '00FF00' + chart.add_series :xData => sheet["B3:E3"], :yData => sheet["B4:E4"], :title => sheet["A3"], :color => 'FF0000' + end + end p.validate.each { |e| puts e.message } diff --git a/examples/extractive.rb b/examples/extractive.rb new file mode 100644 index 00000000..168c5167 --- /dev/null +++ b/examples/extractive.rb @@ -0,0 +1,24 @@ + +data = {:'oil-bbl' => [9591.411671,8860.714604,8237.936509,7700.523067,7231.831842,6819.307902,6453.297235,6126.251755,5832.182748,5566.276532,5324.618706,5103.992757,4901.730597,4715.600022,4543.718862,4384.488699,4236.543136,4098.707009,3969.963937,3849.430276,3736.33405,3629.997786,3529.824424,3435.28568,3345.912388,3261.286414,3181.033882,3104.819438,3032.341401,2963.327615,2897.531915,2834.731083,2774.722219,2717.320479,2662.357097,2609.677666,2559.140642,2510.616021,2463.984182,2419.134871,2375.966292,2334.384311,2294.301748,2255.637735,2218.317165,2182.270173,2147.431696,2113.741056,2081.141602,2049.580377,2019.007821,1989.377502,1960.645871,1932.772042,1905.71759,1879.446367,1853.924338,1829.119424,1805.001366,1781.541597,1758.713122,1736.490414,1714.849315,1693.766943,1673.22161,1653.192744,1633.660817,1614.607284,1596.014514,1577.865741,1560.145009,1542.837121,1525.927597,1509.402632,1493.249056,1477.454298,1462.006352,1446.893746,1432.105512,1417.631159,1403.460647,1389.584361,1375.993094,1362.678018,1349.630672,1336.842938,1324.307026,1312.015455,1299.961043,1288.136885,1276.536347,1265.153047,1253.980847,1243.013838,1232.246332,1221.672852,1211.288119,1201.087047,1191.064732,1181.216443,1171.537618,1162.023853,1152.670896,1143.474642,1134.431125,1125.536513,1116.787101,1108.179309,1099.709672,1091.37484,1083.171571,1075.096725,1067.147265,1059.320247,1051.612821,1044.022223,1036.545778,1029.180889,1021.92504,1014.775789], + +:'gas-mcf' => [940970.153,836556.732,748476.939,673307.64,608520.158,552204.935,502892.3,459432.263,420911.806,386596.658,355889.491,328299.357,303418.988,280907.679,260478.232,241886.857,224925.287,209414.552,195200.029,182147.455,170139.707,159074.174,148860.596,139419.282,130679.626,122578.864,115061.035,108076.1,101579.19,95529.972,89892.104,84632.762,79722.24,75133.597,70842.349,66826.213,63064.865,59539.742,56233.86,53131.662,50218.869,47482.365,44910.082,42490.907,40214.588,38071.662,36053.382,34151.656,32358.989,30668.434,29073.545,27568.334,26147.237,24805.079,23537.041,22338.633,21205.67,20134.246,19120.717,18161.677,17253.941,16394.533,15580.663,14809.724,14079.269,13387.006,12730.786,12108.59,11518.524,10958.809,10427.774,9923.847,9445.55,8991.492,8560.364,8150.934,7762.041,7392.59,7041.551,6707.951,6390.874,6089.452,5802.871,5530.358,5271.185,5024.664,4790.145,4567.013,4354.687,4152.618,3960.286,3777.198,3602.889,3436.919,3278.87,3128.347,2984.976,2848.401,2718.287,2594.316,2476.184,2363.606,2256.309,2154.037,2056.545,1963.599,1874.982,1790.482,1709.903,1633.054,1559.758,1489.844,1423.151,1359.525,1298.821,1240.899,1185.628,1132.883,1082.544,1034.499], + + :'water-bbl' => [940.97,836.557,748.477,673.308,608.52,552.205,502.892,459.432,420.912,386.597,355.889,328.299,303.419,280.908,260.478,241.887,224.925,209.415,195.2,182.147,170.14,159.074,148.861,139.419,130.68,122.579,115.061,108.076,101.579,95.53,89.892,84.633,79.722,75.134,70.842,66.826,63.065,59.54,56.234,53.132,50.219,47.482,44.91,42.491,40.215,38.072,36.053,34.152,32.359,30.668,29.074,27.568,26.147,24.805,23.537,22.339,21.206,20.134,19.121,18.162,17.254,16.395,15.581,14.81,14.079,13.387,12.731,12.109,11.519,10.959,10.428,9.924,9.446,8.991,8.56,8.151,7.762,7.393,7.042,6.708,6.391,6.089,5.803,5.53,5.271,5.025,4.79,4.567,4.355,4.153,3.96,3.777,3.603,3.437,3.279,3.128,2.985,2.848,2.718,2.594,2.476,2.364,2.256,2.154,2.057,1.964,1.875,1.79,1.71,1.633,1.56,1.49,1.423,1.36,1.299,1.241,1.186,1.133,1.083,1.034]} + +#!/usr/bin/env ruby -w -s +# -*- coding: utf-8 -*- +$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" +require 'axlsx' + +p = Axlsx::Package.new +wb = p.workbook +wb.add_worksheet(:name => 'volumes') do |ws| + data.each do |key, values| + ws.add_row [key] + values + end + ws.add_chart(Axlsx::Line3DChart) do |chart| + chart.add_series ws.rows.first.cells(1..ws.rows.first.cells.size) + chart.add_series ws.rows.first.cells(1..ws.rows.first.cells.size) + chart.add_series ws.rows.first.cells(1..ws.rows.first.cells.size) + end +end diff --git a/lib/axlsx/drawing/ax_data_source.rb b/lib/axlsx/drawing/ax_data_source.rb new file mode 100644 index 00000000..5bbfd55b --- /dev/null +++ b/lib/axlsx/drawing/ax_data_source.rb @@ -0,0 +1,25 @@ +module Axlsx + # An axis data source that can contain referenced or literal strings or numbers + # @note only string data types are supported - mainly because we have not implemented a chart type that requires a numerical axis value + class AxDataSource < NumDataSource + + # allowed element tag names + # @return [Array] + def self.allowed_tag_names + [:xVal, :cat] + end + + # creates a new NumDataSource object + # @option options [Array] data An array of Cells or Numeric objects + # @option options [Symbol] tag_name see tag_name + def initialize(options={}) + @tag_name = :cat + @data_type = StrData + @ref_tag_name = :strRef + super(options) + end + + end + +end + diff --git a/lib/axlsx/drawing/bar_series.rb b/lib/axlsx/drawing/bar_series.rb index 25523410..5863dacc 100644 --- a/lib/axlsx/drawing/bar_series.rb +++ b/lib/axlsx/drawing/bar_series.rb @@ -8,7 +8,7 @@ module Axlsx # The data for this series. - # @return [Array, SimpleTypedList] + # @return [NumDataSource] attr_reader :data # The labels for this series. @@ -34,8 +34,8 @@ module Axlsx @shape = :box @colors = [] super(chart, options) - self.labels = CatAxisData.new(options[:labels]) unless options[:labels].nil? - self.data = NamedAxisData.new(:val, options[:data]) unless options[:data].nil? + self.labels = AxDataSource.new({:data => options[:labels]}) unless options[:labels].nil? + self.data = NumDataSource.new(options) unless options[:data].nil? end # @see colors @@ -71,10 +71,10 @@ module Axlsx private # assigns the data for this series - def data=(v) DataTypeValidator.validate "Series.data", [SimpleTypedList], v; @data = v; end + def data=(v) DataTypeValidator.validate "Series.data", [NumDataSource], v; @data = v; end # assigns the labels for this series - def labels=(v) DataTypeValidator.validate "Series.labels", [SimpleTypedList], v; @labels = v; end + def labels=(v) DataTypeValidator.validate "Series.labels", [AxDataSource], v; @labels = v; end end diff --git a/lib/axlsx/drawing/cat_axis_data.rb b/lib/axlsx/drawing/cat_axis_data.rb deleted file mode 100644 index 52a6a542..00000000 --- a/lib/axlsx/drawing/cat_axis_data.rb +++ /dev/null @@ -1,34 +0,0 @@ -# encoding: UTF-8 -module Axlsx - # The CatAxisData class serializes the category axis data for a chart - class CatAxisData < SimpleTypedList - - # Create a new CatAxisData object - # @param [Array, SimpleTypedList] data the data for this category axis. This can be a simple array or a simple typed list of cells. - def initialize(data=[]) - super Object - @list.concat data if data.is_a?(Array) - data.each { |i| @list << i } if data.is_a?(SimpleTypedList) - end - - # Serializes the object - # @param [String] str - # @return [String] - def to_xml_string(str = '') - str << '<c:cat>' - str << '<c:strRef>' - str << '<c:f>' << Axlsx::cell_range(@list) << '</c:f>' - str << '<c:strCache>' - str << '<c:ptCount val="' << size.to_s << '"/>' - each_with_index do |item, index| - v = item.is_a?(Cell) ? item.value.to_s : item - str << '<c:pt idx="' << index.to_s << '"><c:v>' << v.to_s << '</c:v></c:pt>' - end - str << '</c:strCache>' - str << '</c:strRef>' - str << '</c:cat>' - end - - end - -end diff --git a/lib/axlsx/drawing/drawing.rb b/lib/axlsx/drawing/drawing.rb index 7e734113..17b6e0b5 100644 --- a/lib/axlsx/drawing/drawing.rb +++ b/lib/axlsx/drawing/drawing.rb @@ -10,17 +10,18 @@ module Axlsx require 'axlsx/drawing/scaling.rb' require 'axlsx/drawing/axis.rb' + + require 'axlsx/drawing/str_val.rb' + require 'axlsx/drawing/num_val.rb' + require 'axlsx/drawing/str_data.rb' + require 'axlsx/drawing/num_data.rb' + require 'axlsx/drawing/num_data_source.rb' + require 'axlsx/drawing/ax_data_source.rb' + require 'axlsx/drawing/ser_axis.rb' require 'axlsx/drawing/cat_axis.rb' require 'axlsx/drawing/val_axis.rb' - - #TODO rewrite these classes to fully implement: - # CT_AxDataSource for xVal and cat(Category Axis Data) - # CT_NumDataSource for yVal and val(Values) - require 'axlsx/drawing/cat_axis_data.rb' - require 'axlsx/drawing/named_axis_data.rb' - require 'axlsx/drawing/marker.rb' require 'axlsx/drawing/one_cell_anchor.rb' diff --git a/lib/axlsx/drawing/line_series.rb b/lib/axlsx/drawing/line_series.rb index 1d785a23..017822ed 100644 --- a/lib/axlsx/drawing/line_series.rb +++ b/lib/axlsx/drawing/line_series.rb @@ -26,8 +26,8 @@ module Axlsx def initialize(chart, options={}) @labels, @data = nil, nil super(chart, options) - @labels = CatAxisData.new(options[:labels]) unless options[:labels].nil? - @data = NamedAxisData.new('val', options[:data]) unless options[:data].nil? + @labels = AxDataSource.new(:data => options[:labels]) unless options[:labels].nil? + @data = NumDataSource.new(options) unless options[:data].nil? end # @see color @@ -54,10 +54,10 @@ module Axlsx private # assigns the data for this series - def data=(v) DataTypeValidator.validate "Series.data", [SimpleTypedList], v; @data = v; end + def data=(v) DataTypeValidator.validate "Series.data", [NumDataSource], v; @data = v; end # assigns the labels for this series - def labels=(v) DataTypeValidator.validate "Series.labels", [SimpleTypedList], v; @labels = v; end + def labels=(v) DataTypeValidator.validate "Series.labels", [AxDataSource], v; @labels = v; end end end diff --git a/lib/axlsx/drawing/named_axis_data.rb b/lib/axlsx/drawing/named_axis_data.rb deleted file mode 100644 index 2272976c..00000000 --- a/lib/axlsx/drawing/named_axis_data.rb +++ /dev/null @@ -1,35 +0,0 @@ -# encoding: UTF-8 -module Axlsx - # The NamedAxisData class manages the values for a chart value series. - class NamedAxisData < CatAxisData - - # creates a new NamedAxisData Object - # @param [String] name The serialized node name for the axis data object - # @param [Array] The data to associate with the axis data object - def initialize(name, data=[]) - super(data) - @name = name - end - - # Serializes the object - # @param [String] str - # @return [String] - def to_xml_string(str = '') - str << '<c:' << @name.to_s << '>' - str << '<c:numRef>' - str << '<c:f>' << Axlsx::cell_range(@list) << '</c:f>' - str << '<c:numCache>' - str << '<c:formatCode>General</c:formatCode>' - str << '<c:ptCount val="' << size.to_s << '"/>' - each_with_index do |item, index| - v = item.is_a?(Cell) ? item.value.to_s : item - str << '<c:pt idx="' << index.to_s << '"><c:v>' << v.to_s << '</c:v></c:pt>' - end - str << '</c:numCache>' - str << '</c:numRef>' - str << '</c:' << @name.to_s << '>' - end - - end - -end diff --git a/lib/axlsx/drawing/num_data.rb b/lib/axlsx/drawing/num_data.rb new file mode 100644 index 00000000..802a0216 --- /dev/null +++ b/lib/axlsx/drawing/num_data.rb @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +module Axlsx + + #This class specifies data for a particular data point. It is used for both numCache and numLit object + class NumData + + # A string representing the format code to apply. For more information see see the SpreadsheetML numFmt element's (§18.8.30) formatCode attribute. + # @return [String] + attr_reader :format_code + + # creates a new NumVal object + # @option options [String] formatCode + # @option options [Array] :data + # @see StrData + def initialize(options={}) + @format_code = "General" + @pt = SimpleTypedList.new NumVal + options.each do |o| + self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" + end + end + + # Creates the val objects for this data set. I am not overly confident this is going to play nicely with time and data types. + # @param [Array] values An array of cells or values. + def data=(values=[]) + @tag_name = values.first.is_a?(Cell) ? :numCache : :numLit + values.each do |value| + v = value.is_a?(Cell) ? value.value : value + @pt << NumVal.new(:v => v) + end + end + + # @see format_code + def format_code=(v='General') + Axlsx::validate_string(v) + @format_code = v + end + + # serialize the object + def to_xml_string(str = "") + str << '<c:' << @tag_name.to_s << '>' + str << '<c:formatCode>' << format_code.to_s << '</c:formatCode>' + str << '<c:ptCount val="' << @pt.size.to_s << '"/>' + @pt.each_with_index do |num_val, index| + num_val.to_xml_string index, str + end + str << '</c:' << @tag_name.to_s << '>' + end + + end + +end diff --git a/lib/axlsx/drawing/num_data_source.rb b/lib/axlsx/drawing/num_data_source.rb new file mode 100644 index 00000000..60628883 --- /dev/null +++ b/lib/axlsx/drawing/num_data_source.rb @@ -0,0 +1,58 @@ +module Axlsx + # A numeric data source for use by charts. + class NumDataSource + + # The tag name to use when serializing this data source. + # Only items defined in allowed_tag_names are allowed + # @return [Symbol] + attr_reader :tag_name + + attr_reader :data + + # allowed element tag names + # @return [Array] + def self.allowed_tag_names + [:yVal, :val] + end + + # creates a new NumDataSource object + # @option options [Array] data An array of Cells or Numeric objects + # @option options [Symbol] tag_name see tag_name + def initialize(options={}) + # override these three in child classes + @data_type ||= NumData + @tag_name ||= :val + @ref_tag_name ||= :numRef + + @f = nil + @data = @data_type.new(options) + if options[:data] && options[:data].first.is_a?(Cell) + @f = Axlsx::cell_range(options[:data]) + end + options.each do |o| + self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" + end + end + + # sets the tag name for this data source + # @param [Symbol] One of the allowed_tag_names + def tag_name=(v) + Axlsx::RestrictionValidator.validate "#{self.class.name}.tag_name", self.class.allowed_tag_names, v + @tag_name = v + end + + def to_xml_string(str="") + str << '<c:' << tag_name.to_s << '>' + if @f + str << '<c:' << @ref_tag_name.to_s << '>' + str << '<c:f>' << @f.to_s << '</c:f>' + end + @data.to_xml_string str + if @f + str << '</c:' << @ref_tag_name.to_s << '>' + end + str << '</c:' << tag_name.to_s << '>' + end + end +end + diff --git a/lib/axlsx/drawing/num_val.rb b/lib/axlsx/drawing/num_val.rb new file mode 100644 index 00000000..ed655732 --- /dev/null +++ b/lib/axlsx/drawing/num_val.rb @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +module Axlsx + + #This class specifies data for a particular data point. + class NumVal < StrVal + + # A string representing the format code to apply. + # For more information see see the SpreadsheetML numFmt element's (§18.8.30) formatCode attribute. + # @return [String] + attr_reader :format_code + + # creates a new NumVal object + # @option options [String] formatCode + # @option options [Integer] v + def initialize(options={}) + @format_code = "General" + super(options) + end + + # @see format_code + def format_code=(v) + Axlsx::validate_string(v) + @format_code = v + end + + # serialize the object + def to_xml_string(idx, str = "") + Axlsx::validate_unsigned_int(idx) + str << '<c:pt idx="' << idx.to_s << '" formatCode="' << format_code << '"><c:v>' << v.to_s << '</c:v></c:pt>' + end + end +end diff --git a/lib/axlsx/drawing/pie_series.rb b/lib/axlsx/drawing/pie_series.rb index 13f3cd5d..28056c43 100644 --- a/lib/axlsx/drawing/pie_series.rb +++ b/lib/axlsx/drawing/pie_series.rb @@ -32,8 +32,8 @@ module Axlsx @explosion = nil @colors = [] super(chart, options) - self.labels = CatAxisData.new(options[:labels]) unless options[:labels].nil? - self.data = NamedAxisData.new(:val, options[:data]) unless options[:data].nil? + self.labels = AxDataSource.new(:data => options[:labels]) unless options[:labels].nil? + self.data = NumDataSource.new(options) unless options[:data].nil? end # @see colors @@ -64,10 +64,10 @@ module Axlsx private # assigns the data for this series - def data=(v) DataTypeValidator.validate "Series.data", [SimpleTypedList], v; @data = v; end + def data=(v) DataTypeValidator.validate "Series.data", [NumDataSource], v; @data = v; end # assigns the labels for this series - def labels=(v) DataTypeValidator.validate "Series.labels", [SimpleTypedList], v; @labels = v; end + def labels=(v) DataTypeValidator.validate "Series.labels", [AxDataSource], v; @labels = v; end end diff --git a/lib/axlsx/drawing/scatter_series.rb b/lib/axlsx/drawing/scatter_series.rb index 3f4bf289..6084c110 100644 --- a/lib/axlsx/drawing/scatter_series.rb +++ b/lib/axlsx/drawing/scatter_series.rb @@ -25,8 +25,8 @@ module Axlsx def initialize(chart, options={}) @xData, @yData = nil super(chart, options) - @xData = NamedAxisData.new("xVal", options[:xData]) unless options[:xData].nil? - @yData = NamedAxisData.new("yVal", options[:yData]) unless options[:yData].nil? + @xData = AxDataSource.new(:tag_name => :xVal, :data => options[:xData]) unless options[:xData].nil? + @yData = NumDataSource.new({:tag_name => :yVal, :data => options[:yData]}) unless options[:yData].nil? end # @see color diff --git a/lib/axlsx/drawing/str_data.rb b/lib/axlsx/drawing/str_data.rb new file mode 100644 index 00000000..cb4ff54f --- /dev/null +++ b/lib/axlsx/drawing/str_data.rb @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +module Axlsx + + #This specifies the last string data used for a chart. (e.g. strLit and strCache) + # This class is extended for NumData to include the formatCode attribute required for numLit and numCache + class StrData + + # creates a new StrVal object + # @option options [Array] :data + # @option options [String] :tag_name + def initialize(options={}) + @tag_prefix = :str + @type = StrVal + @pt = SimpleTypedList.new(@type) + options.each do |o| + self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" + end + end + + # Creates the val objects for this data set. I am not overly confident this is going to play nicely with time and data types. + # @param [Array] values An array of cells or values. + def data=(values=[]) + @tag_name = values.first.is_a?(Cell) ? :strCache : :strLit + values.each do |value| + v = value.is_a?(Cell) ? value.value : value + @pt << @type.new(:v => v) + end + end + + # serialize the object + def to_xml_string(str = "") + str << '<c:' << @tag_name.to_s << '>' + str << '<c:ptCount val="' << @pt.size.to_s << '"/>' + @pt.each_with_index do |value, index| + value.to_xml_string index, str + end + str << '</c:' << @tag_name.to_s << '>' + end + + end + +end diff --git a/lib/axlsx/drawing/str_val.rb b/lib/axlsx/drawing/str_val.rb new file mode 100644 index 00000000..5f453d97 --- /dev/null +++ b/lib/axlsx/drawing/str_val.rb @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +module Axlsx + + #This class specifies data for a particular data point. + class StrVal + + # a string value. + # @return [String] + attr_reader :v + + # creates a new StrVal object + # @option options [String] v + def initialize(options={}) + @v = "" + @idx = 0 + options.each do |o| + self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" + end + end + # @see v + def v=(v) + @v = v.to_s + end + + # serialize the object + def to_xml_string(idx, str = "") + Axlsx::validate_unsigned_int(idx) + str << '<c:pt idx="' << idx.to_s << '"><c:v>' << v.to_s << '</c:v></c:pt>' + end + + end + +end diff --git a/test/drawing/tc_bar_series.rb b/test/drawing/tc_bar_series.rb index 443427c1..93ebbbbb 100644 --- a/test/drawing/tc_bar_series.rb +++ b/test/drawing/tc_bar_series.rb @@ -11,23 +11,16 @@ class TestBarSeries < Test::Unit::TestCase def test_initialize assert_equal(@series.title.text, "bob", "series title has been applied") - assert_equal(@series.data, [0,1,2], "data option applied") - assert_equal(@series.labels, ["zero", "one","two"], "labels option applied") + assert_equal(@series.data.class, Axlsx::NumDataSource, "data option applied") assert_equal(@series.shape, :cone, "series shape has been applied") + assert(@series.data.is_a?(Axlsx::NumDataSource)) + assert(@series.labels.is_a?(Axlsx::AxDataSource)) end def test_colors assert_equal(@series.colors.size, 3) end - def test_data - assert_equal(@series.data, [0,1,2]) - end - - def test_labels - assert_equal(@series.labels, ["zero", "one", "two"]) - end - def test_shape assert_raise(ArgumentError, "require valid shape") { @series.shape = :teardropt } assert_nothing_raised("allow valid shape") { @series.shape = :box } diff --git a/test/drawing/tc_cat_axis_data.rb b/test/drawing/tc_cat_axis_data.rb index 4c90cf60..09b14be6 100644 --- a/test/drawing/tc_cat_axis_data.rb +++ b/test/drawing/tc_cat_axis_data.rb @@ -1,27 +1,27 @@ -require 'tc_helper.rb' +# require 'tc_helper.rb' -class TestCatAxisData < Test::Unit::TestCase +# class TestCatAxisData < Test::Unit::TestCase - def setup - p = Axlsx::Package.new - @ws = p.workbook.add_worksheet - @chart = @ws.drawing.add_chart Axlsx::Bar3DChart - @series = @chart.add_series :labels=>["zero", "one", "two"] - end +# def setup +# p = Axlsx::Package.new +# @ws = p.workbook.add_worksheet +# @chart = @ws.drawing.add_chart Axlsx::Bar3DChart +# @series = @chart.add_series :labels=>["zero", "one", "two"] +# end - def test_initialize - assert(@series.labels.is_a?Axlsx::SimpleTypedList) - assert_equal(@series.labels, ["zero", "one", "two"]) - end +# def test_initialize +# assert(@series.labels.is_a?Axlsx::SimpleTypedList) +# assert_equal(@series.labels, ["zero", "one", "two"]) +# end - def test_to_xml_string - doc = Nokogiri::XML(@chart.to_xml_string) - assert_equal(doc.xpath("//c:cat/c:strRef/c:f").size,1) - assert_equal(doc.xpath("//c:strCache/c:ptCount[@val='#{@series.labels.size}']").size,1) - @series.labels.each_with_index do |label, index| - assert_equal(doc.xpath("//c:strCache/c:pt[@idx='#{index}']").size,1) - assert_equal(doc.xpath("//c:strCache/c:pt/c:v[text()='#{label}']").size,1) - end - end +# def test_to_xml_string +# doc = Nokogiri::XML(@chart.to_xml_string) +# assert_equal(doc.xpath("//c:cat/c:strRef/c:f").size,1) +# assert_equal(doc.xpath("//c:strCache/c:ptCount[@val='#{@series.labels.size}']").size,1) +# @series.labels.each_with_index do |label, index| +# assert_equal(doc.xpath("//c:strCache/c:pt[@idx='#{index}']").size,1) +# assert_equal(doc.xpath("//c:strCache/c:pt/c:v[text()='#{label}']").size,1) +# end +# end -end +# end diff --git a/test/drawing/tc_chart.rb b/test/drawing/tc_chart.rb index 8047c018..98a72e9c 100644 --- a/test/drawing/tc_chart.rb +++ b/test/drawing/tc_chart.rb @@ -50,8 +50,6 @@ class TestChart < Test::Unit::TestCase s = @chart.add_series :data=>[0,1,2,3], :labels => ["one", 1, "anything"], :title=>"bob" assert_equal(@chart.series.last, s, "series has been added to chart series collection") assert_equal(s.title.text, "bob", "series title has been applied") - assert_equal(s.data, [0,1,2,3], "data option applied") - assert_equal(s.labels, ["one",1,"anything"], "labels option applied") end def test_pn diff --git a/test/drawing/tc_data_source.rb b/test/drawing/tc_data_source.rb new file mode 100644 index 00000000..6b35e008 --- /dev/null +++ b/test/drawing/tc_data_source.rb @@ -0,0 +1,17 @@ + require 'tc_helper.rb' + + class TestNumDataSource < Test::Unit::TestCase + + def setup + @data_source = Axlsx::NumDataSource.new :data => ["1", "2", "3"] + end + + def test_to_xml_string_strLit + str = '<?xml version="1.0" encoding="UTF-8"?>' + str << '<c:chartSpace xmlns:c="' << Axlsx::XML_NS_C << '">' + str << @data_source.to_xml_string + doc = Nokogiri::XML(str) + assert_equal(doc.xpath("//c:val").size, 1) + end + + end diff --git a/test/drawing/tc_line_series.rb b/test/drawing/tc_line_series.rb index a2417549..ef180e39 100644 --- a/test/drawing/tc_line_series.rb +++ b/test/drawing/tc_line_series.rb @@ -11,16 +11,10 @@ class TestLineSeries < Test::Unit::TestCase def test_initialize assert_equal(@series.title.text, "bob", "series title has been applied") - assert_equal(@series.data, [0,1,2], "data option applied") - assert_equal(@series.labels, ["zero", "one","two"], "labels option applied") - end - - def test_data - assert_equal(@series.data, [0,1,2]) - end + assert_equal(@series.labels.class, Axlsx::AxDataSource) + assert_equal(@series.data.class, Axlsx::NumDataSource) - def test_labels - assert_equal(@series.labels, ["zero", "one", "two"]) end + #TODO serialization testing end diff --git a/test/drawing/tc_named_axis_data.rb b/test/drawing/tc_named_axis_data.rb index fe3a32fb..63fb4610 100644 --- a/test/drawing/tc_named_axis_data.rb +++ b/test/drawing/tc_named_axis_data.rb @@ -1,27 +1,27 @@ -require 'tc_helper.rb' +# require 'tc_helper.rb' -class TestNamedAxisData < Test::Unit::TestCase +# class TestNamedAxisData < Test::Unit::TestCase - def setup - p = Axlsx::Package.new - @ws = p.workbook.add_worksheet - @chart = @ws.drawing.add_chart Axlsx::Line3DChart - @series = @chart.add_series :data=>[0,1,2] - end +# def setup +# p = Axlsx::Package.new +# @ws = p.workbook.add_worksheet +# @chart = @ws.drawing.add_chart Axlsx::Line3DChart +# @series = @chart.add_series :data=>[0,1,2] +# end - def test_initialize - assert(@series.data.is_a?Axlsx::SimpleTypedList) - assert_equal(@series.data, [0,1,2]) - end +# def test_initialize +# assert(@series.data.is_a?Axlsx::SimpleTypedList) +# assert_equal(@series.data, [0,1,2]) +# end - def test_to_xml_string - doc = Nokogiri::XML(@chart.to_xml_string) - assert_equal(doc.xpath("//c:val/c:numRef/c:f").size,1) - assert_equal(doc.xpath("//c:numCache/c:ptCount[@val='#{@series.data.size}']").size,1) - @series.data.each_with_index do |datum, index| - assert_equal(doc.xpath("//c:numCache/c:pt[@idx='#{index}']").size,1) - assert_equal(doc.xpath("//c:numCache/c:pt/c:v[text()='#{datum}']").size,1) - end - end +# def test_to_xml_string +# doc = Nokogiri::XML(@chart.to_xml_string) +# assert_equal(doc.xpath("//c:val/c:numRef/c:f").size,1) +# assert_equal(doc.xpath("//c:numCache/c:ptCount[@val='#{@series.data.size}']").size,1) +# @series.data.each_with_index do |datum, index| +# assert_equal(doc.xpath("//c:numCache/c:pt[@idx='#{index}']").size,1) +# assert_equal(doc.xpath("//c:numCache/c:pt/c:v[text()='#{datum}']").size,1) +# end +# end -end +# end diff --git a/test/drawing/tc_num_data.rb b/test/drawing/tc_num_data.rb new file mode 100644 index 00000000..85bf5614 --- /dev/null +++ b/test/drawing/tc_num_data.rb @@ -0,0 +1,27 @@ +require 'tc_helper.rb' + +class TestNumData < Test::Unit::TestCase + + def setup + @num_data = Axlsx::NumData.new :data => [1, 2, 3] + end + + def test_initialize + assert_equal(@num_data.format_code, "General") + end + + def test_format_code + assert_raise(ArgumentError) {@num_data.format_code = 7} + assert_nothing_raised {@num_data.format_code = 'foo_bar'} + end + + def test_to_xml_string + str = '<?xml version="1.0" encoding="UTF-8"?>' + str << '<c:chartSpace xmlns:c="' << Axlsx::XML_NS_C << '">' + str << @num_data.to_xml_string + doc = Nokogiri::XML(str) + assert_equal(doc.xpath("//c:numLit/c:ptCount[@val=3]").size, 1) + assert_equal(doc.xpath("//c:numLit/c:pt/c:v[text()='1']").size, 1) + end + +end diff --git a/test/drawing/tc_num_val.rb b/test/drawing/tc_num_val.rb new file mode 100644 index 00000000..ef27deb9 --- /dev/null +++ b/test/drawing/tc_num_val.rb @@ -0,0 +1,29 @@ +require 'tc_helper.rb' + +class TestNumVal < Test::Unit::TestCase + + def setup + @num_val = Axlsx::NumVal.new :v => 1 + end + + def test_initialize + assert_equal(@num_val.format_code, "General") + assert_equal(@num_val.v, "1") + end + + def test_format_code + assert_raise(ArgumentError) {@num_val.format_code = 7} + assert_nothing_raised {@num_val.format_code = 'foo_bar'} + end + + def test_to_xml_string + str = '<?xml version="1.0" encoding="UTF-8"?>' + str << '<c:chartSpace xmlns:c="' << Axlsx::XML_NS_C << '">' + str << @num_val.to_xml_string(0) + doc = Nokogiri::XML(str) + # lets see if this works? + assert_equal(doc.xpath("//c:pt/c:v[text()='1']").size, 1) + + end + +end diff --git a/test/drawing/tc_pie_series.rb b/test/drawing/tc_pie_series.rb index 66c8da81..d9853850 100644 --- a/test/drawing/tc_pie_series.rb +++ b/test/drawing/tc_pie_series.rb @@ -11,24 +11,17 @@ class TestPieSeries < Test::Unit::TestCase def test_initialize assert_equal(@series.title.text, "bob", "series title has been applied") - assert_equal(@series.data, [0,1,2], "data option applied") - assert_equal(@series.labels, ["zero", "one","two"], "labels option applied") + assert_equal(@series.labels.class, Axlsx::AxDataSource) + assert_equal(@series.data.class, Axlsx::NumDataSource) assert_equal(@series.explosion, nil, "series shape has been applied") end - def test_data - assert_equal(@series.data, [0,1,2]) - end - - def test_labels - assert_equal(@series.labels, ["zero", "one", "two"]) - - end - def test_explosion assert_raise(ArgumentError, "require valid explosion") { @series.explosion = :lots } assert_nothing_raised("allow valid explosion") { @series.explosion = 20 } assert(@series.explosion == 20) end + #TODO test unique serialization parts + end diff --git a/test/drawing/tc_scatter_series.rb b/test/drawing/tc_scatter_series.rb index f84d4697..e97abca7 100644 --- a/test/drawing/tc_scatter_series.rb +++ b/test/drawing/tc_scatter_series.rb @@ -13,15 +13,9 @@ class TestScatterSeries < Test::Unit::TestCase assert_equal(@series.title.text, "exponents", "series title has been applied") end - def test_data - assert_equal(@series.xData, [1,2,4]) - assert_equal(@series.yData, [1,3,9]) - end - def test_to_xml_string doc = Nokogiri::XML(@chart.to_xml_string) assert_equal(doc.xpath("//a:srgbClr[@val='#{@series.color}']").size,4) - end end diff --git a/test/drawing/tc_str_data.rb b/test/drawing/tc_str_data.rb new file mode 100644 index 00000000..a116965b --- /dev/null +++ b/test/drawing/tc_str_data.rb @@ -0,0 +1,18 @@ +require 'tc_helper.rb' + +class TestStrData < Test::Unit::TestCase + + def setup + @str_data = Axlsx::StrData.new :data => ["1", "2", "3"] + end + + def test_to_xml_string_strLit + str = '<?xml version="1.0" encoding="UTF-8"?>' + str << '<c:chartSpace xmlns:c="' << Axlsx::XML_NS_C << '">' + str << @str_data.to_xml_string + doc = Nokogiri::XML(str) + assert_equal(doc.xpath("//c:strLit/c:ptCount[@val=3]").size, 1) + assert_equal(doc.xpath("//c:strLit/c:pt/c:v[text()='1']").size, 1) + end + +end diff --git a/test/drawing/tc_str_val.rb b/test/drawing/tc_str_val.rb new file mode 100644 index 00000000..0e4ca835 --- /dev/null +++ b/test/drawing/tc_str_val.rb @@ -0,0 +1,21 @@ +require 'tc_helper.rb' + +class TestStrVal < Test::Unit::TestCase + + def setup + @str_val = Axlsx::StrVal.new :v => "1" + end + + def test_initialize + assert_equal(@str_val.v, "1") + end + + def test_to_xml_string + str = '<?xml version="1.0" encoding="UTF-8"?>' + str << '<c:chartSpace xmlns:c="' << Axlsx::XML_NS_C << '">' + str << @str_val.to_xml_string(0) + doc = Nokogiri::XML(str) + assert_equal(doc.xpath("//c:pt/c:v[text()='1']").size, 1) + end + +end |
