Class: ColumnChartMultiSeriesWithSimpleLegend

Inherits:
Object
  • Object
show all
Defined in:
lib/column_chart_multi_series_with_simple_legend.rb

Constant Summary collapse

@@default_xml_options =

USAGE: Options :animation => true by default ; Sets if chart has animation on load :tooltip => false by default ; Set the tooltip to use on chart element hover :title => false by default ; Set the title to show in the chart :legend => Sets various legend options to set Possible Options :title => Set legend’s title :format => Set legend format :series => Array of Hashes of series that will display the points on the screen :series => [ { :name => “Serie um e dois”,

    :points => [ { :name => "Nome Ponto Um",   :y => 55 },
                 { :name => "Nome Ponto Dois", :y => 66 } 
               ] 
  } 
]

@@default_xml_options =

  :animation    => true,
  :tooltip      => false, # "{%Name - %YValue"
  :title        => false,
  :legend       => false,
  :series       => [],
  :y_axis       => {
    :format => "%ValuenumDecimals:0"
  }
}
{
  :animation    => true,
  :tooltip      => false, # "{%Name} - {%YValue}"
  :title        => false,
  :legend       => false,
  :series       => [],
  :y_axis       => {
    :format => "{%Value}{numDecimals:0}"
  }
}

Class Method Summary collapse

Class Method Details

.xml(options = {}) ⇒ Object



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
# File 'lib/column_chart_multi_series_with_simple_legend.rb', line 38

def self.xml(options={})
  require 'nokogiri'
  
  p = @@default_xml_options.merge(options)
  
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.anychart {
      xml.settings {
       if p[:animation]
          xml.animation(:enabled => "True")
       else
          xml.animation(:enabled => "False")
       end
      }
      xml.charts {
        xml.chart(:plot_type => "CategorizedVertical") {
          xml.data_plot_settings(:default_series_type => "Bar") {
            xml.bar_series(:point_padding => "0.2", :group_padding => "1") {
              if p[:tooltip]
                xml.tooltip_settings(:enabled => "True") {
                  xml.format p[:tooltip]
                }
              else
                xml.tooltip_settings(:enabled => "False") {}
              end
            }
          }
          
          xml.chart_settings {
            if p[:title]
              xml.title(:enabled => "True") {
                xml.text p[:title]
              }
            end
            if p[:legend]
            xml.legend(:enabled => "True", :position => "Right", :align => "Near") {
              raise ":legend must have a :format option that sets the format that will be displayed for each element" unless p[:legend][:format]
              xml.format p[:legend][:format]
              xml.template {}
              if p[:legend][:title]
                xml.title(:enabled => "True") {
                  xml.text p[:legend][:title]
                }
              else
                xml.title(:enabled => "False") {}
              end
              xml.columns_separator(:enabled => "False") {}
              xml.background {
                xml.inside_margin(:left => "10", :right => "10") {}
              }
            }
            end
            xml.axes {
              xml.y_axis {
                xml.labels {
                  xml.format p[:y_axis][:format]
                }
              }
            }
          }
          
          xml.data {
            series = (p[:series].kind_of?(Array) ? p[:series] : [p[:series]])
            raise "Series incorrectas" if series.empty?
            
            if series.count == 1
              serie = series.first
              xml.series(:name => serie[:name]) {
                serie[:points].each do |point|
                  xml.point(:name => point[:name].to_s, :y => point[:y].to_s)
                end
              }
              else  
              series.each do |serie|
                xml.series(:name => serie[:name]) {
                  # Os varios pontos
                  serie[:points].each do |point|
                    xml.point(:name => point[:name].to_s, :y => point[:y].to_s)
                  end
                }
              end
            end
          }
        }
      }
    }
  end
  builder.to_xml
end