const stats = {‘advisers’: 100, ‘pas’: 681, ‘friends’: 1524, ‘total’: 2305}
const stats_countries = {‘United States’: 438,
‘United Kingdom’: 350,
‘Canada’: 75,
‘Australia’: 53,
‘France’: 38,
‘Germany’: 36,
‘Netherlands’: 34,
‘British’: 30,
‘Ireland’: 27,
‘Italy’: 25,
‘Sweden’: 25,
‘Spain’: 24,
‘Switzerland’: 21,
‘India’: 20,
‘Other (61)’: 219}
Members
const ctx = document.getElementById(‘myChart’).getContext(‘2d’);
const myChart = new Chart(ctx, {
type: ‘pie’,
data: {
labels: [‘Advisers’, ‘Professional Affiliates’, ‘Friends’],
datasets: [{
data: [stats[‘advisers’], stats[‘pas’], stats[‘friends’]],
backgroundColor: [
‘rgba(255, 99, 132, 0.5)’,
‘rgba(54, 162, 235, 0.5)’,
‘rgba(255, 206, 86, 0.5)’
],
borderColor: [
‘rgba(255, 99, 132, 1)’,
‘rgba(54, 162, 235, 1)’,
‘rgba(255, 206, 86, 1)’
],
borderWidth: 1
}]
},
options: {
responsive: false,
maintainAspectRatio: false,
plugins: {
legend: {
display: true,
position: ‘bottom’, // Position the legend at the top
align: ‘center’, // Aligns the legend items (‘start’, ‘center’, or ‘end’)
labels: {
boxWidth: 20, // Adjust the box width if needed
padding: 20, // Adjust padding for better spacing if needed
}
},
datalabels: {
color: ‘#000’,
formatter: (value, ctx) => {
// Combine the label with the value
//let label = ctx.chart.data.labels[ctx.dataIndex];
//return label + ‘: ‘ + value;
return value
},
font: {
weight: ‘bold’
}
}
}
},
plugins: [ChartDataLabels] // Register the plugin
});
// Calculate the total
const total = Object.values(stats_countries).reduce((sum, value) => sum + value, 0);
// Convert each value to a percentage of the total
const percentages = Object.fromEntries(
Object.entries(stats_countries).map(([key, value]) => [key, (value / total * 100).toFixed(0)])
);
// Extract labels and percentage data for the chart
const labels = Object.keys(percentages);
const data = Object.values(percentages);
const colors = [
‘rgba(255, 99, 132, 0.7)’, // Red
‘rgba(54, 162, 235, 0.7)’, // Blue
‘rgba(255, 206, 86, 0.7)’, // Yellow
‘rgba(75, 192, 192, 0.7)’, // Green
‘rgba(153, 102, 255, 0.7)’, // Purple
‘rgba(255, 159, 64, 0.7)’, // Orange
‘rgba(233, 30, 99, 0.7)’, // Pink
‘rgba(32, 178, 170, 0.7)’, // Light Sea Green
‘rgba(255, 215, 0, 0.7)’, // Gold
‘rgba(0, 128, 128, 0.7)’, // Teal
‘rgba(123, 31, 162, 0.7)’, // Dark Purple
‘rgba(0, 183, 195, 0.7)’, // Cyan
‘rgba(214, 69, 65, 0.7)’, // Soft Red
‘rgba(144, 202, 249, 0.7)’, // Light Blue
‘rgba(255, 87, 34, 0.7)’, // Deep Orange
‘rgba(126, 87, 194, 0.7)’, // Indigo
‘rgba(179, 157, 219, 0.7)’, // Lavender
‘rgba(77, 182, 172, 0.7)’, // Sea Green
‘rgba(255, 234, 0, 0.7)’, // Bright Yellow
‘rgba(38, 166, 154, 0.7)’ // Turquoise
];
// Function to get colors for each label
function getColorsForLabels(labels, colors) {
return labels.map((_, index) => colors[index % colors.length]);
}
const backgroundColors = getColorsForLabels(labels, colors);
const borderColors = backgroundColors.map(color => color.replace(/0.7\)$/, ‘1)’));
const ctx2 = document.getElementById(‘myChart_countries’).getContext(‘2d’);
const myChart2 = new Chart(ctx2, {
type: ‘doughnut’,
data: {
labels: labels,
datasets: [{
data: data,
backgroundColor: backgroundColors,
borderColor: borderColors,
borderWidth: 1
}]
},
options: {
responsive: false,
maintainAspectRatio: false,
plugins: {
legend: {
display: true,
position: ‘bottom’, // Position the legend at the top
align: ‘center’, // Aligns the legend items (‘start’, ‘center’, or ‘end’)
labels: {
boxWidth: 20, // Adjust the box width if needed
padding: 20, // Adjust padding for better spacing if needed
}
},
datalabels: {
color: ‘#000’,
formatter: (value, ctx) => {
// Combine the label with the value
//let label = ctx.chart.data.labels[ctx.dataIndex];
//return label + ‘: ‘ + value;
return value + “%”
},
font: {
weight: ‘bold’
}
}
}
},
plugins: [ChartDataLabels] // Register the plugin
});