diff --git a/packages/stat-logger/src/statGraph.js b/packages/stat-logger/src/statGraph.js index 93b7341f1f0..5c88564c9c6 100644 --- a/packages/stat-logger/src/statGraph.js +++ b/packages/stat-logger/src/statGraph.js @@ -100,25 +100,43 @@ export function addGraphToGraphSpec(spec, statsPath, yField, color) { spec.marks.push(lineElement); } -export async function renderGraph(spec, outputPath) { +export async function renderGraph(spec, outputPath, type = 'png') { if (spec.data.length === 0) { throw new Error('graph spec contains no data'); } else if (spec.marks.length === 0) { throw new Error('graph spec has no graphs defined'); } - if (!outputPath.endsWith('.png')) { - outputPath += '.png'; + if (type !== 'png' && type !== 'pdf') { + throw new Error(`invalid output type ${type}, valid types are png or pdf`); + } + + let loadDir = '.'; + let out = process.stdout; + if (outputPath) { + loadDir = path.dirname(outputPath); + if (!outputPath.endsWith(`.${type}`)) { + outputPath += `.${type}`; + } + out = fs.createWriteStream(outputPath); } const view = new vega.View(vega.parse(spec, null), { - loader: vega.loader({ baseURL: path.dirname(outputPath) }), + loader: vega.loader({ baseURL: loadDir }), logger: vega.logger(vega.Warn, 'error'), renderer: 'none', }).finalize(); - const canvas = await view.toCanvas(); - const out = fs.createWriteStream(outputPath); - const stream = canvas.createPNGStream(); + let stream; + if (type === 'png') { + const canvas = await view.toCanvas(); + stream = canvas.createPNGStream(); + } else { + const canvas = await view.toCanvas(1, { + type: 'pdf', + context: { textDrawingMode: 'glyph' }, + }); + stream = canvas.createPDFStream(); + } stream.on('data', chunk => { out.write(chunk); }); diff --git a/packages/swingset-runner/src/graphMem.js b/packages/swingset-runner/src/graphMem.js index 2058c2e695a..f79a38216c9 100644 --- a/packages/swingset-runner/src/graphMem.js +++ b/packages/swingset-runner/src/graphMem.js @@ -22,6 +22,7 @@ export async function main() { let outfile = null; const datafiles = []; + let type = 'png'; while (argv[0]) { const arg = argv.shift(); @@ -31,6 +32,9 @@ export async function main() { case '-o': outfile = argv.shift(); break; + case '--pdf': + type = 'pdf'; + break; default: throw new Error(`invalid flag ${arg}`); } @@ -42,13 +46,6 @@ export async function main() { throw new Error('you must specify some input'); } - if (!outfile) { - outfile = datafiles[0]; - } - if (!outfile.endsWith('.png')) { - outfile += '.png'; - } - const spec = initGraphSpec( datafiles[0], 'block', @@ -67,5 +64,5 @@ export async function main() { colorIdx += 4; } - await renderGraph(spec, outfile); + await renderGraph(spec, outfile, type); } diff --git a/packages/swingset-runner/src/graphTime.js b/packages/swingset-runner/src/graphTime.js index ba8ce52fa6b..8195328e683 100644 --- a/packages/swingset-runner/src/graphTime.js +++ b/packages/swingset-runner/src/graphTime.js @@ -21,6 +21,7 @@ export async function main() { let outfile = null; const datafiles = []; + let type = 'png'; while (argv[0]) { const arg = argv.shift(); @@ -30,6 +31,9 @@ export async function main() { case '-o': outfile = argv.shift(); break; + case '--pdf': + type = 'pdf'; + break; default: throw new Error(`invalid flag ${arg}`); } @@ -41,10 +45,6 @@ export async function main() { throw new Error('you must specify some input'); } - if (!outfile) { - outfile = datafiles[0]; - } - const spec = initGraphSpec( datafiles[0], 'block', @@ -57,5 +57,5 @@ export async function main() { addGraphToGraphSpec(spec, datafiles[i], 'btime', colors[i % colors.length]); } - await renderGraph(spec, outfile); + await renderGraph(spec, outfile, type); }