Alexander Farber
Alexander Farber

Reputation: 22978

How to center-align svg-based Google Charts (since they have position: absolute)

Does anybody please have an idea how to center-align the new (i.e. svg-based) Google Charts?

When I inspect the chart elements at my web page (please scroll down to the jQuery Tabs with several Google Charts at that page), I see that the chart itself is given an absolute position:

<div style="position: absolute; left: 0px; top: 0px; width: 100%; height: 100%; ">
<svg width="800" height="500" style="overflow: hidden; ">

I'm not sure why Google does it, but it breaks my layout (other than that the charts work well for me):

enter image description here

My CSS-code:

    h1,h2,h3,p,div,form { 
    text-align: center; 
}

And I've also tried adding there:

    margin-left: auto; 
    margin-right: auto; 

My JavaScript-code:

var money_data;
var money_chart;
var money_options = {
        width: 800,
        height: 500,
        legend: {position: 'top'},
        areaOpacity: 1.0,
        vAxis: {format: '# $'},
        hAxis: {title: 'Номер недели', titleTextStyle: {color: 'blue'}, slantedText: true, viewWindow: {min: 39, max: 52}},
        colors: ['CCFFCC', '66CC66', 'FF9999'],
        animation: {duration: 1000, easing: 'out'}
};

function initMoney() {
        $.ajax({
                url: '/money-json.php',
                data: { id: 'OK408547485023' },
                dataType: 'json'
        }).done(function(jsonData) {
                money_data = new google.visualization.DataTable(jsonData);
                money_chart = new google.visualization.SteppedAreaChart(document.getElementById('money_chart'));
                google.visualization.events.addListener(money_chart, 'ready', function() {
                        $('#money_slider').slider('enable');
                });
                drawMoney();
        });
}

function drawMoney() {
        money_chart.draw(money_data, money_options);
}

If no CSS-based solution possible here, could I maybe use JavaScript to move the SVG to the same x-position as the money_slider while enabling the latter (please see the above code)?

Upvotes: 1

Views: 9494

Answers (2)

Big Beetle Fan
Big Beetle Fan

Reputation: 155

You will need to make the chart side another div here is my code and it works for me

.chart_outside{
        position:relative;
        display: inline;
        border: 2px solid #FFF;
        width: 500px;
        height: 460px;
        background: #000;
        margin: 5px 5px 5px 5px;
        }

.chart_inside{
        position:absolute;
        right:0px
        ;top:0px;
        }

enter image description here

Upvotes: 1

Cristiano Fontes
Cristiano Fontes

Reputation: 5088

You could set the size of money_chart DIV to the size of the graph ( I am guessing 800px ) and use auto margins like this.

#money_chart{
    margin-left: auto;
    margin-right: auto;
    width: 800px;
}

Upvotes: 2

Related Questions