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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 11eedef03cecfbf3bf36ace58ada3970b107617d5dd717fdda477775acfa8e37
4
- data.tar.gz: bb3256dd8147a96913323821de39b4ce6a57e9c7451b9a1ec69193927c3fb451
3
+ metadata.gz: a197e55c26202f7b93aff2c9685fe4d6a08f2cdfdb2319354edee06d3b3ff205
4
+ data.tar.gz: bc4d718037ed01dbb71da967abc0d647f2ecfab73fd46d11dae1162619a2d3ca
5
5
  SHA512:
6
- metadata.gz: 57aec7e1bbd4157074252372607272f5bcf458311c43e2484a9289dbee948d707fdc11caf558c7e5f4d617b404c02c93416b25ad7644a922a044850709e899ed
7
- data.tar.gz: e0a1021c304196f097009cfbad65eb65d65c1a5650971bff7e2cd1485ea1aba783b3babcd2deb650b11f48c4925214cf386d5ac02c860b9bb9b35a090b632fbd
6
+ metadata.gz: c234c6549061049dd5b602b6d648389668b4e0bb80543b53d80b83077dbc1967dba0a936d6778da488e42e185de1628c47e0ce8e6df5fa1aa402968b5a592a4c
7
+ data.tar.gz: b35dd982e5653902152675b7fc3f5ef7287d62babfb33dce8bc27d59a73fa42ad42aa56b84c57e5dd8e64c6676637187920e30495321e90dc1ce2696ae0a4baf
data/README.md CHANGED
@@ -1,35 +1,53 @@
1
- # Plotlyjs::Ruby
1
+ # Plotlyjs Ruby
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
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
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
7
+ 1. Add this line to your application's Gemfile:
10
8
 
11
- Install the gem and add to the application's Gemfile by executing:
9
+ ```ruby
10
+ gem "plotlyjs-ruby"
11
+ ```
12
12
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
13
+ 2. Run the following command to pin plotly.js-dist in the import map:
14
14
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
15
+ ```bash
16
+ bin/importmap pin plotly.js-dist
17
+ ```
16
18
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
19
+ 3. Add the following code to `app/javascript/application.js`:
18
20
 
19
- ## Usage
21
+ ```js
22
+ import Plotly from "plotly.js-dist"
20
23
 
21
- TODO: Write usage instructions here
24
+ window.Plotly = Plotly
25
+ ```
26
+
27
+ ## Usage
22
28
 
23
- ## Development
29
+ ### Basic Usage
24
30
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
31
+ To create a basic plot, use the following code in your view:
26
32
 
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
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
- ## Contributing
40
+ ### Global Configuration
30
41
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/plotlyjs-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/plotlyjs-ruby/blob/master/CODE_OF_CONDUCT.md).
42
+ To set options for all of your charts, create an initializer file `config/initializers/plotlyjs-ruby.rb` with:
32
43
 
33
- ## Code of Conduct
44
+ ```ruby
45
+ Plotlyjs.configure do |config|
46
+ config.options = {
47
+ responsive: true,
48
+ displayModeBar: false
49
+ }
50
+ end
51
+ ```
34
52
 
35
- Everyone interacting in the Plotlyjs::Ruby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/plotlyjs-ruby/blob/master/CODE_OF_CONDUCT.md).
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
@@ -0,0 +1,9 @@
1
+ module Plotlyjs
2
+ class Config
3
+ attr_accessor :options
4
+
5
+ def initialize
6
+ @options = {}
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module Plotlyjs
2
+ module Utils
3
+ def self.deep_merge(hash_a, hash_b)
4
+ hash_a = hash_a.dup
5
+ hash_b.each_pair do |k, v|
6
+ tv = hash_a[k]
7
+ hash_a[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? deep_merge(tv, v) : v
8
+ end
9
+ hash_a
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module Plotlyjs
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
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
- chart = Chart.new(data, layout, config)
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
- class Chart
14
- attr_reader :data, :layout, :config
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 to_html
23
- element_id = "plotly-chart-#{self.object_id}"
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.1.4
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