Google Chart + Foundation Tabs

5

I'm facing problems with Google Chart + Foundation . Gráfico does not stay in width:100% , and seems to lose formatting. And this happens when I use it in Tab of Foundation . Strange that if you use Grafico on the first tab, it works normal. See the image:

Theproblemseemstobesimilarto this , but I do not know how to adapt that solution which was presented.

HTML:

<div class="row">
    <div class="medium-12 columns">
        <ul class="tabs" data-tabs id="sac-tabs">
            <li class="tabs-title is-active">
                <a href="#panel1" aria-selected="true">Relatórios</a></li>
            <li class="tabs-title"><a href="#panel2">Gráficos</a></li>            
        </ul>
        <div class="tabs-content" data-tabs-content="sac-tabs">
            <div class="tabs-panel is-active" id="panel1">
                <div class="row">
                    <div class="medium-12 columns">
                        CONTEUDO_TAB1
                    </div>
                </div>
            </div>
            <div class="tabs-panel" id="panel2">
                <div class="row">
                    <div class="medium-12 columns">
                        <div id="chart_div"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

JavaScript:

 google.charts.load('current', {'packages': ['corechart']});
        google.charts.setOnLoadCallback(drawVisualization);


        function drawVisualization() {
            // Some raw data (not necessarily accurate)
            var data = google.visualization.arrayToDataTable([
                ['Departamento', 'Concluídas', 'Andamento', 'Pendentes'],
                ['Vendas', 5, 6, 2],
                ['Peças', 3, 4, 6],
                ['Serviços', 1, 2, 3],
                ['Administrativo', 7, 5, 3]
            ]);

            var options = {
                title: 'Gráfico',
                hAxis: {title: 'Departamento'},
                seriesType: 'bars',
                colors: ['#21BA45', '#F90', '#DC3912'],
                series: {5: {type: 'line'}}
            };

            var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
            chart.draw(data, options);
        };
        $(document).foundation();

Editing:

Follow jsFiddle for easy understanding.

    
asked by anonymous 19.12.2016 / 12:52

2 answers

4

This happens because the graph is drawn after div has already been redrawn, so it can not reach 100% .

You can solve by putting a setTimeout , so when you click on the tab, you will wait a while to draw the graph.

setTimeout(function (){
    chart.draw(data, options);
}, 10000);

Instead of 10000 you choose the time you will wait to draw the graph. It would look like this:

$(document).foundation();

google.charts.load('current', {'packages': ['corechart']});
google.charts.setOnLoadCallback(drawVisualization);

function drawVisualization() {
    // Some raw data (not necessarily accurate)
    var data = google.visualization.arrayToDataTable([
        ['Departamento', 'Concluídas', 'Andamento', 'Pendentes'],
        ['Vendas', 5, 6, 2],
        ['Peças', 3, 4, 6],
        ['Serviços', 1, 2, 3],
        ['Administrativo', 7, 5, 3]
    ]);

    var options = {
        title: 'Gráfico',
        hAxis: {title: 'Departamento'},
        seriesType: 'bars',
        colors: ['#21BA45', '#F90', '#DC3912'],
        series: {5: {type: 'line'}}
    };

    var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));

    setTimeout(function (){
        chart.draw(data, options);
    }, 10000);

   // chart.draw(data, options);
};
$(document).foundation();

See working at Jsfiddle .

    
19.12.2016 / 14:04
0

Here's the solution to the problem:

$(document).foundation();

$('#stabs').on('change.zf.tabs', function() {
  switch ($(this).children('.is-active').text().trim()) {
    case 'Tab 1':
      drawVisualization();
      break;

    case 'Tab 2':
      drawVisualization2();
      break;
  }
});

google.charts.load('current', {
    callback: drawVisualization,
    packages: ['corechart']
});

function drawVisualization() {
    var data = google.visualization.arrayToDataTable([
        ['Departamento', 'Concluídas', 'Andamento', 'Pendentes'],
        ['Vendas', 5, 6, 2],
        ['Peças', 3, 4, 6],
        ['Serviços', 1, 2, 3],
        ['Administrativo', 7, 5, 3]
    ]);

    var options = {
        title: 'Gráfico',
        hAxis: {title: 'Departamento'},
        seriesType: 'bars',
        colors: ['#21BA45', '#F90', '#DC3912'],
        series: {5: {type: 'line'}}
    };

    var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}

function drawVisualization2() {
    var data = google.visualization.arrayToDataTable([
        ['Departamento', 'Concluídas', 'Andamento', 'Pendentes'],
        ['Vendas', 5, 6, 2],
        ['Peças', 3, 4, 6],
        ['Serviços', 1, 2, 3],
        ['Administrativo', 7, 5, 3]
    ]);

    var options = {
        title: 'Gráfico',
        hAxis: {title: 'Departamento'},
        seriesType: 'bars',
        colors: ['#21BA45', '#F90', '#DC3912'],
        series: {5: {type: 'line'}}
    };

    var chart = new google.visualization.ComboChart(document.getElementById('chart_div2'));
    chart.draw(data, options);
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.1.2/foundation.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script><linkrel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.1.2/foundation.min.css">
<div class="row">
    <div class="medium-12 columns">
        <ul class="tabs" data-tabs id="stabs">
            <li class="tabs-title is-active">
                <a href="#panel1" aria-selected="true">Tab 1</a></li>
            <li class="tabs-title"><a href="#panel2">Tab 2</a></li>            
        </ul>
        <div class="tabs-content" data-tabs-content="stabs">
            <div class="tabs-panel is-active" id="panel1">
                <div class="row">
                    <div class="medium-12 columns">
                    <p>
                    Tab 1
                    </p>
                    <div id="chart_div"></div>
                    </div>
                </div>
            </div>
            <div class="tabs-panel" id="panel2">
                <div class="row">
                    <div class="medium-12 columns">
                    <p>
                    Tab 2
                    </p>
                       <div id="chart_div2"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
    
30.01.2017 / 16:49