pi_charts 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +40 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/bar_chart.rb +20 -0
- data/examples/bar_chart_hidden_legend.rb +22 -0
- data/examples/bar_chart_stacked.rb +23 -0
- data/examples/debug.rb +5 -0
- data/examples/doughnut_chart.rb +25 -0
- data/examples/doughnut_chart_hidden_legened.rb +28 -0
- data/examples/line_chart.rb +20 -0
- data/examples/line_chart_border.rb +23 -0
- data/examples/line_chart_hidden_legend.rb +23 -0
- data/examples/line_chart_no_line.rb +24 -0
- data/examples/line_chart_tension.rb +23 -0
- data/examples/pie_chart.rb +22 -0
- data/examples/pie_chart_hidden_legend.rb +19 -0
- data/examples/readme_serve_pie.rb +35 -0
- data/examples/sinatra.rb +55 -0
- data/examples/sinatra_line_chart.rb +25 -0
- data/lib/pi_charts.rb +19 -0
- data/lib/pi_charts/bar_chart.rb +125 -0
- data/lib/pi_charts/base.rb +98 -0
- data/lib/pi_charts/config.rb +40 -0
- data/lib/pi_charts/doughnut_chart.rb +27 -0
- data/lib/pi_charts/line_chart.rb +187 -0
- data/lib/pi_charts/pie_chart.rb +52 -0
- data/lib/pi_charts/utils.rb +21 -0
- data/lib/pi_charts/version.rb +3 -0
- data/pi_charts.gemspec +28 -0
- metadata +123 -0
@@ -0,0 +1,27 @@
|
|
1
|
+
module PiCharts
|
2
|
+
|
3
|
+
class Doughnut < Pie
|
4
|
+
def create!(args={})
|
5
|
+
@config.data[:type] = 'doughnut'
|
6
|
+
@config.data[:data][:datasets] = []
|
7
|
+
label = args[:label] || ""
|
8
|
+
data = args[:data] || []
|
9
|
+
color = args[:color] || []
|
10
|
+
data = { data: data, backgroundColor: color, label: label }
|
11
|
+
@config.data[:data][:datasets] << data
|
12
|
+
@config.data[:options][:cutoutpercentage] = 50
|
13
|
+
@config.data[:data][:labels] = []
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
def cutout(percentage=false)
|
18
|
+
if percentage
|
19
|
+
@config.data[:options][:cutoutpercentage] = percentage
|
20
|
+
true
|
21
|
+
else
|
22
|
+
@config.data[:options][:cutoutPercentage]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,187 @@
|
|
1
|
+
module PiCharts
|
2
|
+
|
3
|
+
class Line < Base
|
4
|
+
|
5
|
+
def add_labels(labels)
|
6
|
+
@config.data[:data][:labels] = labels
|
7
|
+
end
|
8
|
+
|
9
|
+
def title(args = {})
|
10
|
+
if args.keys.empty? or ! args.keys.include? [:title]
|
11
|
+
raise "need to specify title with {title: 'name', display: true/false }"
|
12
|
+
end
|
13
|
+
@config.data[:data][:options][:title] = {}
|
14
|
+
@config.data[:data][:options][:title][:text] = args[:title] || ""
|
15
|
+
@config.data[:data][:options][:title][:display] = true unless args[:display] == false
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
19
|
+
def create(args = {})
|
20
|
+
@config.data[:type] = 'line'
|
21
|
+
@config.data[:data][:datasets] = []
|
22
|
+
@config.data[:data][:labels] = []
|
23
|
+
@config.data[:data][:options] = []
|
24
|
+
true
|
25
|
+
end
|
26
|
+
|
27
|
+
def add_dataset(args = {})
|
28
|
+
if args.keys.empty?
|
29
|
+
raise "need to specify dataset with { label: 'name', data: [1,2,3] }"
|
30
|
+
end
|
31
|
+
label = args[:label]
|
32
|
+
data = args[:data]
|
33
|
+
color = args[:color] || random_color
|
34
|
+
fill = args[:fill] || false
|
35
|
+
index = dataset_index(label)
|
36
|
+
if index
|
37
|
+
if data.kind_of?(Array)
|
38
|
+
data.each { |d| @config.data[:data][:datasets][index][:data] << d }
|
39
|
+
else
|
40
|
+
@config.data[:data][:datasets][index][:data] << data
|
41
|
+
end
|
42
|
+
else
|
43
|
+
dataset = {}
|
44
|
+
dataset[:label] = label
|
45
|
+
dataset[:borderColor] = color
|
46
|
+
dataset[:backgroundColor] = color
|
47
|
+
dataset[:fill] = fill
|
48
|
+
dataset[:data] = []
|
49
|
+
if data.kind_of?(Array)
|
50
|
+
data.each { |d| dataset[:data] << d }
|
51
|
+
else
|
52
|
+
dataset[:data] << data
|
53
|
+
end
|
54
|
+
@config.data[:data][:datasets] << dataset
|
55
|
+
end
|
56
|
+
true
|
57
|
+
end
|
58
|
+
|
59
|
+
def tension(args={})
|
60
|
+
if args.keys.empty?
|
61
|
+
false
|
62
|
+
else
|
63
|
+
if args[:dataset]
|
64
|
+
index = dataset_index(args[:dataset])
|
65
|
+
if index
|
66
|
+
@config.data[:data][:datasets][index][:lineTension] = args[:ammount]
|
67
|
+
else
|
68
|
+
raise "dataset doesn't seem to exist"
|
69
|
+
end
|
70
|
+
else
|
71
|
+
raise "need to specify a dataset"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def border(args={})
|
77
|
+
if args.keys.empty?
|
78
|
+
false
|
79
|
+
else
|
80
|
+
if args[:dataset]
|
81
|
+
index = dataset_index(args[:dataset])
|
82
|
+
if index
|
83
|
+
@config.data[:data][:datasets][index][:borderColor] = args[:color] if args[:color]
|
84
|
+
@config.data[:data][:datasets][index][:borderWidth] = args[:width] if args[:width]
|
85
|
+
@config.data[:data][:datasets][index][:borderCapStyle] = args[:cap] if args[:cap]
|
86
|
+
@config.data[:data][:datasets][index][:borderDash] = args[:dash] if args[:dash]
|
87
|
+
@config.data[:data][:datasets][index][:borderDashOffset] = args[:dash_offeset] if args[:dash_offset]
|
88
|
+
@config.data[:data][:datasets][index][:borderJoinStyle] = args[:joint] if args[:joint]
|
89
|
+
else
|
90
|
+
raise "dataset doesn't seem to exist"
|
91
|
+
end
|
92
|
+
else
|
93
|
+
raise "need to specify a dataset"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def point(args={})
|
99
|
+
if args.keys.empty?
|
100
|
+
false
|
101
|
+
else
|
102
|
+
if args[:dataset]
|
103
|
+
index = dataset_index(args[:dataset])
|
104
|
+
if index
|
105
|
+
@config.data[:data][:datasets][index][:pointBorderColor] = args[:color] if args[:color]
|
106
|
+
@config.data[:data][:datasets][index][:pointBorderBackgroundColor] = args[:border] if args[:border]
|
107
|
+
@config.data[:data][:datasets][index][:pointBorderWidth] = args[:width] if args[:width]
|
108
|
+
@config.data[:data][:datasets][index][:pointRadius] = args[:radius] if args[:radius]
|
109
|
+
@config.data[:data][:datasets][index][:pointHoverRadius] = args[:hover] if args[:hover]
|
110
|
+
@config.data[:data][:datasets][index][:pointHitRadius] = args[:hit] if args[:hit]
|
111
|
+
@config.data[:data][:datasets][index][:pointStyle] = args[:style] if args[:style]
|
112
|
+
else
|
113
|
+
raise "dataset doesn't seem to exist"
|
114
|
+
end
|
115
|
+
else
|
116
|
+
raise "need to specify a dataset"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def no_line(dataset=false)
|
122
|
+
unless dataset
|
123
|
+
false
|
124
|
+
else
|
125
|
+
if dataset
|
126
|
+
index = dataset_index(dataset)
|
127
|
+
if index
|
128
|
+
@config.data[:data][:datasets][index][:showLine] = false
|
129
|
+
else
|
130
|
+
raise "dataset doesn't seem to exist"
|
131
|
+
end
|
132
|
+
else
|
133
|
+
raise "need to specify a dataset"
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def span_gaps(dataset=false)
|
139
|
+
unless dataset
|
140
|
+
false
|
141
|
+
else
|
142
|
+
if dataset
|
143
|
+
index = dataset_index(dataset)
|
144
|
+
if index
|
145
|
+
@config.data[:data][:datasets][index][:spanGaps] = true
|
146
|
+
else
|
147
|
+
raise "dataset doesn't seem to exist"
|
148
|
+
end
|
149
|
+
else
|
150
|
+
raise "need to specify a dataset"
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def stepped(dataset = false)
|
156
|
+
unless dataset
|
157
|
+
false
|
158
|
+
else
|
159
|
+
if dataset
|
160
|
+
index = dataset_index(dataset)
|
161
|
+
if index
|
162
|
+
@config.data[:data][:datasets][index][:steppedLine] = true
|
163
|
+
else
|
164
|
+
raise "dataset doesn't seem to exist"
|
165
|
+
end
|
166
|
+
else
|
167
|
+
raise "need to specify a dataset"
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
private
|
173
|
+
|
174
|
+
def dataset_index(label = false)
|
175
|
+
if label
|
176
|
+
@config.data[:data][:datasets].each_with_index do |set, index|
|
177
|
+
if set[:label] == label
|
178
|
+
return index
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
false
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module PiCharts
|
2
|
+
|
3
|
+
class Pie < Base
|
4
|
+
|
5
|
+
def label(label)
|
6
|
+
@config.data[:data][:label] = label
|
7
|
+
true
|
8
|
+
end
|
9
|
+
|
10
|
+
def create(args={})
|
11
|
+
@config.data[:type] = 'pie'
|
12
|
+
@config.data[:data][:datasets] = []
|
13
|
+
label = args[:label] || ""
|
14
|
+
data = args[:data] || []
|
15
|
+
color = args[:color] || []
|
16
|
+
data = { data: data, backgroundColor: color, label: label }
|
17
|
+
@config.data[:data][:datasets] << data
|
18
|
+
@config.data[:data][:labels] = []
|
19
|
+
true
|
20
|
+
end
|
21
|
+
|
22
|
+
def add_dataset(args={})
|
23
|
+
label = args[:label]
|
24
|
+
data = args[:data]
|
25
|
+
color = args[:color] || random_color
|
26
|
+
if @config.data[:data][:labels].include? label
|
27
|
+
index = @config.data[:data][:labels].index(label)
|
28
|
+
if args[:add]
|
29
|
+
@config.data[:data][:datasets].first[:data][index] += data
|
30
|
+
else
|
31
|
+
@config.data[:data][:datasets].first[:data][index] = data
|
32
|
+
end
|
33
|
+
else
|
34
|
+
@config.data[:data][:datasets].first[:data] << data
|
35
|
+
@config.data[:data][:datasets].first[:backgroundColor] << color
|
36
|
+
@config.data[:data][:labels] << label
|
37
|
+
end
|
38
|
+
true
|
39
|
+
end
|
40
|
+
|
41
|
+
def animate(animate=true)
|
42
|
+
if animate
|
43
|
+
@config.data[:options] = true
|
44
|
+
else
|
45
|
+
@config.data[:options] = false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module PiCharts
|
2
|
+
|
3
|
+
class Utils
|
4
|
+
|
5
|
+
def cdn(args={})
|
6
|
+
version = args[:version] || "2.4.0"
|
7
|
+
min = false || args[:min]
|
8
|
+
if min
|
9
|
+
"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/#{version}/Chart.min.js\"></script>"
|
10
|
+
else
|
11
|
+
"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/#{version}/Chart.js\"></script>"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def random_color
|
16
|
+
"##{SecureRandom.hex(3)}"
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/pi_charts.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pi_charts/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "pi_charts"
|
8
|
+
spec.version = PiCharts::VERSION
|
9
|
+
spec.authors = ["Kent Gruber"]
|
10
|
+
spec.email = ["kgruber1@emich.edu"]
|
11
|
+
|
12
|
+
spec.summary = %q{A ruby gem to easily build beautiful charts using chartjs.}
|
13
|
+
spec.description = %q{A ruby gem to easily build beautiful charts using chartjs. If you like charts, ruby, and want beautiful javascript charts.}
|
14
|
+
spec.homepage = "https://github.com/picatz/Pi-Charts"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
|
21
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
spec.bindir = "bin"
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pi_charts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kent Gruber
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: A ruby gem to easily build beautiful charts using chartjs. If you like
|
56
|
+
charts, ruby, and want beautiful javascript charts.
|
57
|
+
email:
|
58
|
+
- kgruber1@emich.edu
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- CODE_OF_CONDUCT.md
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- examples/bar_chart.rb
|
74
|
+
- examples/bar_chart_hidden_legend.rb
|
75
|
+
- examples/bar_chart_stacked.rb
|
76
|
+
- examples/debug.rb
|
77
|
+
- examples/doughnut_chart.rb
|
78
|
+
- examples/doughnut_chart_hidden_legened.rb
|
79
|
+
- examples/line_chart.rb
|
80
|
+
- examples/line_chart_border.rb
|
81
|
+
- examples/line_chart_hidden_legend.rb
|
82
|
+
- examples/line_chart_no_line.rb
|
83
|
+
- examples/line_chart_tension.rb
|
84
|
+
- examples/pie_chart.rb
|
85
|
+
- examples/pie_chart_hidden_legend.rb
|
86
|
+
- examples/readme_serve_pie.rb
|
87
|
+
- examples/sinatra.rb
|
88
|
+
- examples/sinatra_line_chart.rb
|
89
|
+
- lib/pi_charts.rb
|
90
|
+
- lib/pi_charts/bar_chart.rb
|
91
|
+
- lib/pi_charts/base.rb
|
92
|
+
- lib/pi_charts/config.rb
|
93
|
+
- lib/pi_charts/doughnut_chart.rb
|
94
|
+
- lib/pi_charts/line_chart.rb
|
95
|
+
- lib/pi_charts/pie_chart.rb
|
96
|
+
- lib/pi_charts/utils.rb
|
97
|
+
- lib/pi_charts/version.rb
|
98
|
+
- pi_charts.gemspec
|
99
|
+
homepage: https://github.com/picatz/Pi-Charts
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
metadata: {}
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 2.6.8
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: A ruby gem to easily build beautiful charts using chartjs.
|
123
|
+
test_files: []
|