How to Create a CSS Percentage Circle with jQuery

Apr 14, 2023 | Programming

If you’re looking to display dynamic progress on a web page, a percentage circle can be an appealing way to visually represent that data. This guide will walk you through creating your own CSS percentage circle using jQuery, while also providing various customization options.

Step 1: Set Up Your Project

Before getting started, ensure you have jQuery installed in your project. You can either use npm or Bower as follows:

  • For npm, run: npm install percircle
  • For Bower, use: bower install percircle

Step 2: Load the Required Modules

Next, load the jQuery and percircle scripts in your HTML file:

<!DOCTYPE HTML>
<html>
<head>
    <script src="..bower_components/jquery/dist/jquery.min.js"></script>
    <script type="text/javascript" src="..dist/js/percircle.js"></script>
    <link rel="stylesheet" href="..dist/css/percircle.css">
</head>
<html>

Step 3: Create Your HTML Structure

You will need to define the circle objects in your HTML. Here’s an example setup:

<div id="bluecircle" data-percent="17" class="bigdiv">
    <p align="center">
        <img src="_img/big_blue_17.gif" alt="Big blue circle">
    </p>
</div>

Step 4: Initialize the Circle with JavaScript

Once your HTML is set up, you need to let the script do the magic:

<script type="text/javascript">
    $(function() {
        $('#bluecircle').percircle();
    });
</script>

Step 5: Dynamic Updates

To dynamically update the circle, you’ll first need a button or link:

<div id="redBecomesBlue" class="red bigdiv">
    <a href="javascript:void(0);" id="changeCircleFill">Circle to 95.5%</a>
    <p align="center">
        <img src="_img/update-value-dynamically.gif" alt="Big blue circle">
    </p>
</div>

Then implement a click event to change the percentage:

$(document).ready(function() {
    $('#redBecomesBlue').percircle({percent: 25, text: 25});
    $('#changeCircle').click(function(e) {
        e.preventDefault();
        changeCircle();
    });
});

function changeCircle() {
    $('#redBecomesBlue').percircle(text: '', percent: 95.5, progressBarColor: '#1252c0');
}

Step 6: Customization Options

The percircle function can take various options to customize the behavior and appearance:

  • Animate: Animate the progress bar on load (default: true).
  • Clock: Display a clock within the circle (default: false).
  • Countdown: Set a countdown timer (default: false).
  • Progress bar color: Customize the color of the progress bar (default: inherited).

Troubleshooting

Running into issues? Here are some troubleshooting tips:

  • Make sure you have correctly loaded the jQuery library before percircle.
  • Check your HTML for syntax errors, as they can prevent proper rendering.
  • Ensure the paths to your JavaScript and CSS files are correct.
  • If the percentage circle does not update, verify that the jQuery selector in your script matches the circle’s ID.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

You’ve now set up a CSS percentage circle using jQuery! This engaging element can enhance the interactivity of your web projects and provide a clear visual representation of data.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox