Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jheer committed Aug 18, 2015
1 parent b6de69b commit 562a891
Show file tree
Hide file tree
Showing 16 changed files with 63,127 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.DS_Store
.idea
_site
bower_components
coverage
node_modules
output
temp
vega-embed.js
vega-embed.js.map
vega-embed.min.js
27 changes: 27 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2015, University of Washington Interactive Data Lab
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# vega-embed

Publish Vega visualizations as embedded web components.

## Build Process

To build `vega-embed.js` and view the test examples, you must have [npm](https://www.npmjs.com/) installed.

1. Run `npm install` in the vega-embed folder to install dependencies.
2. Run `npm run build`. This will invoke [browserify](http://browserify.org/) to bundle the source files into vega-embed.js, and then [uglify-js](http://lisperator.net/uglifyjs/) to create the minified vega-embed.min.js.
3. Run a local webserver (e.g., `python -m SimpleHTTPServer 8000`) in the vega-embed folder and then point your web browser at the test directory (e.g., `http://localhost:8000/test/`).
38 changes: 38 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "vega-embed",
"main": "vega-embed.js",
"homepage": "http://vega.github.io/vega/",
"description": "Publish Vega visualizations as embedded web components.",
"keywords": [
"vega",
"data",
"visualization",
"component",
"embed"
],
"authors": [
"Jeffrey Heer (http://idl.cs.washington.edu)"
],
"license": "BSD-3-Clause",
"dependencies": {
"d3": "^3.5.6",
"vega": "^2.1.1"
},
"ignore": [
".DS_Store",
".git",
".gitignore",
".npmignore",
".jshintrc",
".travis.yml",
"bin",
"coverage",
"examples",
"index.js",
"node_modules",
"output",
"scripts",
"src",
"test"
]
}
59 changes: 59 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": "vega-embed",
"version": "0.0.1",
"description": "Publish Vega visualizations as embedded web components.",
"keywords": [
"vega",
"data",
"visualization",
"component",
"embed"
],
"repository": {
"type": "git",
"url": "http://github.com/vega/vega-embed.git"
},
"author": {
"name": "Jeffrey Heer",
"url": "http://idl.cs.washington.edu"
},
"license": "BSD-3-Clause",
"devDependencies": {
"d3": "^3.5.6",
"topojson": "^1.6.19",
"vega": "^2.1.1",
"browserify": "^10.2.6",
"browserify-shim": "^3.8.9",
"browserify-versionify": "^1.0.4",
"chai": "^3.0.0",
"istanbul": "latest",
"jshint": "^2.8.0",
"mocha": "^2.2.5",
"uglify-js": "^2.4.24"
},
"scripts": {
"lint": "jshint src/",
"test": "mocha --timeout 30000 --recursive test/",
"cover": "istanbul cover _mocha -- --timeout 30000 --recursive test/",
"build": "browserify src/embed.js -d -s vg.embed > vega-embed.js",
"postbuild": "uglifyjs vega-embed.js -cm > vega-embed.min.js"
},
"browser": {
"buffer": false,
"canvas": false,
"fs": false,
"http": false,
"request": false,
"sync-request": false,
"url": false
},
"browserify": {
"transform": [
"browserify-shim"
]
},
"browserify-shim": {
"d3": "global:d3",
"vega": "global:vg"
}
}
102 changes: 102 additions & 0 deletions src/embed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
var d3 = require('d3'),
vg = require('vega'),
parameter = require('./param'),
post = require('./post');

var config = {
// URL for loading specs into editor
editor_url: 'http://vega.github.io/vega-editor/',

// HTML to inject within view source head element
source_header: '',

// HTML to inject before view source closing body tag
source_footer: ''
};

// Embed a Vega visualization component in a web page.
// el: DOM element in which to place component (DOM node or CSS selector)
// opt: Embedding specification (parsed JSON or string)
// callback: invoked with the generated Vega View instance
function embed(el, opt, callback) {
var source, spec, params = [];

if (opt.source) {
source = opt.source;
spec = JSON.parse(source);
} else if (opt.spec) {
spec = opt.spec;
source = JSON.stringify(spec, null, 2);
} else if (opt.url) {
vg.util.load({url: opt.url}, function(err, data) {
if (err) {
console.error(err);
} else if (!data) {
console.error('No data found at ' + opt.url);
} else {
// load code, extends options, and restart
embed(el, vg.util.extend({source: data}, opt), callback);
}
});
return;
}

// ensure container div has class 'vega-embed'
var div = d3.select(el)
.attr('class', 'vega-embed');

// handle parameters
if (opt.params) {
var pdiv = div.append('div')
.attr('class', 'vega-params');
opt.params.forEach(function(p) {
params = params.concat(parameter(pdiv, p, spec));
});
}

vg.parse.spec(spec, function(chart) {
var view = chart({el: el});

// add child div to house action links
var ctrl = div.append('div')
.attr('class', 'vega-actions');

// add 'View Source' action
ctrl.append('a')
.text('View Source')
.attr('href', '#')
.on('click', function() {
viewSource(source);
d3.event.preventDefault();
});

// add 'Open in Vega Editor' action
ctrl.append('a')
.text('Open in Vega Editor')
.attr('href', '#')
.on('click', function() {
post(window, embed.config.editor_url, {spec: source});
d3.event.preventDefault();
});

// bind all parameter elements
params.forEach(function(el) { el.__vega__ = view; });

// initialize and return visualization
view.update();
if (callback) callback(view);
});
}

function viewSource(source) {
var header = '<html><head>' + config.source_header + '</head>' + '<body><pre><code class="json">';
var footer = '</code></pre>' + config.source_footer + '</body></html>';
var win = window.open('');
win.document.write(header + source + footer);
win.document.title = 'Vega JSON Source';
}

// make config externally visible
embed.config = config;

module.exports = embed;
Loading

0 comments on commit 562a891

Please sign in to comment.