Skip to content

Instantly share code, notes, and snippets.

@mpvvliet
Created November 5, 2018 21:54
Show Gist options
  • Select an option

  • Save mpvvliet/91b19b90a246c032f447b283f0f8f5de to your computer and use it in GitHub Desktop.

Select an option

Save mpvvliet/91b19b90a246c032f447b283f0f8f5de to your computer and use it in GitHub Desktop.
Sample lambda demonstrating how to render an image using ChartJS
'user strict';
const debug = require('debug');
const { CanvasRenderService } = require('chartjs-node-canvas');
const fs = require('fs');
// bug workaround: https://github.com/vmpowerio/chartjs-node/issues/26
if (global.CanvasGradient === undefined) {
global.CanvasGradient = function() {};
}
async function renderChartWithChartJS() {
const width = 400;
const height = 400;
const configuration = {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: (value) => '$' + value
}
}]
}
}
};
const chartCallback = (ChartJS) => {
ChartJS.defaults.global.responsive = true;
ChartJS.defaults.global.maintainAspectRatio = false;
};
debug("Rendering chart");
const canvasRenderService = new CanvasRenderService(width, height, chartCallback);
return await canvasRenderService.renderToBuffer(configuration);
}
module.exports.renderChart = async function(event, context, callback) {
const buffer = await renderChartWithChartJS();
fs.writeFileSync('/tmp/chartjs-test.png', buffer);
const response = {
statusCode: 200,
body: null
};
callback(null, response);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment