Instantly share code, notes, and snippets.

Embed
What would you like to do?
Handlebar helper I use with grunt-assemble to generate charts as images for a static blog
Handlebars.registerHelper('chart', function(width, height, type, title, options) {
var config = JSON.parse(options.fn(this)),
canvas = new Canvas(width, height),
ctx = canvas.getContext('2d'),
method = type[0].toUpperCase() + type.slice(1),
uniqueName = crypto.createHash('md5').update(width + height + type + title + options.fn(this)).digest('hex'),
chartFileName = uniqueName + '.png';
new Chart(ctx)[method](config);
canvas.toBuffer(function (err, buf) {
if (err) throw err;
fs.writeFileSync(path.join(__dirname, '..', 'build', 'charts', chartFileName), buf);
});
return new Handlebars.SafeString(
'<figure class="chart fullwidth">' +
'<img src="/charts/' + chartFileName + '" alt="' + title + '">' +
'<figcaption>' + title + '</figcaption>' +
'</figure>'
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment