plotlyjs-ruby 0.1.3 → 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: 6dfeaaac593a469ac699cab7c40f1eb814a22aebfd65f16860e658f9128b1c25
4
- data.tar.gz: ae108f8493c8593d2b3f7d4a7b54e5203a6080abb934c21355bdd781e5023724
3
+ metadata.gz: a197e55c26202f7b93aff2c9685fe4d6a08f2cdfdb2319354edee06d3b3ff205
4
+ data.tar.gz: bc4d718037ed01dbb71da967abc0d647f2ecfab73fd46d11dae1162619a2d3ca
5
5
  SHA512:
6
- metadata.gz: 602e28c90c17073db1c2ac9c335483459ae2bcc045e04b1ebf0d735e8f7aed789c081770aa4b13bbceb5da21052647f5fce20a590dbf820c654a07d3b1d486c4
7
- data.tar.gz: fbd2e219e402f681b6282b7e573942e99645a61d100777ded72d48022c29dd79949a6fc91a322f6af2ddd84c6010598acf58c18a7ee9d67aa4d5041ec3f229ff
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.3"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/plotlyjs-ruby.rb CHANGED
@@ -1,45 +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
- if ("Plotly" in window) {
37
- #{createjs}
38
- }
39
- </script>
40
- HTML
41
-
42
- html.respond_to?(:html_safe) ? html.html_safe : html
21
+ def configure
22
+ yield(configuration)
43
23
  end
44
24
  end
45
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plotlyjs-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nightswinger
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-28 00:00:00.000000000 Z
11
+ date: 2024-12-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A lightweight and straightforward Ruby wrapper for Plotly.js, enabling
14
14
  developers to create interactive, high-quality data visualizations effortlessly.
@@ -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