Skip to content

Instantly share code, notes, and snippets.

@wprater
Forked from dandavison/chart.coffee
Last active December 17, 2015 22:29
Show Gist options
  • Save wprater/5682740 to your computer and use it in GitHub Desktop.
Save wprater/5682740 to your computer and use it in GitHub Desktop.
class BaseChart
accessors:
width: 100
height: 100
_margin: {}
@margin: top: 0, right: 0, bottom: 0, left: 0
# http://bost.ocks.org/mike/chart/
@chart: ->
instance = new this
chart = (selection) ->
selection.each (d, i) -> instance.draw(this, d, i)
mixedChart = _.extend chart,
instance, _.cloneDeep instance.__proto__
mixedChart
constructor: ->
@buildAccessors()
Object.keys(Vis.margin).forEach (k) =>
@_margin[k] = Vis.margin[k]
draw: (el, d, i) ->
throw new Error 'You must declare a #draw function.'
margin: (margin) ->
return @_margin unless arguments.length
for k,v of margin
@_margin[k] = v
this
# Generate the getters/setters in the accessors property.
buildAccessors: ->
for name, accessor of @accessors
continue if this[name]?
do (name, accessor) =>
this[name] = (_) ->
return accessor unless arguments.length
accessor = _
this
class MyChart extends BaseChart
draw: (elem, d, i) ->
# generate chart here; `d` is the data and `elem` is the element
chart = MyChart.chart()
chart.width(800).height(120)
d3.select('body').datum(data).call(chart)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment