summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/drawing/bar_chart.rb
blob: 0dcf2c112d56217362eaa86ec83874c65d1d05c9 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# frozen_string_literal: true

module Axlsx
  # The BarChart is a two dimentional barchart 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 [Integer]
    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

    # Overlap between series
    # @return [Integer]
    def overlap
      @overlap ||= 0
    end

    # The shape of the bars or columns
    # must be one of  [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax]
    # @return [Symbol]
    def shape
      @shape ||= :box
    end

    # 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 [Symbol] shape
    # @see Chart
    def initialize(frame, options = {})
      @vary_colors = true
      @gap_width, @overlap, @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)
      RangeValidator.validate "BarChart.gap_width", 0, 500, v
      @gap_width = v
    end
    alias :gapWidth= :gap_width=

    def overlap=(v)
      RangeValidator.validate "BarChart.overlap", -100, 100, v
      @overlap = v
    end

    # The shape 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:overlap val="' << @overlap.to_s << '"/>' unless @overlap.nil?
        str << '<c:gapWidth val="' << @gap_width.to_s << '"/>' unless @gap_width.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