summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRandy Morgan (@morgan_randy) <[email protected]>2018-02-06 20:44:05 +0900
committerGitHub <[email protected]>2018-02-06 20:44:05 +0900
commita5a3e049a77a5553c2fba4fae809e3e242c51dbb (patch)
treeade949bac4a1ff93be45ca021a4cd6779af89ed9
parent96b68c64a7b63c95dcd22c01772c8e834656ad9d (diff)
parentef396a558b35644c79b74e009acb44b52db36ed1 (diff)
downloadcaxlsx-a5a3e049a77a5553c2fba4fae809e3e242c51dbb.tar.gz
caxlsx-a5a3e049a77a5553c2fba4fae809e3e242c51dbb.zip
Merge pull request #563 from MedText/master
Add support for Area Charts and 2D Bar Charts
-rw-r--r--lib/axlsx/drawing/area_chart.rb99
-rw-r--r--lib/axlsx/drawing/area_series.rb110
-rw-r--r--lib/axlsx/drawing/bar_chart.rb143
-rw-r--r--lib/axlsx/drawing/drawing.rb3
-rw-r--r--test/drawing/tc_area_chart.rb39
-rw-r--r--test/drawing/tc_area_series.rb71
-rw-r--r--test/drawing/tc_bar_chart.rb71
7 files changed, 536 insertions, 0 deletions
diff --git a/lib/axlsx/drawing/area_chart.rb b/lib/axlsx/drawing/area_chart.rb
new file mode 100644
index 00000000..2002840c
--- /dev/null
+++ b/lib/axlsx/drawing/area_chart.rb
@@ -0,0 +1,99 @@
+# encoding: UTF-8
+module Axlsx
+
+ # The AreaChart 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::AreaChart, :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 AreaChart < Chart
+
+ # the category axis
+ # @return [CatAxis]
+ def cat_axis
+ axes[:cat_axis]
+ end
+ alias :catAxis :cat_axis
+
+ # the category axis
+ # @return [ValAxis]
+ def val_axis
+ axes[:val_axis]
+ end
+ alias :valAxis :val_axis
+
+ # 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
+ super(frame, options)
+ @series_type = AreaSeries
+ @d_lbls = nil
+ end
+
+ # @see grouping
+ def grouping=(v)
+ RestrictionValidator.validate "AreaChart.grouping", [:percentStacked, :standard, :stacked], v
+ @grouping = v
+ end
+
+ # The node name to use in serialization. As AreaChart is used as the
+ # base class for Liine3DChart we need to be sure to serialize the
+ # chart based on the actual class type and not a fixed node name.
+ # @return [String]
+ def node_name
+ path = self.class.to_s
+ if i = path.rindex('::')
+ path = path[(i+2)..-1]
+ end
+ path[0] = path[0].chr.downcase
+ path
+ end
+
+ # Serializes the object
+ # @param [String] str
+ # @return [String]
+ def to_xml_string(str = '')
+ super(str) do
+ str << ("<c:" << node_name << ">")
+ str << ('<c:grouping val="' << grouping.to_s << '"/>')
+ str << ('<c:varyColors val="' << vary_colors.to_s << '"/>')
+ @series.each { |ser| ser.to_xml_string(str) }
+ @d_lbls.to_xml_string(str) if @d_lbls
+ yield if block_given?
+ axes.to_xml_string(str, :ids => true)
+ str << ("</c:" << node_name << ">")
+ axes.to_xml_string(str)
+ end
+ end
+
+ # The axes for this chart. AreaCharts have a category and value
+ # axis.
+ # @return [Axes]
+ def axes
+ @axes ||= Axes.new(:cat_axis => CatAxis, :val_axis => ValAxis)
+ end
+ end
+end
diff --git a/lib/axlsx/drawing/area_series.rb b/lib/axlsx/drawing/area_series.rb
new file mode 100644
index 00000000..c039869e
--- /dev/null
+++ b/lib/axlsx/drawing/area_series.rb
@@ -0,0 +1,110 @@
+# encoding: UTF-8
+module Axlsx
+ # A AreaSeries defines the title, data and labels for line charts
+ # @note The recommended way to manage series is to use Chart#add_series
+ # @see Worksheet#add_chart
+ # @see Chart#add_series
+ class AreaSeries < Series
+
+ # The data for this series.
+ # @return [ValAxisData]
+ attr_reader :data
+
+ # The labels for this series.
+ # @return [CatAxisData]
+ attr_reader :labels
+
+ # The fill color for this series.
+ # Red, green, and blue is expressed as sequence of hex digits, RRGGBB. A perceptual gamma of 2.2 is used.
+ # @return [String]
+ attr_reader :color
+
+ # show markers on values
+ # @return [Boolean]
+ attr_reader :show_marker
+
+ # custom marker symbol
+ # @return [String]
+ attr_reader :marker_symbol
+
+ # line smoothing on values
+ # @return [Boolean]
+ attr_reader :smooth
+
+ # Creates a new series
+ # @option options [Array, SimpleTypedList] data
+ # @option options [Array, SimpleTypedList] labels
+ # @param [Chart] chart
+ def initialize(chart, options={})
+ @show_marker = false
+ @marker_symbol = options[:marker_symbol] ? options[:marker_symbol] : :default
+ @smooth = false
+ @labels, @data = nil, nil
+ super(chart, options)
+ @labels = AxDataSource.new(:data => options[:labels]) unless options[:labels].nil?
+ @data = NumDataSource.new(options) unless options[:data].nil?
+ end
+
+ # @see color
+ def color=(v)
+ @color = v
+ end
+
+ # @see show_marker
+ def show_marker=(v)
+ Axlsx::validate_boolean(v)
+ @show_marker = v
+ end
+
+ # @see marker_symbol
+ def marker_symbol=(v)
+ Axlsx::validate_marker_symbol(v)
+ @marker_symbol = v
+ end
+
+ # @see smooth
+ def smooth=(v)
+ Axlsx::validate_boolean(v)
+ @smooth = v
+ end
+
+ # Serializes the object
+ # @param [String] str
+ # @return [String]
+ def to_xml_string(str = '')
+ super(str) do
+ if color
+ str << '<c:spPr><a:solidFill>'
+ str << ('<a:srgbClr val="' << color << '"/>')
+ str << '</a:solidFill>'
+ str << '<a:ln w="28800">'
+ str << '<a:solidFill>'
+ str << ('<a:srgbClr val="' << color << '"/>')
+ str << '</a:solidFill>'
+ str << '</a:ln>'
+ str << '<a:round/>'
+ str << '</c:spPr>'
+ end
+
+ if !@show_marker
+ str << '<c:marker><c:symbol val="none"/></c:marker>'
+ elsif @marker_symbol != :default
+ str << '<c:marker><c:symbol val="' + @marker_symbol.to_s + '"/></c:marker>'
+ end
+
+ @labels.to_xml_string(str) unless @labels.nil?
+ @data.to_xml_string(str) unless @data.nil?
+ str << ('<c:smooth val="' << ((smooth) ? '1' : '0') << '"/>')
+ end
+ end
+
+ private
+
+ # assigns the data for this series
+ 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", [AxDataSource], v; @labels = v; end
+
+ end
+end
diff --git a/lib/axlsx/drawing/bar_chart.rb b/lib/axlsx/drawing/bar_chart.rb
new file mode 100644
index 00000000..7efd0ec5
--- /dev/null
+++ b/lib/axlsx/drawing/bar_chart.rb
@@ -0,0 +1,143 @@
+# encoding: UTF-8
+module Axlsx
+
+ # The BarChart is a three dimentional barchart (who would have guessed?) that you can add to your worksheet.
+ # @see Worksheet#add_chart
+ # @see Chart#add_series
+ # @see Package#serialize
+ # @see README for an example
+ class BarChart < Chart
+
+ # the category axis
+ # @return [CatAxis]
+ def cat_axis
+ axes[:cat_axis]
+ end
+ alias :catAxis :cat_axis
+
+ # the value axis
+ # @return [ValAxis]
+ def val_axis
+ axes[:val_axis]
+ end
+ alias :valAxis :val_axis
+
+ # The direction of the bars in the chart
+ # must be one of [:bar, :col]
+ # @return [Symbol]
+ def bar_dir
+ @bar_dir ||= :bar
+ end
+ alias :barDir :bar_dir
+
+ # space between bar or column clusters, as a percentage of the bar or column width.
+ # @return [String]
+ attr_reader :gap_depth
+ alias :gapDepth :gap_depth
+
+ # space between bar or column clusters, as a percentage of the bar or column width.
+ # @return [String]
+ def gap_width
+ @gap_width ||= 150
+ end
+ alias :gapWidth :gap_width
+
+ #grouping for a column, line, or area chart.
+ # must be one of [:percentStacked, :clustered, :standard, :stacked]
+ # @return [Symbol]
+ def grouping
+ @grouping ||= :clustered
+ end
+
+ # The shabe of the bars or columns
+ # must be one of [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax]
+ # @return [Symbol]
+ def shape
+ @shape ||= :box
+ end
+
+ # validation regex for gap amount percent
+ GAP_AMOUNT_PERCENT = /0*(([0-9])|([1-9][0-9])|([1-4][0-9][0-9])|500)%/
+
+ # Creates a new bar chart object
+ # @param [GraphicFrame] frame The workbook that owns this chart.
+ # @option options [Cell, String] title
+ # @option options [Boolean] show_legend
+ # @option options [Symbol] bar_dir
+ # @option options [Symbol] grouping
+ # @option options [String] gap_width
+ # @option options [String] gap_depth
+ # @option options [Symbol] shape
+ # @see Chart
+ def initialize(frame, options={})
+ @vary_colors = true
+ @gap_width, @gap_depth, @shape = nil, nil, nil
+ super(frame, options)
+ @series_type = BarSeries
+ @d_lbls = nil
+ end
+
+ # The direction of the bars in the chart
+ # must be one of [:bar, :col]
+ def bar_dir=(v)
+ RestrictionValidator.validate "BarChart.bar_dir", [:bar, :col], v
+ @bar_dir = v
+ end
+ alias :barDir= :bar_dir=
+
+ #grouping for a column, line, or area chart.
+ # must be one of [:percentStacked, :clustered, :standard, :stacked]
+ def grouping=(v)
+ RestrictionValidator.validate "BarChart.grouping", [:percentStacked, :clustered, :standard, :stacked], v
+ @grouping = v
+ end
+
+ # space between bar or column clusters, as a percentage of the bar or column width.
+ def gap_width=(v)
+ RegexValidator.validate "BarChart.gap_width", GAP_AMOUNT_PERCENT, v
+ @gap_width=(v)
+ end
+ alias :gapWidth= :gap_width=
+
+ # space between bar or column clusters, as a percentage of the bar or column width.
+ def gap_depth=(v)
+ RegexValidator.validate "BarChart.gap_didth", GAP_AMOUNT_PERCENT, v
+ @gap_depth=(v)
+ end
+ alias :gapDepth= :gap_depth=
+
+ # The shabe of the bars or columns
+ # must be one of [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax]
+ def shape=(v)
+ RestrictionValidator.validate "BarChart.shape", [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax], v
+ @shape = v
+ end
+
+ # Serializes the object
+ # @param [String] str
+ # @return [String]
+ def to_xml_string(str = '')
+ super(str) do
+ str << '<c:barChart>'
+ str << ('<c:barDir val="' << bar_dir.to_s << '"/>')
+ str << ('<c:grouping val="' << grouping.to_s << '"/>')
+ str << ('<c:varyColors val="' << vary_colors.to_s << '"/>')
+ @series.each { |ser| ser.to_xml_string(str) }
+ @d_lbls.to_xml_string(str) if @d_lbls
+ str << ('<c:gapWidth val="' << @gap_width.to_s << '"/>') unless @gap_width.nil?
+ str << ('<c:gapDepth val="' << @gap_depth.to_s << '"/>') unless @gap_depth.nil?
+ str << ('<c:shape val="' << @shape.to_s << '"/>') unless @shape.nil?
+ axes.to_xml_string(str, :ids => true)
+ str << '</c:barChart>'
+ axes.to_xml_string(str)
+ end
+ end
+
+ # A hash of axes used by this chart. Bar charts have a value and
+ # category axes specified via axes[:val_axes] and axes[:cat_axis]
+ # @return [Axes]
+ def axes
+ @axes ||= Axes.new(:cat_axis => CatAxis, :val_axis => ValAxis)
+ end
+ end
+end
diff --git a/lib/axlsx/drawing/drawing.rb b/lib/axlsx/drawing/drawing.rb
index 2b593606..e9da37a5 100644
--- a/lib/axlsx/drawing/drawing.rb
+++ b/lib/axlsx/drawing/drawing.rb
@@ -9,6 +9,7 @@ module Axlsx
require 'axlsx/drawing/line_series.rb'
require 'axlsx/drawing/scatter_series.rb'
require 'axlsx/drawing/bubble_series.rb'
+ require 'axlsx/drawing/area_series.rb'
require 'axlsx/drawing/scaling.rb'
require 'axlsx/drawing/axis.rb'
@@ -35,10 +36,12 @@ module Axlsx
require 'axlsx/drawing/chart.rb'
require 'axlsx/drawing/pie_3D_chart.rb'
require 'axlsx/drawing/bar_3D_chart.rb'
+ require 'axlsx/drawing/bar_chart.rb'
require 'axlsx/drawing/line_chart.rb'
require 'axlsx/drawing/line_3D_chart.rb'
require 'axlsx/drawing/scatter_chart.rb'
require 'axlsx/drawing/bubble_chart.rb'
+ require 'axlsx/drawing/area_chart.rb'
require 'axlsx/drawing/picture_locking.rb'
require 'axlsx/drawing/pic.rb'
diff --git a/test/drawing/tc_area_chart.rb b/test/drawing/tc_area_chart.rb
new file mode 100644
index 00000000..b3013c12
--- /dev/null
+++ b/test/drawing/tc_area_chart.rb
@@ -0,0 +1,39 @@
+require 'tc_helper.rb'
+
+class TestAreaChart < 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::AreaChart, :title => "fishery"
+ end
+
+ def teardown
+ end
+
+ def test_initialization
+ assert_equal(@chart.grouping, :standard, "grouping defualt incorrect")
+ assert_equal(@chart.series_type, Axlsx::AreaSeries, "series type incorrect")
+ assert(@chart.cat_axis.is_a?(Axlsx::CatAxis), "category axis not created")
+ assert(@chart.val_axis.is_a?(Axlsx::ValAxis), "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
diff --git a/test/drawing/tc_area_series.rb b/test/drawing/tc_area_series.rb
new file mode 100644
index 00000000..00229a39
--- /dev/null
+++ b/test/drawing/tc_area_series.rb
@@ -0,0 +1,71 @@
+require 'tc_helper.rb'
+
+class TestAreaSeries < Test::Unit::TestCase
+
+ def setup
+ p = Axlsx::Package.new
+ @ws = p.workbook.add_worksheet :name=>"hmmm"
+ chart = @ws.add_chart Axlsx::AreaChart, :title => "fishery"
+ @series = chart.add_series(
+ :data => [0,1,2],
+ :labels => ["zero", "one", "two"],
+ :title => "bob",
+ :color => "#FF0000",
+ :show_marker => true,
+ :smooth => true
+ )
+ end
+
+ def test_initialize
+ assert_equal(@series.title.text, "bob", "series title has been applied")
+ assert_equal(@series.labels.class, Axlsx::AxDataSource)
+ assert_equal(@series.data.class, Axlsx::NumDataSource)
+ end
+
+ def test_show_marker
+ assert_equal(true, @series.show_marker)
+ @series.show_marker = false
+ assert_equal(false, @series.show_marker)
+ end
+
+ def test_smooth
+ assert_equal(true, @series.smooth)
+ @series.smooth = false
+ assert_equal(false, @series.smooth)
+ end
+
+ def test_marker_symbol
+ assert_equal(:default, @series.marker_symbol)
+ @series.marker_symbol = :circle
+ assert_equal(:circle, @series.marker_symbol)
+ end
+
+ def test_to_xml_string
+ doc = Nokogiri::XML(wrap_with_namespaces(@series))
+ assert(doc.xpath("//srgbClr[@val='#{@series.color}']"))
+ assert_equal(xpath_with_namespaces(doc, "//c:marker").size, 0)
+ assert(doc.xpath("//smooth"))
+
+ @series.marker_symbol = :diamond
+ doc = Nokogiri::XML(wrap_with_namespaces(@series))
+ assert_equal(xpath_with_namespaces(doc, "//c:marker/c:symbol[@val='diamond']").size, 1)
+
+ @series.show_marker = false
+ doc = Nokogiri::XML(wrap_with_namespaces(@series))
+ assert_equal(xpath_with_namespaces(doc, "//c:marker/c:symbol[@val='none']").size, 1)
+ end
+
+ def wrap_with_namespaces(series)
+ '<c:chartSpace xmlns:c="' <<
+ Axlsx::XML_NS_C <<
+ '" xmlns:a="' <<
+ Axlsx::XML_NS_A <<
+ '">' <<
+ series.to_xml_string <<
+ '</c:chartSpace>'
+ end
+
+ def xpath_with_namespaces(doc, xpath)
+ doc.xpath(xpath, "a" => Axlsx::XML_NS_A, "c" => Axlsx::XML_NS_C)
+ end
+end
diff --git a/test/drawing/tc_bar_chart.rb b/test/drawing/tc_bar_chart.rb
new file mode 100644
index 00000000..ca1ca016
--- /dev/null
+++ b/test/drawing/tc_bar_chart.rb
@@ -0,0 +1,71 @@
+require 'tc_helper.rb'
+
+class TestBarChart < 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::BarChart, :title => "fishery"
+ end
+
+ def teardown
+ end
+
+ def test_initialization
+ assert_equal(@chart.grouping, :clustered, "grouping defualt incorrect")
+ assert_equal(@chart.series_type, Axlsx::BarSeries, "series type incorrect")
+ assert_equal(@chart.bar_dir, :bar, " bar direction incorrect")
+ assert(@chart.cat_axis.is_a?(Axlsx::CatAxis), "category axis not created")
+ assert(@chart.val_axis.is_a?(Axlsx::ValAxis), "value access not created")
+ end
+
+ def test_bar_direction
+ assert_raise(ArgumentError, "require valid bar direction") { @chart.bar_dir = :left }
+ assert_nothing_raised("allow valid bar direction") { @chart.bar_dir = :col }
+ assert(@chart.bar_dir == :col)
+ end
+
+ def test_grouping
+ assert_raise(ArgumentError, "require valid grouping") { @chart.grouping = :inverted }
+ assert_nothing_raised("allow valid grouping") { @chart.grouping = :standard }
+ assert(@chart.grouping == :standard)
+ end
+
+
+ def test_gapWidth
+ assert_raise(ArgumentError, "require valid gap width") { @chart.gap_width = 200 }
+ assert_nothing_raised("allow valid gapWidth") { @chart.gap_width = "200%" }
+ assert(@chart.gap_width == "200%")
+ end
+
+ def test_gapDepth
+ assert_raise(ArgumentError, "require valid gap_depth") { @chart.gap_depth = 200 }
+ assert_nothing_raised("allow valid gap_depth") { @chart.gap_depth = "200%" }
+ assert(@chart.gap_depth == "200%")
+ end
+
+ def test_shape
+ assert_raise(ArgumentError, "require valid shape") { @chart.shape = :star }
+ assert_nothing_raised("allow valid shape") { @chart.shape = :cone }
+ assert(@chart.shape == :cone)
+ end
+
+ def test_to_xml_string
+ 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
+
+ def test_to_xml_string_has_axes_in_correct_order
+ str = @chart.to_xml_string
+ cat_axis_position = str.index(@chart.axes[:cat_axis].id.to_s)
+ val_axis_position = str.index(@chart.axes[:val_axis].id.to_s)
+ assert(cat_axis_position < val_axis_position, "cat_axis must occur earlier than val_axis in the XML")
+ end
+end