Created
April 23, 2014 15:55
-
-
Save itzaks/11221058 to your computer and use it in GitHub Desktop.
Lightweight Pie Chart module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = | |
| class Pie | |
| pos: 0 | |
| colors: ['yellow', 'black'] | |
| constructor: (@canvas, {@yes, @no}) -> | |
| @data = [@yes, @no] | |
| @context = canvas.getContext("2d") | |
| #uses underscore for reduce but just swap with vanilla js if needed | |
| @total = _.reduce @data, (memo, num) -> memo + num | |
| @draw() | |
| draw: -> | |
| center = x: @canvas.width / 2, y: @canvas.height / 2 | |
| pi = Math.PI | |
| for value, i in @data | |
| percentage = (value / @total) | |
| @context.fillStyle = @colors[i] | |
| @context.beginPath() | |
| @context.moveTo center.x, center.y | |
| @context.arc center.x, center.y, center.y, @pos, @pos + (pi * 2 * percentage), no | |
| @context.lineTo center.x, center.y | |
| @context.fill() | |
| @pos += pi * 2 * percentage |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: