diff options
| author | Randy Morgan (@morgan_randy) <[email protected]> | 2013-03-15 19:42:27 -0700 |
|---|---|---|
| committer | Randy Morgan (@morgan_randy) <[email protected]> | 2013-03-15 19:42:27 -0700 |
| commit | 17f135ab215ba05b85ef92a15ed0038ea7556de9 (patch) | |
| tree | bfc0525408d7438355512d010ca38d672210064e | |
| parent | 38e1638fa715398429797276058f2c18e9e21e9b (diff) | |
| parent | 4cabb13e1cff88137d01ae4fae0d6f654725d4e9 (diff) | |
| download | caxlsx-17f135ab215ba05b85ef92a15ed0038ea7556de9.tar.gz caxlsx-17f135ab215ba05b85ef92a15ed0038ea7556de9.zip | |
Merge pull request #178 from Programatica/master
Add LineChart
| -rw-r--r-- | lib/axlsx/drawing/drawing.rb | 1 | ||||
| -rw-r--r-- | lib/axlsx/drawing/line_3D_chart.rb | 47 | ||||
| -rw-r--r-- | lib/axlsx/drawing/line_chart.rb | 87 | ||||
| -rw-r--r-- | lib/axlsx/drawing/line_series.rb | 11 | ||||
| -rw-r--r-- | test/drawing/tc_line_chart.rb | 40 |
5 files changed, 140 insertions, 46 deletions
diff --git a/lib/axlsx/drawing/drawing.rb b/lib/axlsx/drawing/drawing.rb index 46d5a88b..7567dea1 100644 --- a/lib/axlsx/drawing/drawing.rb +++ b/lib/axlsx/drawing/drawing.rb @@ -33,6 +33,7 @@ module Axlsx require 'axlsx/drawing/chart.rb' require 'axlsx/drawing/pie_3D_chart.rb' require 'axlsx/drawing/bar_3D_chart.rb' + require 'axlsx/drawing/line_chart.rb' require 'axlsx/drawing/line_3D_chart.rb' require 'axlsx/drawing/scatter_chart.rb' diff --git a/lib/axlsx/drawing/line_3D_chart.rb b/lib/axlsx/drawing/line_3D_chart.rb index b843f48e..627cfe83 100644 --- a/lib/axlsx/drawing/line_3D_chart.rb +++ b/lib/axlsx/drawing/line_3D_chart.rb @@ -19,29 +19,12 @@ module Axlsx # @see Chart#add_series # @see Series # @see Package#serialize - class Line3DChart < Chart - - # the category axis - # @return [CatAxis] - attr_reader :catAxis - - # the category axis - # @return [ValAxis] - attr_reader :valAxis - - # the category axis - # @return [Axis] - attr_reader :serAxis + class Line3DChart < LineChart # space between bar or column clusters, as a percentage of the bar or column width. # @return [String] attr_reader :gapDepth - #grouping for a column, line, or area chart. - # must be one of [:percentStacked, :clustered, :standard, :stacked] - # @return [Symbol] - attr_reader :grouping - # validation regex for gap amount percent GAP_AMOUNT_PERCENT = /0*(([0-9])|([1-9][0-9])|([1-4][0-9][0-9])|500)%/ @@ -60,25 +43,9 @@ module Axlsx # @see Chart # @see View3D def initialize(frame, options={}) - @vary_colors = false @gapDepth = nil - @grouping = :standard - @catAxId = rand(8 ** 8) - @valAxId = rand(8 ** 8) - @serAxId = rand(8 ** 8) - @catAxis = CatAxis.new(@catAxId, @valAxId) - @valAxis = ValAxis.new(@valAxId, @catAxId) - @serAxis = SerAxis.new(@serAxId, @valAxId) super(frame, options) - @series_type = LineSeries @view_3D = View3D.new({:perspective=>30}.merge(options)) - @d_lbls = nil - end - - # @see grouping - def grouping=(v) - RestrictionValidator.validate "Bar3DChart.grouping", [:percentStacked, :standard, :stacked], v - @grouping = v end # @see gapDepth @@ -92,19 +59,7 @@ module Axlsx # @return [String] def to_xml_string(str = '') super(str) do |str_inner| - str_inner << '<c:line3DChart>' - str_inner << '<c:grouping val="' << grouping.to_s << '"/>' - str_inner << '<c:varyColors val="' << vary_colors.to_s << '"/>' - @series.each { |ser| ser.to_xml_string(str_inner) } - @d_lbls.to_xml_string(str) if @d_lbls str_inner << '<c:gapDepth val="' << @gapDepth.to_s << '"/>' unless @gapDepth.nil? - str_inner << '<c:axId val="' << @catAxId.to_s << '"/>' - str_inner << '<c:axId val="' << @valAxId.to_s << '"/>' - str_inner << '<c:axId val="' << @serAxId.to_s << '"/>' - str_inner << '</c:line3DChart>' - @catAxis.to_xml_string str_inner - @valAxis.to_xml_string str_inner - @serAxis.to_xml_string str_inner end end end diff --git a/lib/axlsx/drawing/line_chart.rb b/lib/axlsx/drawing/line_chart.rb new file mode 100644 index 00000000..7bf925e6 --- /dev/null +++ b/lib/axlsx/drawing/line_chart.rb @@ -0,0 +1,87 @@ +# encoding: UTF-8 +module Axlsx + + # The LineChart is a two dimentional line chart (who would have guessed?) that you can add to your worksheet. + # @example Creating a chart + # # This example creates a line in a single sheet. + # require "rubygems" # if that is your preferred way to manage gems! + # require "axlsx" + # + # p = Axlsx::Package.new + # ws = p.workbook.add_worksheet + # ws.add_row ["This is a chart with no data in the sheet"] + # + # chart = ws.add_chart(Axlsx::LineChart, :start_at=> [0,1], :end_at=>[0,6], :title=>"Most Popular Pets") + # chart.add_series :data => [1, 9, 10], :labels => ["Slimy Reptiles", "Fuzzy Bunnies", "Rottweiler"] + # + # @see Worksheet#add_chart + # @see Worksheet#add_row + # @see Chart#add_series + # @see Series + # @see Package#serialize + class LineChart < Chart + + # the category axis + # @return [CatAxis] + attr_reader :catAxis + + # the category axis + # @return [ValAxis] + attr_reader :valAxis + + # the category axis + # @return [Axis] + attr_reader :serAxis + + # must be one of [:percentStacked, :clustered, :standard, :stacked] + # @return [Symbol] + attr_reader :grouping + + # Creates a new line chart object + # @param [GraphicFrame] frame The workbook that owns this chart. + # @option options [Cell, String] title + # @option options [Boolean] show_legend + # @option options [Symbol] grouping + # @see Chart + def initialize(frame, options={}) + @vary_colors = false + @grouping = :standard + @catAxId = rand(8 ** 8) + @valAxId = rand(8 ** 8) + @serAxId = rand(8 ** 8) + @catAxis = CatAxis.new(@catAxId, @valAxId) + @valAxis = ValAxis.new(@valAxId, @catAxId) + @serAxis = SerAxis.new(@serAxId, @valAxId) + super(frame, options) + @series_type = LineSeries + @d_lbls = nil + end + + # @see grouping + def grouping=(v) + RestrictionValidator.validate "Bar3DChart.grouping", [:percentStacked, :standard, :stacked], v + @grouping = v + end + + # Serializes the object + # @param [String] str + # @return [String] + def to_xml_string(str = '') + super(str) do |str_inner| + str_inner << "<c:#{self.class.name.demodulize.camelcase(:lower)}>" + str_inner << '<c:grouping val="' << grouping.to_s << '"/>' + str_inner << '<c:varyColors val="' << vary_colors.to_s << '"/>' + @series.each { |ser| ser.to_xml_string(str_inner) } + @d_lbls.to_xml_string(str) if @d_lbls + yield str_inner if block_given? + str_inner << '<c:axId val="' << @catAxId.to_s << '"/>' + str_inner << '<c:axId val="' << @valAxId.to_s << '"/>' + str_inner << '<c:axId val="' << @serAxId.to_s << '"/>' + str_inner << "</c:#{self.class.name.demodulize.camelcase(:lower)}>" + @catAxis.to_xml_string str_inner + @valAxis.to_xml_string str_inner + @serAxis.to_xml_string str_inner + end + end + end +end diff --git a/lib/axlsx/drawing/line_series.rb b/lib/axlsx/drawing/line_series.rb index 017822ed..f70bdb49 100644 --- a/lib/axlsx/drawing/line_series.rb +++ b/lib/axlsx/drawing/line_series.rb @@ -19,6 +19,10 @@ module Axlsx # @return [String] attr_reader :color + # show markers on values + # @return [Boolean] + attr_reader :show_marker + # Creates a new series # @option options [Array, SimpleTypedList] data # @option options [Array, SimpleTypedList] labels @@ -35,11 +39,18 @@ module Axlsx @color = v end + # @see show_marker + def show_marker=(v) + Axlsx::validate_boolean(v) + @show_marker = v + end + # Serializes the object # @param [String] str # @return [String] def to_xml_string(str = '') super(str) do + str << '<c:marker><c:symbol val="none"/></c:marker>' unless @show_marker if color str << '<c:spPr><a:solidFill>' str << '<a:srgbClr val="' << color << '"/>' diff --git a/test/drawing/tc_line_chart.rb b/test/drawing/tc_line_chart.rb new file mode 100644 index 00000000..07e23702 --- /dev/null +++ b/test/drawing/tc_line_chart.rb @@ -0,0 +1,40 @@ +require 'tc_helper.rb' + +class TestLineChart < Test::Unit::TestCase + + def setup + @p = Axlsx::Package.new + ws = @p.workbook.add_worksheet + @row = ws.add_row ["one", 1, Time.now] + @chart = ws.add_chart Axlsx::LineChart, :title => "fishery" + end + + def teardown + end + + def test_initialization + assert_equal(@chart.grouping, :standard, "grouping defualt incorrect") + assert_equal(@chart.series_type, Axlsx::LineSeries, "series type incorrect") + assert(@chart.catAxis.is_a?(Axlsx::CatAxis), "category axis not created") + assert(@chart.valAxis.is_a?(Axlsx::ValAxis), "value access not created") + assert(@chart.serAxis.is_a?(Axlsx::SerAxis), "value access not created") + end + + def test_grouping + assert_raise(ArgumentError, "require valid grouping") { @chart.grouping = :inverted } + assert_nothing_raised("allow valid grouping") { @chart.grouping = :stacked } + assert(@chart.grouping == :stacked) + end + + def test_to_xml + schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD)) + doc = Nokogiri::XML(@chart.to_xml_string) + errors = [] + schema.validate(doc).each do |error| + errors.push error + puts error.message + end + assert(errors.empty?, "error free validation") + end + +end |
