ArigatoManga
ArigatoManga

Reputation: 702

Not able to convert Canvas with chart and images using toDataURL()

I have a bar chart (using chart.js) with labels as images and when i try to export the canvas to image it only exports the chart part not the images(labels), but the same canvas when I right click and save as image is able to exporting correctly (both the chart and label images).

Can someone help me with this issue.

my js file:

var config = {
    type: 'horizontalBar',
  data: {
    labels:['a', 'b', 'c', 'd', 'e', 'f'],
    datasets: [
      {
        data: [ "20", "10", "2", "5", "1", "22" ],
        backgroundColor: [ '#f1f33a', '#99d695', "#cc8e50", '#ec9089', '#8e7bd4', '#eef5ce' ]   
      }]
  },
  options: {
    animation: {
      duration: 1,
      onComplete: function(charttt) {
        var chartInstance = charttt.chart,
        ctx = chartInstance.ctx;

        ctx.textAlign = 'right';
        ctx.textBaseline = 'bottom';

        this.data.datasets.forEach(function (dataset) {
            var meta = chartInstance.controller.getDatasetMeta(i);
            meta.data.forEach(function (bar, index) {
                var img = new Image();
                img.src = 'https://i.imgur.com/yDYW1I7.png';
                var data = dataset.data[index];

                ctx.fillStyle = 'red';
                ctx.fillText(data, bar._model.x - 20, bar._model.y - 10);
                img.onload = function() {
                  ctx.drawImage(img, bar._model.x, bar._model.y, 10, 10);
                }
            });
        });
        var url = document.getElementById('barChart').toDataURL('image/png');
        document.getElementById("testimg").src = url;
        }
    },
    legend: {
      "display": false
    },
    responsive:false,
    chartArea: {
        backgroundColor: 'blue'
    }
  }
};

var ctx = document.getElementById("barChart").getContext("2d");
var chartt = new Chart(ctx, config);

Upvotes: 2

Views: 731

Answers (1)

WhiteHat
WhiteHat

Reputation: 61275

i'm thinking you're problem is here...

    this.data.datasets.forEach(function (dataset) {
        var meta = chartInstance.controller.getDatasetMeta(i);

you're trying to get the meta of dataset i
but i is not defined

need to add the argument to the forEach function

    this.data.datasets.forEach(function (dataset, i) {  // <-- here
        var meta = chartInstance.controller.getDatasetMeta(i);

see following working snippet...

$(document).ready(function() {
  var config = {
      type: 'horizontalBar',
    data: {
      labels:['a', 'b', 'c', 'd', 'e', 'f'],
      datasets: [
        {
          data: [ "20", "10", "2", "5", "1", "22" ],
          backgroundColor: [ '#f1f33a', '#99d695', "#cc8e50", '#ec9089', '#8e7bd4', '#eef5ce' ]
        }]
    },
    options: {
      animation: {
        duration: 1,
        onComplete: function(charttt) {
          var chartInstance = charttt.chart,
          ctx = chartInstance.ctx;

          ctx.textAlign = 'right';
          ctx.textBaseline = 'bottom';

          this.data.datasets.forEach(function (dataset, i) {
              var meta = chartInstance.controller.getDatasetMeta(i);
              meta.data.forEach(function (bar, index) {
                  var img = new Image();
                  img.src = 'https://i.imgur.com/yDYW1I7.png';
                  var data = dataset.data[index];

                  ctx.fillStyle = 'red';
                  ctx.fillText(data, bar._model.x - 20, bar._model.y - 10);
                  img.onload = function() {
                    ctx.drawImage(img, bar._model.x, bar._model.y, 10, 10);
                  }
              });
          });
          var url = document.getElementById('barChart').toDataURL('image/png');
          document.getElementById("testimg").src = url;
          }
      },
      legend: {
        "display": false
      },
      responsive:false,
      chartArea: {
          backgroundColor: 'blue'
      }
    }
  };

  var ctx = document.getElementById("barChart").getContext("2d");
  var chartt = new Chart(ctx, config);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<canvas id="barChart"></canvas>
<img id="testimg"/>


EDIT

there are two issues causing the images not to appear in the export.

first, adding images from another domain taints the canvas.
afterwards, a security error will prevent the canvas from being exported.
a work around is to add the crossorigin attribute to the image being added to the chart.

img.setAttribute('crossorigin', 'anonymous');

next, before exporting the chart,
we have to wait for all of the images to load,
in the img.onload event.

see following working snippet...

$(document).ready(function() {
  var config = {
      type: 'horizontalBar',
    data: {
      labels:['a', 'b', 'c', 'd', 'e', 'f'],
      datasets: [
        {
          data: [ "20", "10", "2", "5", "1", "22" ],
          backgroundColor: [ '#f1f33a', '#99d695', "#cc8e50", '#ec9089', '#8e7bd4', '#eef5ce' ]
        }]
    },
    options: {
      animation: {
        duration: 1,
        onComplete: function(charttt) {
          var chartInstance = charttt.chart,
          ctx = chartInstance.ctx;

          ctx.textAlign = 'right';
          ctx.textBaseline = 'bottom';

          var countDatasets = this.data.datasets.length;
          this.data.datasets.forEach(function (dataset, i) {
              var meta = chartInstance.controller.getDatasetMeta(i);
              var countMeta = meta.data.length;
              meta.data.forEach(function (bar, index) {
                  var img = new Image();
                  img.setAttribute('crossorigin', 'anonymous');
                  img.src = 'https://i.imgur.com/yDYW1I7.png';
                  var data = dataset.data[index];

                  ctx.fillStyle = 'red';
                  ctx.fillText(data, bar._model.x - 20, bar._model.y - 10);
                  img.onload = function() {
                    ctx.drawImage(img, bar._model.x, bar._model.y, 10, 10);
                    countMeta--;
                    if (countMeta === 0) {
                      countDatasets--;
                    }
                    if ((countDatasets === 0) && (countMeta === 0)) {
                      var url = document.getElementById('barChart').toDataURL('image/png');
                      document.getElementById("testimg").src = url;
                    }
                  }
              });
          });
        }
      },
      legend: {
        "display": false
      },
      responsive:false,
      chartArea: {
          backgroundColor: 'blue'
      }
    }
  };

  var ctx = document.getElementById("barChart").getContext("2d");
  var chartt = new Chart(ctx, config);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<canvas id="barChart"></canvas>
<img id="testimg"/>

Upvotes: 1

Related Questions