Giter VIP home page Giter VIP logo

easy-pie-chart's Introduction

easy-pie-chart

Lightweight plugin to render simple, animated and retina optimized pie charts

Version Build Status Dependencies Status Analytics

Features

  • highly customizable
  • very easy to implement
  • resolution independent (retina optimized)
  • uses requestAnimationFrame for smooth animations on modern devices and
  • works in all modern browsers, even in IE7+ with excanvas

framework support

  • Vanilla JS (no dependencies) (~872 bytes)
  • jQuery plugin (~921 bytes)
  • Angular Module (~983 bytes)

Get started

Installation

You can also use bower to install the component:

$ bower install jquery.easy-pie-chart

jQuery

To use the easy pie chart plugin you need to load the current version of jQuery (> 1.6.4) and the source of the plugin.

<div class="chart" data-percent="73" data-scale-color="#ffb400">73%</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="/path/to/jquery.easy-pie-chart.js"></script>
<script>
    $(function() {
        $('.chart').easyPieChart({
            //your options goes here
        });
    });
</script>

Vanilla JS

If you don't want to use jQuery, implement the Vanilla JS version without any dependencies.

<div class="chart" data-percent="73">73%</div>

<script src="/path/to/easy-pie-chart.js"></script>
<script>
    var element = document.querySelector('.chart');
    new EasyPieChart(element, {
        // your options goes here
    });
</script>

AngularJS

<div ng-controller="chartCtrl">
    <div easypiechart options="options" percent="percent"></div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<script src="../dist/angular.easypiechart.min.js"></script>
<script>
    var app = angular.module('app', ['easypiechart']);
    app.controller('chartCtrl', ['$scope', function ($scope) {
        $scope.percent = 65;
        $scope.options = {
            animate:{
                duration:0,
                enabled:false
            },
            barColor:'#2C3E50',
            scaleColor:false,
            lineWidth:20,
            lineCap:'circle'
        };
    }]);
</script>

Options

You can pass these options to the initialize function to set a custom look and feel for the plugin.

Property (Type) Default Description
barColor #ef1e25 The color of the curcular bar. You can either pass a valid css color string, or a function that takes the current percentage as a value and returns a valid css color string.
trackColor #f2f2f2 The color of the track, or false to disable rendering.
scaleColor #dfe0e0 The color of the scale lines, false to disable rendering.
scaleLength 5 Length of the scale lines (reduces the radius of the chart).
lineCap round Defines how the ending of the bar line looks like. Possible values are: butt, round and square.
lineWidth 3 Width of the chart line in px.
size 110 Size of the pie chart in px. It will always be a square.
rotate 0 Rotation of the complete chart in degrees.
animate object Object with time in milliseconds and boolean for an animation of the bar growing ({ duration: 1000, enabled: true }), or false to deactivate animations.
easing defaultEasing Easing function or string with the name of a jQuery easing function

Callbacks

All callbacks will only be called if animate is not false.

Callback(params, ...) Description
onStart(from, to) Is called at the start of any animation.
onStep(from, to, currentValue) Is called during animations providing the current value (the method is scoped to the context of th eplugin, so you can access the DOM element via this.el).
onStop(from, to) Is called at the end of any animation.

Plugin api

jQuery

$(function() {
    // instantiate the plugin
    ...
    // update
    $('.chart').data('easyPieChart').update(40);
    ...
    // disable animation
    $('.chart').data('easyPieChart').disableAnimation();
    ...
    // enable animation
    $('.chart').data('easyPieChart').enableAnimation();
});

Vanilla JS

// instantiate the plugin
var chart = new EasyPieChart(element, options);
// update
chart.update(40);
// disable animation
chart.disableAnimation();
// enable animation
chart.enableAnimation();
Using a gradient
new EasyPieChart(element, {
  barColor: function(percent) {
    var ctx = this.renderer.getCtx();
    var canvas = this.renderer.getCanvas();
    var gradient = ctx.createLinearGradient(0,0,canvas.width,0);
        gradient.addColorStop(0, "#ffe57e");
        gradient.addColorStop(1, "#de5900");
    return gradient;
  }
});

AngularJS

For a value binding you need to add the percent attribute and bind it to your controller.

RequireJS

When using RequireJS you can define your own name. Examples can be found in the demo/requirejs.html.

Browser Support

Native support

  • Chrome
  • Safari
  • FireFox
  • Opera
  • Internet Explorer 9+

Support for Internet Explorer 7 and 8 with excanvas polyfill.

Test

To run the test just use the karma adapter of grunt: grunt test

Credits

