score:41
The chart seem to be async so you will probably need to provide a callback when the animation has finished or else the canvas will be empty.
var options = {
bezierCurve : false,
onAnimationComplete: done /// calls function done() {} at end
};
score:-1
You can also use the toBase64Image() method setting animation: false
var options = {
bezierCurve : false,
animation: false
};
score:0
@EH_warch You need to use the Complete callback to generate your base64:
onAnimationComplete: function(){
console.log(this.toBase64Image())
}
If you see a white image, it means you called the toBase64Image before it finished rendering.
score:2
You can access afterRender
hook by using plugins
.
And here are all the plugin api available.
In html file:
<html>
<canvas id="myChart"></canvas>
<div id="imgWrap"></div>
</html>
In js file:
var chart = new Chart(ctx, {
...,
plugins: [{
afterRender: function () {
// Do anything you want
renderIntoImage()
},
}],
...,
});
const renderIntoImage = () => {
const canvas = document.getElementById('myChart')
const imgWrap = document.getElementById('imgWrap')
var img = new Image();
img.src = canvas.toDataURL()
imgWrap.appendChild(img)
canvas.style.display = 'none'
}
score:3
You should use the Chartjs API function toBase64Image()
instead and call it after the animation is complete. Therefore:
var pieChart, URI;
var options = {
animation : {
onComplete : function(){
URI = pieChart.toBase64Image();
}
}
};
var content = {
type: 'pie', //whatever, not relevant for this example
data: {
datasets: dataset //whatever, not relevant for this example
},
options: options
};
pieChart = new Chart(pieChart, content);
Example
You can check this example and run it
var chart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Standing costs', 'Running costs'], // responsible for how many bars are gonna show on the chart
// create 12 datasets, since we have 12 items
// data[0] = labels[0] (data for first bar - 'Standing costs') | data[1] = labels[1] (data for second bar - 'Running costs')
// put 0, if there is no data for the particular bar
datasets: [{
label: 'Washing and cleaning',
data: [0, 8],
backgroundColor: '#22aa99'
}, {
label: 'Traffic tickets',
data: [0, 2],
backgroundColor: '#994499'
}, {
label: 'Tolls',
data: [0, 1],
backgroundColor: '#316395'
}, {
label: 'Parking',
data: [5, 2],
backgroundColor: '#b82e2e'
}, {
label: 'Car tax',
data: [0, 1],
backgroundColor: '#66aa00'
}, {
label: 'Repairs and improvements',
data: [0, 2],
backgroundColor: '#dd4477'
}, {
label: 'Maintenance',
data: [6, 1],
backgroundColor: '#0099c6'
}, {
label: 'Inspection',
data: [0, 2],
backgroundColor: '#990099'
}, {
label: 'Loan interest',
data: [0, 3],
backgroundColor: '#109618'
}, {
label: 'Depreciation of the vehicle',
data: [0, 2],
backgroundColor: '#109618'
}, {
label: 'Fuel',
data: [0, 1],
backgroundColor: '#dc3912'
}, {
label: 'Insurance and Breakdown cover',
data: [4, 0],
backgroundColor: '#3366cc'
}]
},
options: {
responsive: false,
legend: {
position: 'right' // place legend on the right side of chart
},
scales: {
xAxes: [{
stacked: true // this should be set to make the bars stacked
}],
yAxes: [{
stacked: true // this also..
}]
},
animation : {
onComplete : done
}
}
});
function done(){
alert(chart.toBase64Image());
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx" width="700"></canvas>
score:10
First convert your Chart.js canvas to base64 string.
var url_base64 = document.getElementById('myChart').toDataURL('image/png');
Set it as a href attribute for anchor tag.
link.href = url_base64;
<a id='link' download='filename.png'>Save as Image</a>
score:31
Chart.JS API has changed since this was posted and older examples did not seem to be working for me. here is an updated fiddle that works on the newer versions
HTML:
<body>
<canvas id="canvas" height="450" width="600"></canvas>
<img id="url" />
</body>
JS:
function done(){
alert("haha");
var url=myLine.toBase64Image();
document.getElementById("url").src=url;
}
var options = {
bezierCurve : false,
animation: {
onComplete: done
}
};
var myLine = new
Chart(document.getElementById("canvas").getContext("2d"),
{
data:lineChartData,
type:"line",
options:options
}
);
Source: stackoverflow.com
Related Query
- Converting Chart.js canvas chart to image using .toDataUrl() results in blank image
- Blank PNG image using Chart JS . toBase64Image() function
- Using chart.js to output chart as a saved image rather than using canvas
- How to save Chart JS charts as image without black background using blobs and filesaver?
- I am using chartjs node canvas to render a chart however the graph options are being ignored
- How to get the data attribute of the canvas chart created using chartjs
- How to add image inside the doughnut chart using chart.js?
- Writing blank on canvas using fillText
- How to plot a line chart which has both the start and points outside the canvas area using Chart.js
- Refreshing the image of a chart using chart.js
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- How to convert chart.js library chart to image in pure node.js without using HTML and CSS?
- How to run Chart.js samples using source code
- How to make the size of pie chart fixed without using canvas (chart.js)
- Is there a way to store a chart as an image directly to server using Chart.js?
- How to add text inside the doughnut chart using Chart.js?
- How to clear a chart from a canvas so that hover events cannot be triggered?
- Moving vertical line when hovering over the chart using chart.js
- create a multi line chart using Chart.js
- How to add an on click event to my Line chart using Chart.js
- Display line chart with connected dots using chartJS
- Dynamically update the options of a chart in chartjs using Javascript
- How to Draw Gantt chart using chart js or other libraries
- How to add images as labels to Canvas Charts using chart.js
- Detecting hover events over parts of a chart using Chart.js
- Overlapping Bar Chart using Chart.js
- How to add text in centre of the doughnut chart using Chart.js?
- How to create stacked bar chart using react-chartjs-2?
- set background color to save canvas chart
- Display a limited number of labels only on X-Axis of Line Chart using Chart.js
More Query from same tag
- Update angular-chart.js on filtered list
- Unable to access list elements in Django Webpage
- Chartjs - How to update the data from values in my database?
- Show nested objects in Chart.js
- Chart.js is not displayed when page loads first time
- How to feed hour and minute data into chartJS
- Angularjs - Can't call the value from form to graph
- How to inject “Chart.js” to my module?
- Chart.JS: How to make sharp lines to smooth curved lines
- I cant get Legend to work for my chartjs donut chart
- Charts on Admin-on-rest
- Gauge Chart using Chart.js V3
- How to know which bar is clicked on chart.js bar chart with multiple data?
- Chartjs - Doughnut chart with multi layer and running value
- Charts.JS - Too Many Charts? - Loading Issue
- How do i find area under the graph
- PHP string data passed to Chart.js not rendering well
- Populate colors on a Chart.js chart dynamically
- How to add "shadow" on hovering bar chart?
- Is it posssible to create a Line-Chart with only 2 values? - Chart.js
- Chart.js how to modify an existing legend
- charts are not being show with wicked_pdf
- ChartJSNodeCanvas plugin chartjs-plugin-annotation creates error: HTMLCanvasElement is not defined
- Format chart.js x-axis labels to specific timezone
- How can I put sign (%) on data in ChartJS?
- ChartJS tooltip values aren't matching the data after updating multiple charts
- change space between horizontal (grid) lines for line chart
- findOne returns undefined in onRendered
- Chart.js - Why the chart cannot rendered in a child component but in the father component can?
- The chart list is not showing