I have data in specific format like below:
const clockData = [
{ label: "Balance", value: 360 },
{ label: "Booking 1", value: 255 },
{ label: "Break 1", value: 45 },
{ label: "Booking 2", value: 60 }.. and so on
];
// Compute the position of each group on the pie:
var pie = d3.pie()
// .startAngle(-1.5)
.value(function(d) {return d.value; }).sort(null)
var data_ready = pie(d3.entries(data))
console.log(data_ready);
const clockData = [
{ label: "Balance", value: 360 },
{ label: "Booking 1", value: 255 },
{ label: "Break 1", value: 45 },
{ label: "Booking 2", value: 60 }
];
// Use colors from newColors array
const color = d3.scaleOrdinal()
.domain(clockData.map(d => d.label))
.range(newColors);
// Calculate total time (720 minutes for a 12-hour clock)
const totalTime = 720;
// Convert time to minutes (e.g., 9 am = 9 * 60 = 540 minutes)
let timeInMinutes = initialTimeInMinutes;
console.log("initialTimeInMinutes ",initialTimeInMinutes);
// Example usage
console.log(getAngleOffset(0)); // Should print -0.25 (12 o'clock)
console.log(getAngleOffset(180)); // Should print 0 (3 o'clock)
console.log(getAngleOffset(360)); // Should print 0.25 (6 o'clock)
console.log(getAngleOffset(540)); // Should print 0.50 (9 o'clock)
console.log(getAngleOffset(720)); // Should print -0.25 (12 o'clock again)
// Testing intermediate values
console.log(getAngleOffset(90)); // Should print between -0.25 and 0
console.log(getAngleOffset(270)); // Should print between 0 and 0.25
console.log(getAngleOffset(450)); // Should print between 0.25 and 0.50
console.log(getAngleOffset(630)); // Should print between 0.50 and -0.25
let magicConstant = getAngleOffset(initialTimeInMinutes)
// Calculate angles for each slice based on the proportion of total time
// Calculate angles for each slice based on the proportion of total time
formattedData.forEach(d => {
d.startAngle = ((timeInMinutes / totalTime) + magicConstant) * 2 * Math.PI;
d.endAngle = (((timeInMinutes + d.value) / totalTime) + magicConstant) * 2 * Math.PI;
timeInMinutes += d.value; // Update time for the next slice
});
// Build the pie chart: Basically, each part of the pie is a path that we build using the arc function.
// Build the clock representation
svg.selectAll()
.data(clockData)
.enter()
.append('path')
.attr('d', d3.arc()
.innerRadius(42) // This is the size of the donut hole
.outerRadius(radius)
)
.attr('fill', d => color(d.label));
function getAngleOffset(minutes) {
const totalMinutes = 720; // Total minutes in a 12-hour clock
const offsets = {
0: -0.25, // 12 o'clock
180: 0, // 3 o'clock
360: 0.25, // 6 o'clock
540: 0.50, // 9 o'clock
720: -0.25 // Back to 12 o'clock
};
// Find the closest base point
const basePoints = [0, 180, 360, 540, 720];
let closestBase = 0;
for (let i = 0; i < basePoints.length; i++) {
if (minutes <= basePoints[i]) {
closestBase = basePoints[i];
break;
}
}
// Calculate the proportion of the way to the next base point
let previousBase = (closestBase === 0) ? 720 : basePoints[basePoints.indexOf(closestBase) - 1];
let proportion = (minutes - previousBase) / (closestBase - previousBase);
// Calculate the offset
let startOffset = offsets[previousBase];
let endOffset = offsets[closestBase];
return startOffset + proportion * (endOffset - startOffset);
}
now i want to plot that clock data on donut chart like 540 minutes represents the position of clock is at 9 and also the bookings can be as many as possible but it is not plotting correctly on donut chart here i am considering the donut chart as clock with 12 is -0.25 offset 0 is at 3, offset 0.25 is at 6, and offset 0.50 at 9.
Any help on that will be appreciated much