Skip to content

Commit a7feac8

Browse files
committed
Allow inline spec, add compact stringify lib.
1 parent 45e0dc7 commit a7feac8

File tree

13 files changed

+1022
-45
lines changed

13 files changed

+1022
-45
lines changed

_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ description: Vega-Lite - a high-level grammar for statistical graphics.
1616

1717
baseurl: "" # the subpath of your site, e.g. /blog/
1818
url: "http://vega.github.io/vega-lite/" # the base hostname & protocol for your site
19-
exclude: [bin, coverage, examples/_original, examples/_output, examples/_diff, Gemfile, Gemfile.lock, gulp, lib, node_modules, scripts, test, typings, package.json, bower.json, src, tsconfig.json, tslint.json, nodemon.json, CONTRIBUTING.md, README.md, LICENSE]
19+
exclude: [bin, coverage, examples/_original, examples/_output, examples/_diff, Gemfile, Gemfile.lock, gulp, lib/schema.json, node_modules, scripts, test, typings, package.json, bower.json, src, tsconfig.json, tslint.json, nodemon.json, CONTRIBUTING.md, README.md, LICENSE]
2020

2121
# Build settings
2222
markdown: redcarpet # use redcarpet to enable backtick code

_layouts/default.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
<script src="{{site.baseurl}}/vega-lite.min.js"></script>
1414
<script src="{{site.baseurl}}/bower_components/vega-embed/vega-embed.js" charset="utf-8"></script>
1515
<script src="{{site.baseurl}}/bower_components/highlightjs/highlight.pack.js" charset="utf-8"></script>
16+
<script src="{{site.baseurl}}/lib/json3-compactstringify.js"></script>
1617

17-
<script>var BASEURL = "{{site.baseurl}}";</script>
18+
<script>
19+
JSON3 = JSON3.noConflict();
20+
var BASEURL = "{{site.baseurl}}";
21+
</script>
1822
<script src="{{site.baseurl}}/site/static/docs.js" charset="utf-8"></script>
1923

2024
<title> {{site.title}}: {{page.title}} </title>

lib/json3-compactstringify.js

Lines changed: 937 additions & 0 deletions
Large diffs are not rendered by default.

site/docs/bin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ The following specification creates a Histogram.
4848
Basically, the bar marks show count of the number of data in each `bin`.
4949

5050
<div id="ex-histogram" class="side"></div>
51-
<script>example("histogram", "")</script>
51+
<script>example("histogram")</script>

site/docs/data.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Inline Data can be specified using `values` property.
3232
For example, a data table with two rows and two columns (`a` and `b`) in a spec might look like this:
3333

3434
<div id="ex-bar" class="side"></div>
35-
<script>example("bar", '', {class: 'side'})</script>
35+
<script>example("bar")</script>
3636

3737

3838
## Data from URL

site/docs/mark.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ Mapping a field to either only `x` (or only `y`) of `point` mark creates a dot p
4646
Mapping fields to both the `x` and `y` dimensions creates a scatter plot.
4747

4848
<div id="ex-scatter" class="side"></div>
49-
<script>example("scatter", "")</script>
49+
<script>example("scatter")</script>
5050

