gerbilcharts 0.1.8 → 0.1.9

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/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ == 0.0.9 2009-09-10
2
+ * Changes
3
+ * PIE Charts
4
+ * Fixed color scheme stylesheet brushmetal.css to be more pleasing
5
+ * Internal helper class svg_arc added
6
+
1
7
  == 0.0.7 2009-08-15
2
8
  * Changes
3
9
  * Bug fixes with stacked area
data/Manifest.txt CHANGED
@@ -54,6 +54,7 @@ lib/gerbilcharts/surfaces/surface.rb
54
54
  lib/gerbilcharts/surfaces/title_panel.rb
55
55
  lib/gerbilcharts/surfaces/tracker.rb
56
56
  lib/gerbilcharts/surfaces/vertical_axis.rb
57
+ lib/gerbilcharts/surfaces/pie_surface.rb
57
58
  lib/gerbilcharts/charts/area_chart.rb
58
59
  lib/gerbilcharts/charts/bar_chart_compact.rb
59
60
  lib/gerbilcharts/charts/bar_chart.rb
@@ -61,6 +62,7 @@ lib/gerbilcharts/charts/chart_base.rb
61
62
  lib/gerbilcharts/charts/impulse_chart.rb
62
63
  lib/gerbilcharts/charts/line_chart.rb
63
64
  lib/gerbilcharts/charts/stacked_area_chart.rb
65
+ lib/gerbilcharts/charts/pie_chart.rb
64
66
  lib/gerbilcharts/svgdc/svgdc.rb
65
67
  lib/gerbilcharts/svgdc/svg_element.rb
66
68
  lib/gerbilcharts/svgdc/filters.rb
@@ -75,3 +77,4 @@ lib/gerbilcharts/svgdc/svg_polygon.rb
75
77
  lib/gerbilcharts/svgdc/svg_polyline.rb
76
78
  lib/gerbilcharts/svgdc/svg_rect.rb
77
79
  lib/gerbilcharts/svgdc/svg_text.rb
80
+ lib/gerbilcharts/svgdc/svg_arc.rb
@@ -14,3 +14,4 @@ require 'gerbilcharts/charts/bar_chart'
14
14
  require 'gerbilcharts/charts/impulse_chart'
15
15
  require 'gerbilcharts/charts/line_chart'
16
16
  require 'gerbilcharts/charts/stacked_area_chart'
17
+ require 'gerbilcharts/charts/pie_chart'
@@ -0,0 +1,24 @@
1
+ module GerbilCharts::Charts
2
+
3
+ # = Pie chart
4
+ # A standard pie chart.
5
+ # The latest values of each model is presented as a slice
6
+ #
7
+ class PieChart < ChartBase
8
+
9
+ def initialize(opt={})
10
+ super(opt)
11
+ end
12
+
13
+ def create_chart_elements
14
+
15
+ @thechart.create_filter(GerbilCharts::SVGDC::LinearGradientVertical.new("vertgrad","rgb(255,255,255)","rgb(224,224,224)"))
16
+ # other elements
17
+ @thechart.add_child(GerbilCharts::Surfaces::SurfaceBackground.new(:orient => ORIENT_OVERLAY))
18
+ @thechart.add_child(GerbilCharts::Surfaces::TitlePanel.new(:orient => ORIENT_OVERLAY, :dim => 30, :just => :left))
19
+ @thechart.add_child(GerbilCharts::Surfaces::PieSurface.new(:orient => ORIENT_OVERLAY), :anchor => true)
20
+ end
21
+ end
22
+
23
+
24
+ end
@@ -1,5 +1,3 @@
1
- .surfacepanel
2
- {
3
1
  fill: none;
4
2
  stroke-width: 1;
5
3
  stroke: black;
@@ -42,92 +40,102 @@
42
40
  }
43
41
  #item0
44
42
  {
45
- stroke: blue;
46
- fill: blue;
43
+ stroke: none;
44
+ fill: #00FF00;
47
45
  }
48
46
  #item1
49
47
  {
50
- stroke: red;
48
+ stroke: none;
51
49
  fill: red;
52
50
  }
53
51
  #item2
54
52
  {
55
- stroke: yellow;
56
- fill: yellow ;
53
+ stroke: none;
54
+ fill: gold;
57
55
  }
