Skip to content

Instantly share code, notes, and snippets.

@kirs
Created November 22, 2011 16:32
Show Gist options
  • Save kirs/1386103 to your computer and use it in GitHub Desktop.
Save kirs/1386103 to your computer and use it in GitHub Desktop.
$ = jQuery
$.fn.extend
charty: (options) ->
settings =
dataField: 'hours'
color: '#000'
margin: 2
settings = $.extend settings, options
return @each () ->
new charty this, settings
class charty
constructor: (@element, @settings) ->
$el = $(@element)
spans = $el.children()
total_width = $el.width()
max_height = $el.height()
hour_height = Math.floor(max_height/12)
bar_width = Math.floor(total_width/31) - @settings.margin
$(spans).each (i, el) =>
data = $(el).data(@settings.dataField)
if data > 12
data = 12
$(el).width(bar_width).height(hour_height*data || Math.floor(hour_height/3)).css(
"background":@settings.color
"margin-right":@settings.margin
)
empty_spans_count = 31 - spans.length
empty_spans = while empty_spans_count -= 1
"<span style='width: #{bar_width}px; height: #{Math.floor(hour_height/3)}px; background: #{@settings.color}; margin-right: #{@settings.margin}px'></span>"
console.log empty_spans
$(@element).append(empty_spans.join(''))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment