score:1
I solved it by using a duplicate Donut (2nd canvas) with a value of 100, no animation and my desired background-color, and positioned it absolute, underneath the 1st one.
However, this is a nasty trick, and very inefficient, so I'm still hoping for a correct answer.
score:0
Probably there's no way how you can do it in the canvas element. I would place a absolute positioned element beyond the canvas. here's an example:
.fakeCircle {
position: absolute;
z-index: 0;
border-radius: 90px;
-webkit-border-radius: 90px;
-moz-border-radius: 90px;
background-color: #dadce8;
width: 50px;
height: 50px;
top: 12px;
left: 12px;
}
.fakeCircle:after {
position: absolute;
z-index: 0;
border-radius: 50px;
-webkit-border-radius: 50px;
-moz-border-radius: 50px;
background-color: #fff;
width: 34px;
height: 34px;
content: "";
}
score:2
I used @Jonlunsford 's code, but it didn't work when I upgraded ChartJS to 3.x.
According to the Migration Guide, It says
Chart.innerRadius now lives on doughnut, pie, and polarArea controllers
So I changed the code to:
import { Chart, DoughnutController } from 'chart.js'
type DoughnutChartBackgroundPluginOptions = {
enabled: boolean
color: string
}
function handler(chart: Chart<'doughnut'>, args, options: DoughnutChartBackgroundPluginOptions) {
const { ctx, width, height } = chart
const { innerRadius } = chart.getDatasetMeta(chart.data.datasets.length - 1).controller as DoughnutController
const { outerRadius } = chart.getDatasetMeta(0).controller as DoughnutController
const radiusLength = outerRadius - innerRadius
if (options.enabled) {
const x = width / 2,
y = height / 2
ctx.beginPath()
ctx.arc(x, y, outerRadius - radiusLength / 2, 0, 2 * Math.PI)
ctx.lineWidth = radiusLength
ctx.strokeStyle = options.color
ctx.stroke()
}
}
export default {
id: 'doughnutChartBackground',
beforeDatasetsDraw: handler,
}
Then, when creating a chart, you can use the options as follows:
...
plugins: {
legend: {
display: false,
},
doughnutBackground: {
enabled: true,
color: '#E4E6E6',
},
...
},
score:11
Thought I'd post a recent solution that worked for me, using v2.1.0 which introduced plugins.
Chart with no value displaying a background vs chart with value covering the background, only the main chart will animate, the background is just a simple arc:
I first registered a plugin per their docs:
var radiusBackground = function() {
var self = this;
self.draw = function(chartInstance) {
if(chartInstance.options.radiusBackground) {
var x = chartInstance.chart.canvas.clientWidth / 2,
y = chartInstance.chart.canvas.clientHeight / 2,
ctx = chartInstance.chart.ctx;
ctx.beginPath();
ctx.arc(x, y, chartInstance.outerRadius - (chartInstance.radiusLength / 2), 0, 2 * Math.PI);
ctx.lineWidth = chartInstance.radiusLength;
ctx.strokeStyle = chartInstance.options.radiusBackground.color || '#d1d1d1';
ctx.stroke();
}
};
// see http://www.chartjs.org/docs/#advanced-usage-creating-plugins for plugin interface
return {
beforeDatasetsDraw: self.draw,
onResize: self.draw
}
};
// Register with Chart JS
Chart.plugins.register(new radiusBackground());
The singleton syntax was just to be able to reduce duplication and use the same draw
method for multiple plugin events.
Then I used my new registered plugin like so:
var chartElement = document.getElementById('doughnut-chart');
var chart = new Chart(chartElement, {
type: 'doughnut',
options: {
// Here is where we enable the 'radiusBackground'
radiusBackground: {
color: '#d1d1d1' // Set your color per instance if you like
},
cutoutPercentage: 90,
title: {
display: false,
},
legend: {
display: false,
},
},
data: {
labels: ["Type 1", "Type 2", "Type 3"],
datasets: [{
data: [2, 5, 1],
backgroundColor: ["#a3c7c9","#889d9e","#647678"],
borderWidth: 0,
hoverBackgroundColor: ["#96b7b9","#718283","#5c6b6d"]
}]
}
});
Source: stackoverflow.com
Related Query
- Chart.js line chart set background color
- set background color to save canvas chart
- Is it possible to set the background color of a sector in a radar chart in Chart.js?
- Chart area background color chartjs
- How set color family to pie chart in chart.js
- How to set a full length background color for each bar in chartjs bar
- how to set chart.js grid color for line chart
- Chartjs doughnut chart with gradient color
- Chart.js - Color specific parts of the background in a line chart
- How to set single color on each bar in angular chart js
- How to add background color for doughnut mid using chart,js
- Chart js background color changes on click
- How to use set the color for each bar in a bar chart using chartjs?
- Change the background color of tooltip dynamically using chart color
- Remove background color in chartjs line chart
- How to change background color of labels in line chart from chart.js?
- Background color of the chart area in chartjs not working
- Chart JS Line Graph multitooltipkey background color issue
- Set background color for individual bars using ChartJS?
- Chart Js - Globally set bar chart color based on value
- How can i give the full Legend a background color in chart js?
- How to make the background color of the chart as gradient using Chart.js
- Multi Level Color in Doughnut Chart in ng2-charts
- Legend color not working with randomly generated background colors in chartjs pie chart
- How can I add background color of length bars in chart (chartJS)?
- angular chart js set fill color of bar chart
- ChartJs - set background color of the space between ticks
- The colors of the bar chart in chart.js is not showing. I tried background color and fill color but none of it worked
- Chart.js Polar Area Chart scale background color
- react-chartjs-2 how to set multiple background levels within a line chart
More Query from same tag
- Chartjs not picking up twig variable for label
- How to remove the line/rule of an axis in Chart.js?
- Chart.js not showing in my view
- impossible to remove scale from radar chart (chart.js)
- Chart.JS Get Json data for chart and return dataset where a type equals a certain type
- Vue Chart.js component does not render
- Chartjs background color multiple Dataset
- Chart.js Treemap Custom text
- How to pass values to a chart (chart.js / morris.js)
- ChartJS creating var takes each character and outputs undefined
- ChartJS does not render correctly
- how to remove odd number from yaxes in chartjs using angular?
- Chart js cut the title and the legends
- How can i change tick val to title in ChartJS
- Remove x-axis label/text in chart.js
- ChartJs. How to stretch up line graph to the edges?
- angular-chart.js doughnut chart : how to show data in the center of a chart
- How can I build a double doughnut chart that spins when triggered
- Why my ChartJs only Coloring one section?
- Zoom and Pan in react-chartjs-2
- ChartJS customize left and bottom axe border
- Chart.js stacked bar chart in opposite direction
- How to make bootstrap 4 column height the same as row height
- chartjs default background color to columns
- How to convert an array of strings to float in Javascript
- creating a chart.js scatter graph with variables for data
- chartjs - how to set the order in which the different charts are displayed
- How do I remove the y-axis labels from a graph?
- $(...).getContext("2d") is not a function when using chart.js
- How to set default callback for tooltip title with chart.js