Skip to content

Commit

Permalink
fix bug where react-style frameworks might call draw() on a node befo…
Browse files Browse the repository at this point in the history
…re it being actually attached to the DOM - now we wait & poll for the chart to be in the DOM before attempting to draw it
  • Loading branch information
jcarver committed Jan 13, 2017
1 parent cdacff6 commit 47feaec
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 21 deletions.
43 changes: 22 additions & 21 deletions dist/suave-charts.min.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/coffeescript/abstract_chart.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ class AbstractChart
@listenerBound = false
@axisLabelPadding = 10

waitToBeInDom: (func) ->
if document.body.contains(@svg.container[0][0])
func()
else
setTimeout(func, 100)

draw: () ->
unless @listenerBound
window.addEventListener("resize", @render)
Expand Down
3 changes: 3 additions & 0 deletions src/coffeescript/bar_chart.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ class BarChart extends AbstractChart

draw: (data) ->
super()
@waitToBeInDom(() => @drawInternal(data))

drawInternal: (data) ->
normalizedBars = ((if Object.prototype.toString.call(d) == "[object Array]" then d else [d]) for d in data.bars)

@y.domain([0, d3.max(d3.merge(normalizedBars, (bars) -> d3.max(bars)))])
Expand Down
3 changes: 3 additions & 0 deletions src/coffeescript/donut_chart.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class DonutChart extends AbstractChart

draw: (data) =>
super()
@waitToBeInDom(() => @drawInternal(data))

drawInternal: (data) =>
s = (sum, d) -> sum + d[1]
arcSelection = @svg.chart.selectAll(".arc").data(@pie(data))
@enter = arcSelection.enter()
Expand Down
3 changes: 3 additions & 0 deletions src/coffeescript/histogram.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class Histogram extends AbstractChart

draw: (data) ->
super()
@waitToBeInDom(() => @drawInternal(data))

drawInternal: (data) ->
hist = d3.layout.histogram()
if @options.domain
@x.domain(@options.domain)
Expand Down
3 changes: 3 additions & 0 deletions src/coffeescript/line_chart.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ class LineChart extends AbstractChart

draw: (data) ->
super()
@waitToBeInDom(() => @drawInternal(data))

drawInternal: (data) ->
{ labels, lines, xDomain, yDomain } = @formatData(data, @options.stack)

if @options.xScale == "ordinal"
Expand Down
4 changes: 4 additions & 0 deletions src/coffeescript/scatterplot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ class ScatterPlot extends AbstractChart
super(selector, options)

draw: (data) ->
super()
@waitToBeInDom(() => @drawInternal(data))

drawInternal: (data) ->
@x.domain d3.extent(data, extractX)
@y.domain [0, d3.max(data, extractY)]

Expand Down

0 comments on commit 47feaec

Please sign in to comment.