angular-chart.js icon indicating copy to clipboard operation
angular-chart.js copied to clipboard

.toBase64Image()

Open TWKDreamState opened this issue 11 years ago • 3 comments

Hi,

What would be the best way to use the .toBase64Image() option from chart.js (http://www.chartjs.org/docs/ - prototype method) in angular-chart.js?

I'd like to be able to click a button and produce a PDF using jsPDF, which accepts a base64 image. Therefore, I'd need to do this in the controller once the charts have completed rendering. Is there a way to access this in the controller (given that the chart is a directive? - I'm fairly new to angular).

Many thanks!

TWKDreamState avatar Mar 02 '15 14:03 TWKDreamState

You could listen to the "create" and "update" events on the scope, and assign the chart to a variable and use it to generate the base64 image e.g. $scope.$on('create', function (event, chart) { ... });

jtblin avatar Mar 02 '15 22:03 jtblin

My apologies for not getting back immediately.

I'm going to have a look into this, although I'm not fully sure how to implement it (as an Angular newbie). I'm looking to have six graphs on one page that I'd like to export to PDF. I've been contemplating using the canvas.toDataURL and disabling PDF export on ie8 using Modernizr. In your opinion, will using the chart.js toBase64Image() method provide any advantage over the canvas.toDataURL (image quality, etc).

I've also considered sending the charts somehow to the back end node server and using phantom.js to create a pdf, but it seems to be an overly complicated method. Are there any other methods for gettings the charts rendered with angular-chart to a PDF that are worth exploring?

TWKDreamState avatar Mar 06 '15 08:03 TWKDreamState

chart.js toBase64Image() is just a wrapper around canvas.toDataURL() so that should be exactly the same. See https://github.com/nnnick/Chart.js/blob/019e985c8f468bf0700e5f7c4ff9b5268651e787/Chart.js#L1046

There are examples to do that with jspdf on SO

If you want to do that from server side, you can have a look at the integration tests in this repo. I use phantomjs to take screenshot and test image differences.

jtblin avatar Mar 08 '15 10:03 jtblin