vega 0.1.0 → 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: 52225cdedf189b9d6c9b589c52fd7bb10d6c9aa1dc17685b920b60fabee42d66
4
- data.tar.gz: 82dd13b27708aa6ad57e4358d75c4913e232c8f85b4eb2768598dc53d81f82fd
3
+ metadata.gz: 81ef88e1766446a7d367331b58abcc2113a05cc3a914891dc81f5f82a026070f
4
+ data.tar.gz: 31f67095fcebf1cafa5bf8e50b81bf8a6830082d43bb383a7b6c299034907d24
5
5
  SHA512:
6
- metadata.gz: c1ed453f8f337c3863dadafe79a8e44a3a22896fc114c6de6e9b3646908deb86b9fb3d76d826a174ee21722a1b2a4ba9905ad13b2594592d074cd3611fa37bbd
7
- data.tar.gz: 58be98b855093e3b6f554fc094a85cf5f79dd3efd10304cb5ba0bd05749277a66fba1e4f1e2c502af401cea6b9ca7269f40bdbb6b8d6e8c37033def50bc5119f
6
+ metadata.gz: fbd22ed80b972dbbc1ab134755a4903b255b328963a8533e7e7cae29403cd1ac81aca3f84f8a0b0f42e93515a18d85a5ec582193a3a1d020f79527a66b8fd1aa
7
+ data.tar.gz: 19fad72d73ceddfaf669e3a0c0a970bdaeac5996018522e3acffca488533723e4e79a9609741399aefc1480a74891ac8f2b4db1a62540f107694fcc3e25e0a8d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,22 @@
1
+ ## 0.2.0 (2021-10-23)
2
+
3
+ - Added experimental support for `importmap-rails`
4
+ - Updated Vega to 5.21.0, Vega-Lite to 5.1.1, and Vega-Embed to 6.19.1
5
+
6
+ ## 0.1.3 (2021-03-14)
7
+
8
+ - Added Vega-Interpreter
9
+
10
+ ## 0.1.2 (2021-02-09)
11
+
12
+ - Updated Vega to 5.19.1, Vega-Lite to 4.17.0, and Vega-Embed to 6.15.1
13
+
14
+ ## 0.1.1 (2020-09-27)
15
+
16
+ - Added `repeat`, `resolve`, `spec` and `selection` methods
17
+ - Added `hconcat` and `vconcat` methods
18
+ - Added support for Daru data frames
19
+
1
20
  ## 0.1.0 (2020-09-26)
2
21
 
3
22
  - First release
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  BSD 3-Clause License
2
2
 
3
- Copyright (c) 2020, Andrew Kane.
3
+ Copyright (c) 2020-2021, Andrew Kane.
4
4
  All rights reserved.
5
5
 
6
6
  Redistribution and use in source and binary forms, with or without
data/README.md CHANGED
@@ -2,8 +2,12 @@
2
2
 
