I am using c3.js, AngularJs and bootstrap to render some charts. The setup is as follows:
Bind a variable to
<select>
form to change between chart typesI have a
$watch
on the same variable which triggers the chart rendering.- Depending on variable I render multiple charts, different types depending on it's value. I have two charts per row, each one inside a
col-XX-6
. - Each chart group is hidden/shown using
ng-show="myVariable== 'typeX'"
depending on the same variable that triggers the render.
So, view and c3 render are acting on the same variable.
The first render is triggered by watch on $viewContentLoaded
and everything works fine. After I change the select option, the first chart in the column renders with 100% width, disregarding bootstrap template. My theory is that c3.js renders the chart "too fast", before the view kicks in and sets the proper width. Further investigation:
- Adding a $timeout on renderer fixes the problem but this is unacceptable solution
- Adding a fixed width on charts works but is not acceptable, it needs to adapt to bootstrap
- So far it seems that the only solution is to listen to the actual DOM change to see when the DOM actually becomes visible and only then start the render. But it seems that this technique is highly discouraged.
How do I fix this problem the proper way?
Edit 1: It seems that even if I render all the charts once and never refresh them, they still rarely but randomly oversize when shown.
Edit 2: Plunkr: http://plnkr.co/edit/mU5m694VoDB5GtXrmPdP
Plunkr works (...) but basically, it happens to me when you'd switch from type2 to type1, the first graph would take 100% width and mess up everything. I'll see if I can find something in Plunkr that would make it break.
Edit 3: Well, perhaps not working that well afterall. I noticed charts sometimes do not render (blank page) and they only appear after I inspect element or fiddle with the screen in some way. Seems like it could be the same problem behind this. You should increase the plunker preview to simulate a wider screen.