anychart_helpers 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{anychart_helpers}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["jduarte"]
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "anychart_helpers.gemspec",
27
+ "lib/anychart_base_xml.rb",
27
28
  "lib/anychart_helpers.rb",
28
29
  "lib/column_chart_multi_series_with_simple_legend.rb",
29
30
  "test/helper.rb",
@@ -0,0 +1,167 @@
1
+ class AnychartBase
2
+ # USAGE: Options
3
+ # :animation => true by default ; Sets if chart has animation on load
4
+ # :tooltip => false by default ; Set the tooltip to use on chart element hover
5
+ # :title => false by default ; Set the title to show in the chart
6
+ # :legend => Sets various legend options to set
7
+ ## Possible Options
8
+ ### :title => Set legend's title
9
+ ### :format => Set legend format
10
+ # :series => Array of Hashes of series that will display the points on the screen
11
+ ## :series => [ { :name => "Serie um e dois",
12
+ ## :points => [ { :name => "Nome Ponto Um", :y => 55 },
13
+ ## { :name => "Nome Ponto Dois", :y => 66 }
14
+ ## ]
15
+ ## }
16
+ ## ]
17
+
18
+ @@default_xml_options = {
19
+ :settings => {
20
+ :animation => true
21
+ },
22
+ :chart => {
23
+ # :plot_type => "CategorizedVertical"
24
+ }
25
+ :data_plot_settings => {
26
+
27
+ },
28
+ :data_plot_settings_series => {
29
+ # :bar_series => {
30
+ # :attrs => {
31
+ # :group_padding => 1,
32
+ # :point_padding => 0.2,
33
+ # }
34
+ # :children => {
35
+ # :tooltip_settings => {
36
+ # :format => "{%Name} - {%YValue}"
37
+ # }
38
+ # }
39
+ # }
40
+ },
41
+ :chart_settings => {
42
+ :title => "",
43
+ :legend => {
44
+ :enabled => false,
45
+ :position => "Right",
46
+ :align => "Near",
47
+ :format => "{%Icon} {%Name} ({%Value})",
48
+ :background => {
49
+ :inside_margin => {
50
+ :left => 10,
51
+ :right => 10
52
+ }
53
+ }
54
+ }
55
+ },
56
+ :axes => {
57
+ :y_axis => {
58
+ :enabled => true,
59
+ :format => "{%Value}{numDecimals:0}"
60
+ },
61
+ :x_axis => {
62
+ :enabled => false,
63
+ :format => "{%Value}{numDecimals:0}"
64
+ }
65
+ },
66
+ :series => []
67
+ }
68
+
69
+ def self.xml(options={})
70
+ require 'nokogiri'
71
+ p = @@default_xml_options.merge(options)
72
+
73
+ animation = p[:settings][:animation]
74
+ chart_plot_type = self.mandatory( p[:chart][:plot_type] , ":chart => :plot_type")
75
+ data_plot_settings_default_series_type = self.mandatory( p[:data_plot_settings][:default_series_type], "[:data_plot_settings][:default_series_type]" )
76
+ aux = self.mandatory( p[:data_plot_settings_series])
77
+ data_plot_settings_series_name , data_plot_settings_series = aux.keys.first.to_s , aux[aux.keys.first]
78
+
79
+
80
+ builder = Nokogiri::XML::Builder.new do |xml|
81
+ xml.anychart {
82
+ xml.settings {
83
+ xml.animation(:enabled => "True") if animation
84
+ }
85
+ xml.charts {
86
+ xml.chart(:plot_type => chart_plot_type) {
87
+ xml.data_plot_settings(:default_series_type => "Bar") {
88
+ xml.bar_series(:point_padding => "0.2", :group_padding => "1") {
89
+ if p[:tooltip]
90
+ xml.tooltip_settings(:enabled => "True") {
91
+ xml.format p[:tooltip]
92
+ }
93
+ else
94
+ xml.tooltip_settings(:enabled => "False") {}
95
+ end
96
+ }
97
+ }
98
+
99
+ xml.chart_settings {
100
+ if p[:title]
101
+ xml.title(:enabled => "True") {
102
+ xml.text p[:title]
103
+ }
104
+ else
105
+ xml.title(:enabled => "False") {}
106
+ end
107
+ if p[:legend]
108
+ xml.legend(:enabled => "True", :position => "Right", :align => "Near") {
109
+ raise ":legend must have a :format option that sets the format that will be displayed for each element" unless p[:legend][:format]
110
+ xml.format p[:legend][:format]
111
+ xml.template {}
112
+ if p[:legend][:title]
113
+ xml.title(:enabled => "True") {
114
+ xml.text p[:legend][:title]
115
+ }
116
+ else
117
+ xml.title(:enabled => "False") {}
118
+ end
119
+ xml.columns_separator(:enabled => "False") {}
120
+ xml.background {
121
+ xml.inside_margin(:left => "10", :right => "10") {}
122
+ }
123
+ }
124
+ end
125
+ xml.axes {
126
+ xml.y_axis {
127
+ xml.labels {
128
+ xml.format p[:y_axis][:format]
129
+ }
130
+ }
131
+ }
132
+ }
133
+
134
+ xml.data {
135
+ series = (p[:series].kind_of?(Array) ? p[:series] : [p[:series]])
136
+ raise "Series incorrectas" if series.empty?
137
+
138
+ if series.count == 1
139
+ serie = series.first
140
+ xml.series(:name => serie[:name]) {
141
+ serie[:points].each do |point|
142
+ xml.point(:name => point[:name].to_s, :y => point[:y].to_s)
143
+ end
144
+ }
145
+ else
146
+ series.each do |serie|
147
+ xml.series(:name => serie[:name]) {
148
+ # Os varios pontos
149
+ serie[:points].each do |point|
150
+ xml.point(:name => point[:name].to_s, :y => point[:y].to_s)
151
+ end
152
+ }
153
+ end
154
+ end
155
+ }
156
+ }
157
+ }
158
+ }
159
+ end
160
+ builder.to_xml
161
+ end
162
+
163
+ def self.mandatory(object_that_must_exist, message)
164
+ raise (message + " Must Exist") if object_that_must_exist.blank?
165
+ object_that_must_exist
166
+ end
167
+ end
@@ -3,12 +3,38 @@ class ColumnChartMultiSeriesWithSimpleLegend
3
3
  # :animation => true by default ; Sets if chart has animation on load
4
4
  # :tooltip => false by default ; Set the tooltip to use on chart element hover
5
5
  # :title => false by default ; Set the title to show in the chart
6
+ # :legend => Sets various legend options to set
7
+ ## Possible Options
8
+ ### :title => Set legend's title
9
+ ### :format => Set legend format
10
+ # :series => Array of Hashes of series that will display the points on the screen
11
+ ## :series => [ { :name => "Serie um e dois",
12
+ ## :points => [ { :name => "Nome Ponto Um", :y => 55 },
13
+ ## { :name => "Nome Ponto Dois", :y => 66 }
14
+ ## ]
15
+ ## }
16
+ ## ]
17
+ # @@default_xml_options = {
18
+ # :animation => true,
19
+ # :tooltip => false, # "{%Name} - {%YValue}"
20
+ # :title => false,
21
+ # :legend => false,
22
+ # :series => [],
23
+ # :y_axis => {
24
+ # :format => "{%Value}{numDecimals:0}"
25
+ # }
26
+ # }
27
+
6
28
  @@default_xml_options = {
7
29
  :animation => true,
8
- :tooltip => "{%Name} - {%YValue}",
9
- :title => false
30
+ :tooltip => false, # "{%Name} - {%YValue}"
31
+ :title => false,
32
+ :legend => false,
33
+ :series => [],
34
+ :y_axis => {
35
+ :format => "{%Value}{numDecimals:0}"
36
+ }
10
37
  }
11
-
12
38
  def self.xml(options={})
13
39
  require 'nokogiri'
14
40
 
