rbplotly 0.1.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +64 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +176 -51
  5. data/lib/plotly/assets/plotly.min.js +3620 -0
  6. data/lib/plotly/attributes.rb +221 -0
  7. data/lib/plotly/figure.rb +236 -0
  8. data/lib/plotly/html.rb +150 -0
  9. data/lib/plotly/plot.rb +11 -28
  10. data/lib/plotly/schema/plot-schema.json +57234 -0
  11. data/lib/plotly/schema.rb +96 -0
  12. data/lib/plotly/serializer.rb +49 -0
  13. data/lib/plotly/subplots.rb +118 -0
  14. data/lib/plotly/version.rb +7 -1
  15. data/lib/plotly.rb +21 -0
  16. data/lib/rbplotly.rb +3 -4
  17. metadata +34 -179
  18. data/.gitignore +0 -11
  19. data/.rspec +0 -2
  20. data/.rubocop.yml +0 -22
  21. data/.travis.yml +0 -9
  22. data/CODE_OF_CONDUCT.md +0 -49
  23. data/Gemfile +0 -4
  24. data/Guardfile +0 -12
  25. data/Rakefile +0 -9
  26. data/bin/console +0 -14
  27. data/bin/setup +0 -8
  28. data/docs/images/line_chart.png +0 -0
  29. data/examples/Bar Charts.ipynb +0 -254
  30. data/examples/Basic Usage.ipynb +0 -227
  31. data/examples/Histograms.ipynb +0 -79
  32. data/examples/Line charts.ipynb +0 -88
  33. data/examples/Pie Charts.ipynb +0 -151
  34. data/examples/Scatter Plots.ipynb +0 -224
  35. data/examples/heatmaps.ipynb +0 -150
  36. data/lib/plotly/axis.rb +0 -18
  37. data/lib/plotly/castable.rb +0 -20
  38. data/lib/plotly/client.rb +0 -45
  39. data/lib/plotly/data.rb +0 -33
  40. data/lib/plotly/exportable.rb +0 -23
  41. data/lib/plotly/layout.rb +0 -33
  42. data/lib/plotly/line.rb +0 -17
  43. data/lib/plotly/marker.rb +0 -23
  44. data/lib/plotly/offline/exportable.rb +0 -36
  45. data/lib/plotly/offline/html.rb +0 -46
  46. data/lib/plotly/offline/plotly.min.js +0 -59
  47. data/lib/plotly/offline/templates/body.erb +0 -26
  48. data/lib/plotly/offline/templates/plot.erb +0 -12
  49. data/lib/plotly/util.rb +0 -11
  50. data/rbplotly.gemspec +0 -35
@@ -1,150 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": 1,
6
- "metadata": {
7
- "collapsed": false,
8
- "scrolled": true
9
- },
10
- "outputs": [
11
- {
12
- "data": {
13
- "text/html": [
14
- "\n",
15
- " <script>\n",
16
- " requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min'] }})\n",
17
- " </script>\n",
18
- "\n",
19
- "\n",
20
- "<div id=\"538b8031-8157-4473-aaab-221d4569c87b\" style=\"height: 100%; width: 100%;\"></div>\n",
21
- "\n",
22
- "<script>\n",
23
- " require(['plotly'], function(Plotly) { \n",
24
- "Plotly.newPlot(\n",
25
- " '538b8031-8157-4473-aaab-221d4569c87b',\n",
26
- " [{\"z\":[[1,20,30],[20,1,60],[30,60,1]],\"type\":\"heatmap\"}],\n",
27
- " {},\n",
28
- " {\"linkText\":\"Export to plot.ly\",\"showLink\":true}\n",
29
- ")\n",
30
- "\n",
31
- "window.addEventListener('resize', function() {\n",
32
- " Plotly.Plots.resize(document.getElementById('538b8031-8157-4473-aaab-221d4569c87b'))\n",
33
- "})\n",
34
- " }) \n",
35
- "</script>"
36
- ],
37
- "text/plain": [
38
- "#<Plotly::Offline::HTML:0x007ff4ac144a30 @id=\"538b8031-8157-4473-aaab-221d4569c87b\", @data=[{:z=>[[1, 20, 30], [20, 1, 60], [30, 60, 1]], :type=>:heatmap}], @layout={}, @config={:linkText=>\"Export to plot.ly\", :showLink=>true}, @embedded=true>"
39
- ]
40
- },
41
- "metadata": {},
42
- "output_type": "display_data"
43
- }
44
- ],
45
- "source": [
46
- "# Basic Heatmap\n",
47
- "require 'rbplotly'\n",
48
- "\n",
49
- "z = [\n",
50
- " [1, 20, 30],\n",
51
- " [20, 1, 60],\n",
52
- " [30, 60, 1]\n",
53
- "]\n",
54
- "\n",
55
- "data = [{\n",
56
- " z: z,\n",
57
- " type: :heatmap\n",
58
- "}]\n",
59
- "\n",
60
- "plot = Plotly::Plot.new(data: data)\n",
61
- "plot.show"
62
- ]
63
- },
64
- {
65
- "cell_type": "code",
66
- "execution_count": 2,
67
- "metadata": {
68
- "collapsed": false
69
- },
70
- "outputs": [
71
- {
72
- "data": {
73
- "text/html": [
74
- "\n",
75
- " <script>\n",
76
- " requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min'] }})\n",
77
- " </script>\n",
78
- "\n",
79
- "\n",
80
- "<div id=\"30fe907e-b3ae-4b61-ae76-e40bd75080c4\" style=\"height: 100%; width: 100%;\"></div>\n",
81
- "\n",
82
- "<script>\n",
83
- " require(['plotly'], function(Plotly) { \n",
84
- "Plotly.newPlot(\n",
85
- " '30fe907e-b3ae-4b61-ae76-e40bd75080c4',\n",
86
- " [{\"z\":[[0,6,5,5,3,9,6,6,9,7],[6,3,2,5,9,1,4,6,7,4],[7,6,8,2,1,0,0,1,3,7],[6,8,8,4,3,1,0,8,5,3],[1,2,4,0,2,6,4,2,7,6],[3,2,9,6,2,7,5,6,1,5],[2,0,3,4,4,8,6,9,2,9],[3,5,0,6,7,0,8,6,6,0],[0,8,3,9,7,6,5,0,9,9],[6,4,3,5,2,8,2,8,6,8]],\"type\":\"heatmap\",\"x\":[\"x1\",\"x2\",\"x3\",\"x4\",\"x5\",\"x6\",\"x7\",\"x8\",\"x9\",\"x10\"],\"y\":[\"y1\",\"y2\",\"y3\",\"y4\",\"y5\",\"y6\",\"y7\",\"y8\",\"y9\",\"y10\"]}],\n",
87
- " {\"width\":500,\"height\":500},\n",
88
- " {\"linkText\":\"Export to plot.ly\",\"showLink\":true}\n",
89
- ")\n",
90
- "\n",
91
- "window.addEventListener('resize', function() {\n",
92
- " Plotly.Plots.resize(document.getElementById('30fe907e-b3ae-4b61-ae76-e40bd75080c4'))\n",
93
- "})\n",
94
- " }) \n",
95
- "</script>"
96
- ],
97
- "text/plain": [
98
- "#<Plotly::Offline::HTML:0x007ff4ac82e0d0 @id=\"30fe907e-b3ae-4b61-ae76-e40bd75080c4\", @data=[{:z=>[[0, 6, 5, 5, 3, 9, 6, 6, 9, 7], [6, 3, 2, 5, 9, 1, 4, 6, 7, 4], [7, 6, 8, 2, 1, 0, 0, 1, 3, 7], [6, 8, 8, 4, 3, 1, 0, 8, 5, 3], [1, 2, 4, 0, 2, 6, 4, 2, 7, 6], [3, 2, 9, 6, 2, 7, 5, 6, 1, 5], [2, 0, 3, 4, 4, 8, 6, 9, 2, 9], [3, 5, 0, 6, 7, 0, 8, 6, 6, 0], [0, 8, 3, 9, 7, 6, 5, 0, 9, 9], [6, 4, 3, 5, 2, 8, 2, 8, 6, 8]], :type=>:heatmap, :x=>[\"x1\", \"x2\", \"x3\", \"x4\", \"x5\", \"x6\", \"x7\", \"x8\", \"x9\", \"x10\"], :y=>[\"y1\", \"y2\", \"y3\", \"y4\", \"y5\", \"y6\", \"y7\", \"y8\", \"y9\", \"y10\"]}], @layout={:width=>500, :height=>500}, @config={:linkText=>\"Export to plot.ly\", :showLink=>true}, @embedded=true>"
99
- ]
100
- },
101
- "metadata": {},
102
- "output_type": "display_data"
103
- }
104
- ],
105
- "source": [
106
- "require 'rbplotly'\n",
107
- "\n",
108
- "n = 10\n",
109
- "data = {\n",
110
- " x: (1..n).map { |i| \"x#{i}\" },\n",
111
- " y: (1..n).map { |i| \"y#{i}\" },\n",
112
- " z: (1..n).map { (1..n).map { rand(10) } },\n",
113
- " type: :heatmap\n",
114
- "}\n",
115
- "\n",
116
- "layout = {\n",
117
- " width: 500,\n",
118
- " height: 500\n",
119
- "}\n",
120
- "\n",
121
- "plot = Plotly::Plot.new(data: [data], layout: layout)\n",
122
- "plot.show"
123
- ]
124
- },
125
- {
126
- "cell_type": "code",
127
- "execution_count": null,
128
- "metadata": {
129
- "collapsed": true
130
- },
131
- "outputs": [],
132
- "source": []
133
- }
134
- ],
135
- "metadata": {
136
- "kernelspec": {
137
- "display_name": "Ruby 2.3.1",
138
- "language": "ruby",
139
- "name": "ruby"
140
- },
141
- "language_info": {
142
- "file_extension": ".rb",
143
- "mimetype": "application/x-ruby",
144
- "name": "ruby",
145
- "version": "2.3.1"
146
- }
147
- },
148
- "nbformat": 4,
149
- "nbformat_minor": 1
150
- }
data/lib/plotly/axis.rb DELETED
@@ -1,18 +0,0 @@
1
- require 'plotly/castable'
2
-
3
- module Plotly
4
- class Axis
5
- include Castable
6
-
7
- ATTRS = %i(title range zeroline).freeze
8
- attr_accessor(*ATTRS)
9
-
10
- # @option opts [String] title
11
- # @option opts [Array] range
12
- # @option opts [Boolean] zeroline
13
- def initialize(opts)
14
- return unless opts.is_a?(Hash)
15
- opts.each { |k, v| instance_variable_set("@#{k}", v) }
16
- end
17
- end
18
- end
@@ -1,20 +0,0 @@
1
- module Plotly
2
- module Castable
3
- def to_h
4
- instance_variables.map do |attribute|
5
- k = attribute.to_s.delete('@').to_sym
6
- v = instance_variable_get(attribute)
7
-
8
- v = v.to_h if in_plotly_module?(v.class)
9
-
10
- [k, v]
11
- end.to_h
12
- end
13
-
14
- private
15
-
16
- def in_plotly_module?(klazz)
17
- klazz.to_s.split('::').first == 'Plotly'
18
- end
19
- end
20
- end
data/lib/plotly/client.rb DELETED
@@ -1,45 +0,0 @@
1
- require 'faraday'
2
- require 'base64'
3
- require 'json'
4
-
5
- module Plotly
6
- class Client
7
- attr_reader :conn
8
-
9
- # @param username [String]
10
- # @param api_key [String]
11
- def initialize(username, api_key)
12
- @conn = Faraday.new(
13
- url: 'https://api.plot.ly/v2',
14
- headers: build_headers(username, api_key)
15
- )
16
- end
17
-
18
- private
19
-
20
- # @param username [String]
21
- # @param api_key [String]
22
- # @return [Hash] HTTP headers
23
- def build_headers(username, api_key)
24
- encoded_auth = Base64.encode64("#{username}:#{api_key}")
25
- {
26
- 'plotly-client-platform' => "Ruby #{Plotly::VERSION}",
27
- 'content-type' => 'application/json',
28
- authorization: "Basic #{encoded_auth}"
29
- }
30
- end
31
- end
32
-
33
- class << self
34
- # Set the default client which is automatically used when any clients aren't assigned.
35
- def auth(username, api_key)
36
- @client = Client.new(username, api_key)
37
- end
38
-
39
- # @return [Plotly::Client]
40
- # @raise [RuntimeError]
41
- def client
42
- @client ? @client : raise('Authentication required')
43
- end
44
- end
45
- end
data/lib/plotly/data.rb DELETED
@@ -1,33 +0,0 @@
1
- require 'plotly/castable'
2
- require 'plotly/marker'
3
-
4
- module Plotly
5
- class Data
6
- include Castable
7
-
8
- attr_accessor :x, :y, :z, :values, :labels, :mode, :type, :name, :text, :colorscale, :marker
9
-
10
- # @option opts [Array] x
11
- # @option opts [Array] y
12
- # @option opts [Array] z
13
- # @option opts [Array] values
14
- # @option opts [Array] labels
15
- # @option opts [String] mode
16
- # @option opts [String] type
17
- # @option opts [String] name
18
- # @option opts [Array] text array of strings
19
- # @option opts [Array] colorscale
20
- # @option opts [Hash or Plotly::Marker] marker
21
- def initialize(opts = {})
22
- opts.each do |k, v|
23
- v.convert_to(Plotly::Marker) if k == marker
24
- instance_variable_set("@#{k}", v)
25
- end
26
- end
27
-
28
- def marker=(marker)
29
- marker.convert_to(Plotly::Marker)
30
- @marker = marker
31
- end
32
- end
33
- end
@@ -1,23 +0,0 @@
1
- module Plotly
2
- module Exportable
3
- # @option format [String]
4
- # @option path [String]
5
- # @option width [FIxnum]
6
- # @option height [FIxnum]
7
- # @option scale [FIxnum]
8
- # @option client [Plotly::Client]
9
- def download_image(format: :png, path: 'plot.png', client: ::Plotly.client,
10
- width: nil, height: nil, scale: nil)
11
- payload = {
12
- figure: { data: @data.map(&:to_h), layout: @layout.to_h },
13
- format: format
14
- }
15
- payload[:height] = height unless height.nil?
16
- payload[:width] = width unless width.nil?
17
- payload[:scale] = scale unless scale.nil?
18
-
19
- res = client.conn.post('images', payload.to_json)
20
- IO.binwrite(path, res.body)
21
- end
22
- end
23
- end
data/lib/plotly/layout.rb DELETED
@@ -1,33 +0,0 @@
1
- require 'plotly/castable'
2
- require 'plotly/axis'
3
-
4
- module Plotly
5
- class Layout
6
- include Castable
7
-
8
- ATTRS = %i(title barmode xaxis yaxis width height).freeze
9
- attr_accessor(*ATTRS)
10
-
11
- AXISES = %i(xaxis yaxis).freeze
12
-
13
- # @option opts [String] title
14
- # @option opts [String] barmode
15
- # @option opts [Hash] xaxis
16
- # @option opts [Hash] yaxis
17
- def initialize(opts = {})
18
- return unless opts.is_a?(Hash)
19
-
20
- opts.each do |k, v|
21
- v.convert_to(Plotly::Axis) if AXISES.include?(k)
22
- instance_variable_set("@#{k}", v)
23
- end
24
- end
25
-
26
- AXISES.each do |axis_name|
27
- define_method("#{axis_name}=") do |axis|
28
- axis.convert_to(Plotly::Axis)
29
- instance_variable_set("@#{axis_name}", axis)
30
- end
31
- end
32
- end
33
- end
data/lib/plotly/line.rb DELETED
@@ -1,17 +0,0 @@
1
- require 'plotly/castable'
2
-
3
- module Plotly
4
- class Line
5
- include Castable
6
-
7
- ATTRS = %i(width color).freeze
8
- attr_accessor(*ATTRS)
9
-
10
- # @option opts [Fixnum] width
11
- # @option opts [String] color 'rgba(0, 0, 200, .5)'
12
- def initialize(opts)
13
- return unless opts.is_a?(Hash)
14
- opts.each { |k, v| instance_variable_set("@#{k}", v) }
15
- end
16
- end
17
- end
data/lib/plotly/marker.rb DELETED
@@ -1,23 +0,0 @@
1
- require 'plotly/castable'
2
- require 'plotly/line'
3
-
4
- module Plotly
5
- class Marker
6
- include Castable
7
-
8
- ATTRS = %i(size color opacity line).freeze
9
- attr_accessor(*ATTRS)
10
-
11
- # @option opts [Fixnum] size
12
- # @option opts [String] color e.g.'rgba(0, 0, 200, .5)'
13
- # @option opts [Hash or Plotly::Line] line
14
- def initialize(opts)
15
- return unless opts.is_a?(Hash)
16
-
17
- opts.each do |k, v|
18
- v.convert_to(Plotly::Line) if k.to_sym == :line
19
- instance_variable_set("@#{k}", v)
20
- end
21
- end
22
- end
23
- end
@@ -1,36 +0,0 @@
1
- require 'uuidtools'
2
- require 'launchy'
3
- require 'plotly/offline/html'
4
-
5
- module Plotly
6
- module Offline
7
- module Exportable
8
- # for IRuby
9
- def show
10
- # @todo unless defined? IRuby
11
- html = create_html(@data, layout: @layout, embedded: true)
12
- IRuby.display(html)
13
- end
14
-
15
- # @param path [String]
16
- # @param open [Boolean]
17
- def generate_html(path: 'plot.html', open: true)
18
- html = create_html(@data, layout: @layout)
19
- File.write(path, html.render)
20
- Launchy.open(File.absolute_path(path)) if open
21
- end
22
-
23
- def to_html
24
- html = create_html(@data, layout: @layout, embedded: true)
25
- html.render
26
- end
27
-
28
- private
29
-
30
- def create_html(data, layout: {}, embedded: false)
31
- id = UUIDTools::UUID.random_create.to_s
32
- HTML.new(id, data.map(&:to_h), layout: layout.to_h, embedded: embedded)
33
- end
34
- end
35
- end
36
- end
@@ -1,46 +0,0 @@
1
- require 'erb'
2
- require 'json'
3
-
4
- module Plotly
5
- module Offline
6
- class HTML
7
- DEFAULT_CONFIG = { linkText: 'Export to plot.ly', showLink: true }.freeze
8
- PLOTLY_JS_CDN_URL = 'https://cdn.plot.ly/plotly-latest.min'.freeze
9
- LOCAL_PLOTLY_JS = File.read(File.expand_path('../plotly.min.js', __FILE__))
10
-
11
- # @param id [String]
12
- # @param data [Array]
13
- # @option layout [Hash]
14
- # @option config [Hash]
15
- # @option embedded [Boolean]
16
- def initialize(id, data, layout: {}, config: DEFAULT_CONFIG, embedded: false)
17
- @id = id
18
- @data = data
19
- @layout = layout
20
- @config = config
21
- @embedded = embedded
22
- end
23
-
24
- # @return [String] HTML document
25
- def render
26
- template_name = @embedded ? :body : :plot
27
- template = template_for(template_name)
28
- erb = ERB.new(template)
29
- erb.result(binding)
30
- end
31
-
32
- # This method is used to embed plots into IRuby notebooks.
33
- # @return [Array]
34
- def to_iruby
35
- ['text/html', render]
36
- end
37
-
38
- private
39
-
40
- def template_for(name)
41
- template_path = File.expand_path("../templates/#{name}.erb", __FILE__)
42
- File.read(template_path)
43
- end
44
- end
45
- end
46
- end