Google Chart + Foundation Tabs

Asked

Viewed 75 times

5

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

inserir a descrição da imagem aqui

The problem seems to be similar to this, but I do not know how to adapt this solution that 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:

Follows jsFiddle to facilitate understanding.

  • You can reproduce the problem in a jsFiddle?

  • Sergio, I edited with jsFiddle

2 answers

4


This happens because the graph is drawn after the div has already been redenrized, making him unable to reach 100%.

You can solve by putting one setTimeout, so when you click on the tab, you’ll wait a while to draw the chart.

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

In place of 10000 you choose the time you will wait to draw the chart. 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 on Jsfiddle.

  • 1

    That’s right Tais, thank you very much :)

0

Follow the solution of 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>
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.1.2/foundation.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<link rel="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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.