simple_charts 0.0.6 → 0.0.7

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.
@@ -3,14 +3,13 @@ require 'simple_charts/version'
3
3
  module SimpleCharts
4
4
  class Chart
5
5
 
6
- def create_pie_paths(data, opts)
7
-
6
+ def create_doughnut_paths(data, opts)
8
7
  total = data.inject(:+)
9
8
  segments = data.map{ |val| 360 * val / total }
10
9
  centroid = opts[:width] / 2
11
- centroid_padding = 5
12
- radius = centroid - centroid_padding
13
-
10
+ svg_padding = 5
11
+ centroid_padding = opts[:centroid_padding] || 0
12
+ radius = centroid - svg_padding
14
13
  end_angle = 0
15
14
  paths = []
16
15
 
@@ -18,12 +17,28 @@ module SimpleCharts
18
17
  start_angle = end_angle
19
18
  end_angle = start_angle + segment
20
19
 
21
- x1 = (centroid + radius * Math.cos(Math::PI * start_angle / 180)).round.to_s
22
- y1 = (centroid + radius * Math.sin(Math::PI * start_angle / 180)).round.to_s
23
- x2 = (centroid + radius * Math.cos(Math::PI * end_angle / 180)).round.to_s
24
- y2 = (centroid + radius * Math.sin(Math::PI * end_angle / 180)).round.to_s
20
+ outer_edge_start_x = (centroid + radius * Math.cos(Math::PI * start_angle / 180)).round.to_s
21
+ outer_edge_start_y = (centroid + radius * Math.sin(Math::PI * start_angle / 180)).round.to_s
22
+ outer_edge_end_x = (centroid + radius * Math.cos(Math::PI * end_angle / 180)).round.to_s
23
+ outer_edge_end_y = (centroid + radius * Math.sin(Math::PI * end_angle / 180)).round.to_s
24
+ inner_edge_end_x = (centroid + centroid_padding * Math.cos(Math::PI * end_angle / 180)).round.to_s
25
+ inner_edge_end_y = (centroid + centroid_padding * Math.sin(Math::PI * end_angle / 180)).round.to_s
26
+ inner_edge_start_x = (centroid + centroid_padding * Math.cos(Math::PI * start_angle / 180)).round.to_s
27
+ inner_edge_start_y = (centroid + centroid_padding * Math.sin(Math::PI * start_angle / 180)).round.to_s
28
+
29
+ # move to start of outer arc
30
+ path = 'M' + outer_edge_start_x + ',' + outer_edge_start_y
31
+
32
+ # arc to outer edge of segment
33
+ path += ' A ' + radius.to_s + ',' + radius.to_s + ' 0 ' + ((end_angle - start_angle > 180) ? '1' : '0') + ' 1 ' + outer_edge_end_x + ',' + outer_edge_end_y
34
+
35
+ # line to inner edge of segment
36
+ path += ' L ' + inner_edge_end_x + ',' + inner_edge_end_y
37
+
38
+ # arc to inner other edge of segment
39
+ path += ' A ' + centroid_padding.to_s + ',' + centroid_padding.to_s + ' 0 ' + ((end_angle - start_angle > 180) ? '1' : '0') + ' 0 ' + inner_edge_start_x + ',' + inner_edge_start_y + ' z'
25
40
 
26
- paths << 'M' + centroid.to_s + ',' + centroid.to_s + ' L' + x1 + ',' + y1 + ' A' + radius.to_s + ',' + radius.to_s + ' 0 ' + ((end_angle - start_angle > 180) ? '1' : '0') + ',1 ' + x2 + ',' + y2 + ' z'
41
+ paths << path
27
42
  end
28
43
 
29
44
  paths
@@ -32,13 +47,10 @@ module SimpleCharts
32
47
  def render(type, data, opts)
33
48
  svg = "<svg width='#{opts[:width]}' height='#{opts[:height]}' id='#{opts[:id]}' xmlns='http://www.w3.org/2000/svg'>"
34
49
 
35
- case type
36
- when 'pie'
37
- create_pie_paths(data, opts).each_with_index do |path, index|
38
- svg += "<path d='#{path}' id='#{opts[:id]}-path-#{index}'></path>"
39
- end
40
- else
41
- # error
50
+ if type == 'pie' || type == 'doughnut'
51
+ create_doughnut_paths(data, opts).each_with_index do |path, index|
52
+ svg += "<path d='#{path}' id='#{opts[:id]}-path-#{index}'></path>"
53
+ end
42
54
  end
43
55
 
44
56
  svg + '</svg>'
@@ -1,3 +1,3 @@
1
1
  module SimpleCharts
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.7'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_charts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -69,7 +69,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
69
69
  version: '0'
70
70
  segments:
71
71
  - 0
72
- hash: -3323566945862839880
72
+ hash: -649686454733890820
73
73
  required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  none: false
75
75
  requirements:
@@ -78,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
78
  version: '0'
79
79
  segments:
80
80
  - 0
81
- hash: -3323566945862839880
81
+ hash: -649686454733890820
82
82
  requirements: []
83
83
  rubyforge_project:
84
84
  rubygems_version: 1.8.24