Skip to content

Instantly share code, notes, and snippets.

@joepie91
Forked from Nickibrochner/drawchart.js
Created July 14, 2017 13:55
Show Gist options
  • Save joepie91/8f2941d58ba7ef33e8dca5af7a6bae56 to your computer and use it in GitHub Desktop.
Save joepie91/8f2941d58ba7ef33e8dca5af7a6bae56 to your computer and use it in GitHub Desktop.

Revisions

  1. @Nickibrochner Nickibrochner revised this gist Jul 14, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions drawchart.js
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@ const ChartjsNode = require('chartjs-node');
    var chartNode = new ChartjsNode(600, 600);
    var randomnumber=Math.random();
    var imagename = "testimage"+randomnumber+".png"
    module.exports = imagename

    // each api returns a Promise
    chartNode.drawChart({
    @@ -75,6 +76,5 @@ chartNode.drawChart({
    return chartNode.writeImageToFile('image/png', '/home/nicki/akkredibot/img/'+imagename);
    })
    .then(() => {
    module.exports.imagename = imagename
    chartNode.destroy() // now the chart is written at ./testimage.png
    chartNode.destroy()
    });
  2. @Nickibrochner Nickibrochner created this gist Jul 14, 2017.
    80 changes: 80 additions & 0 deletions drawchart.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    const ChartjsNode = require('chartjs-node');
    // 600x600 canvas size
    var chartNode = new ChartjsNode(600, 600);
    var randomnumber=Math.random();
    var imagename = "testimage"+randomnumber+".png"

    // each api returns a Promise
    chartNode.drawChart({
    type: 'bar',
    data: {
    labels: ["2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017"],
    datasets: [{
    label: 'Antal akkrediteringer',
    data: [57, 125, 249, 262, 271, 289, 227, 98, 126, 93, 41],
    backgroundColor: [
    'rgba(54, 162, 235, 0.2)',
    'rgba(54, 162, 235, 0.2)',
    'rgba(54, 162, 235, 0.2)',
    'rgba(54, 162, 235, 0.2)',
    'rgba(54, 162, 235, 0.2)',
    'rgba(54, 162, 235, 0.2)',
    'rgba(54, 162, 235, 0.2)',
    'rgba(54, 162, 235, 0.2)',
    'rgba(54, 162, 235, 0.2)',
    'rgba(54, 162, 235, 0.2)',
    'rgba(54, 162, 235, 0.2)'
    ],
    borderColor: [
    'rgba(54, 162, 235, 1)',
    'rgba(54, 162, 235, 1)',
    'rgba(54, 162, 235, 1)',
    'rgba(54, 162, 235, 1)',
    'rgba(54, 162, 235, 1)',
    'rgba(54, 162, 235, 1)',
    'rgba(54, 162, 235, 1)',
    'rgba(54, 162, 235, 1)',
    'rgba(54, 162, 235, 1)',
    'rgba(54, 162, 235, 1)',
    'rgba(54, 162, 235, 1)'
    ],
    borderWidth: 1
    }]
    },
    options: {
    layout: {
    padding:{
    left: 30,
    right: 30,
    top: 30,
    bottom: 30
    }
    },
    scales: {
    yAxes: [{
    ticks: {
    beginAtZero: true
    }
    }]
    }
    }
    })
    .then(() => {
    // now we have a chart
    // lets get the image stream
    return chartNode.getImageStream('image/png');
    })
    .then(imageStream => {
    // now you can do anything with the image, like upload to S3
    // lets get the image buffer
    return chartNode.getImageBuffer('image/png');
    })
    .then(imageBuffer => {
    // now you can modify the raw PNG buffer if you'd like
    // want to write the image directly to the disk, no problem
    return chartNode.writeImageToFile('image/png', '/home/nicki/akkredibot/img/'+imagename);
    })
    .then(() => {
    module.exports.imagename = imagename
    chartNode.destroy() // now the chart is written at ./testimage.png
    });