vega 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 52225cdedf189b9d6c9b589c52fd7bb10d6c9aa1dc17685b920b60fabee42d66
4
+ data.tar.gz: 82dd13b27708aa6ad57e4358d75c4913e232c8f85b4eb2768598dc53d81f82fd
5
+ SHA512:
6
+ metadata.gz: c1ed453f8f337c3863dadafe79a8e44a3a22896fc114c6de6e9b3646908deb86b9fb3d76d826a174ee21722a1b2a4ba9905ad13b2594592d074cd3611fa37bbd
7
+ data.tar.gz: 58be98b855093e3b6f554fc094a85cf5f79dd3efd10304cb5ba0bd05749277a66fba1e4f1e2c502af401cea6b9ca7269f40bdbb6b8d6e8c37033def50bc5119f
@@ -0,0 +1,3 @@
1
+ ## 0.1.0 (2020-09-26)
2
+
3
+ - First release
@@ -0,0 +1,29 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2020, Andrew Kane.
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,304 @@
1
+ # Vega.rb
2
+
3
+ Interactive charts for Ruby, powered by [Vega](https://vega.github.io/vega/) and [Vega-Lite](https://vega.github.io/vega-lite/)
4
+
5
+ Works with Rails, iRuby, and other frameworks
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application’s Gemfile:
10
+
11
+ ```ruby
12
+ gem 'vega'
13
+ ```
14
+
15
+ The follow the instructions for how you plan to use it:
16
+
17
+ - [Rails 6 / Webpacker](#rails)
18
+ - [Rails 5 / Sprockets](#rails)
19
+ - [iRuby](#iruby)
20
+ - [Other](#other)
21
+
22
+ ### Rails 6 / Webpacker
23
+
24
+ Run:
25
+
26
+ ```sh
27
+ yarn add vega vega-lite vega-embed
28
+ ```
29
+
30
+ And add to `app/javascript/packs/application.js`:
31
+
32
+ ```js
33
+ window.vegaEmbed = require("vega-embed").default
34
+ ```
35
+
36
+ ### Rails 5 / Sprockets
37
+
38
+ Add to `app/assets/javascripts/application.js`:
39
+
40
+ ```js
41
+ //= require vega
42
+ //= require vega-lite
43
+ //= require vega-embed
44
+ ```
45
+
46
+ ### iRuby
47
+
48
+ No additional set up is needed.
49
+
50
+ ### Other
51
+
52
+ For Sinatra and other web frameworks, include the Vega JavaScript files on pages with charts:
53
+
54
+ ```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>
58
+ ```
59
+
60
+ ## Getting Started
61
+
62
+ Vega is a visualization grammar, and Vega-Lite is a high-level grammar built on top of it. We recommend using Vega-Lite by default and moving to Vega for advanced use cases.
63
+
64
+ Create visualizations by chaining together methods:
65
+
66
+ ```ruby
67
+ Vega.lite.data(data).mark("bar").height(200)
68
+ ```
69
+
70
+ There are methods for each of the top-level properties. The docs are a great source of examples:
71
+
72
+ - [Vega-Lite docs](https://vega.github.io/vega-lite/docs/)
73
+ - [Vega docs](https://vega.github.io/vega/docs/)
74
+
75
+ ## Example
76
+
77
+ Create a bar chart
78
+
79
+ ```ruby
80
+ 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
+ )
94
+ .mark(type: "bar", tooltip: true)
95
+ .encoding(
96
+ x: {field: "a", type: "ordinal"},
97
+ y: {field: "b", type: "quantitative"}
98
+ )
99
+ ```
100
+
101
+ The chart will automatically render in iRuby. For Rails, render it in your view:
102
+
103
+ ```erb
104
+ <%= Vega.lite.data(...) %>
105
+ ```
106
+
107
+ ## Vega-Lite
108
+
109
+ Start a Vega-Lite chart with:
110
+
111
+ ```ruby
112
+ Vega.lite
113
+ ```
114
+
115
+ ### Data
116
+
117
+ [Docs](https://vega.github.io/vega-lite/docs/data.html)
118
+
119
+ The data source can be an array
120
+
121
+ ```ruby
122
+ data(values: [{x: "A", y: 1}, {x: "B", y: 2}])
123
+ ```
124
+
125
+ Or a URL
126
+
127
+ ```ruby
128
+ data(url: "https://www.example.com")
129
+ ```
130
+
131
+ You can also specify the data format
132
+
133
+ ```ruby
134
+ data(url: "https://www.example.com/data.csv", format: {type: "csv"})
135
+ ```
136
+
137
+ ### Transforms
138
+
139
+ [Docs](https://vega.github.io/vega-lite/docs/transform.html)
140
+
141
+ ```ruby
142
+ transform(bin: true, field: "a", as: "binned a")
143
+ ```
144
+
145
+ ### Marks
146
+
147
+ [Docs](https://vega.github.io/vega-lite/docs/mark.html)
148
+
149
+ Bar chart
150
+
151
+ ```ruby
152
+ mark("bar")
153
+ ```
154
+
155
+ Line chart
156
+
157
+ ```ruby
158
+ mark("line")
159
+ ```
160
+
161
+ Pie chart
162
+
163
+ ```ruby
164
+ mark("pie")
165
+ ```
166
+
167
+ Area chart
168
+
169
+ ```ruby
170
+ mark("area")
171
+ ```
172
+
173
+ Enable tooltips
174
+
175
+ ```ruby
176
+ mark(type: "bar", tooltip: true)
177
+ ```
178
+
179
+ ### Encoding
180
+
181
+ [Docs](https://vega.github.io/vega-lite/docs/mark.html)
182
+
183
+ ```ruby
184
+ encoding(x: {field: "a", type: "ordinal"})
185
+ ```
186
+
187
+ ### Projection
188
+
189
+ [Docs](https://vega.github.io/vega-lite/docs/projection.html)
190
+
191
+ ```ruby
192
+ projection(type: "albersUsa")
193
+ ```
194
+
195
+ ### Configuration
196
+
197
+ [Docs](https://vega.github.io/vega-lite/docs/config.html)
198
+
199
+ Set the font
200
+
201
+ ```ruby
202
+ config(font: "Helvetica")
203
+ ```
204
+
205
+ ### Top-Level Properties
206
+
207
+ [Docs](https://vega.github.io/vega-lite/docs/spec.html#top-level)
208
+
209
+ Set width and height
210
+
211
+ ```ruby
212
+ width(500).height(300)
213
+ ```
214
+
215
+ Set the background color
216
+
217
+ ```ruby
218
+ background("#000")
219
+ ```
220
+
221
+ Set padding
222
+
223
+ ```ruby
224
+ padding(5)
225
+ # or
226
+ padding(left: 5, top: 5, right: 5, bottom: 5)
227
+ ```
228
+
229
+ ### Embed Options
230
+
231
+ [Docs](https://github.com/vega/vega-embed#options)
232
+
233
+ Set embed options
234
+
235
+ ```ruby
236
+ embed_options(actions: true)
237
+ ```
238
+
239
+ ## Vega
240
+
241
+ You can also use Vega directly. In this case, you don’t need to include Vega-Lite in the JavaScript files.
242
+
243
+ Start a Vega chart with:
244
+
245
+ ```ruby
246
+ Vega.start
247
+ ```
248
+
249
+ ## Spec
250
+
251
+ You can also create a specification by hand
252
+
253
+ ```ruby
254
+ spec = {
255
+ "$schema" => "https://vega.github.io/schema/vega-lite/v4.json",
256
+ "data" => {"url" => "https://www.example.com"},
257
+ # ...
258
+ }
259
+ ```
260
+
261
+ And render it in Rails
262
+
263
+ ```erb
264
+ <%= Vega.render(spec) %>
265
+ ```
266
+
267
+ Or display it in iRuby
268
+
269
+ ```ruby
270
+ Vega.display(spec)
271
+ ```
272
+
273
+ Get the spec for a chart
274
+
275
+ ```ruby
276
+ chart.spec
277
+ ```
278
+
279
+ ## History
280
+
281
+ View the [changelog](https://github.com/ankane/vega/blob/master/CHANGELOG.md)
282
+
283
+ ## Contributing
284
+
285
+ Everyone is encouraged to help improve this project. Here are a few ways you can help:
286
+
287
+ - [Report bugs](https://github.com/ankane/vega/issues)
288
+ - Fix bugs and [submit pull requests](https://github.com/ankane/vega/pulls)
289
+ - Write, clarify, or fix documentation
290
+ - Suggest or add new features
291
+
292
+ To get started with development:
293
+
294
+ ```sh
295
+ git clone https://github.com/ankane/vega.git
296
+ cd vega
297
+ bundle install
298
+ bundle exec rake test
299
+ ```
300
+
301
+ Resources for contributors:
302
+
303
+ - [Vega specification](https://vega.github.io/vega/docs/specification/)
304
+ - [Vega-Lite specification](https://vega.github.io/vega-lite/docs/spec.html)
@@ -0,0 +1,36 @@
1
+ # stdlib
2
+ require "erb"
3
+ require "json"
4
+ require "securerandom"
5
+
6
+ # modules
7
+ require "vega/method_helpers"
8
+ require "vega/base_chart"
9
+ require "vega/chart"
10
+ require "vega/lite_chart"
11
+ require "vega/spec"
12
+ require "vega/version"
13
+
14
+ # integrations
15
+ require "vega/engine" if defined?(Rails)
16
+
17
+ module Vega
18
+ class << self
19
+ # save chart method for now
20
+ def start
21
+ Chart.new
22
+ end
23
+
24
+ def lite
25
+ LiteChart.new
26
+ end
27
+
28
+ def render(spec)
29
+ Spec.new(spec).to_s
30
+ end
31
+
32
+ def display(spec)
33
+ IRuby.display(Spec.new(spec))
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,51 @@
1
+ module Vega
2
+ class BaseChart
3
+ extend MethodHelpers
4
+
5
+ attr_reader :spec
6
+
7
+ def initialize
8
+ @spec = {
9
+ "$schema": @schema,
10
+ width: "container",
11
+ height: "container"
12
+ # maybe add later
13
+ # config: {mark: {tooltip: true}}
14
+ }
15
+ end
16
+
17
+ def embed_options!(value)
18
+ usermeta!(embedOptions: value)
19
+ self
20
+ end
21
+ immutable_method :embed_options
22
+
23
+ def to_s
24
+ Spec.new(spec).to_s
25
+ end
26
+
27
+ def to_iruby
28
+ Spec.new(spec).to_iruby
29
+ end
30
+
31
+ private
32
+
33
+ def initialize_dup(*)
34
+ # dup one-level up
35
+ @spec = @spec.transform_values(&:dup)
36
+ super
37
+ end
38
+
39
+ def data_value(value)
40
+ value = value.to_a if defined?(Rover::DataFrame) && value.is_a?(Rover::DataFrame)
41
+ case value
42
+ when Array
43
+ {values: value}
44
+ when String
45
+ {url: value}
46
+ else
47
+ value
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ module Vega
2
+ class Chart < BaseChart
3
+ # https://vega.github.io/vega/docs/specification/
4
+ scalar_methods \
5
+ :description, :background, :width, :height,
6
+ :padding, :autosize, :title, :encode
7
+
8
+ hash_methods \
9
+ :config, :usermeta
10
+
11
+ array_methods \
12
+ :signals, :scales, :projections, :axes, :legends, :marks
13
+
14
+ def initialize
15
+ @schema = "https://vega.github.io/schema/vega/v5.json"
16
+ super()
17
+ end
18
+
19
+ def data!(value)
20
+ (@spec[:data] ||= []) << data_value(value)
21
+ self
22
+ end
23
+ immutable_method :data
24
+ end
25
+ end