Skip to content

Instantly share code, notes, and snippets.

@tnajdek
Created March 31, 2015 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tnajdek/e1e431c6f05b36e3bee2 to your computer and use it in GitHub Desktop.
Save tnajdek/e1e431c6f05b36e3bee2 to your computer and use it in GitHub Desktop.
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