5151
By mapping a third field to the `size` channel in the [scatter plot](#scatter), we can create a bubble plot instead.
5252

5353
<div id="ex-scatter_bubble" class="side"></div>
54-
<script>example("scatter_bubble", "")</script>
54+
<script>example("scatter_bubble")</script>
5555

5656

5757
<a id="ex-scatter_color_shape"></a>
@@ -129,7 +129,7 @@ This produces a layered bar chart.
129129
To make it clear that bars are layered, we can make marks semi-transparent by setting the `opacity` to 0.6.
130130

131131
<div id="ex-bar_layered_transparent" class="side"><div class="example-vis" style="min-width: 500px"></div></div>
132-
<script>example("bar_layered_transparent", "")</script>
132+
<script>example("bar_layered_transparent")</script>
133133

134134
<!-- [Faceting](#encoding.md) a bar chart can produce a grouped bar chart. -->
135135
<!--
@@ -145,7 +145,7 @@ Using `line` with one dimension (typically on `x`) and one measure (typically on
145145

146146

147147
<div id="ex-line" class="side"></div>
148-
<script>example("line", "")</script>
148+
<script>example("line")</script>
149149

150150

151151
We can add create multiple lines by grouping along different attributes, such as `color` or `detail`.
@@ -154,7 +154,7 @@ In the example below, we group points using a new field mapped to `color`. This
154154

155155

156156
<div id="ex-line_color" class="side"></div>
157-
<script>example("line_color", "")</script>
157+
<script>example("line_color")</script>
158158

159159

160160
Alternatively, we can map the same field to `detail`, creating multiple lines, but with the same color instead.

site/docs/timeunit.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ Currently supported values are: `'year'`, `'month'`, `'day'`, `'date'`, `'hours'
3636
This example shows temperature in Seattle over the months.
3737

3838
<div id="ex-line_month" class="side"></div>
39-
<script>example("line_month", "")</script>
39+
<script>example("line_month")</script>

site/docs/transform.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ __Examples__
4343
This example use `calculate` to derive a new field, then `filter` data based on the new field.
4444

4545
<div id="ex-bar_filter_calc" class="side"></div>
46-
<script>example("bar_filter_calc", "")</script>
46+
<script>example("bar_filter_calc")</script>
4747

4848

4949
<!-- TODO population use calc to derive Male / Female -->

site/index.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,27 @@ Vega-Lite specifications consist of simple mappings of variables in a data set t
1212
This documentation outlines the syntax and semantics of Vega-Lite specifications, and how you can embed Vega-Lite visualizations in your applications.
1313

1414

15+
<div id="ex-point_1d" class="side"></div>
16+
<script>
17+
var vlSpec = {
18+
"data": {
19+
"values": [
20+
{"a":"A", "b":28}, {"a":"B", "b":55}, {"a":"C", "b":43},
21+
{"a":"D", "b":91}, {"a":"E", "b":81}, {"a":"F", "b":53},
22+
{"a":"G", "b":19}, {"a":"H", "b":87}, {"a":"I", "b":52}
23+
]
24+
},
25+
"mark": "bar",
26+
"encoding": {
27+
"x": {"type": "ordinal","field": "a"},
28+
"y": {"type": "quantitative","field": "b"}
29+
}
30+
};
31+
32+
example("point_1d", vlSpec)
33+
</script>
34+
35+
1536
Want to learn more? [Read the getting started tutorial]({{site.baseurl}}/tutorial.html).
1637

1738
## Basic usage

site/static/docs.ts

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,54 @@
1-
/* tslint:disable */
1+
/* tslint:disable:no-unused-variable */
22

33
declare const BASEURL, hljs;
44

5-
function example(name, dir) {
6-
const fullUrl = BASEURL + '/examples/' + (dir ? (dir + '/') : '') + name + '.json';
5+
function renderExample(name: string, text: string) {
6+
const target = '#ex-' + name.replace('/', '-');
7+
const $target = d3.select(target);
8+
$target.classed('example', true);
79

8-
d3.text(fullUrl, function(error, text) {
9-
const target = '#ex-' + name.replace('/', '-');
10-
const $target = d3.select(target);
11-
$target.classed('example', true);
10+
const code = $target.insert('pre', 'div.example-vis').attr('class', 'example-code')
11+
.append('code').attr('class', 'json').text(text);
12+
hljs.highlightBlock(code.node());
1213

13-
const code = $target.insert('pre', "div.example-vis").attr('class', 'example-code')
14-
.append('code').attr('class', 'json').text(text);
15-
hljs.highlightBlock(code.node());
14+
let vis = $target.select('div.example-vis');
15+
if (vis.empty()) {
16+
vis = $target.append('div').attr('class', 'example-vis');
17+
}
1618

17-
let vis = $target.select('div.example-vis');
18-
if (vis.empty()) {
19-
vis = $target.append('div').attr('class', 'example-vis');
20-
}
19+
const spec = JSON.parse(text);
20+
if (spec.data.url) {
21+
spec.data.url = BASEURL + '/' + spec.data.url;
22+
}
2123

22-
const spec = JSON.parse(text);
23-
if (spec.data.url) {
24-
spec.data.url = BASEURL + '/' + spec.data.url;
24+
vg.embed(vis.node(), {
25+
mode: 'vega-lite',
26+
spec: spec,
27+
renderer: 'svg',
28+
actions: {
29+
source: false,
30+
export: false
2531
}
32+
});
33+
}
2634

27-
vg.embed(vis.node(), {
28-
mode: 'vega-lite',
29-
spec: spec,
30-
renderer: 'svg',
31-
actions: {
32-
source: false,
33-
export: false
35+
/**
36+
* Show example in the decs. Provide the name of the example or a spec.
37+
*/
38+
function example(name: string, dirOrSpec) {
39+
if (dirOrSpec !== null && typeof dirOrSpec === 'object') {
40+
const spec = JSON3.stringify(dirOrSpec, null, 2, 80);
41+
renderExample(name, spec);
42+
} else {
43+
const dir = dirOrSpec;
44+
const fullUrl = BASEURL + '/examples/' + (dir ? (dir + '/') : '') + name + '.json';
45+
46+
d3.text(fullUrl, function(error, text) {
47+
if (error) {
48+
console.error(error);
49+
} else {
50+
renderExample(name, text);
3451
}
3552
});
36-
});
53+
}
3754
}

0 commit comments

Comments
 (0)