vega 0.2.1 → 0.2.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30bfea4df021c4f5b65530adbc1c83f5a07f5bf323faf0b25e817f9b8e2d56b7
4
- data.tar.gz: 06ab9a178a4edfea2a0360a95dd33eca2e3197bbdd196bb764c52114028ea511
3
+ metadata.gz: 455e0c4c1814906c620cc31ddd7ded69330f51b8722abfe23f895ab0bd0e47b0
4
+ data.tar.gz: 2264a6053aa5588decd30420935452f9c7426d707f5564ff05644e473a463296
5
5
  SHA512:
6
- metadata.gz: f6e7098cdb5660031ab98e3838c9bb68d25bf338614fd41e0fe2b31588aef2c4953cce0059a61c0338075533de5a17076a050cf75ff95e52a2a0d46611dd6954
7
- data.tar.gz: 9d79099d412b36555e9cacb5594d5022706760ea05a880606920b326b77a09f2d373433dc1f82dde49d451c3a76f035f8b6d7de1c197a1361c8735faee90919f
6
+ metadata.gz: f08e7e57fc295a1f43c5ea0766c69582e6a4ece29dec97beefce16ace22665a3b09d26b9c0c1166628a0454d67f2f0e4f12a0e64d8edf2963c7980f9cb10dc09
7
+ data.tar.gz: 9e0f2da88f1fe97adb15d8135fd66b3b55782e2c103c7f26cd0290fddc0383ff3b7b4911240da7ce6dbd72da2ad06d6e544c4d512444bccd25f3edb1121b3009
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.2.2 (2021-11-06)
2
+
3
+ - Added experimental support for exporting charts
4
+ - Removed automatic pinning for `importmap-rails` (still experimental)
5
+
1
6
  ## 0.2.1 (2021-10-24)
2
7
 
3
8
  - Added support for `nil` height
data/README.md CHANGED
@@ -26,7 +26,15 @@ The follow the instructions for how you plan to use it:
26
26
 
27
27
  ### Rails 7 / Importmap
28
28
 
29
- Add to `app/javascript/application.js`:
29
+ Add to `config/importmap.rb`:
30
+
31
+ ```ruby
32
+ pin "vega", to: "vega.js"
33
+ pin "vega-lite", to: "vega-lite.js"
34
+ pin "vega-embed", to: "vega-embed.js"
35
+ ```
36
+
37
+ And add to `app/javascript/application.js`:
30
38
 
31
39
  ```js
32
40
  import "vega"
@@ -331,11 +339,43 @@ Get the spec for a chart
331
339
  chart.spec
332
340
  ```
333
341
 
342
+ ## Exporting Charts (experimental)
343
+
344
+ Export charts to PNG, SVG, or PDF. This requires Node.js and npm 7+. Run:
345
+
346
+ ```sh
347
+ yarn add vega-cli vega-lite
348
+ ```
349
+
350
+ For PNG, use:
351
+
352
+ ```ruby
353
+ File.binwrite("chart.png", chart.to_png)
354
+ ```
355
+
356
+ For SVG, use:
357
+
358
+ ```ruby
359
+ File.binwrite("chart.svg", chart.to_svg)
360
+ ```
361
+
362
+ For PDF, use:
363
+
364
+ ```ruby
365
+ File.binwrite("chart.pdf", chart.to_pdf)
366
+ ```
367
+
334
368
  ## Content Security Policy (CSP)
335
369
 
336
370
  By default, the Vega parser uses the Function constructor, which [can cause issues with CSP](https://vega.github.io/vega/usage/interpreter/).
337
371
 
338
- For Rails 7 / Importmap, add to `app/javascript/application.js`:
372
+ For Rails 7 / Importmap, add to `config/importmap.rb`:
373
+
374
+ ```ruby
375
+ pin "vega-interpreter", to: "vega-interpreter.js"
376
+ ```
377
+
378
+ And add to `app/javascript/application.js`:
339
379
 
340
380
  ```js
341
381
  import "vega-interpreter"
@@ -50,5 +50,15 @@ module Vega
50
50
  value
51
51
  end
52
52
  end
53
+
54
+ def export(cmd)
55
+ require "open3"
56
+
57
+ pkg = cmd.start_with?("vg") ? "vega-cli" : "vega-lite"
58
+ # use --no to prevent automatic installs
59
+ stdout, stderr, status = Open3.capture3("npm", "exec", "--no", "--package=#{pkg}", "--", cmd, stdin_data: to_json, binmode: true)
60
+ raise "Command failed: #{stderr}" unless status.success? && stdout.bytesize > 0
61
+ stdout
62
+ end
53
63
  end
54
64
  end
data/lib/vega/chart.rb CHANGED
@@ -23,5 +23,17 @@ module Vega
23
23
  self
24
24
  end
25
25
  immutable_method :data
26
+
27
+ def to_png
28
+ export("vg2png")
29
+ end
30
+
31
+ def to_svg
32
+ export("vg2svg")
33
+ end
34
+
35
+ def to_pdf
36
+ export("vg2pdf")
37
+ end
26
38
  end
27
39
  end
data/lib/vega/engine.rb CHANGED
@@ -5,7 +5,6 @@ module Vega
5
5
  # for importmap
6
6
  if defined?(Importmap)
7
7
  initializer "vega.importmap", after: "importmap" do |app|
8
- app.importmap.draw(Engine.root.join("config/importmap.rb"))
9
8
  app.config.assets.precompile << "vega-embed.js"
10
9
  app.config.assets.precompile << "vega-interpreter.js"
11
10
  app.config.assets.precompile << "vega-lite.js"
@@ -29,5 +29,17 @@ module Vega
29
29
  dup.spec!(*args)
30
30
  end
31
31
  end
32
+
33
+ def to_png
34
+ export("vl2png")
35
+ end
36
+
37
+ def to_svg
38
+ export("vl2svg")
39
+ end
40
+
41
+ def to_pdf
42
+ export("vl2pdf")
43
+ end
32
44
  end
33
45
  end
data/lib/vega/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Vega
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vega
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-24 00:00:00.000000000 Z
11
+ date: 2021-11-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: andrew@ankane.org
@@ -19,7 +19,6 @@ files:
19
19
  - CHANGELOG.md
20
20
  - LICENSE.txt
21
21
  - README.md
22
- - config/importmap.rb
23
22
  - lib/vega.rb
24
23
  - lib/vega/base_chart.rb
25
24
  - lib/vega/chart.rb
data/config/importmap.rb DELETED
@@ -1 +0,0 @@
1
- pin_all_from File.expand_path("../vendor/assets/javascripts", __dir__)