How to create circular progress(pie chart) like indicator

3 min read 08-10-2024
How to create circular progress(pie chart) like indicator


Creating a circular progress indicator, commonly referred to as a pie chart, is a popular and visually appealing way to represent data in a compact format. In this article, we will walk through the steps needed to create a circular progress indicator, provide examples of code implementation, and share insights on best practices for designing effective indicators.

Understanding Circular Progress Indicators

A circular progress indicator visually conveys how much of a certain task has been completed. It's often used in user interfaces to represent loading states, completion rates, and other tasks that require user attention. This visual feedback allows users to grasp information quickly and clearly.

Scenario Overview

Imagine you are developing a web application where users can track their fitness progress. You want to create an engaging circular progress indicator that visually represents the percentage of fitness goals achieved.

Original Code Example

Let’s start with a simple example of a circular progress indicator using HTML5 and CSS:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Circular Progress Indicator</title>
    <style>
        .circular-progress {
            position: relative;
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background: conic-gradient(#4caf50 0%, #4caf50 var(--progress), #ddd var(--progress), #ddd 100%);
        }

        .percentage {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 20px;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="circular-progress" style="--progress: 75%;">
        <div class="percentage">75%</div>
    </div>
</body>
</html>

Code Breakdown

  1. HTML Structure: The circular progress indicator is created with a div that has a class of circular-progress. Inside this div, another div displays the percentage of completion.

  2. CSS Styles:

    • The outer div is styled to create a circular shape using border-radius: 50%.
    • The background property utilizes conic-gradient, which allows for creating a gradient effect in a circular manner.
    • The --progress variable dynamically represents the completion percentage, which can be adjusted accordingly.

Insights and Analysis

While the basic implementation above provides a foundation for a circular progress indicator, we can enhance it by allowing dynamic updates. For example, you might want to update the progress based on user interactions or real-time data.

Here's an example of JavaScript code that updates the progress:

function updateProgress(value) {
    const circularProgress = document.querySelector('.circular-progress');
    const percentageDisplay = document.querySelector('.percentage');

    circularProgress.style.setProperty('--progress', `${value}%`);
    percentageDisplay.textContent = `${value}%`;
}

// Example: Update progress to 85% after 2 seconds
setTimeout(() => {
    updateProgress(85);
}, 2000);

Best Practices for Circular Progress Indicators

  1. Clarity: Ensure the percentage displayed is clear and easily readable. Avoid overly complex designs that may confuse users.

  2. Colors: Use contrasting colors for different sections of the pie chart to emphasize the progress while maintaining overall aesthetic appeal.

  3. Accessibility: Consider incorporating text labels or tooltips for accessibility purposes, ensuring that all users can understand the progress.

  4. Responsiveness: Design your circular progress indicator to be responsive, ensuring it looks good on various devices and screen sizes.

  5. Animation: Adding a subtle animation when updating the progress can make the indicator more engaging, but be careful not to make it distracting.

Conclusion

Creating a circular progress (pie chart) indicator is a straightforward process that adds a professional touch to any web application. By following the steps outlined in this article, you can implement a visually appealing and functional progress indicator. Remember to keep user experience in mind and utilize best practices to enhance clarity and accessibility.

Additional Resources

By implementing the concepts and practices discussed above, you will have a powerful tool to communicate progress effectively within your applications. Happy coding!