minichart 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7a7348b03ecdf74aeaa57f5fba37079542211e40
4
+ data.tar.gz: b85da04d30a6fe59f8b38b688e29962b8a69c1a2
5
+ SHA512:
6
+ metadata.gz: d14c1514bc83eb80ab82b05476999824097a378af0729f47ba02e8c998e60495bbf94c79834ccbd35f531a57056ac70c15e9609e36e047724334e3009a9744c8
7
+ data.tar.gz: 8141275bea9a34dad840cd7c26d19b0c693b839f4162fddb87a2c7ac77fe81286c7dae653636befb848087097e797c6056acce759de370ab689e5df650737dbd
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ Minichart - SVG Chart Generator
2
+ ==================================================
3
+
4
+
5
+ ---
6
+
7
+ Create SVG mini charts with Ruby
8
+
9
+ ---
10
+
11
+ Install
12
+ --------------------------------------------------
13
+
14
+ ```
15
+ $ gem install minichart
16
+ ```
17
+
18
+ Or with bundler:
19
+
20
+ ```ruby
21
+ gem 'minichart'
22
+ ```
23
+
24
+ Example
25
+ --------------------------------------------------
26
+
27
+ ```ruby
28
+ require 'minichart'
29
+ include Minichart
30
+
31
+ plot = LineChart.new
32
+ plot.aspect_ratio = 2
33
+ plot.data = [10, 30, 20, 40, 30]
34
+ plot.save 'line'
35
+ ```
36
+
37
+ See more examples in the examples folder.
data/lib/minichart.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'victor'
2
+ require 'minichart/chart'
3
+ require 'minichart/line_chart'
4
+ require 'minichart/bar_chart'
@@ -0,0 +1,33 @@
1
+ module Minichart
2
+ class BarChart < Chart
3
+ def build
4
+ opts = { x_point_count: data.size }
5
+
6
+ inverted_points(opts).each do |x, y|
7
+ svg.rect bar_options x, y
8
+ end
9
+ end
10
+
11
+ private
12
+
13
+ def style
14
+ @style ||= { stroke_width: 1, stroke: '#eee' }
15
+ end
16
+
17
+ def bar_width
18
+ @bar_width ||= width / data.size
19
+ end
20
+
21
+ def bar_options(x, y)
22
+ y = y*height
23
+ bar_height = height-y
24
+ {
25
+ x: x*width,
26
+ y: y, width:
27
+ bar_width, height:
28
+ bar_height,
29
+ style: style
30
+ }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,47 @@
1
+ module Minichart
2
+ class Chart
3
+ attr_accessor :data, :aspect_ratio
4
+
5
+ def save(name)
6
+ svg.rect x: 0, y: 0, width: width, height: height, fill: '#ccc'
7
+ build
8
+ svg.save name
9
+ end
10
+
11
+ def build
12
+ raise "Subeclass needs to implement #build"
13
+ end
14
+
15
+ def inverted_points(opts={})
16
+ normalized_points(opts).map { |point| [point[0], 1-point[1]] }
17
+ end
18
+
19
+ def normalized_points(opts={})
20
+ x_point_count = opts[:x_point_count] || data.count-1
21
+
22
+ range = (data.max - data.min).to_f
23
+ x_width = 1/(x_point_count).to_f
24
+ result = []
25
+
26
+ data.each_with_index do |y, index|
27
+ x = index*x_width
28
+ y = (y-data.min)/range
29
+ result << [x,y]
30
+ end
31
+
32
+ result
33
+ end
34
+
35
+ def width
36
+ @width ||= (aspect_ratio * height).round
37
+ end
38
+
39
+ def height
40
+ 100
41
+ end
42
+
43
+ def svg
44
+ @svg ||= SVG.new viewBox: "0 0 #{width} #{height}", style: { background: '#ddd' }
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ module Minichart
2
+ class LineChart < Chart
3
+ def build
4
+ svg.polyline fill: :none, stroke: :black, stroke_width: 2, points: points
5
+ end
6
+
7
+ def points
8
+ result = []
9
+ inverted_points.each do |point|
10
+ x = width*point[0]
11
+ y = height*point[1]
12
+ result << "#{x},#{y}"
13
+ end
14
+ result
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Minichart
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minichart
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Danny Ben Shitrit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: victor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: runfile
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: runfile-tasks
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '9.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '9.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: filewatcher
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.5'
83
+ description: Generate mini charts with SVG
84
+ email: db@dannyben.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - README.md
90
+ - lib/minichart.rb
91
+ - lib/minichart/bar_chart.rb
92
+ - lib/minichart/chart.rb
93
+ - lib/minichart/line_chart.rb
94
+ - lib/minichart/version.rb
95
+ homepage: https://github.com/DannyBen/minichart
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: 2.0.0
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.6.6
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: SVG Mini Charts
119
+ test_files: []