58
56
  #item3
59
57
  {
60
- stroke: aqua;
58
+ stroke: none;
61
59
  fill: magenta;
62
60
  }
63
61
  #item4
64
62
  {
65
- stroke: gray;
66
- fill: blue;
63
+ stroke: none;
64
+ fill: olive;
67
65
  }
68
66
  #item5
69
67
  {
70
- stroke: green;
71
- fill: green;
68
+ stroke: none;
69
+ fill: #F88017;
72
70
  }
73
71
  #item6
74
72
  {
75
- stroke: orange;
76
- fill: orange;
73
+ stroke: none;
74
+ fill: #1F75FE;
77
75
  }
78
76
  #item7
79
77
  {
80
- stroke: lime;
81
- fill: violet;
78
+ stroke: none;
79
+ fill: #B87333;
82
80
  }
83
81
  #item8
84
82
  {
85
- stroke: brown;
86
- fill: navy;
83
+ stroke: none;
84
+ fill: #640E29;
85
+ }
86
+ #item9
87
+ {
88
+ stroke: none;
89
+ fill: aqua;
90
+ }
91
+ #item10
92
+ {
93
+ stroke: none;
94
+ fill: black;
87
95
  }
88
96
  #lineitem0
89
97
  {
90
- stroke: blue;
98
+ stroke: none;
91
99
  stroke-width: 2;
92
100
  }
93
101
  #lineitem1
94
102
  {
95
- stroke: red;
103
+ stroke: none;
96
104
  stroke-width: 2;
97
105
  }
98
106
  #lineitem2
99
107
  {
100
- stroke: yellow;
108
+ stroke: none;
101
109
  stroke-width: 2;
102
110
  }
103
111
  #lineitem3
104
112
  {
105
- stroke: aqua;
113
+ stroke: none;
106
114
  stroke-width: 2;
107
115
  }
108
116
  #lineitem4
109
117
  {
110
- stroke: gray;
118
+ stroke: none;
111
119
  stroke-width: 2;
112
120
  }
113
121
  #lineitem5
114
122
  {
115
- stroke: green;
123
+ stroke: none;
116
124
  stroke-width: 2;
117
125
  }
118
126
  #lineitem6
119
127
  {
120
- stroke: orange;
128
+ stroke: none;
121
129
  stroke-width: 2;
122
130
  }
123
131
  #lineitem7
124
132
  {
125
- stroke: lime;
133
+ stroke: none;
126
134
  stroke-width: 2;
127
135
  }
128
136
  #lineitem8
129
137
  {
130
- stroke: brown;
138
+ stroke: none;
131
139
  stroke-width: 2;
132
140
  }
133
141
  .axislabel
@@ -172,8 +180,17 @@
172
180
  }
173
181
  .elementlabel
174
182
  {
175
- font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
183
+ font-family: sans;
176
184
  font-size: 10px;
185
+ fill: black;
186
+ stroke: none;
187
+ }
188
+ .elementvalue
189
+ {
190
+ font-family: sans;
191
+ font-size: 8px;
192
+ fill: black;
193
+ stroke: none;
177
194
  }
178
195
  .trackerpanel
179
196
  {
@@ -1,7 +1,6 @@
1
1
  module GerbilCharts::Surfaces
2
2
 
3
3
  # = Pie Surface
4
- # Not yet implemented
5
4
  class PieSurface < Surface
6
5
  def initialize(opts={})
7
6
  super(opts)
@@ -11,6 +10,77 @@ class PieSurface < Surface
11
10
  g.rectangle_r(@bounds, {:class => @class})
12
11
  end
13
12
 
14
- end
13
+
14
+ def int_render(g)
15
+
16
+ # bail out of empty models quickly
17
+ if parent.modelgroup.empty?
18
+ g.textout(@bounds.left + @bounds.width/2, @bounds.height/2,
19
+ parent.modelgroup.empty_caption,{"text-anchor" => "middle"})
20
+ return
21
+ end
22
+
23
+ # compute totals
24
+ y_total = 0
25
+ parent.modelgroup.each_model_with_index do | mod, i|
26
+ y_total = y_total + mod.latest_val
27
+ end
28
+
29
+
30
+ radius = @bounds.height * 0.35
31
+ radius_label = radius + 10
32
+ cx = @bounds.width/2
33
+ cy = @bounds.height/2
34
+ current_pos_x = cx + radius
35
+ current_pos_y = cy
36
+
15
37
 
38
+ # draw each slice
39
+ tot_angle =0
40
+ parent.modelgroup.each_model_with_index do | mod, i|
41
+ mod_angle = ((mod.latest_val*360).to_f)/(y_total.to_f)
42
+ half_angle = tot_angle + mod_angle/2
43
+ tot_angle = tot_angle + mod_angle
44
+
45
+ new_pos_x = cx + (Math.cos((Math::PI/180)*tot_angle)*radius)
46
+ new_pos_y = cy + (Math.sin((Math::PI/180)*tot_angle)*radius)
47
+ label_pos_x = cx + (Math.cos((Math::PI/180)*half_angle)*radius_label)
48
+ label_pos_y = cy + (Math.sin((Math::PI/180)*half_angle)*radius_label)
49
+
50
+ percent_mod = (mod.latest_val*360/y_total)
51
+ percent = (percent_mod*100/360)
52
+ percent_pos_x = cx + ((Math.cos((Math::PI/180)*half_angle)*radius_label)*0.6)
53
+ percent_pos_y = cy + ((Math.sin((Math::PI/180)*half_angle)*radius_label)*0.6)
54
+
55
+ opts = {:id => "item#{i}" }
56
+ g.addshape(GerbilCharts::SVGDC::SVGArc.new(cx,cy,radius,radius,
57
+ current_pos_x,current_pos_y,
58
+ new_pos_x,new_pos_y,mod_angle),
59
+ opts)
60
+
61
+ if(tot_angle > 270)
62
+ if(label_pos_x > cx ) && (label_pos_y > cy )
63
+ g.textout( label_pos_x, label_pos_y, "#{mod.latest_val}", :class => 'elementlabel' )
64
+ g.textout( label_pos_x, label_pos_y+10, "#{mod.name}", :class => 'elementlabel' )
65
+ else
66
+ g.textout( label_pos_x+10 , label_pos_y , "#{mod.latest_val}", :class => 'elementlabel')
67
+ g.textout( label_pos_x+10 , label_pos_y+10, "#{mod.name}", :class => 'elementlabel')
68
+ end
69
+
70
+ elsif(label_pos_x > cx ) && (label_pos_y > cy )
71
+ g.textout( label_pos_x, label_pos_y, "#{mod.latest_val}", :class => 'elementlabel' )
72
+ g.textout( label_pos_x, label_pos_y+10, "#{mod.name}", :class => 'elementlabel' )
73
+ else
74
+ g.textout( label_pos_x-30 , label_pos_y , "#{mod.latest_val}", :class => 'elementlabel')
75
+ g.textout( label_pos_x-30 , label_pos_y+10, "#{mod.name}", :class => 'elementlabel')
76
+ end
77
+
78
+ if(percent > 8)
79
+ g.textout(percent_pos_x,percent_pos_y, "#{percent}%", :class => 'elementvalue')
80
+ end
81
+ current_pos_x = new_pos_x
82
+ current_pos_y = new_pos_y
83
+ end
84
+ end
85
+ end
16
86
  end
@@ -20,3 +20,4 @@ require 'gerbilcharts/svgdc/svg_polygon'
20
20
  require 'gerbilcharts/svgdc/svg_polyline'
21
21
  require 'gerbilcharts/svgdc/svg_rect'
22
22
  require 'gerbilcharts/svgdc/svg_text'
23
+ require 'gerbilcharts/svgdc/svg_arc'
@@ -0,0 +1,40 @@
1
+ module GerbilCharts::SVGDC
2
+
3
+ # = SVGARC - a simple elliptical arc
4
+ #
5
+ class SVGArc < SVGShape
6
+
7
+ attr_accessor :cx,:cy
8
+ attr_accessor :px,:py
9
+ attr_accessor :qx,:qy
10
+ attr_accessor :r1,:r2
11
+ attr_accessor :angle
12
+
13
+
14
+
15
+ # cx,cy = center point
16
+ # r1,r2 = radius 1, 2
17
+ # px,py = starting point
18
+ # qx,qy = ending point
19
+ #
20
+ # To create rectangle top left corner at (10,10) and width 200,height 100 do :
21
+ # SVGRect.new (10,10,200,150)
22
+ def initialize(cx,cy,r1,r2,px,py,qx,qy,angle)
23
+ @cx,@cy,@r1,@r2,@px,@py,@qx,@qy=cx,cy,r1,r2,px,py,qx,qy
24
+ @angle=angle
25
+ super()
26
+ end
27
+
28
+ def render(xfrag)
29
+ d_str = "M#{cx},#{cy} L#{px},#{py} A#{r1},#{r2} 0 #{anglestr} #{qx},#{qy} "
30
+ h= { :d => d_str }
31
+ render_base(xfrag) { xfrag.path( h.merge(render_attributes)) }
32
+ end
33
+
34
+ def anglestr
35
+ angle >= 180 ? "1,1" : "0,1"
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -2,7 +2,7 @@ module GerbilCharts
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 8
5
+ TINY = 9
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
data/test/test_pie.rb ADDED
@@ -0,0 +1,39 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ require 'trafgen'
4
+
5
+ # test pie chart
6
+ class TestPie < Test::Unit::TestCase
7
+
8
+
9
+ # test sales figures of 3 sales people
10
+ # show in a pie chart
11
+ def test_monthly_sales
12
+
13
+
14
+ modelgroup = GerbilCharts::Models::SimpleTimeSeriesModelGroup.new(
15
+ :title => "Sales figures",
16
+ :timeseries => (1..6).collect { |month| Time.local(2008,month) },
17
+ :models => [ ["Bruce", 1, 10, 18, 28, 80, 134],
18
+ ["Rex" , 112,22, 45, 70, 218, 309],
19
+ ["Sharmila", 112,22, 45, 70, 218, 245],
20
+ ["Wasim", 112,22, 45, 70, 218, 145],
21
+ ["Buzo" , 0, 23, 25, 40, 18, 59],
22
+ ["Vipin", 145,112, 22, 45, 18, 70],
23
+ ["This is a long label", 145,112, 22, 45, 18, 245],
24
+ ["9th item ", 145,112, 22, 45, 18, 45],
25
+ ["Others ", 145,112, 22, 45, 18, 15],
26
+ ["VIVEK", 145,112, 22, 45, 18, 170]
27
+ ]
28
+ )
29
+
30
+ mychart = GerbilCharts::Charts::PieChart.new( :width => 350, :height => 200, :style => 'brushmetal.css')
31
+ mychart.modelgroup=modelgroup
32
+ mychart.render('/tmp/pie_monthly_sales.svg')
33
+ end
34
+
35
+
36
+
37
+ end
38
+
39
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gerbilcharts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vivek Rajagopalan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-08 00:00:00 +05:30
12
+ date: 2009-09-11 00:00:00 +05:30
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -99,6 +99,7 @@ files:
99
99
  - lib/gerbilcharts/charts/impulse_chart.rb
100
100
  - lib/gerbilcharts/charts/line_chart.rb
101
101
  - lib/gerbilcharts/charts/stacked_area_chart.rb
102
+ - lib/gerbilcharts/charts/pie_chart.rb
102
103
  - lib/gerbilcharts/svgdc/svgdc.rb
103
104
  - lib/gerbilcharts/svgdc/svg_element.rb
104
105
  - lib/gerbilcharts/svgdc/filters.rb
@@ -113,6 +114,7 @@ files:
113
114
  - lib/gerbilcharts/svgdc/svg_polyline.rb
114
115
  - lib/gerbilcharts/svgdc/svg_rect.rb
115
116
  - lib/gerbilcharts/svgdc/svg_text.rb
117
+ - lib/gerbilcharts/svgdc/svg_arc.rb
116
118
  has_rdoc: true
117
119
  homepage: http://gerbilcharts.rubyforge.org
118
120
  licenses: []
@@ -150,6 +152,7 @@ test_files:
150
152
  - test/test_helper.rb
151
153
  - test/test_svgdc.rb
152
154
  - test/test_noob.rb
155
+ - test/test_pie.rb
153
156
  - test/test_render_string.rb
154
157
  - test/test_models.rb
155
158
  - test/test_charts.rb