.
218 | Calculate chart margins and canvas dimensions.
219 | ###
220 | updateDimensions: ->
221 | container = @container()
222 | if container?
223 | bounds = container.getBoundingClientRect()
224 |
225 | @margin =
226 | left: @calcMaxTextWidth()
227 | right: 50
228 |
229 | @canvasWidth = bounds.width - @margin.left - @margin.right
230 |
231 | unless @height()
232 | barCount = @_barData().length
233 | @canvasHeight = barCount * (@barHeight() + @barPadding())
234 |
235 | @svg.attr('height', @canvasHeight)
236 |
237 | updateChartScale: ->
238 | extent = ForestD3.Utils.extent @data().get(), {y: 0}
239 |
240 | @yScale = d3.scale.linear()
241 | .domain(extent.y)
242 | .range([0, @canvasWidth])
243 |
244 | ###
245 | Draws the chart frame. Things like backdrop and canvas.
246 | ###
247 | updateChartFrame: ->
248 | padding = 10
249 | barCenter = @barHeight() / 2 + 5
250 | @labelGroup = @svg.selectAll('g.bar-labels').data([0])
251 | @labelGroup.enter().append('g').classed('bar-labels', true)
252 | @labelGroup
253 | .attr('transform',
254 | "translate(#{@margin.left - padding},#{barCenter})"
255 | )
256 |
257 | @barGroup = @svg.selectAll('g.bars').data([0])
258 | @barGroup.enter().append('g').classed('bars', true)
259 | @barGroup
260 | .attr('transform', "translate(#{@margin.left},0)")
261 |
262 | @valueGroup = @svg.selectAll('g.bar-values').data([0])
263 | @valueGroup.enter().append('g').classed('bar-values', true)
264 | @valueGroup
265 | .attr('transform',
266 | "translate(#{@margin.left + padding},#{barCenter})"
267 | )
--------------------------------------------------------------------------------
/src/base.coffee:
--------------------------------------------------------------------------------
1 | @ForestD3.BaseChart = class BaseChart
2 | constructor: (domContainer)->
3 | @properties = {}
4 |
5 | @container domContainer
6 |
7 | @_metadata = {}
8 | @_dispatch = d3.dispatch(
9 | 'rendered',
10 | 'stateUpdate',
11 | 'tooltipBisect'
12 | 'tooltipHidden'
13 | )
14 |
15 | @plugins = {}
16 |
17 | ###
18 | Auto resize the chart if user resizes the browser window.
19 | ###
20 | @resize = =>
21 | if @autoResize()
22 | @render()
23 |
24 | window.addEventListener 'resize', @resize
25 | @_attachStateHandlers()
26 |
27 | ###
28 | Call this method to remove chart from the document and any artifacts
29 | it has (like tooltips) and event handlers.
30 | ###
31 | destroy: ->
32 | domContainer = @container()
33 | if domContainer?.parentNode?
34 | domContainer.parentNode.removeChild domContainer
35 |
36 | window.removeEventListener 'resize', @resize
37 |
38 | on: (type, listener)->
39 | try
40 | @_dispatch.on type, listener
41 | catch e
42 | throw new Error "Chart does not recognize the event '#{type}'."
43 |
44 | @
45 |
46 | trigger: (type)->
47 | @_dispatch[type].apply @, Array.prototype.slice.call(arguments, 1)
48 |
49 | @
50 |
51 | _attachStateHandlers: ->
52 |
53 | container: (d)->
54 | unless d?
55 | return @properties['container']
56 | else
57 | if d.select? and d.node?
58 | # This is a d3 selection
59 | d = d.node()
60 | else if typeof(d) is 'string'
61 | d = document.querySelector d
62 |
63 | @properties['container'] = d
64 | @svg = @createSvg()
65 |
66 | return @
67 |
68 | ###
69 | Create an