score:0
I was strugling with that too and you self-answer did not help my case. I am using symfony 3.3 and Chart.js 2 and whatever I did, did not work properly. So I have solved it in a different manner (maybe not a clean one) and I wanted to post it here for inspiration to others.
I needed to export a page, that I was presenting to the user in a browser. In browser, I used Javascript to get picture out of the rendered graph with
animation: {
onComplete: function(animation) {
console.log('done');
var url=document.getElementById("barChartByCountryWeight{{ part }}{{ subsetKey }}").toDataURL();
$.ajax({
url: 'saveChartImages',
type: 'POST',
data: { 'barChartByCountryWeight{{ part }}{{ subsetKey }}': url },
success: function(result) {
console.log(result);
console.log('the request was successfully sent to the server');
},
error: function (request, error) {
console.log(arguments[0]['responseText']);
console.log(" Can't do because: " + error);
}
});
}
}
And on server side I put it in session and in a controller for the PDF export, I have taken the image from session and put the image in the HTML, that is converted to PDF.
Hope that helps.
score:0
Yup I had solved this problem try to use Chartjs 1 not new chart js because laravel snappy uses wkhtmltopdf which doesn't support css animation and new chartjs uses css animation https://github.com/chartjs/Chart.js/issues/4767 show that and the solution i found is to use google chart which uses svg so you can get high resolution chart
score:0
I have implemented the working code for this issue. You can check out the working code here.
NOTE: For generating pdf you must disable the Chart JS
animation or add the option javascript-delay=>1000
to the wkhtmltopdf
options.
score:2
Solved it by downgrading wkhtmltopdf: 0.12.4 > 0.12.2.1 chart.js version seemed to have no influence. I used 2.7.0. Fixed width and height seem to be required as well.
Edit: Since wkhtmltopdf is dead, I switched to Puppeteer recently.
score:2
I was dealing with the same issue using rotativa to export my ASP.NET MVC page with Chart.JS to PDF with no luck.
After a couple of days I finally found a super-easy solution to achieve my goal. What I did is simply to use the .toBase64Image() method of Chart.JS to encode the chart to a base64 string variable in Javascript. Then I saved this string into a model and then on the PDF html page a used tag where i put the base64encoded string to a scr property and the result is great :-)
javascript:
//save Chart as Image
var url_base64 = document.getElementById('myChart').toDataURL('image/png');
//set the string as a value of a hidden element
document.getElementById('base64graph').value = url_base64;
PDF view:
<img style='display:block; width:900px;height:400px;position:relative;margin:auto;text-align:center;' id='base64image'
src='@Model.base64graph' />
score:5
Try adding this,
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.0.0/polyfill.min.js"></script>
It worked for me.
Source: https://github.com/wkhtmltopdf/wkhtmltopdf/issues/2952
score:15
Found the answer. After I created a separate file, outside the framework, i did some tests again. It rendered the graph in the browser so I tried to use the command tool WKhtmltopdf, and it did not worked, when it did with other examples (see Update #1). So there is something wrong with my php page.
Ran the same tests that I did in the framework, and got the answer for my problem. By introducing a parent div tag width dimensions in the canvas tag it made the graph render in the page.
<div style="width:800px;height:200;">
<canvas id="myChart" style="width:800px;height:200;"></canvas>
</div>
The proposition was found in this site: Github, so thanks laguiz.
score:20
Here's the code that works with wkhtmltopdf
version 0.12.5
:
chart.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<style>
.reportGraph {width:900px}
</style>
</head>
<body>
<div class="reportGraph"><canvas id="canvas"></canvas></div>
<script type="text/javascript">
// wkhtmltopdf 0.12.5 crash fix.
// https://github.com/wkhtmltopdf/wkhtmltopdf/issues/3242#issuecomment-518099192
'use strict';
(function(setLineDash) {
CanvasRenderingContext2D.prototype.setLineDash = function() {
if(!arguments[0].length){
arguments[0] = [1,0];
}
// Now, call the original method
return setLineDash.apply(this, arguments);
};
})(CanvasRenderingContext2D.prototype.setLineDash);
Function.prototype.bind = Function.prototype.bind || function (thisp) {
var fn = this;
return function () {
return fn.apply(thisp, arguments);
};
};
function drawGraphs() {
new Chart(
document.getElementById("canvas"), {
"responsive": false,
"type":"line",
"data":{"labels":["January","February","March","April","May","June","July"],"datasets":[{"label":"My First Dataset","data":[65,59,80,81,56,55,40],"fill":false,"borderColor":"rgb(75, 192, 192)","lineTension":0.1}]},
"options":{}
}
);
}
window.onload = function() {
drawGraphs();
};
</script>
</body>
</html>
Run:
$ wkhtmltopdf chart.html chart.pdf
:
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
Source: stackoverflow.com
Related Query
- Wkhtmltopdf does not render Chart.JS 2.5.0 graph
- Bar Chart of ChartJS does not Render
- Chartjs does not render chart when set responsive option to true
- ChartJs Memory Leak | Garbage Collection does not clean Chart Object or Arrays after render
- Charts.js does not render chart until I open the console
- Why ChartJS's Bar Chart does not render bar for a specific value?
- ChartJs does not render chart when binding canvas id in Angular
- Chart.js line chart does not render data lines on iOS
- React js with react-chart js, does not render doughnut chart
- React-chart does not render the legend for PIE chart
- Adding new data to empty Chart.js chart does not render new data correctly
- Edge browser does not render chart using chart js
- Chart.js - Line chart does not render all points when using Point[] format
- Html chart does not fit a small Android WebView
- Chart looks only grey, does not show the color - Chartjs,discordjs
- Angular-chart.js - Make line chart does not curve
- Vue Chart.js component does not render
- Chart.js Chart in Angular does not load until resizing browser window
- Chart.js in AngularJS tabset does not render
- Chartjs (Non Vue) Not Rendering Graph Chart inside V-if/V-show
- My pie chart (chartJs) does not appear
- Chart.js chart does not display when inside an ngIf.
- Chartjs bar-chart does not render when values are equal
- I am using chartjs node canvas to render a chart however the graph options are being ignored
- Angular2 and ng2-charts does not display any graph
- Showing 'undefined%' in the graph if data does not exist
- Adding object data to data structure of chart.js chart does not show any data
- Chart does not display on webpage in Django app (with Chart.js)
- Chart.JS does not render and display a sad face icon
- fill: 'origin' property does not work for my chart
More Query from same tag
- charts labels and data with php arrays
- chartjs - Drawing vertical line on integer x axis value
- How do I make an interactive line chart
- Animate Chart.JS Only Once
- How to update a chart taking by querying a database?
- How to get dataset from MySQL using Flask?
- Import data from Excel and use in Chart.js
- function not return values
- Chart.js in Angular 10: Specified colors not shown in multi-series bar chart (instead random colors)
- passing each element of array in the labels of char.js
- Using JSON in Chart.js with Angular.js
- Inverting X axis number labels in chart.js scatter chart
- Updating a Pie Chart in React as Data is Entered
- Line Chart using Chart js with time data
- Can't bind to 'chartType' since it isn't a known property of 'canvas' with angular12
- Chart.js plugins overlapping tooltips
- Use multi data to draw chart with chartjs
- JavaScript array value to chart.js graph as stacked graph
- How do you create rounded corners on the top and bottom of a bar chart and the between 2 part still draw like image?
- Can't reuse a template in Vue.js
- How to use saved data in Ionic/Angular?
- Chart.Js Doughnut not calculating JSON data properly
- Pass Ajax response array to Chart.js
- Background color of the chart area in chartjs not working
- Change time format in Chart.js date
- Chart Js Stacked Bar Chart with Multiple levels or categories
- How to make ng2-Chart Scrollable for large amount of data sets?
- Chart.js causes Angular 2 error
- How to make the chart smaller than its container
- call Column that is complex in query