MorrisJS Chart not being drawn on Bootstrap Tab

MorrisJS Chart
MorrisJS Chart

There is one common problem when using MorrisJS chart on Bootstrap Tab is that the charts are not drawn on the view.

When first loading the UI, only the charts on active tabs are drawn while charts on other tabs won’t show up on tab click due to UI invalidation since there is no action trigger to draw the charts via tab activation.

Understanding this problem, we can fix it by triggering the chart drawing by calling method redraw() support by Morris.

Also, because the charts are drawn into SVG format, so we need to config its CSS to occupy full width of container, otherwise, charts will be cut off partially.

By listening to the tab activation, we call the methods in order:

$('a[data-toggle="tab"]').on('', function(e) {
    var target = $("href");
    switch (target) {
        case "#tab-1":
            $('svg').css({ width: '100%' });
        case "#tab-2":
            $('svg').css({ width: '100%' });