Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-sizing Google chart in window

Please help me how to automatically re-size the Google chart when the windows re-size too.

Here is some code:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(drawChart);
    function drawChart() {
        var data = google.visualization.arrayToDataTable([
            ['Day', 'Open ticket', 'Closed ticket'],
            ['Day 1',  10,   400],
            ['Day 2',  170,  460],
            ['Day 3',  60,   1120],
            ['Day 4',  30,   540],
        ]);
        var options = {
            title: 'DAILY GRAPH',
            hAxis: {title: 'NOVEMBER',  titleTextStyle: {color: '#333'}},
            vAxis: {minValue: 0}
        };
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
    }
</script>
like image 223
user3172075 Avatar asked Dec 08 '25 22:12

user3172075


1 Answers

Here's one way:

Add this event handler:

 window.onresize = drawChart;

Then base the chart width and height on a percentage of the window's width and height:

    var width = .4 * window.innerHeight;
    var height = .4 * window.innerWidth;

    var options = {
        title: 'DAILY GRAPH',
        width: width,
        height: height,
        hAxis: { title: 'NOVEMBER', titleTextStyle: { color: '#333' } },
        vAxis: { minValue: 0 }
    };
like image 184
Steve Wellens Avatar answered Dec 11 '25 11:12

Steve Wellens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!