summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/drawing/axis.rb
blob: a8bd3afa4f6c5d5f39868cc637f077f903a91eee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
module Axlsx
 # the access class defines common properties and values for a chart axis.
  class Axis

    # the id of the axis. 
    # @return [Integer]
    attr_reader :axId

    # The perpendicular axis
    # @return [Integer]
    attr_reader :crossAx

    # The scaling of the axis
    # @see Scaling
    # @return [Scaling]
    attr_reader :scaling
    
    # The position of the axis
    # must be one of [:l, :r, :t, :b]
    # @return [Symbol]
    attr_reader :axPos

    # the position of the tick labels
    # must be one of [:nextTo, :high, :low]
    # @return [Symbol]
    attr_reader :tickLblPos

    # The number format format code for this axis
    # default :General
    # @return [String]
    attr_reader :format_code

    # specifies how the perpendicular axis is crossed
    # must be one of [:autoZero, :min, :max]
    # @return [Symbol]
    attr_reader :crosses 

    # Creates an Axis object
    # @param [Integer] axId the id of this axis
    # @param [Integer] crossAx the id of the perpendicular axis
    # @option options [Symbol] axPos
    # @option options [Symbol] crosses
    # @option options [Symbol] tickLblPos
    # @raise [ArgumentError] If axId or crossAx are not unsigned integers
    def initialize(axId, crossAx, options={})
      Axlsx::validate_unsigned_int(axId)
      Axlsx::validate_unsigned_int(crossAx)
      @axId = axId
      @crossAx = crossAx
      @format_code = "General"
      @scaling = Scaling.new(:orientation=>:minMax)      
      self.axPos = :b
      self.tickLblPos = :nextTo
      self.format_code = "General"
      self.crosses = :autoZero
      options.each do |o|
        self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
      end
    end
    # The position of the axis
    # must be one of [:l, :r, :t, :b]
    def axPos=(v) RestrictionValidator.validate "#{self.class}.axPos", [:l, :r, :b, :t], v; @axPos = v; end

    # the position of the tick labels
    # must be one of [:nextTo, :high, :low1]
    def tickLblPos=(v) RestrictionValidator.validate "#{self.class}.tickLblPos", [:nextTo, :high, :low], v; @tickLblPos = v; end

    # The number format format code for this axis
    # default :General
    def format_code=(v) Axlsx::validate_string(v); @format_code = v; end

    # specifies how the perpendicular axis is crossed
    # must be one of [:autoZero, :min, :max]
    def crosses=(v) RestrictionValidator.validate "#{self.class}.crosses", [:autoZero, :min, :max], v; @crosses = v; end

    # Serializes the common axis
    # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
    # @return [String]
    def to_xml(xml)
      xml.axId :val=>@axId
      @scaling.to_xml(xml)
      xml.delete :val=>0
      xml.axPos :val=>@axPos
      xml.majorGridlines
      xml.numFmt :formatCode => @format_code, :sourceLinked=>"1"
      xml.majorTickMark :val=>"none"
      xml.minorTickMark :val=>"none"
      xml.tickLblPos :val=>@tickLblPos
      xml.crossAx :val=>@crossAx
      xml.crosses :val=>@crosses
    end    
  end
end