Skip to content

Commit

Permalink
feat: Add PDF and stdout support to stat-logger graphing
Browse files Browse the repository at this point in the history
  • Loading branch information
FUDCo committed Mar 16, 2020
1 parent f23e902 commit 22238e7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
32 changes: 25 additions & 7 deletions packages/stat-logger/src/statGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
13 changes: 5 additions & 8 deletions packages/swingset-runner/src/graphMem.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export async function main() {

let outfile = null;
const datafiles = [];
let type = 'png';

while (argv[0]) {
const arg = argv.shift();
Expand All @@ -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}`);
}
Expand All @@ -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',
Expand All @@ -67,5 +64,5 @@ export async function main() {
colorIdx += 4;
}

await renderGraph(spec, outfile);
await renderGraph(spec, outfile, type);
}
10 changes: 5 additions & 5 deletions packages/swingset-runner/src/graphTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export async function main() {

let outfile = null;
const datafiles = [];
let type = 'png';

while (argv[0]) {
const arg = argv.shift();
Expand All @@ -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}`);
}
Expand All @@ -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',
Expand All @@ -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);
}

0 comments on commit 22238e7

Please sign in to comment.