plotlyjs-ruby 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +36 -18
- data/lib/plotlyjs/chart.rb +40 -0
- data/lib/plotlyjs/config.rb +9 -0
- data/lib/plotlyjs/utils.rb +12 -0
- data/lib/plotlyjs/version.rb +1 -1
- data/lib/plotlyjs-ruby.rb +10 -36
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a197e55c26202f7b93aff2c9685fe4d6a08f2cdfdb2319354edee06d3b3ff205
|
4
|
+
data.tar.gz: bc4d718037ed01dbb71da967abc0d647f2ecfab73fd46d11dae1162619a2d3ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c234c6549061049dd5b602b6d648389668b4e0bb80543b53d80b83077dbc1967dba0a936d6778da488e42e185de1628c47e0ce8e6df5fa1aa402968b5a592a4c
|
7
|
+
data.tar.gz: b35dd982e5653902152675b7fc3f5ef7287d62babfb33dce8bc27d59a73fa42ad42aa56b84c57e5dd8e64c6676637187920e30495321e90dc1ce2696ae0a4baf
|
data/README.md
CHANGED
@@ -1,35 +1,53 @@
|
|
1
|
-
# Plotlyjs
|
1
|
+
# Plotlyjs Ruby
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/plotlyjs/ruby`. To experiment with that code, run `bin/console` for an interactive prompt.
|
3
|
+
A lightweight and straightforward Ruby wrapper for Plotly.js, enabling developers to create interactive, high-quality data visualizations effortlessly.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
|
7
|
+
1. Add this line to your application's Gemfile:
|
10
8
|
|
11
|
-
|
9
|
+
```ruby
|
10
|
+
gem "plotlyjs-ruby"
|
11
|
+
```
|
12
12
|
|
13
|
-
|
13
|
+
2. Run the following command to pin plotly.js-dist in the import map:
|
14
14
|
|
15
|
-
|
15
|
+
```bash
|
16
|
+
bin/importmap pin plotly.js-dist
|
17
|
+
```
|
16
18
|
|
17
|
-
|
19
|
+
3. Add the following code to `app/javascript/application.js`:
|
18
20
|
|
19
|
-
|
21
|
+
```js
|
22
|
+
import Plotly from "plotly.js-dist"
|
20
23
|
|
21
|
-
|
24
|
+
window.Plotly = Plotly
|
25
|
+
```
|
26
|
+
|
27
|
+
## Usage
|
22
28
|
|
23
|
-
|
29
|
+
### Basic Usage
|
24
30
|
|
25
|
-
|
31
|
+
To create a basic plot, use the following code in your view:
|
26
32
|
|
27
|
-
|
33
|
+
```erb
|
34
|
+
<%= Plotlyjs.new_plot(
|
35
|
+
[{ x: [1, 2, 3, 4, 5] }, { y: [1, 2, 4, 8, 16]}],
|
36
|
+
{ width: 600, height: 250, margin: { t: 0 } }
|
37
|
+
) %>
|
38
|
+
```
|
28
39
|
|
29
|
-
|
40
|
+
### Global Configuration
|
30
41
|
|
31
|
-
|
42
|
+
To set options for all of your charts, create an initializer file `config/initializers/plotlyjs-ruby.rb` with:
|
32
43
|
|
33
|
-
|
44
|
+
```ruby
|
45
|
+
Plotlyjs.configure do |config|
|
46
|
+
config.options = {
|
47
|
+
responsive: true,
|
48
|
+
displayModeBar: false
|
49
|
+
}
|
50
|
+
end
|
51
|
+
```
|
34
52
|
|
35
|
-
|
53
|
+
For more details, follow the [Plotly Getting Started Guide](https://plot.ly/javascript/getting-started/).
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Plotlyjs
|
2
|
+
class Chart
|
3
|
+
attr_reader :data, :layout, :config
|
4
|
+
|
5
|
+
def initialize(data, layout, config)
|
6
|
+
@data = data
|
7
|
+
@layout = layout
|
8
|
+
@config = config
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_html
|
12
|
+
element_id = "plotly-chart-#{self.object_id}"
|
13
|
+
|
14
|
+
js_vars = {
|
15
|
+
data: data.to_json,
|
16
|
+
layout: layout.to_json,
|
17
|
+
config: config.to_json
|
18
|
+
}
|
19
|
+
|
20
|
+
createjs = "Plotly.newPlot('#{element_id}', %{data}, %{layout}, %{config});" % js_vars
|
21
|
+
|
22
|
+
html = <<~HTML
|
23
|
+
<div id="#{element_id}"></div>
|
24
|
+
<script>
|
25
|
+
(function() {
|
26
|
+
var createChart = function() { #{createjs} };
|
27
|
+
|
28
|
+
if ("Plotly" in window) {
|
29
|
+
createChart();
|
30
|
+
} else {
|
31
|
+
window.addEventListener('load', createChart);
|
32
|
+
};
|
33
|
+
})();
|
34
|
+
</script>
|
35
|
+
HTML
|
36
|
+
|
37
|
+
html.respond_to?(:html_safe) ? html.html_safe : html
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/plotlyjs/version.rb
CHANGED
data/lib/plotlyjs-ruby.rb
CHANGED
@@ -1,51 +1,25 @@
|
|
1
1
|
require 'json'
|
2
2
|
|
3
|
+
require_relative 'plotlyjs/chart'
|
4
|
+
require_relative 'plotlyjs/config'
|
5
|
+
require_relative 'plotlyjs/utils'
|
3
6
|
require_relative 'plotlyjs/version'
|
4
7
|
|
5
8
|
module Plotlyjs
|
6
9
|
class << self
|
7
10
|
def new_plot(data, layout = {}, config = {})
|
8
|
-
|
11
|
+
merged_config = Plotlyjs::Utils.deep_merge(configuration.options, config)
|
12
|
+
|
13
|
+
chart = Chart.new(data, layout, merged_config)
|
9
14
|
chart.to_html
|
10
15
|
end
|
11
|
-
end
|
12
16
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
def initialize(data, layout, config)
|
17
|
-
@data = data
|
18
|
-
@layout = layout
|
19
|
-
@config = config
|
17
|
+
def configuration
|
18
|
+
@configuration ||= Config.new
|
20
19
|
end
|
21
20
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
25
|
-
js_vars = {
|
26
|
-
data: data.to_json,
|
27
|
-
layout: layout.to_json,
|
28
|
-
config: config.to_json
|
29
|
-
}
|
30
|
-
|
31
|
-
createjs = "Plotly.newPlot('#{element_id}', %{data}, %{layout}, %{config});" % js_vars
|
32
|
-
|
33
|
-
html = <<~HTML
|
34
|
-
<div id="#{element_id}"></div>
|
35
|
-
<script>
|
36
|
-
(function() {
|
37
|
-
var createChart = function() { #{createjs} };
|
38
|
-
|
39
|
-
if ("Plotly" in window) {
|
40
|
-
createChart();
|
41
|
-
} else {
|
42
|
-
window.addEventListener('load', createChart);
|
43
|
-
};
|
44
|
-
})();
|
45
|
-
</script>
|
46
|
-
HTML
|
47
|
-
|
48
|
-
html.respond_to?(:html_safe) ? html.html_safe : html
|
21
|
+
def configure
|
22
|
+
yield(configuration)
|
49
23
|
end
|
50
24
|
end
|
51
25
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: plotlyjs-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nightswinger
|
@@ -25,6 +25,9 @@ files:
|
|
25
25
|
- README.md
|
26
26
|
- Rakefile
|
27
27
|
- lib/plotlyjs-ruby.rb
|
28
|
+
- lib/plotlyjs/chart.rb
|
29
|
+
- lib/plotlyjs/config.rb
|
30
|
+
- lib/plotlyjs/utils.rb
|
28
31
|
- lib/plotlyjs/version.rb
|
29
32
|
- sig/plotlyjs/ruby.rbs
|
30
33
|
homepage: https://github.com/nightswinger/plotlyjs-ruby
|