forked from vega/vega
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
224 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1 @@ | ||
vg.headless = (function() { | ||
|
||
var svgNS = 'version="1.1" xmlns="http://www.w3.org/2000/svg" ' + | ||
'xmlns:xlink="http://www.w3.org/1999/xlink"'; | ||
|
||
function extractSVG(view) { | ||
var p = view.padding(), | ||
w = view.width() + (p ? p.left + p.right : 0), | ||
h = view.height() + (p ? p.top + p.bottom : 0), | ||
svg = ""; | ||
|
||
// build svg text | ||
d3.selectAll("svg").each(function() { | ||
svg = this.innerHTML + svg; | ||
}); | ||
svg = svg.replace(/ href=/g, " xlink:href="); // requires a hack. sigh. | ||
|
||
return { | ||
svg : '<svg ' | ||
+ 'width="' + w + '" ' | ||
+ 'height="' + h + '" ' | ||
+ svgNS + '>' + svg + '</svg>' | ||
}; | ||
} | ||
|
||
function extractCanvas(view) { | ||
return {canvas: view.canvas()}; | ||
} | ||
|
||
function render(opt, callback) { | ||
function draw(chart) { | ||
try { | ||
// create and render view | ||
var view = chart({ | ||
data: opt.data, | ||
renderer: opt.renderer | ||
}).update(); | ||
|
||
if (opt.renderer === "svg") { | ||
// extract rendered svg | ||
callback(null, extractSVG(view)); | ||
} else { | ||
// extract rendered canvas | ||
var r = view.renderer(); | ||
if (r.pendingImages() === 0) { | ||
// if no images loading, return now | ||
callback(null, extractCanvas(view)); | ||
} else { | ||
// if images loading, poll until ready | ||
function wait() { | ||
if (r.pendingImages() === 0) { | ||
view.render(); // re-render with all images | ||
callback(null, extractCanvas(view)); | ||
} else setTimeout(wait, 10); | ||
} | ||
wait(); | ||
} | ||
} | ||
} catch (err) { | ||
callback(err, null); | ||
} | ||
} | ||
vg.parse.spec(opt.spec, draw, vg.HeadlessView.Factory); | ||
} | ||
|
||
function convert(spec, type, callback) { | ||
type = type || "canvas"; | ||
var opt = { | ||
renderer: type, | ||
spec: spec, | ||
data: null | ||
}; | ||
render(opt, callback); | ||
} | ||
|
||
return { | ||
convert: convert, | ||
render: render | ||
}; | ||
|
||
})(); | ||
vg.headless = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
vg.headless.render = function(opt, callback) { | ||
function draw(chart) { | ||
try { | ||
// create and render view | ||
var view = chart({ | ||
data: opt.data, | ||
renderer: opt.renderer | ||
}).update(); | ||
|
||
if (opt.renderer === "svg") { | ||
// extract rendered svg | ||
callback(null, {svg: view.svg()}); | ||
} else { | ||
// extract rendered canvas, waiting for any images to load | ||
view.canvasAsync(function(canvas) { | ||
callback(null, {canvas: canvas}); | ||
}); | ||
} | ||
} catch (err) { | ||
callback(err, null); | ||
} | ||
} | ||
|
||
vg.parse.spec(opt.spec, draw, vg.headless.View.Factory); | ||
}; |
Oops, something went wrong.