3
3
  Interactive charts for Ruby, powered by [Vega](https://vega.github.io/vega/) and [Vega-Lite](https://vega.github.io/vega-lite/)
4
4
 
5
+ [See it in action](https://vega.dokkuapp.com)
6
+
5
7
  Works with Rails, iRuby, and other frameworks
6
8
 
9
+ [![Build Status](https://github.com/ankane/vega-ruby/workflows/build/badge.svg?branch=master)](https://github.com/ankane/vega-ruby/actions)
10
+
7
11
  ## Installation
8
12
 
9
13
  Add this line to your application’s Gemfile:
@@ -14,11 +18,24 @@ gem 'vega'
14
18
 
15
19
  The follow the instructions for how you plan to use it:
16
20
 
17
- - [Rails 6 / Webpacker](#rails)
18
- - [Rails 5 / Sprockets](#rails)
21
+ - [Rails 7 / Importmap](#rails-7--importmap) (experimental)
22
+ - [Rails 6 / Webpacker](#rails-6--webpacker)
23
+ - [Rails 5 / Sprockets](#rails-5--sprockets)
19
24
  - [iRuby](#iruby)
20
25
  - [Other](#other)
21
26
 
27
+ ### Rails 7 / Importmap
28
+
29
+ Add to `app/javascript/application.js`:
30
+
31
+ ```js
32
+ import "vega"
33
+ import "vega-lite"
34
+ import "vega-embed"
35
+
36
+ window.dispatchEvent(new Event("vega:load"));
37
+ ```
38
+
22
39
  ### Rails 6 / Webpacker
23
40
 
24
41
  Run:
@@ -52,9 +69,9 @@ No additional set up is needed.
52
69
  For Sinatra and other web frameworks, include the Vega JavaScript files on pages with charts:
53
70
 
54
71
  ```html
55
- <script src="https://cdn.jsdelivr.net/npm/vega@5.16.1"></script>
56
- <script src="https://cdn.jsdelivr.net/npm/vega-lite@4.16.2"></script>
57
- <script src="https://cdn.jsdelivr.net/npm/vega-embed@6.12.2"></script>
72
+ <script src="https://cdn.jsdelivr.net/npm/vega@5.21.0"></script>
73
+ <script src="https://cdn.jsdelivr.net/npm/vega-lite@5.1.1"></script>
74
+ <script src="https://cdn.jsdelivr.net/npm/vega-embed@6.19.1"></script>
58
75
  ```
59
76
 
60
77
  ## Getting Started
@@ -78,23 +95,11 @@ Create a bar chart
78
95
 
79
96
  ```ruby
80
97
  Vega.lite
81
- .data(
82
- values: [
83
- {a: "A", b: 28},
84
- {a: "B", b: 55},
85
- {a: "C", b: 43},
86
- {a: "D", b: 91},
87
- {a: "E", b: 81},
88
- {a: "F", b: 53},
89
- {a: "G", b: 19},
90
- {a: "H", b: 87},
91
- {a: "I", b: 52}
92
- ]
93
- )
98
+ .data([{city: "A", sales: 28}, {city: "B", sales: 55}, {city: "C", sales: 43}])
94
99
  .mark(type: "bar", tooltip: true)
95
100
  .encoding(
96
- x: {field: "a", type: "ordinal"},
97
- y: {field: "b", type: "quantitative"}
101
+ x: {field: "city", type: "nominal"},
102
+ y: {field: "sales", type: "quantitative"}
98
103
  )
99
104
  ```
100
105
 
@@ -116,22 +121,28 @@ Vega.lite
116
121
 
117
122
  [Docs](https://vega.github.io/vega-lite/docs/data.html)
118
123
 
119
- The data source can be an array
124
+ Data can be an array
120
125
 
121
126
  ```ruby
122
- data(values: [{x: "A", y: 1}, {x: "B", y: 2}])
127
+ data([{x: "A", y: 1}, {x: "B", y: 2}])
123
128
  ```
124
129
 
125
130
  Or a URL
126
131
 
127
132
  ```ruby
128
- data(url: "https://www.example.com")
133
+ data("https://www.example.com/data.json")
129
134
  ```
130
135
 
131
- You can also specify the data format
136
+ Or a Rover data frame
132
137
 
133
138
  ```ruby
134
- data(url: "https://www.example.com/data.csv", format: {type: "csv"})
139
+ data(df)
140
+ ```
141
+
142
+ Or a data generator
143
+
144
+ ```ruby
145
+ data(sequence: {start: 0, stop: 10, step: 1, as: "x"})
135
146
  ```
136
147
 
137
148
  ### Transforms
@@ -192,7 +203,51 @@ encoding(x: {field: "a", type: "ordinal"})
192
203
  projection(type: "albersUsa")
193
204
  ```
194
205
 
195
- ### Configuration
206
+ ### View Composition
207
+
208
+ [Docs](https://vega.github.io/vega-lite/docs/composition.html)
209
+
210
+ Faceting
211
+
212
+ ```ruby
213
+ facet(row: {field: "x"})
214
+ ```
215
+
216
+ Layering
217
+
218
+ ```ruby
219
+ layer(view)
220
+ ```
221
+
222
+ Concatenation
223
+
224
+ ```ruby
225
+ hconcat(view)
226
+ vconcat(view)
227
+ concat(view)
228
+ ```
229
+
230
+ Repeating
231
+
232
+ ```ruby
233
+ repeat(row: ["a", "b", "c"])
234
+ ```
235
+
236
+ Resolving
237
+
238
+ ```ruby
239
+ resolve(scale: {color: "independent"})
240
+ ```
241
+
242
+ ### Selections
243
+
244
+ [Docs](https://vega.github.io/vega-lite/docs/selection.html)
245
+
246
+ ```ruby
247
+ selection(x: {type: "single"})
248
+ ```
249
+
250
+ ### Config
196
251
 
197
252
  [Docs](https://vega.github.io/vega-lite/docs/config.html)
198
253
 
@@ -276,24 +331,52 @@ Get the spec for a chart
276
331
  chart.spec
277
332
  ```
278
333
 
334
+ ## Content Security Policy (CSP)
335
+
336
+ By default, the Vega parser uses the Function constructor, which [can cause issues with CSP](https://vega.github.io/vega/usage/interpreter/).
337
+
338
+ For Rails 7 / Importmap, add to `app/javascript/application.js`:
339
+
340
+ ```js
341
+ import "vega-interpreter"
342
+ ```
343
+
344
+ For Rails 6 / Webpacker, run:
345
+
346
+ ```sh
347
+ yarn add vega-interpreter
348
+ ```
349
+
350
+ For Rails 5 / Sprockets, add to `app/assets/javascripts/application.js`:
351
+
352
+ ```js
353
+ //= require vega-interpreter
354
+ ```
355
+
356
+ And set embed options for your charts
357
+
358
+ ```ruby
359
+ embed_options(ast: true)
360
+ ```
361
+
279
362
  ## History
280
363
 
281
- View the [changelog](https://github.com/ankane/vega/blob/master/CHANGELOG.md)
364
+ View the [changelog](https://github.com/ankane/vega-ruby/blob/master/CHANGELOG.md)
282
365
 
283
366
  ## Contributing
284
367
 
285
368
  Everyone is encouraged to help improve this project. Here are a few ways you can help:
286
369
 
287
- - [Report bugs](https://github.com/ankane/vega/issues)
288
- - Fix bugs and [submit pull requests](https://github.com/ankane/vega/pulls)
370
+ - [Report bugs](https://github.com/ankane/vega-ruby/issues)
371
+ - Fix bugs and [submit pull requests](https://github.com/ankane/vega-ruby/pulls)
289
372
  - Write, clarify, or fix documentation
290
373
  - Suggest or add new features
291
374
 
292
375
  To get started with development:
293
376
 
294
377
  ```sh
295
- git clone https://github.com/ankane/vega.git
296
- cd vega
378
+ git clone https://github.com/ankane/vega-ruby.git
379
+ cd vega-ruby
297
380
  bundle install
298
381
  bundle exec rake test
299
382
  ```
@@ -0,0 +1 @@
1
+ pin_all_from File.expand_path("../vendor/assets/javascripts", __dir__)
@@ -2,8 +2,6 @@ module Vega
2
2
  class BaseChart
3
3
  extend MethodHelpers
4
4
 
5
- attr_reader :spec
6
-
7
5
  def initialize
8
6
  @spec = {
9
7
  "$schema": @schema,
@@ -28,6 +26,10 @@ module Vega
28
26
  Spec.new(spec).to_iruby
29
27
  end
30
28
 
29
+ def to_json
30
+ spec.to_json
31
+ end
32
+
31
33
  private
32
34
 
33
35
  def initialize_dup(*)
@@ -38,6 +40,7 @@ module Vega
38
40
 
39
41
  def data_value(value)
40
42
  value = value.to_a if defined?(Rover::DataFrame) && value.is_a?(Rover::DataFrame)
43
+ value = value.to_a[0] if defined?(Daru::DataFrame) && value.is_a?(Daru::DataFrame)
41
44
  case value
42
45
  when Array
43
46
  {values: value}
data/lib/vega/chart.rb CHANGED
@@ -11,6 +11,8 @@ module Vega
11
11
  array_methods \
12
12
  :signals, :scales, :projections, :axes, :legends, :marks
13
13
 
14
+ attr_reader :spec
15
+
14
16
  def initialize
15
17
  @schema = "https://vega.github.io/schema/vega/v5.json"
16
18
  super()
data/lib/vega/engine.rb CHANGED
@@ -1,5 +1,16 @@
1
1
  module Vega
2
2
  class Engine < ::Rails::Engine
3
3
  # for assets
4
+
5
+ # for importmap
6
+ if defined?(Importmap)
7
+ initializer "vega.importmap", after: "importmap" do |app|
8
+ app.importmap.draw(Engine.root.join("config/importmap.rb"))
9
+ app.config.assets.precompile << "vega-embed.js"
10
+ app.config.assets.precompile << "vega-interpreter.js"
11
+ app.config.assets.precompile << "vega-lite.js"
12
+ app.config.assets.precompile << "vega.js"
13
+ end
14
+ end
4
15
  end
5
16
  end
@@ -2,16 +2,16 @@ module Vega
2
2
  class LiteChart < BaseChart
3
3
  # https://vega.github.io/vega-lite/docs/spec.html
4
4
  scalar_methods \
5
- :background, :padding, :autosize, :title, :name, :description, :width, :height, :mark
5
+ :background, :padding, :autosize, :title, :name, :description, :width, :height, :mark, :spec, :repeat
6
6
 
7
7
  hash_methods \
8
- :config, :usermeta, :projection, :datasets, :encoding
8
+ :config, :usermeta, :projection, :datasets, :encoding, :facet, :resolve, :selection, :view
9
9
 
10
10
  array_methods \
11
- :transform, :layer
11
+ :transform, :layer, :hconcat, :vconcat, :concat
12
12
 
13
13
  def initialize
14
- @schema = "https://vega.github.io/schema/vega-lite/v4.json"
14
+ @schema = "https://vega.github.io/schema/vega-lite/v5.json"
15
15
  super()
16
16
  end
17
17
 
@@ -20,5 +20,14 @@ module Vega
20
20
  self
21
21
  end
22
22
  immutable_method :data
23
+
24
+ undef spec
25
+ def spec(*args)
26
+ if args.empty?
27
+ @spec
28
+ else
29
+ dup.spec!(*args)
30
+ end
31
+ end
23
32
  end
24
33
  end
@@ -25,7 +25,8 @@ module Vega
25
25
  def array_methods(*methods)
26
26
  methods.each do |method|
27
27
  define_method("#{method}!") do |value|
28
- (@spec[method] ||= []) << value
28
+ value = [value] unless value.is_a?(Array)
29
+ (@spec[method] ||= []).push(*value)
29
30
  self
30
31
  end
31
32
  immutable_method(method)
data/lib/vega/spec.rb CHANGED
@@ -11,7 +11,14 @@ module Vega
11
11
  output = <<~EOS
12
12
  #{html}
13
13
  <script>
14
- #{js}
14
+ (function() {
15
+ var createChart = function() { #{js} };
16
+ if ("vegaEmbed" in window) {
17
+ createChart();
18
+ } else {
19
+ window.addEventListener("vega:load", createChart, true);
20
+ }
21
+ })();
15
22
  </script>
16
23
  EOS
17
24
  output.respond_to?(:html_safe) ? output.html_safe : output
@@ -25,12 +32,13 @@ module Vega
25
32
  <script>
26
33
  require.config({
27
34
  paths: {
28
- 'vega': 'https://cdn.jsdelivr.net/npm/vega@5.16.1?noext',
29
- 'vega-lite': 'https://cdn.jsdelivr.net/npm/vega-lite@4.16.2?noext',
30
- 'vega-embed': 'https://cdn.jsdelivr.net/npm/vega-embed@6.12.2?noext'
35
+ 'vega': 'https://cdn.jsdelivr.net/npm/vega@5.21.0?noext',
36
+ 'vega-util': 'https://cdn.jsdelivr.net/npm/vega-util@1.17.0?noext',
37
+ 'vega-lite': 'https://cdn.jsdelivr.net/npm/vega-lite@5.1.1?noext',
38
+ 'vega-embed': 'https://cdn.jsdelivr.net/npm/vega-embed@6.19.1?noext'
31
39
  }
32
40
  });
33
- require(['vega', 'vega-lite', 'vega-embed'], function(vega, vegaLite, vegaEmbed) {
41
+ require(['vega', 'vega-util', 'vega-lite', 'vega-embed'], function(vega, vegaUtil, vegaLite, vegaEmbed) {
34
42
  #{js}
35
43
  });
36
44
  </script>
data/lib/vega/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Vega
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2015-2018, University of Washington Interactive Data Lab
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+
10
+ 2. Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ 3. Neither the name of the copyright holder nor the names of its contributors
15
+ may be used to endorse or promote products derived from this software
16
+ without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -1,4 +1,4 @@
1
- Copyright (c) 2015-2018, University of Washington Interactive Data Lab
1
+ Copyright (c) 2015-2021, University of Washington Interactive Data Lab
2
2
  All rights reserved.
3
3
 
4
4
  Redistribution and use in source and binary forms, with or without