score:18
if you want a customized background color then, you'd have to draw a background with your preferred color, and you can do so, like this ...
var backgroundcolor = 'white';
chart.plugins.register({
beforedraw: function(c) {
var ctx = c.chart.ctx;
ctx.fillstyle = backgroundcolor;
ctx.fillrect(0, 0, c.chart.width, c.chart.height);
}
});
demo
// draw background
var backgroundcolor = 'white';
chart.plugins.register({
beforedraw: function(c) {
var ctx = c.chart.ctx;
ctx.fillstyle = backgroundcolor;
ctx.fillrect(0, 0, c.chart.width, c.chart.height);
}
});
// chart
var canvas = $('#nobidschart').get(0);
var mychart = new chart(canvas, {
type: 'line',
data: {
labels: [1, 2, 3, 4, 5],
datasets: [{
label: 'line chart',
data: [1, 2, 3, 4, 5],
backgroundcolor: 'rgba(255, 0, 0, 0.2)',
bordercolor: 'rgba(255, 0, 0, 0.5)',
pointbackgroundcolor: 'black'
}]
}
});
// save as image
$('#save').click(function() {
canvas.toblob(function(blob) {
saveas(blob, "pretty image.png");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/filesaver.js/1.3.3/filesaver.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.5.0/chart.min.js"></script>
<button id="save">save</button>
<canvas id="nobidschart"></canvas>
score:0
in react, with react-chartjs-2, i was able to set background color of chart like so:
const plugin = {
beforedraw: (chartctx) => {
const ctx = chartctx.canvas.getcontext('2d');
ctx.save();
ctx.globalcompositeoperation = 'destination-over';
ctx.fillstyle = 'white';
ctx.fillrect(0, 0, chartctx.width, chartctx.height);
ctx.restore();
}
};
and then add the plugin to the chart:
<line ref={chartref} data={chartdata} options={options} plugins={[plugin]} />
docs reference: https://www.chartjs.org/docs/latest/configuration/canvas-background.html#color
to save chart as image:
i created a function that uses the tobase64image function to extract the image. i attached this function to a button to help me download chart image on click of button.
function downloadimage(){
const link = document.createelement("a");
link.download = `${chart.name || 'chart'}.jpg`
link.href = chartref.current.tobase64image('image/jpeg', 1);
link.click();
}
score:13
as i stated in my comment to the accepted answer, it bothered me that the beforedraw event causes the fillrect code to get called multiple times. (once per data point as far as i can see.)
but i couldn't get that approach to work when called on any other event. however, i just took the coding approach described in: https://stackoverflow.com/a/50126796/165164 and plugged it into code registered to run on the afterrender event and it does just what i want: run once and leave the background white.
chart.plugins.register({
afterrender: function(c) {
console.log("afterrender called");
var ctx = c.chart.ctx;
ctx.save();
// this line is apparently essential to getting the
// fill to go behind the drawn graph, not on top of it.
// technique is taken from:
// https://stackoverflow.com/a/50126796/165164
ctx.globalcompositeoperation = 'destination-over';
ctx.fillstyle = 'white';
ctx.fillrect(0, 0, c.chart.width, c.chart.height);
ctx.restore();
}
});
please visit (and up vote) the linked answer to the other posted question.
Source: stackoverflow.com
Related Query
- How to save Chart JS charts as image without black background using blobs and filesaver?
- How to convert chart.js library chart to image in pure node.js without using HTML and CSS?
- How to create a gantt chart using Chart.js and populate dates?
- How to add background image in bar chart column in Chart.js?
- How to draw outer labels for polar chart using ng2-charts and chart.js at fixed positions outside the rings?
- How to export a chart in Excel and PDF format using Chart.js
- using react-chartjs-2 , How can I save my chart as png using a download button
- How to update a chart using VueJS and ChartJS
- How to plot chart from external JSON and format X-AXIS to show time using Chart.JS?
- Flask Socketio | Update and plot a chart using background tasks created by Flask Executor or ThreadPoolExecutor
- how to increase space between legend and chart in chartjs (ng2charts ) using angular
- How do you set x and y axis and Title for a line chart using charts.js?
- how to reduce list chart to one and use select dropdown to show selection without refresh page?
- How to make the background color of the chart as gradient using Chart.js
- How can I change background color of a specific area in my chart's grid using Chart.js and Annotation?
- How to add image inside the doughnut chart using chart.js?
- How to remove old chart and append new chart to div using chartjs
- How to plot a line chart which has both the start and points outside the canvas area using Chart.js
- How to show symbols after the label and before the numeric value using chart.js Bar chart in Angular
- Chart.js how to show line chart without displaying the labels on xaxis and yaxis
- How do i have a single Chart.vue component instead of different chart components and then display charts from it by changing id of the API
- Want to save chart.js charts to image using django cron job
- How to Draw a line on chart without a plot point 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 draw Line chart using chart.js and datalabels should be shown
- Chart changes different on zoom when using multiple charts with chart.js and flask
- How to achieve a mixed chart of 'horizontal' bars and line chart using Chart.js?
- How to take data from an API and create a chart on that using Chart.js and Angular 8?
- How can I build some chart with Chart.js and ng2-charts using an array of revenus & budgets per month date as Data source?
- How to run Chart.js samples using source code
More Query from same tag
- Chart.js label and point getting cutoff on the right
- Get X, Y onClick chart coordinates in ChartJS
- Flickering of charts and getcontext error with chartjs in the context of Vuejs
- flask altering python list
- how to set color of label in chartjs?
- Ng2 charts - Animation doesn't work on line chart onInit
- Set animation dynamically in Chart.JS
- Making my website interactive with the size of the screen/browser
- ChartJS create chart Ajax
- Using Different Colors In a Chart
- Chart.js plugins overlapping tooltips
- Doughnut Chart not displaying data with Chart Js and Backbone js
- Label issue in doughnut chart using chartjs
- Angular4 ng2-charts TypeError: Cannot read property 'length' of undefined
- change stroke line color in chart according to datasets in react native
- Javascript Chart: How to hide the data
- How to push Firestore data to ChartJS?
- Switching between different Chart.js charts using the same canvas
- How to call a typescript arrow function and a javascript function in the same event
- Chartjs: Align first tick in first index of axis
- Ajax reload data chat.js showing error on mouse hover on graph
- How to add padding to the vertical scale (X-axis) in Chart.js?
- Chartjs Nested Pie/Doughnut charts
- Remove empty spaces created by null values in Chart.js
- Chart.js: grid lines only on dataset points
- Chart.js Tooltip Information
- chart.js Multiple barchart in time scale won't display all
- Create Vue.js + Chart.js charts using datasets stored outside in a csv file
- Avoid the first GridLine to be dashed on Chart JS
- ChartJS xAxis time formats don't change for days