From d7fa44f2bf454c4077a1fb985a777f2ec32bd695 Mon Sep 17 00:00:00 2001 From: Sergio Cambra Date: Thu, 14 Mar 2013 14:04:17 +0100 Subject: add lineChart (2D) and show_marker to LineSeries (for 2D line chart) --- lib/axlsx/drawing/line_3D_chart.rb | 47 +------------------- lib/axlsx/drawing/line_chart.rb | 87 ++++++++++++++++++++++++++++++++++++++ lib/axlsx/drawing/line_series.rb | 11 +++++ 3 files changed, 99 insertions(+), 46 deletions(-) create mode 100644 lib/axlsx/drawing/line_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 << '' - str_inner << '' - str_inner << '' - @series.each { |ser| ser.to_xml_string(str_inner) } - @d_lbls.to_xml_string(str) if @d_lbls str_inner << '' unless @gapDepth.nil? - str_inner << '' - str_inner << '' - str_inner << '' - str_inner << '' - @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..e926fb79 --- /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 << "" + str_inner << '' + str_inner << '' + @series.each { |ser| ser.to_xml_string(str_inner) } + @d_lbls.to_xml_string(str) if @d_lbls + yield str_inner + str_inner << '' + str_inner << '' + str_inner << '' + str_inner << "" + @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 << '' unless @show_marker if color str << '' str << '' -- cgit v1.2.3 From 9debcd9ded081c7eefa6c63515f91e74842ab7ea Mon Sep 17 00:00:00 2001 From: Sergio Cambra Date: Thu, 14 Mar 2013 14:09:01 +0100 Subject: fix loading LineChart --- lib/axlsx/drawing/drawing.rb | 1 + 1 file changed, 1 insertion(+) 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' -- cgit v1.2.3 From 846d43dd87747dfef24f05cc4d0ac483714bf3cc Mon Sep 17 00:00:00 2001 From: Sergio Cambra Date: Thu, 14 Mar 2013 14:11:21 +0100 Subject: only yield if block given --- lib/axlsx/drawing/line_chart.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/axlsx/drawing/line_chart.rb b/lib/axlsx/drawing/line_chart.rb index e926fb79..645565a8 100644 --- a/lib/axlsx/drawing/line_chart.rb +++ b/lib/axlsx/drawing/line_chart.rb @@ -73,7 +73,7 @@ module Axlsx str_inner << '' @series.each { |ser| ser.to_xml_string(str_inner) } @d_lbls.to_xml_string(str) if @d_lbls - yield str_inner + yield str_inner if block_given? str_inner << '' str_inner << '' str_inner << '' -- cgit v1.2.3 From 3b4c24194be05db4810c553fe7ee3eca31013993 Mon Sep 17 00:00:00 2001 From: Sergio Cambra Date: Thu, 14 Mar 2013 14:21:06 +0100 Subject: fix line chart name --- lib/axlsx/drawing/line_chart.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/axlsx/drawing/line_chart.rb b/lib/axlsx/drawing/line_chart.rb index 645565a8..7bf925e6 100644 --- a/lib/axlsx/drawing/line_chart.rb +++ b/lib/axlsx/drawing/line_chart.rb @@ -68,7 +68,7 @@ module Axlsx # @return [String] def to_xml_string(str = '') super(str) do |str_inner| - str_inner << "" + str_inner << "" str_inner << '' str_inner << '' @series.each { |ser| ser.to_xml_string(str_inner) } @@ -77,7 +77,7 @@ module Axlsx str_inner << '' str_inner << '' str_inner << '' - str_inner << "" + str_inner << "" @catAxis.to_xml_string str_inner @valAxis.to_xml_string str_inner @serAxis.to_xml_string str_inner -- cgit v1.2.3 From 4cabb13e1cff88137d01ae4fae0d6f654725d4e9 Mon Sep 17 00:00:00 2001 From: Sergio Cambra Date: Fri, 15 Mar 2013 17:57:20 +0100 Subject: add test and remove activesupport methods --- test/drawing/tc_line_chart.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 test/drawing/tc_line_chart.rb 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 -- cgit v1.2.3