Module: Anychart::XML::Pie

Defined in:
lib/pie.rb

Constant Summary collapse

@@default_xml_options =

Options

options = => :doughnut,

  :tooltip => "${%Valueth)( mil)( bil),numDecimals:2",
  :chart   => {
    :title   => "ACME Corp. apparel sales through different retail channels",
    :legend  => true,
    :label   => "%YPercentOfSeriesnumDecimals:1%",
    :marker  => true
  },
  :data    => {
    :series => [
      => "Year 2003",
         :points  => [ {:name  => "Department Store", :value => "6371664",
                        => "Discount Stores", :value => "7216301",
                       => "Men's/Women's Stores", :value => "1486621",
                       => "Juvenile Specialty Stores", :value => "786622",
                       => "All other outlets", :value => "900000", :color => "DarkSeaGreen"
                     ]
       }
      ]
  }
}
{
:type         => :pie, # :pie or :doughnut
:animation    => true,
:tooltip      => "{%Name} - ${%Value}", # "{%Name} - {%YValue}"
:chart        => {
  :title        => false,
  :legend       => false, # if true or false: shows or not; if string shows with that String as legend -> format
  :label        => false, # label_settings -> format: {%YPercentOfSeries}{numDecimals:1}%
  :marker       => true   # true or false
  },
  :data => {
    :series => [
      {:name => "Serie de omissao",
        :points => [{:name => "X1", :value => "1"},{:name => "X2", :value => "2"}]    
      }
    ]
  }
}

Class Method Summary collapse

Class Method Details

.validate_options(options) ⇒ Object



134
135
136
# File 'lib/pie.rb', line 134

def self.validate_options(options)
  raise "type must be :pie or :doughnut" unless options[:type] == :pie or options[:type] == :doughnut
end

.xml(options = {}) ⇒ Object



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

def self.xml(options={})
  p = @@default_xml_options.merge(options)
  
  validate_options(p)
  
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.anychart {
      xml.settings {
        xml.animation(:enabled => "True") if p[:animation]
      }
      
      xml.charts {
          xml.chart(:plot_type => p[:type].to_s.capitalize) {
        
          xml.data_plot_settings(:enable_3d_mode => "false") {
            xml.pie_series {
              if p[:tooltip]
                xml.tooltip_settings(:enabled => "True") {
                  xml.format p[:tooltip]
                  xml.position(:anchor => "CenterTop", :padding => "10") {}
                }
              else
                xml.tooltip_settings(:enabled => "False") {}
              end
              
              # XML Labels que aparecem no interior da Pie/Doughnut
              if p[:chart][:label]
                xml.label_settings(:enabled => "true") {
                  xml.position(:anchor => "Center", :valign => "Center", :halign => "Center", :padding => "0") {}
                  xml.background(:enabled => "false") {}
                  xml.font(:size => "11", :color => "White") {
                    xml.effects {
                      xml.drop_shadow(:enabled => "true", :distance => "1", :opacity => "0.9", :blur_x => "2", :blur_y => "2") {}
                    }
                  }
                  xml.format p[:chart][:label]
                }
              end # label_settings
              
              if p[:chart][:marker]
                xml.marker_settings(:enabled => "true") {
                  xml.marker(:type => "None") {}
                  xml.states {
                    xml.hover {
                      xml.marker(:type => "Circle", :anchor => "CenterTop")
                    }
                  }
                } 
              end # marker_settings
              
            } # pie_series
          
          } # data_plot_settings

          xml.chart_settings {
            Anychart::XML.chart_background(xml,p) # background
            Anychart::XML.chart_title(xml,p) # title
            Anychart::XML.legend(xml,p) # legend    
          } # chart_settings
            
            xml.data {
              series = (p[:data][:series].kind_of?(Array) ? p[:data][:series] : [p[:data][:series]])

              series.each do |serie|
                xml.series(:name => serie[:name], :type => "Pie") {
                  # Os varios pontos
                  serie[:points].each do |point|
                    pt = {:name => point[:name].to_s, :y => point[:value].to_s}
                    pt.merge!({:color => point[:color].to_s}) if point[:color]
                    xml.point(pt)
                  end
                } 
              end # series

            } # data
            
          } # chart
          
      } # charts
  
    } # anychart
  end
  
  builder.to_xml.gsub(/>[ \t\r\n]+</, '><')
end