chart_js 1.0.0pre
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 +67 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/chart_js.gemspec +27 -0
- data/examples/bar_chart.rb +18 -0
- data/examples/build.rb +20 -0
- data/examples/debug.rb +6 -0
- data/examples/line_chart.rb +18 -0
- data/examples/radar_chart.rb +18 -0
- data/examples/sinatra.rb +23 -0
- data/lib/chart_js.rb +21 -0
- data/lib/chart_js/chart/bar_chart/background.rb +20 -0
- data/lib/chart_js/chart/bar_chart/bar_chart.rb +94 -0
- data/lib/chart_js/chart/bar_chart/border.rb +48 -0
- data/lib/chart_js/chart/bar_chart/data.rb +37 -0
- data/lib/chart_js/chart/bar_chart/dataset.rb +87 -0
- data/lib/chart_js/chart/bar_chart/dataset/background.rb +20 -0
- data/lib/chart_js/chart/bar_chart/dataset/border.rb +48 -0
- data/lib/chart_js/chart/bar_chart/dataset/line.rb +16 -0
- data/lib/chart_js/chart/bar_chart/dataset/point.rb +55 -0
- data/lib/chart_js/chart/bar_chart/dataset/point_border.rb +44 -0
- data/lib/chart_js/chart/bar_chart/dataset/point_hover.rb +44 -0
- data/lib/chart_js/chart/bar_chart/file.rb +26 -0
- data/lib/chart_js/chart/bar_chart/line.rb +16 -0
- data/lib/chart_js/chart/bar_chart/opts.rb +18 -0
- data/lib/chart_js/chart/bar_chart/point.rb +55 -0
- data/lib/chart_js/chart/bar_chart/point_border.rb +44 -0
- data/lib/chart_js/chart/bar_chart/point_hover.rb +44 -0
- data/lib/chart_js/chart/chart.rb +99 -0
- data/lib/chart_js/chart/charts.rb +22 -0
- data/lib/chart_js/chart/data.rb +37 -0
- data/lib/chart_js/chart/dataset.rb +87 -0
- data/lib/chart_js/chart/dataset/background.rb +20 -0
- data/lib/chart_js/chart/dataset/border.rb +48 -0
- data/lib/chart_js/chart/dataset/line.rb +16 -0
- data/lib/chart_js/chart/dataset/point.rb +55 -0
- data/lib/chart_js/chart/dataset/point_border.rb +44 -0
- data/lib/chart_js/chart/dataset/point_hover.rb +44 -0
- data/lib/chart_js/chart/file.rb +26 -0
- data/lib/chart_js/chart/line_chart.rb +99 -0
- data/lib/chart_js/chart/line_chart/background.rb +20 -0
- data/lib/chart_js/chart/line_chart/border.rb +48 -0
- data/lib/chart_js/chart/line_chart/data.rb +37 -0
- data/lib/chart_js/chart/line_chart/dataset.rb +87 -0
- data/lib/chart_js/chart/line_chart/dataset/background.rb +20 -0
- data/lib/chart_js/chart/line_chart/dataset/border.rb +48 -0
- data/lib/chart_js/chart/line_chart/dataset/line.rb +16 -0
- data/lib/chart_js/chart/line_chart/dataset/point.rb +55 -0
- data/lib/chart_js/chart/line_chart/dataset/point_border.rb +44 -0
- data/lib/chart_js/chart/line_chart/dataset/point_hover.rb +44 -0
- data/lib/chart_js/chart/line_chart/file.rb +26 -0
- data/lib/chart_js/chart/line_chart/line.rb +16 -0
- data/lib/chart_js/chart/line_chart/line_chart.rb +94 -0
- data/lib/chart_js/chart/line_chart/opts.rb +18 -0
- data/lib/chart_js/chart/line_chart/point.rb +55 -0
- data/lib/chart_js/chart/line_chart/point_border.rb +44 -0
- data/lib/chart_js/chart/line_chart/point_hover.rb +44 -0
- data/lib/chart_js/chart/opts.rb +18 -0
- data/lib/chart_js/chart/radar_chart/background.rb +20 -0
- data/lib/chart_js/chart/radar_chart/border.rb +48 -0
- data/lib/chart_js/chart/radar_chart/data.rb +37 -0
- data/lib/chart_js/chart/radar_chart/dataset.rb +87 -0
- data/lib/chart_js/chart/radar_chart/dataset/background.rb +20 -0
- data/lib/chart_js/chart/radar_chart/dataset/border.rb +48 -0
- data/lib/chart_js/chart/radar_chart/dataset/line.rb +16 -0
- data/lib/chart_js/chart/radar_chart/dataset/point.rb +55 -0
- data/lib/chart_js/chart/radar_chart/dataset/point_border.rb +44 -0
- data/lib/chart_js/chart/radar_chart/dataset/point_hover.rb +44 -0
- data/lib/chart_js/chart/radar_chart/file.rb +26 -0
- data/lib/chart_js/chart/radar_chart/line.rb +16 -0
- data/lib/chart_js/chart/radar_chart/opts.rb +18 -0
- data/lib/chart_js/chart/radar_chart/point.rb +55 -0
- data/lib/chart_js/chart/radar_chart/point_border.rb +44 -0
- data/lib/chart_js/chart/radar_chart/point_hover.rb +44 -0
- data/lib/chart_js/chart/radar_chart/radar_chart.rb +94 -0
- data/lib/chart_js/version.rb +3 -0
- metadata +169 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0a2713c9196c3d35025af1e7bb53c642f85290e4
|
4
|
+
data.tar.gz: ee8e2c484bd08f69e6da46fdfaead46081b5a57b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 969504c4340a4f2f041fd720fc43168dc7d6ddb44c0431177ded05546a5505a2bfa1ee9aeba1ea8d8fc075859ddda5ad5308882d953d0a8cdc9f3b7f6c20c895
|
7
|
+
data.tar.gz: 3cdc111e2fa229eba1067490557846608e4d9b32e908bbf1537a1b97493002586a07bb643f9a3289ff7ebf92723ca71eb1b2be1ab085ad6bb0c166c1b9e956c4
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at kgruber1@emich.edu. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Kent Gruber
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Chart JS
|
2
|
+
|
3
|
+
[Chart JS](http://www.chartjs.org/) is a simple yet flexible JavaScript charting library. This [gem](https://rubygems.org/) is a Ruby [Domain Specific Language](https://en.wikipedia.org/wiki/Domain-specific_language) which allows you to easily build charts without touching a single line of JavaScript or HTML.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
This gem is still in development. Install with `--pre`.
|
8
|
+
|
9
|
+
$ gem install chart_js --pre
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
A simple example to generate a static html file.
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
require "chart_js"
|
17
|
+
|
18
|
+
ChartJS.line do
|
19
|
+
file "example.html"
|
20
|
+
data do
|
21
|
+
labels ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
|
22
|
+
dataset "Cats" do
|
23
|
+
color :random
|
24
|
+
data [12, 19, 3, 5, 2, 3]
|
25
|
+
end
|
26
|
+
dataset "Dogs" do
|
27
|
+
color :random
|
28
|
+
data [10, 12, 3, 4, 5, 3]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
## Web Framework Integration
|
35
|
+
|
36
|
+
`chart_js` plays well with others.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require "chart_js"
|
40
|
+
require "sinatra"
|
41
|
+
|
42
|
+
get "/" do
|
43
|
+
chart = ChartJS.line do
|
44
|
+
data do
|
45
|
+
labels ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
|
46
|
+
dataset "Cats" do
|
47
|
+
color :random
|
48
|
+
data [12, 19, 3, 5, 2, 3]
|
49
|
+
end
|
50
|
+
dataset "Dogs" do
|
51
|
+
color :random
|
52
|
+
data [10, 12, 3, 4, 5, 3]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
chart.to_html
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
## Screenshot
|
61
|
+
|
62
|
+
![example](https://i.imgur.com/kFl4efW.png)
|
63
|
+
|
64
|
+
## License
|
65
|
+
|
66
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
67
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "chart_js"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/chart_js.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "chart_js/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "chart_js"
|
8
|
+
spec.version = ChartJS::VERSION
|
9
|
+
spec.authors = ["Kent 'picat' Gruber"]
|
10
|
+
spec.email = ["kgruber1@emich.edu"]
|
11
|
+
|
12
|
+
spec.summary = %q{A simple ruby DSL to build responive charts for the web using Chart.js}
|
13
|
+
#spec.description = %q{TODO: Write a longer description or delete this line.}
|
14
|
+
spec.homepage = "https://github.com/picatz/chart_js"
|
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
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# path setting magic for example directory only
|
2
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
3
|
+
require "chart_js"
|
4
|
+
|
5
|
+
ChartJS.bar do
|
6
|
+
file "example.html"
|
7
|
+
data do
|
8
|
+
labels ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"]
|
9
|
+
dataset "Apples" do
|
10
|
+
color :random
|
11
|
+
data [12, 19, 3, 5, 2, 3]
|
12
|
+
end
|
13
|
+
dataset "Pears" do
|
14
|
+
color :random
|
15
|
+
data [10, 12, 3, 4, 5, 3]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/examples/build.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# path setting magic for example directory only
|
2
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
3
|
+
require "chart_js"
|
4
|
+
require "pry"
|
5
|
+
|
6
|
+
ChartJS.build do
|
7
|
+
file "example.html"
|
8
|
+
type "line"
|
9
|
+
data do
|
10
|
+
labels ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"]
|
11
|
+
dataset "Apples" do
|
12
|
+
color :random
|
13
|
+
data [12, 19, 3, 5, 2, 3]
|
14
|
+
end
|
15
|
+
dataset "Pears" do
|
16
|
+
color :random
|
17
|
+
data [10, 12, 3, 4, 5, 3]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/examples/debug.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# path setting magic for example directory only
|
2
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
3
|
+
require "chart_js"
|
4
|
+
|
5
|
+
ChartJS.line do
|
6
|
+
file "example.html"
|
7
|
+
data do
|
8
|
+
labels ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
|
9
|
+
dataset "Cats" do
|
10
|
+
color :random
|
11
|
+
data [12, 19, 3, 5, 2, 3]
|
12
|
+
end
|
13
|
+
dataset "Dogs" do
|
14
|
+
color :random
|
15
|
+
data [10, 12, 3, 4, 5, 3]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# path setting magic for example directory only
|
2
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
3
|
+
require "chart_js"
|
4
|
+
|
5
|
+
ChartJS.radar do
|
6
|
+
file "example.html"
|
7
|
+
data do
|
8
|
+
labels ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"]
|
9
|
+
dataset "Apples" do
|
10
|
+
color :random
|
11
|
+
data [12, 19, 3, 5, 2, 3]
|
12
|
+
end
|
13
|
+
dataset "Pears" do
|
14
|
+
color :random
|
15
|
+
data [10, 12, 3, 4, 5, 3]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/examples/sinatra.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# path setting magic for example directory only
|
2
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
3
|
+
require "chart_js"
|
4
|
+
require "sinatra"
|
5
|
+
|
6
|
+
set :bind, "0.0.0.0"
|
7
|
+
|
8
|
+
get "/" do
|
9
|
+
chart = ChartJS.line do
|
10
|
+
data do
|
11
|
+
labels ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
|
12
|
+
dataset "Cats" do
|
13
|
+
color :random
|
14
|
+
data [12, 19, 3, 5, 2, 3]
|
15
|
+
end
|
16
|
+
dataset "Dogs" do
|
17
|
+
color :random
|
18
|
+
data [10, 12, 3, 4, 5, 3]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
chart.to_html
|
23
|
+
end
|
data/lib/chart_js.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "chart_js/version"
|
2
|
+
require "chart_js/chart/chart"
|
3
|
+
require "chart_js/chart/charts"
|
4
|
+
|
5
|
+
module ChartJS
|
6
|
+
def self.build(&block)
|
7
|
+
Chart.new(&block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.line(&block)
|
11
|
+
Charts.line(&block)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.radar(&block)
|
15
|
+
Charts.radar(&block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.bar(&block)
|
19
|
+
Charts.bar(&block)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module ChartJS
|
2
|
+
|
3
|
+
class Background
|
4
|
+
|
5
|
+
def initialize(container)
|
6
|
+
@container = container
|
7
|
+
end
|
8
|
+
|
9
|
+
def build(&block)
|
10
|
+
instance_eval(&block)
|
11
|
+
@container
|
12
|
+
end
|
13
|
+
|
14
|
+
def color(value)
|
15
|
+
@container['backgroundColor'] = value
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require "json"
|
2
|
+
require "securerandom"
|
3
|
+
require_relative "data.rb"
|
4
|
+
require_relative "opts.rb"
|
5
|
+
require_relative "file.rb"
|
6
|
+
|
7
|
+
module ChartJS
|
8
|
+
|
9
|
+
class BarChart
|
10
|
+
|
11
|
+
def initialize(&block)
|
12
|
+
@type = "bar"
|
13
|
+
@data = nil
|
14
|
+
@opts = nil
|
15
|
+
@file = false
|
16
|
+
build(&block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def build(&block)
|
20
|
+
instance_eval(&block)
|
21
|
+
if @file
|
22
|
+
f = SaveFile.new(@file, html: to_html)
|
23
|
+
f.save!
|
24
|
+
end
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_h
|
29
|
+
{ type: @type, data: @data.to_h, options: @opts.to_h }
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_json(type = :pretty)
|
33
|
+
return JSON.pretty_generate(to_h) if type == :pretty
|
34
|
+
to_h.to_json
|
35
|
+
end
|
36
|
+
|
37
|
+
def data(&block)
|
38
|
+
return @data unless block_given?
|
39
|
+
@data = Data.new.build(&block)
|
40
|
+
end
|
41
|
+
|
42
|
+
def opts(&block)
|
43
|
+
return @opts unless block_given?
|
44
|
+
@opts = Opts.new.build(&block)
|
45
|
+
end
|
46
|
+
|
47
|
+
def cdn(version: "2.6.0", min: true)
|
48
|
+
if min
|
49
|
+
"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/#{version}/Chart.min.js\"></script>"
|
50
|
+
else
|
51
|
+
"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/#{version}/Chart.js\"></script>"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def random_color
|
56
|
+
"##{SecureRandom.hex(3)}"
|
57
|
+
end
|
58
|
+
|
59
|
+
def random_id
|
60
|
+
SecureRandom.uuid
|
61
|
+
end
|
62
|
+
|
63
|
+
def script(config: to_json, id: random_id)
|
64
|
+
"<script>
|
65
|
+
var config = #{config}
|
66
|
+
window.onload = function() {
|
67
|
+
var ctx = document.getElementById(\"#{id}\").getContext(\"2d\");
|
68
|
+
new Chart(ctx, config);
|
69
|
+
};
|
70
|
+
</script>"
|
71
|
+
end
|
72
|
+
|
73
|
+
def to_html(width: "60%", heigth: width, head: true, cdn: head, version: "2.6.0", body: true, id: random_id, script: true)
|
74
|
+
str = []
|
75
|
+
if cdn
|
76
|
+
str << "<head>#{cdn(version: version)}</head>"
|
77
|
+
end
|
78
|
+
str << "<body>" if body
|
79
|
+
str << "<div id=\"canvas-holder\" style=\"width:#{width} heigth:#{heigth}\">"
|
80
|
+
str << "<canvas id=\"#{id}\"/>"
|
81
|
+
str << "</div>"
|
82
|
+
str << "</body>" if body
|
83
|
+
str << script(id: id) if script
|
84
|
+
str.join("\n")
|
85
|
+
end
|
86
|
+
|
87
|
+
def file(path, &block)
|
88
|
+
@file = path
|
89
|
+
@file_block = block
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|