Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove error message on MS Excel 2010 #263

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
96 changes: 96 additions & 0 deletions lib/axlsx/drawing/bar_chart.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
module Axlsx

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

#grouping for a column, line, or area chart.
# must be one of [:percentStacked, :clustered, :standard, :stacked]
# @return [Symbol]
def grouping
@grouping ||= :clustered
end

# 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

# validation regex for gap amount percent
GAP_AMOUNT_PERCENT = /0*(([0-9])|([1-9][0-9])|([1-4][0-9][0-9])|500)%/

def initialize(frame, options={})
@gap_width = nil
super(frame, options)
@series_type = BarSeries
@d_lbls = nil
@vary_colors = true
end

def to_xml_string(str = '')
super(str) do |str_inner|
str_inner << '<c:barChart>'
str_inner << '<c:barDir val="' << bar_dir.to_s << '"/>'
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_inner) if @d_lbls
str_inner << '<c:gapWidth val="' << @gap_width.to_s << '"/>' unless @gap_width.nil?
axes.to_xml_string(str_inner, :ids => true)
str_inner << '</c:barChart>'
axes.to_xml_string(str_inner)
end
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=

# 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
1 change: 1 addition & 0 deletions lib/axlsx/drawing/bar_series.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def shape=(v)
# @return [String]
def to_xml_string(str = '')
super(str) do |str_inner|
@legend_color = @colors.first

colors.each_with_index do |c, index|
str_inner << '<c:dPt>'
Expand Down
59 changes: 45 additions & 14 deletions lib/axlsx/drawing/chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ class Chart
# @option options [Array|String|Cell] start_at The X, Y coordinates defining the top left corner of the chart.
# @option options [Array|String|Cell] end_at The X, Y coordinates defining the bottom right corner of the chart.
def initialize(frame, options={})
@style = 18
@view_3D = nil
@style = 18
@view_3D = nil
@graphic_frame=frame
@graphic_frame.anchor.drawing.worksheet.workbook.charts << self
@series = SimpleTypedList.new Series
@show_legend = true
@series = SimpleTypedList.new Series
@show_legend = true
@display_blanks_as = :gap
@series_type = Series
@title = Title.new
@series_type = Series
@title = Title.new
@bg_color = nil
parse_options options
start_at(*options[:start_at]) if options[:start_at]
end_at(*options[:end_at]) if options[:end_at]
Expand Down Expand Up @@ -53,10 +54,16 @@ def d_lbls
# Indicates that colors should be varied by datum
# @return [Boolean]
attr_reader :vary_colors

# Configures the vary_colors options for this chart
# @param [Boolean] v The value to set
def vary_colors=(v) Axlsx::validate_boolean(v); @vary_colors = v; end
def vary_colors=(v)
Axlsx::validate_boolean(v); @vary_colors = v;
end

# the background color for chart
# @return [String]
attr_reader :bg_color

# The title object for the chart.
# @return [Title]
Expand All @@ -71,6 +78,7 @@ def vary_colors=(v) Axlsx::validate_boolean(v); @vary_colors = v; end
# @return [Boolean]
attr_reader :show_legend


# How to display blank values
# Options are
# * gap: Display nothing
Expand Down Expand Up @@ -98,6 +106,11 @@ def pn
"#{CHART_PN % (index+1)}"
end

# @see color
def bg_color=(v)
@bg_color = v
end

# The title object for the chart.
# @param [String, Cell] v
# @return [Title]
Expand All @@ -113,18 +126,24 @@ def title=(v)
# Show the legend in the chart
# @param [Boolean] v
# @return [Boolean]
def show_legend=(v) Axlsx::validate_boolean(v); @show_legend = v; end
def show_legend=(v)
Axlsx::validate_boolean(v); @show_legend = v;
end

# How to display blank values
# @see display_blanks_as
# @param [Symbol] v
# @return [Symbol]
def display_blanks_as=(v) Axlsx::validate_display_blanks_as(v); @display_blanks_as = v; end
def display_blanks_as=(v)
Axlsx::validate_display_blanks_as(v); @display_blanks_as = v;
end

# The style for the chart.
# see ECMA Part 1 §21.2.2.196
# @param [Integer] v must be between 1 and 48
def style=(v) DataTypeValidator.validate "Chart.style", Integer, v, lambda { |arg| arg >= 1 && arg <= 48 }; @style = v; end
def style=(v)
DataTypeValidator.validate "Chart.style", Integer, v, lambda { |arg| arg >= 1 && arg <= 48 }; @style = v;
end

# backwards compatibility to allow chart.to and chart.from access to anchor markers
# @note This will be disconinued in version 2.0.0. Please use the end_at method
Expand All @@ -146,6 +165,7 @@ def add_series(options={})
@series.last
end


# Serializes the object
# @param [String] str
# @return [String]
Expand Down Expand Up @@ -176,10 +196,18 @@ def to_xml_string(str = '')
str << '<c:dispBlanksAs val="' << display_blanks_as.to_s << '"/>'
str << '<c:showDLblsOverMax val="1"/>'
str << '</c:chart>'
# chart background
if @bg_color
str << '<c:spPr>'
str << '<a:solidFill>'
str << '<a:srgbClr val="' << @bg_color << '"/>'
str << '</a:solidFill>'
str << '</c:spPr>'
end
str << '<c:printSettings>'
str << '<c:headerFooter/>'
# str << '<c:headerFooter/>'
str << '<c:pageMargins b="1.0" l="0.75" r="0.75" t="1.0" header="0.5" footer="0.5"/>'
str << '<c:pageSetup/>'
# str << '<c:pageSetup/>'
str << '</c:printSettings>'
str << '</c:chartSpace>'
end
Expand Down Expand Up @@ -224,7 +252,10 @@ def end_at(x=10, y=10)
end

# sets the view_3D object for the chart
def view_3D=(v) DataTypeValidator.validate "#{self.class}.view_3D", View3D, v; @view_3D = v; end
def view_3D=(v)
DataTypeValidator.validate "#{self.class}.view_3D", View3D, v; @view_3D = v;
end

alias :view3D= :view_3D=

end
Expand Down
3 changes: 2 additions & 1 deletion lib/axlsx/drawing/drawing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module Axlsx
require 'axlsx/drawing/view_3D.rb'
require 'axlsx/drawing/chart.rb'
require 'axlsx/drawing/pie_3D_chart.rb'
require 'axlsx/drawing/bar_chart.rb'
require 'axlsx/drawing/bar_3D_chart.rb'
require 'axlsx/drawing/line_chart.rb'
require 'axlsx/drawing/line_3D_chart.rb'
Expand Down Expand Up @@ -154,7 +155,7 @@ def relationships
# @param [String] str
# @return [String]
def to_xml_string(str = '')
str << '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
str << '<?xml version="1.0" encoding="UTF-8"?>'
str << '<xdr:wsDr xmlns:xdr="' << XML_NS_XDR << '" xmlns:a="' << XML_NS_A << '">'
anchors.each { |anchor| anchor.to_xml_string(str) }
str << '</xdr:wsDr>'
Expand Down
2 changes: 1 addition & 1 deletion lib/axlsx/drawing/line_series.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def to_xml_string(str = '')
str << '<a:srgbClr val="' << color << '"/>'
str << '</a:solidFill>'
str << '</a:ln>'
str << '<a:round/>'
# str << '<a:round/>'
str << '</c:spPr>'
end
str << '<c:marker><c:symbol val="none"/></c:marker>' unless @show_marker
Expand Down
65 changes: 62 additions & 3 deletions lib/axlsx/drawing/series.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@ class Series
# @return [SeriesTitle]
attr_reader :title

# The String of rgb color to apply to chart legend
# @return [String]
attr_reader :legend_color

#The Hash of trendline options
# @return [Hash]
attr_reader :trendline

# Creates a new series
# @param [Chart] chart
# @option options [Integer] order
# @option options [String] title
def initialize(chart, options={})
@order = nil
@order = nil
self.chart = chart
@chart.series << self
parse_options options
Expand All @@ -40,7 +48,9 @@ def order
end

# @see order
def order=(v) Axlsx::validate_unsigned_int(v); @order = v; end
def order=(v)
Axlsx::validate_unsigned_int(v); @order = v;
end

# @see title
def title=(v)
Expand All @@ -49,10 +59,36 @@ def title=(v)
@title = v
end

# @see trendline
def trendline=(v)
@trendline = v
end

# set trendline_color
def trendline_color
if @trendline[:color]
@trendline[:color]
else
'00000'
end
end

#set trenline_weight
def trendline_weight
if @trendline[:weight]
@trendline[:weight].to_s
else
'1'
end

end

private

# assigns the chart for this series
def chart=(v) DataTypeValidator.validate "Series.chart", Chart, v; @chart = v; end
def chart=(v)
DataTypeValidator.validate "Series.chart", Chart, v; @chart = v;
end

# Serializes the object
# @param [String] str
Expand All @@ -63,6 +99,29 @@ def to_xml_string(str = '')
str << '<c:order val="' << (order || index).to_s << '"/>'
title.to_xml_string(str) unless title.nil?
yield str if block_given?
#set color at legend
if @legend_color
str << '<c:spPr>'
str << '<a:solidFill>'
str << '<a:srgbClr val="'<< legend_color.to_s << '"/>'
str << '</a:solidFill>'
str << '</c:spPr>'
end

if @trendline
str << '<c:trendline>'
str << '<c:spPr>'
str << '<a:ln w="' << trendline_weight << '">'
str << '<a:solidFill>'
str << '<a:srgbClr val="' << trendline_color << '"/>'
str << '</a:solidFill>'
str << '</a:ln>'
str << '</c:spPr>'
str << '<c:trendlineType val="linear"/>'
str << '<c:dispRSqr val="0"/>'
str << '<c:dispEq val="0"/>'
str << '</c:trendline>'
end
str << '</c:ser>'
end
end
Expand Down
5 changes: 2 additions & 3 deletions lib/axlsx/drawing/title.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def cell=(v)
# @param [String] str
# @return [String]
def to_xml_string(str = '')
str << '<c:title>'
unless @text.empty?
str << '<c:title>'
str << '<c:tx>'
if @cell.is_a?(Cell)
str << '<c:strRef>'
Expand All @@ -68,11 +68,10 @@ def to_xml_string(str = '')
str << '</c:rich>'
end
str << '</c:tx>'
end
str << '<c:layout/>'
str << '<c:overlay val="0"/>'
str << '</c:title>'
end
end

end
end
Loading