@@ -27,43 +53,69 @@ class ColumnChartMultiSeriesWithSimpleLegend
27
53
  xml.chart(:plot_type => "CategorizedVertical") {
28
54
  xml.data_plot_settings(:default_series_type => "Bar") {
29
55
  xml.bar_series(:point_padding => "0.2", :group_padding => "1") {
30
- xml.tooltip_settings(:enabled => "True") {
31
- xml.format "{%Name} - {%YValue}"
32
- }
56
+ if p[:tooltip]
57
+ xml.tooltip_settings(:enabled => "True") {
58
+ xml.format p[:tooltip]
59
+ }
60
+ else
61
+ xml.tooltip_settings(:enabled => "False") {}
62
+ end
33
63
  }
34
64
  }
35
65
 
36
66
  xml.chart_settings {
37
- xml.title(:enabled => "True") {
38
- xml.text "Title Here"
39
- }
67
+ if p[:title]
68
+ xml.title(:enabled => "True") {
69
+ xml.text p[:title]
70
+ }
71
+ end
72
+ if p[:legend]
40
73
  xml.legend(:enabled => "True", :position => "Right", :align => "Near") {
41
- xml.format "{%Icon} {%Name} ({%Value})"
74
+ raise ":legend must have a :format option that sets the format that will be displayed for each element" unless p[:legend][:format]
75
+ xml.format p[:legend][:format]
42
76
  xml.template {}
43
- xml.title(:enabled => "false") {}
44
- xml.columns_separator(:enabled => "false") {}
77
+ if p[:legend][:title]
78
+ xml.title(:enabled => "True") {
79
+ xml.text p[:legend][:title]
80
+ }
81
+ else
82
+ xml.title(:enabled => "False") {}
83
+ end
84
+ xml.columns_separator(:enabled => "False") {}
45
85
  xml.background {
46
86
  xml.inside_margin(:left => "10", :right => "10") {}
47
87
  }
48
88
  }
89
+ end
49
90
  xml.axes {
50
91
  xml.y_axis {
51
92
  xml.labels {
52
- xml.format "{%Value}{numDecimals:0}"
93
+ xml.format p[:y_axis][:format]
53
94
  }
54
95
  }
55
96
  }
56
97
  }
57
98
 
58
99
  xml.data {
59
- # As varias series
60
- [1].each do |serie|
61
- xml.series(:name => "Nome Serie #{serie}") {
62
- # Os varios pontos
63
- [12,24,45,54,94].each do |point|
64
- xml.point(:name => "Nome Ponto #{point}", :y => point.to_s)
100
+ series = (p[:series].kind_of?(Array) ? p[:series] : [p[:series]])
101
+ raise "Series incorrectas" if series.empty?
102
+
103
+ if series.count == 1
104
+ serie = series.first
105
+ xml.series(:name => serie[:name]) {
106
+ serie[:points].each do |point|
107
+ xml.point(:name => point[:name].to_s, :y => point[:y].to_s)
65
108
  end
66
109
  }
110
+ else
111
+ series.each do |serie|
112
+ xml.series(:name => serie[:name]) {
113
+ # Os varios pontos
114
+ serie[:points].each do |point|
115
+ xml.point(:name => point[:name].to_s, :y => point[:y].to_s)
116
+ end
117
+ }
118
+ end
67
119
  end
68
120
  }
69
121
  }
@@ -72,91 +124,4 @@ class ColumnChartMultiSeriesWithSimpleLegend
72
124
  end
73
125
  builder.to_xml
74
126
  end
75
- end
76
-
77
-
78
- # <anychart>
79
- # −
80
- # <settings>
81
- # <animation enabled="True"/>
82
- # </settings>
83
- # −
84
- # <charts>
85
- # −
86
- # <chart plot_type="CategorizedVertical">
87
- # −
88
- # <data_plot_settings default_series_type="Bar">
89
- # −
90
- # <bar_series point_padding="0.2" group_padding="1">
91
- # −
92
- # <tooltip_settings enabled="True">
93
- # <format>{%Name} - {%YValue}</format>
94
- # </tooltip_settings>
95
- # </bar_series>
96
- # </data_plot_settings>
97
- # −
98
- # <chart_settings>
99
- # −
100
- # <title enabled="true">
101
- # <text>Multi-Series: with Simple Legend</text>
102
- # </title>
103
- # −
104
- # <legend enabled="true" position="Right" align="Near">
105
- # <format>{%Icon} {%Name} ({%Value})</format>
106
- # <template/>
107
- # <title enabled="false"/>
108
- # <columns_separator enabled="false"/>
109
- # −
110
- # <background>
111
- # <inside_margin left="10" right="10"/>
112
- # </background>
113
- # </legend>
114
- # −
115
- # <axes>
116
- # −
117
- # <y_axis>
118
- # −
119
- # <labels>
120
- # <format>{%Value}{numDecimals:0}</format>
121
- # </labels>
122
- # </y_axis>
123
- # </axes>
124
- # </chart_settings>
125
- # −
126
- # <data>
127
- # −
128
- # <series name="Series 1">
129
- # <point name="P1" y="12"/>
130
- # <point name="P2" y="24"/>
131
- # <point name="P3" y="46"/>
132
- # <point name="P4" y="52"/>
133
- # <point name="P5" y="88"/>
134
- # </series>
135
- # −
136
- # <series name="Series 2">
137
- # <point name="P1" y="23"/>
138
- # <point name="P2" y="45"/>
139
- # <point name="P3" y="46"/>
140
- # <point name="P4" y="86"/>
141
- # <point name="P5" y="45"/>
142
- # </series>
143
- # −
144
- # <series name="Series 3">
145
- # <point name="P1" y="35"/>
146
- # <point name="P2" y="76"/>
147
- # <point name="P3" y="87"/>
148
- # <point name="P4" y="42"/>
149
- # <point name="P5" y="17"/>
150
- # </series>
151
- # −
152
- # <series name="Series 4">
153
- # <point name="P1" y="33"/>
154
- # <point name="P2" y="39"/>
155
- # <point name="P3" y="46"/>
156
- # <point name="P4" y="69"/>
157
- # <point name="P5" y="87"/>
158
- # </series>
159
- # </data>
160
- # </chart>
161
- # </charts>
162
- # </anychart>
127
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anychart_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jduarte
@@ -49,6 +49,7 @@ files:
49
49
  - Rakefile
50
50
  - VERSION
51
51
  - anychart_helpers.gemspec
52
+ - lib/anychart_base_xml.rb
52
53
  - lib/anychart_helpers.rb
53
54
  - lib/column_chart_multi_series_with_simple_legend.rb
54
55
  - test/helper.rb