Thanks to Rafal Bromirski for designing this dribble shot which inspired me building this plugin.

Copyright

Copyright (c) 2015 Robert Fleischmann, contributors. Released under the MIT, GPL licenses

easy-pie-chart's People

Contributors

alex-pex avatar amsross avatar brunocavalcante avatar chafnan avatar chenys avatar dotansimha avatar epost avatar evrpress avatar frankvanrest avatar jennieji avatar levito avatar linkgod avatar mediastuttgart avatar nullcosmos avatar peters avatar procynic avatar rendro avatar scatcher avatar sgtpepper43 avatar stereoscott avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

easy-pie-chart's Issues

Draw arrow at the end of the chart

Hi

It would be great to add support for drawing arrow (or other shapes) at the end of the chart. I guess there are 2 ways to do it, either place an image at the end or draw it directly on canvas. I am struggling with this currently, is there anyone with some ideas how to achieve this?

Thanks

Not working on iPad 1

There is an javscript error. 'undefined' is not a function.
It seem it is only a problem with an old safari version

Implemented it:

jQuery(document).ready(function($) {
    $('.chart').easyPieChart({
        size: '200',
        lineWidth: '6',
        lineCap: 'square',
        scaleColor: '#d5d5d5',
        trackColor: '#eeeeee',
        barColor: '#0099cc',
        easing: 'easeOutBounce',
        onStep: function(from, to, percent) {
            $(this.el).find('.percent').text(Math.round(percent));
        }
    });

    var chart = window.chart = $('.chart').data('easyPieChart');

    $('.js_update').on('click', function() {
        chart.update(Math.random()*100);
    });
});

100 on data-percent shows 99% on pie

Hi great plugin and great work. just noticed that in your example page and in my page as well, if I put 100 in the data attribute then it shows always 99% on the bar. am i doing something wrong ?

[AngularJS] Options dynamically

Is it possible to change options dynamically, and chart redraw automatically ? I have tried and with no success.

Something like this:

var app = angular.module('app', ['easypiechart']);
app.controller('chartCtrl', ['$scope', function ($scope,frameworkFactory) {
...
   $scope.start = function(){
        var data = frameworkFactory.getData();
        data.$get().then(function(v){
            $scope.percent = data[$scope.data].percent;
            $scope.options = data[$scope.data].options;
        });
    }
    $scope.start();
...

Animate causes random values on ios

I was using easy-pie-chart in a website template, when I noticed that on my iphone 4 (safari), the charts were coming up with random values - sometimes in the negatives, and 4 digits long. Both the charts and the numbers were random. I disabled all other js (other than jquery) and they were still all over the show. At a guess, I disabled animation and they acted fine.

This is what was in my initilization code and html if you want to try and replicate it. I would try and help solve it, but I have no idea what I'm doing with javascript

<script>
$(function() {
    $('.chart').easyPieChart({
        barColor: '#60899a',
        trackColor: false,
        scaleColor: false,
        lineCap: 'round',
        rotate: 180,
        lineWidth: 10,
        size: 150,
        //animate: 1000,


        onStep: function(value) {
            this.$el.find('span').text(~~value);
        }

    });
});
</script>

<div class="chart" data-percent="50"><span>50</span></div>

Negative values

Greetings,

Excellent work with this plugin, just for the record, I got some negative values (or imcoplete ones) after a webpage reload (Ctrl + R or F5), for example, I tell to the plugin draw 35% sometimes it draws 29% or so, in a worse scenario it give me negative percents, also the negative percents appear after a long time stay on the page.

This is my code:

$('.percentage-light').easyPieChart({
barColor: function(percent) {
percent /= 100;
return "rgb(" + Math.round(255 * (1-percent)) + ", " + Math.round(255 * percent) + ", 0)";
},
trackColor: '#666',
scaleColor: false,
lineCap: 'butt',
rotate: -90,
lineWidth: 15,
animate: 1000,
onStep: function(value) {
this.$el.find('span').text(~~value);
}
});

image001

Thanks for your help.
Andrés.

The example in IE8 on XP doesn't work.

I've had a problem with making your plugin work with IE8 and then realized that your example doesn't work either. The code fails at date.now() on line 131. I've tried a few different ways to fix it but when I use Date().getTime() for example then the percentages seem to go in all directions. It's like there is slight flicker at the end of the animation and then a change in percentages that doesn't seem to make sense. FF and Chrome are fine displaying the right values.

Sam

Better parameter handling for colors

If trackColor or scaleColor are set without the preceding # then the plugin fails to color the chart on FF (it works fine in chrome).

I would add a simple check to add the # to the beginning of the color if it's not there.

chart inner circle color

Could you please make this plugin that also allows you too give your chart a color for your inner circle.

Showing an img as Value

Hello.

I am using your plugin to show a countdown until a webrequest gets started. Thsi webrequest takes about 3 seconds. During that time I would like to show a preloader-gif inside the piechart.

I have tried this: $('span', $('.chart')).text('>img src="images/ajax-loader.gif" /<');

I have reversed the < > because github would try to parse it.

Instead of the image it prints just the raw text.

Any idea how I could do that ?

Animate causes random values on ios

I was using easy-pie-chart in a website template, when I noticed that on my iphone 4 (safari), the charts were coming up with random values - sometimes in the negatives, and 4 digits long. Both the charts and the numbers were random. I disabled all other js (other than jquery) and they were still all over the show. At a guess, I disabled animation and they acted fine.

This is what was in my initilization code and html if you want to try and replicate it. I would try and help solve it, but I have no idea what I'm doing with javascript

$(function() {
$('.chart').easyPieChart({
barColor: '#60899a',
trackColor: false,
scaleColor: false,
lineCap: 'round',
rotate: 180,
lineWidth: 10,
size: 150,
//animate: 1000,
onStep: function(value) {
this.$el.find('span').text(~~value);
}
});
});

50

angular module: Move options into single attribute and provide it as json

We should provide the options format for the angular module as JSON format in a separated attribute to be more conventional. That also will solve issues like referencing to variables (#44).

Example for easy-pie-chart:

<div 
    easypiehcart
    ng-options="{ barColor: '#ef1e25', trackColor': '#f2f2f2', 'scaleColor': '#dfe0e0', 'scaleLength': 5 }"
    ng-percent="percent"
></div>

A way better:

<div easypiechart ng-options="options" ng-percent="percent"></div>
var app = angular.module('app', ['easypiechart']);
app.controller('chartCtrl', ['$scope', function ($scope) {
    $scope.percent = 65;
    $scope.options = { 
        barColor: '#ef1e25', 
        trackColor: '#f2f2f2', 
        scaleColor: '#dfe0e0', 
        scaleLength: 5
    };
}]);

What do you think about it ?

AMD support

Hi there

Can you add AMD support? For RequireJS ...

Thx!

Custom track styles

Hi, Is it possible to add a inset shadow to the track so the graph will pop more? I know I can only send a color hex value through for now.

AMD Support not working (RequireJS)

I keep on getting Uncaught TypeError: Cannot read property 'fn' of undefined which means PieChart is trying to execute before jQuery is loaded.

My RequireJS looks good though..

requirejs.config({
    "baseUrl": "/theme/Ias/js/lib",
    "paths": {
        "app":"../app",
        "jquery":"//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min",
        "jquery-ui":'jquery-ui-1.10.3.effects.min',
        "EasyPieChart":"jquery.easypiechart.min",
        "typekit":"//use.typekit.net/knu6zcf",
    },         
});

requirejs(["app/common"]);

reload "onmouseover" but keep the same value

Hello, Thank you for this work.
I was trying a way to reload the chart "onmouseover" and I did that by adding it here --> jQuery ('.updateEasyPieChart').on('mouseover', function(e) {...}. But I dont know how to update it, but keep the same percentage value(ex:80%). There is anyway to do that?

Thank you

"scaleColor: false" shows up as an error on Firefox for MAC 13.0.1

Hi there,

When the scaleColor is set to false, the chart will not be render on firefox I don't know why. I look up the Error Console and it shows:

uncaught exception: [Exception... "Could not convert Javascript argument arg 0 [[email protected]]" nsresult: "0x80570009(NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: ... :: <TOP_LEVEL> :: line 70" data: no];

Can someone look into the issue? Thanks

HTML Content

Hi,

I'd like the content of my 'chart' div to be two line, rather than just one 'percent' or 'total' line. When I put in a
or any other HTML that would cause a line break, the second line is displayed outside the graphic below it. Am I doing something wrong? Is there a way to do this?

Thanks,

  • Adam

Render the graph in full-size canvas

Hey,

Thank you so much for taking care of that little bug with firefox. Here's a another question or possibly a request. I guess when you create a pie chart there's always a border gap reserved for scale in canvas. Is it possible to remove that gap and let the pie chart take the whole space of canvas when the scaleColor is set to false? Once again, thanks for the great work here.

Brian

Decimal places

Would it be possible to display numbers with decimal places in the center? They are all rounded at the moment. 58,7% makes 59% in the chart. Perhaps there could be an option to configure this.

AngularJS Directive: set options with variables

hi! At first thx for the great plugin and the angular support :) From the examples I have seen that the options for the chart are passed as strings. Is it possible to configure the chart instance using scope variables in the controller? As a result the configuration would be reusable and very flexible.

0% in IE8 shows full pie chart

Hi there,

It seems that in IE8 any chart at 0% shows a full pie chart, but if you go up just by 1% it shows correctly. It also shows 100% as full bar. Is there a simple workaround for this?

Have you got fix for Retina displays

I noted today, testing that on retina display ( such as iPad4 ) the canvas shrinks. I have read some articles on setting the retina display ( hidef ) within the script, just wondered if a implemented fix has been made or if indeed others have similar issues

Mootools + jQuery + easyPieChart

Hi Robert,

We are using your code in a site where we have jQuery and Mootools side by side and unfortunately your script failed to work in this environment.

Here is an example:
http://jsfiddle.net/6ZdNZ/

Error is:
TypeError: this.renderer.draw is not a function

At:
// initial draw
this.renderer.draw(currentValue);

Easing animations?

Apologies if this has been brought up before, but is it possible to add easing effects to the line animation?

Cheers!

Responsive?

Is the Chart Responsive?

I tried setting the width & height of the canvas element as well as it's div container to smaller sizes as resolutions decrease. It works fine in Chrome, but if I open it up on my iPhone the size doesn't change.

Any suggestions?

Lazy Load

Is there any way to animate only when it appears on screen on computers also support for mobile devices, thanks

Add tests for a better development process

I think we should add tests for all features and implementations to guarantee a better and more stable development process.

My suggestion is to add Jasmine tests and integrate them with the grunt workflow for fast and automated testing.

Ideas, insights, comments or thoughts are welcome.

Angular version better documentation

Please you should add some example or a better documentation for the use of angularJs.

I'm trying to migrate to angularJs, but cannot make change to barcolor according to percent.

This was done with:

        barColor: function(percent) {
            percent /= 100;
            return "rgb(" + Math.round(255 * (1-percent)) + ", " + Math.round(255 * percent) + ", 0)";
        },

But how can I made the same with angularJs?

Check this plunker: http://plnkr.co/edit/7yQ1SiIPHFh62yxwnW9e?p=preview

Thanks

Update data(percent) with new percent in update() method

Since the plugin can be initialized from data-percent via this.$el.data('percent) the update() method should update the data parameter to the new percent.

this.update = function(percent) {
      percent = parseFloat(percent) || 0;
      _this.$el.data('percent', percent);
      // ... continue

I don't know coffee script or I would make a pull request.

edit: formatting with backticks

Gradient color

Why not add a function to create a gradient color from 0 to 100?

use pie chart with php

I try to use EASY PIE CHART but i have some problems to use the chart because I would like to use a numb instead a percentage. is it possible?

In my JS I put this:

var chart = x
$(function() {
$('.chart').data('easyPieChart').update(chart);
});

and into my php:

the problem is when my chart variable change i don't see my chart!!!

Is there a way to add a GOAL setting to the chart?

Enhancement suggestion: Would like to be able to show current percentage (e.g. 40%) with a target/goal of 60%. When the current value = goal, change the color of the bar (to be green), otherwise the bar is red. Something like the below...
goals

Unable to get IE8 working except compatibility mode

Running the old-ie.html demo, the chart didn't render in IE8 "standards mode". Seems to work fine in IE7 and IE8 compatibility mode. Hoping to get this solved. I noticed the problem in my app and tried it on the demo app with the same results.

Result:
image

setting 'trackColor:false' disables lineWidth

Hello,

it seems like using a trackColor with value of false makes the piechart not use the lineWidth, rendering the chart with 1px.

Is this to be expected?

I mean, having a different lineWidth and no trackColor is an "extension request" or an "issue report"?

thank You.

Updating on load

Hi there

I'm trying to use this in an application but have a problem in that the HTML is generated via an XSL transformation, I can get a number value into the div but can't seem to get the value into the data-percentage attribute. So I'm trying to find a way of updating the data-percentage attribute with the value of another element - am guessing this is done somehow using this.$el. but can't find an example of this anywhere. Would i need to give the data attribute a starting value like 0 and update on window load?

Many thanks in advance for any help

Missing update function for other attributes too

I would like to add some simple interactivity to the chart, for example a simple hover, that would change the barColor...

so I did ...
var pie_options = {
....,
barColor : "#bbb"
}
$(selector).easyPieChart(pie_options);

$(selector).on("mouseover", function(){
pieOptions.barColor: "#666";
$(selector).easyPieChart(pie_options);
});

but nothing happens

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.