Question

Trying do render Google charts in hidden elements without lucky. The fiddle bellow has an example with bootstrap tabs:

Fiddle

Got some help from Andrew here: How to draw a google chart when a tab is showed? but the chart now is re-rendering anytime I interact with it, (for example with the range filter).

The HTML code is:

<div class="container">
    <div class="tabbable">
        <ul class="nav nav-tabs" data-tabs="tabs">
            <li class="active">
                <a href="#A" data-toggle="tab">Tab A</a>
            </li>
            <li class="">
               <a href="#B" data-toggle="tab">Tab B</a>
            </li>
            <li class="">
                <a href="#C" data-toggle="tab">Tab C</a>
            </li>
        </ul>
        <div class="tab-content">
            <div class="tab-pane active" id="A">
                <div id='ChartA'></div>
            </div>
            <div class="tab-pane" id="B">
                <div id='ChartB'></div>
            </div>
            <div class="tab-pane" id="C">
                <div id='ChartC'></div>
            </div>
        </div>
    </div>
</div>

All the JS code is in the fiddle.

Was it helpful?

Solution

A simple example, all I'm doing here is reloading the graphs when the tabs show event gets fired for each tab. I'm sure there's a more elegant solution but it does the job.

$("a[href='#A']").on('shown.bs.tab', function (e) {
    google.load('visualization', '1', {
        packages: ['timeline'],
        callback: drawChartA
    });
});

$("a[href='#B']").on('shown.bs.tab', function (e) {
    google.load('visualization', '1', {
        packages: ['timeline'],
        callback: drawChartB
    });
});

$("a[href='#C']").on('shown.bs.tab', function (e) {
    google.load('visualization', '1', {
        packages: ['timeline'],
        callback: drawChartC
    });
});

OTHER TIPS

Simple as that:

$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  window.dispatchEvent(new Event('resize'));
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top