javascript - Render Chartjs on hidden DIV - Stack Overflow

I'm trying to render many charts using chart.js on a hidden div, then convert them to images to us

I'm trying to render many charts using chart.js on a hidden div, then convert them to images to use in a PDF report.

My problem is: if the chart is not rendered on the DOM, I can't capture it and draw it on a canvas. Until the chart is shown, it doesn't render. I've tried to use the methods of draw and render from chart.js but they don't produce the desired results.

Here's my example HTML:

<div style="display: none;">
  <canvas id="myChart"></canvas>
</div>

And my corresponding JavaScript:

var canvas: any = document.getElementById('myChart');
var ctx: any = canvas.getContext('2d');
var chart = new Chart(ctx, {
    // The type of chart we want to create
    type: 'line',
    // The data for our dataset
    data: {
      labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
      datasets: [{
            label: 'My First dataset',
            backgroundColor: 'rgb(255, 99, 132)',
            borderColor: 'rgb(255, 99, 132)',
            data: [0, 10, 5, 2, 20, 30, 45]
          }]
        },

        // Configuration options go here
        options: {}
      });

for (var id in Chart.instances) {
    Chart.instances[id].resize();
    Chart.instances[id].draw('1s');
    Chart.instances[id].render('1s');
    console.log(Chart.instances[id].toBase64Image());
}

Is there any way to manually render the chart without displaying it, then capture the output as an image?

I'm trying to render many charts using chart.js on a hidden div, then convert them to images to use in a PDF report.

My problem is: if the chart is not rendered on the DOM, I can't capture it and draw it on a canvas. Until the chart is shown, it doesn't render. I've tried to use the methods of draw and render from chart.js but they don't produce the desired results.

Here's my example HTML:

<div style="display: none;">
  <canvas id="myChart"></canvas>
</div>

And my corresponding JavaScript:

var canvas: any = document.getElementById('myChart');
var ctx: any = canvas.getContext('2d');
var chart = new Chart(ctx, {
    // The type of chart we want to create
    type: 'line',
    // The data for our dataset
    data: {
      labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
      datasets: [{
            label: 'My First dataset',
            backgroundColor: 'rgb(255, 99, 132)',
            borderColor: 'rgb(255, 99, 132)',
            data: [0, 10, 5, 2, 20, 30, 45]
          }]
        },

        // Configuration options go here
        options: {}
      });

for (var id in Chart.instances) {
    Chart.instances[id].resize();
    Chart.instances[id].draw('1s');
    Chart.instances[id].render('1s');
    console.log(Chart.instances[id].toBase64Image());
}

Is there any way to manually render the chart without displaying it, then capture the output as an image?

Share Improve this question edited Feb 6, 2020 at 4:29 Dean 2,28120 silver badges28 bronze badges asked Feb 6, 2020 at 4:09 Paulo Galdo SandovalPaulo Galdo Sandoval 2,2036 gold badges30 silver badges44 bronze badges 4
  • What does this have to do with Angular exactly? – Dean Commented Feb 6, 2020 at 4:13
  • @Dean my bad, wrong tag, it's coded on angular but it doesn't applies, fixing now – Paulo Galdo Sandoval Commented Feb 6, 2020 at 4:15
  • What, specifically, is happening? Are you getting an error? Is the image appearing but garbled? Take a look at this answer. Also off the top of my head, you could probably render it on a canvass that is visible but not in the viewport. – Dean Commented Feb 6, 2020 at 4:30
  • @Dean the canvas used to render the chart is always on height and width in 0, and when i try to get the base64 value from the chart is blank – Paulo Galdo Sandoval Commented Feb 6, 2020 at 4:31
Add a ment  | 

1 Answer 1

Reset to default 7

The fastest hack would be to replace:

<div style="display: none;">

with

<div style="position:absolute;left:-9999px;top:-9999px;">

and it will work: https://jsfiddle/FGRibreau/n1cx9d70/2/ :)

PS: If you need to render the chart server-side (e.g. in a batch) in order to then embed it inside PDF reports then you might want to consider dedicated services like Image-Charts.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745221439a4617267.html

相关推荐

  • javascript - Render Chartjs on hidden DIV - Stack Overflow

    I'm trying to render many charts using chart.js on a hidden div, then convert them to images to us

    11小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信