score:0
Accepted answer
you can use a custom inline plugin for this in combination with a scriptable option for the borderwidth
itself.
plugin:
{
id: 'circlearoundpiedoughnut',
beforedatasetsdraw: (chart, args, options) => {
if (chart.data.datasets[0].data.length > 1) return;
const {
ctx,
canvas
} = chart;
const {
top,
left,
width,
height
} = chart.chartarea;
const x = left + width / 2;
const y = top + height / 2;
const outerradius = chart._metasets[0].data[0].outerradius;
ctx.beginpath()
ctx.arc(x, y, outerradius + ((options.width || 0) / 2), 0, 2 * math.pi);
ctx.linewidth = options.width || 1
ctx.strokestyle = options.color || '#000';
ctx.stroke();
}
}
live example:
var options = {
type: 'doughnut',
data: {
labels: ["red"],
datasets: [{
label: '# of votes',
data: [12],
borderwidth: (ctx) => (ctx.chart.data.datasets[0].data.length === 1 ? 0 : 3),
backgroundcolor: ["red"]
}]
},
options: {
radius: '98%',
plugins: {
legend: {
display: false
},
circlearoundpiedoughnut: {
width: 6, // width of the border, defaults to 1
color: 'blue' // color of the border, defaults to black
}
}
},
plugins: [{
id: 'circlearoundpiedoughnut',
beforedatasetsdraw: (chart, args, options) => {
if (chart.data.datasets[0].data.length > 1) return;
const {
ctx,
canvas
} = chart;
const {
top,
left,
width,
height
} = chart.chartarea;
const x = left + width / 2;
const y = top + height / 2;
const outerradius = chart._metasets[0].data[0].outerradius;
ctx.beginpath()
ctx.arc(x, y, outerradius + ((options.width || 0) / 2), 0, 2 * math.pi);
ctx.linewidth = options.width || 1
ctx.strokestyle = options.color || '#000';
ctx.stroke();
}
}]
}
var ctx = document.getelementbyid('chartjscontainer').getcontext('2d');
new chart(ctx, options);
<body>
<canvas id="chartjscontainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/3.3.0/chart.js"></script>
</body>
score:0
that's brilliant - i've implemented this, but seem to be getting a slightly boxy circle that extends beyond the bounds of the canvas in circumstances where the function draws the border where there's only one datapoint. any ideas on how to fix?
function makepie(a,b,c,d){
if (b <= 0) {n = 'n'} else {n = 'y'}
if (c <= 0) {o = 'n'} else {o = 'y'}
if (d <= 0) {p = 'n'} else {p = 'y'}
q = n + o + p
if (q == 'nnn') {s = [a];r = ['indianred'];u = ['emails']}
if (q == 'yyy') {s = [a, b, c, d];r = ['indianred', 'salmon', 'pink', 'mistyrose'];u = ['emails','calls','meetings','working']}
if (q == 'ynn') {s = [a, b];r = ['indianred', 'salmon'];u = ['emails','calls']}
if (q == 'nyn') {s = [a, c];r = ['indianred', 'pink', ];u = ['emails','working']}
if (q == 'nny') {s = [a, d];r = ['indianred', 'mistyrose'];u = ['emails','working']}
if (q == 'yyn') {s = [a, b, c];r = ['indianred', 'salmon', 'pink'];u = ['emails','calls','meetings']}
if (q == 'yny') {s = [a, b, d];r = ['indianred', 'salmon', 'mistyrose'];u = ['emails','calls','working']}
if (q == 'nyy') {s = [a, c, d];r = ['indianred', 'pink', 'mistyrose'];u = ['emails','meetings','working']}
var options = {
type: 'pie',
data: {
labels: u,
datasets: [{
data: s,
bordercolor: '#6f6f6f',
borderwidth: (ctx) => (ctx.chart.data.datasets[0].data.length === 1 ? 0 : 3),
backgroundcolor: r
}]
},
options: {
hover: {mode: null},
animation: {duration: (math.floor(math.random() * 30) + 20) * 100},
legend: {display: false,labels: {display: false}},
rotation: (math.floor(math.random() * 5)+1),
tooltips: {enabled: false},
plugins: {
legend: {display: false},
circlearoundpiedoughnut: {
width: 3,
color: '#6f6f6f'
}
}
},
plugins: [{
id: 'circlearoundpiedoughnut',
beforedatasetsdraw: (chart, args, options) => {
if (chart.data.datasets[0].data.length > 1) return;
var {
ctx,
canvas
} = chart;
var {
top,
left,
width,
height
} = chart.chartarea;
var x = left + width / 2;
var y = top + height / 2;
var outerradius = chart._metasets[0].data[0].outerradius;
ctx.beginpath()
ctx.arc(x, y, outerradius + ((options.width || 0) / 2), 0, 2 * math.pi);
ctx.linewidth = options.width || 1
ctx.strokestyle = options.color || '#000';
ctx.stroke();
}
}],
}
var ctx = document.getelementbyid('chartjscontainer').getcontext('2d');
new chart(ctx, options);
}
Source: stackoverflow.com
Related Query
- Is there a way to give a pie chart in chart.js a different color outer border than the border color between segments?
- Is there any way to use 2 different color for the same bar in a chart?
- chart.js pie chart background colors: is there any way to generate them randomly?
- How to style a pie chart in chart js? I want to change the border color, border width and give them shadow
- chart.js Is there way to color line chart ticks depending on value
- ChartJS – is there any way to remove blank space around pie charts?
- How set color family to pie chart in chart.js
- chartjs datalabels change font and color of text displaying inside pie chart
- Chart.js v2 is there a way to draw bar chart horizontally?
- Different color for each column in angular-chartjs bar chart
- chart js - Apply different color for each x-axes label
- Angular 8 & ChartJs change color in pie chart
- Chart.js different scaleLine color of radar chart (angular)
- Use transparent stroke color with chartjs pie chart
- Is there a way to change color of a chart's grid in y-axis - ng2-charts
- How to use segment property to color line / border color based on value in chart js?
- Is there any way to get Chiselled Effect in chart js?
- Is there any way to change the font color and size of labels in Chartjs 3.0.0
- Is there a convenient way to print values to a JS chart from a CSV file without converting it?
- Chart.js Doughnut chart inner label different than outer
- Is there a way in chartjs to display different Boolean values with an offset in Y over a common timeline?
- How can i give the full Legend a background color in chart js?
- Legend color not working with randomly generated background colors in chartjs pie chart
- Is there any way to remove extra space and Horizontal line from Bar chart of Chart.js?
- Pie Donut chart with different sector sizes
- Is there a way to display a chart label that has no data behind it in Chart.js
- How to assign different background colors to chart.js pie chart data set?
- ChartJS different border color for legend
- Show/hide All nested data in ChartJS Pie chart when outer is shown/hidden
- Is there any way to change the font size of labels in bar chart in Chart.js v3?
More Query from same tag
- Show base64 image next to canvas with charts.js
- Displaying every nth label on the x-axis
- How to get chartjs column chart in ng-repeat
- Space between ticks marks and X axis with chart.js
- How to update chart at the end of every for loop in Chart.js?
- How to load an array of external data in QuickChart?
- chartJS Horizontal Bar - Hide Stacked Bars But Still Show In Tooltip
- Chart.js not updating or receiving data from Flask python
- Chartjs Nested Pie/Doughnut charts
- how to creat a dynamic datasets and new dynamic multi yAxes
- Possible to create quadrant chart with ChartJS, with the "origin" centered at a single point?
- One common component for Chart.js to use with different y-axis With React.js
- Input data in german data format combined with ChartsJs
- How to draw a range label on x axis using chartJS
- How to display labels outside the pie chart border?
- How to put 2 charts next to each other
- Data will not draw onto my chart
- How to set max ticks count in chart.js 3.*.*
- chart.js not getting linked with online database and php
- Show values on each arc doughnut Chart.js
- Background-color for area below line chart in chartJs?
- Charts.js v2: always show custom (html) tooltips on chart
- ChartJS - Override Attributes (AngularJS)
- chartnew.js - remove lines from pie chart on 100%
- chartJS, how to disable user from clicking on legend names and changing the graph?
- Combine Chart.js programmatic and legend based dataset toggling
- Drawing linear chart without gaps in Chart.js
- Displaying Legend only once when Several pie charts are drawn in the same page using chart.js
- Why Chart.js v2 time based data not aligning with X axis correctly?
- problem hidding the label in chartjs (ng2chart for angular)