Khaine775
Khaine775

Reputation: 2765

Google Charts chart area out of bounds

I have a Google Charts annotation chart like the one below inside some tabs. The problem is, it's not being drawn properly as if it's a little out of bounds somehow.

I assume it's related to the chart being drawn when the page loads and then when I click on the tab, the size of the chart's container changes. But I'm not sure how to fix that. Any hint is greatly appreciated.

This is my chart:

google.load('visualization', '1', { 'packages': ['annotationchart'] });
google.setOnLoadCallback(drawChart);

function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('date', 'Date');
    data.addColumn('number', 'Kepler-22b mission');
    data.addColumn('string', 'Kepler title');
    data.addColumn('string', 'Kepler text');
    data.addColumn('number', 'Gliese 163 mission');
    data.addColumn('string', 'Gliese title');
    data.addColumn('string', 'Gliese text');
    data.addRows([
        [
            new Date(2314, 2, 15), 12400, undefined, undefined,
            10645, undefined, undefined
        ],
        [
            new Date(2314, 2, 16), 24045, 'Lalibertines', 'First encounter',
            12374, undefined, undefined
        ],
        [
            new Date(2314, 2, 17), 35022, 'Lalibertines', 'They are very tall',
            15766, 'Gallantors', 'First Encounter'
        ],
        [
            new Date(2314, 2, 18), 12284, 'Lalibertines', 'Attack on our crew!',
            34334, 'Gallantors', 'Statement of shared principles'
        ],
        [
            new Date(2314, 2, 19), 8476, 'Lalibertines', 'Heavy casualties',
            66467, 'Gallantors', 'Mysteries revealed'
        ],
        [
            new Date(2314, 2, 20), 0, 'Lalibertines', 'All crew lost',
            79463, 'Gallantors', 'Omniscience achieved'
        ]
    ]);

    var options = {
        displayAnnotations: true,
        'height': 500,
        'width': 900,
    };

    var chart = new google.visualization.AnnotationChart(document.getElementById('chart_div'));
    var chart2 = new google.visualization.AnnotationChart(document.getElementById('chart_div2'));
    var chart3 = new google.visualization.AnnotationChart(document.getElementById('chart_div3'));

    chart.draw(data, options);
    chart2.draw(data, options);
    chart3.draw(data, options);
}

And this is one of the tabs:

<ul class="nav nav-tabs" data-tabs="tabs">
@foreach (var item in Model)
{
    <li id="wellnav" class="@(item.Well == 1 ? "active" : "")"><a id="id2"
                                                                  href="#[email protected]" 
                                                                  data-toggle="tab"">@item.Well</a></li>
}

<div id="tabs" class="tab-content">
    <div id="tab-1" class="tab-pane">
    <div id="chart_div"></div>       
</div>

UPDATE:

Here's an example on Fiddle on what I'm seeing. Notice how the chart in tab 2 has broken dimensions or, at least, doesn't look right. The fiddle is borrowed and modified from this post.

http://jsfiddle.net/06rwpft8/1/

Upvotes: 0

Views: 431

Answers (1)

Me.Name
Me.Name

Reputation: 12544

It would seem the chart isn't rendered properly on hidden objects. Perhaps there are better ways to handle it directly in the google chart settings, but a solution would be to draw the actual chart when the tab is shown. For example, call the draw once if the tab is clicked:

$('a[title=content_2]').one('click',function(){
    chart2.draw(data, options);
});

Fiddle

Upvotes: 1

Related Questions