Created
March 31, 2015 19:09
-
-
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
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
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