bboysupaman
bboysupaman

Reputation: 1334

Multiple Google Charts

I am attempting to create multiple Google Charts, but I can't get it to work. I've tried everything I could find on Stack Overflow. I most recently attempted this fix, but it didn't work. I think I may be missing something. Can anyone see anything wrong with the code as it stands now?

Expected Behavior:

Page displays bar graph. Then, a line graph is displayed underneath the bar graph.

Current Behavior:

Page displays bar graph. Line graph does not display.

Here is JSFiddle. On a side note, the JavaScript only seems to work inline on JSFiddle. If I moved it into the JavaScript section, it did not function properly. Maybe this has something to do with the external resource that was called?

Regardless, I am currently doing this all inline for this experiment.

HTML:

<!DOCTYPE html>
<html>
<head>
    <script src="https://www.google.com/jsapi" type="text/javascript">
    </script>
    <script type="text/javascript">
    // Load the Visualization API and the chart packages.
    google.load('visualization', '1.1', {
        packages: ['line', 'bar', 'corechart']
    });
    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);
    // Callback that creates and populates a data table,
    // instantiates the charts, passes in the data and
    // draws them.
    function drawChart() {
        // Create the data table.
        var BarData = new google.visualization.arrayToDataTable([
            ['', 'Customer', 'Segment Avg'],
            ['TTM Sales', 4, 2],
            ['TTM Orders', 5, 3],
            ['TTM Categories', 7, 4]
        ]);
        // Create the data table.
        var LineData = new google.visualization.arrayToDataTable([
            ['Year', 'Customer', 'Segment Avg'],
            ['2011', 4, 5],
            ['2012', 5, 3],
            ['2013', 4, 2]
        ]);
        // Set chart options
        var BarOptions = {
            chart: {
                title: 'Performance',
            },
            width: 900,
            height: 500
        };
        // Set chart options
        var LineOptions = {
            chart: {
                title: 'Sales History'
            },
            width: 900,
            height: 500
        };
        // Instantiate and draw our chart, passing in some options.
        var BarChart = new google.charts.Bar(document.getElementById(
            'bar_chart'));
        BarChart.draw(BarData, BarOptions);
        var LineChart = new google.charts.Line(document.getElementById(
            'line_chart'));
        LineChart.draw(LineData, LineOptions);
    };
    </script>
    <title>Test Chart Page</title>
</head>
<body>
    <!--Divs that will hold the charts-->
    <div id="bar_chart"></div>
    <div id="line_chart"></div>
</body>
</html>

Upvotes: 6

Views: 3030

Answers (4)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59318

It seems some changes have been made in the latest version of Google Charts API that causes this behavior, but there is a reliable way to render multiple charts on a single page. The idea is to render the next chart once the previous one is rendered, for that purpose you could utilize ready event handler.

Having said that, replace

var barChart = new google.charts.Bar(document.getElementById('bar_chart'));
barChart.draw(barData, barOptions);
var lineChart = new google.charts.Line(document.getElementById('line_chart'));
lineChart.draw(lineData, lineOptions);

with

var barChart = new google.charts.Bar(document.getElementById('bar_chart'));
google.visualization.events.addOneTimeListener(barChart, 'ready', function () {
      var lineChart = new google.charts.Line(document.getElementById('line_chart'));
      lineChart.draw(lineData, lineOptions);
});
barChart.draw(barData, barOptions);

Working example

google.load('visualization', '1.1', {
        packages: ['line', 'bar', 'corechart']
    });
    // Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawCharts);

function drawCharts() {
    // Create the data table.
    var barData = new google.visualization.arrayToDataTable([
        ['', 'Customer', 'Segment Avg'],
        ['TTM Sales', 4, 2],
        ['TTM Orders', 5, 3],
        ['TTM Categories', 7, 4]
    ]);
    // Create the data table.
    var lineData = new google.visualization.arrayToDataTable([
        ['Year', 'Customer', 'Segment Avg'],
        ['2011', 4, 5],
        ['2012', 5, 3],
        ['2013', 4, 2]
    ]);
    // Set chart options
    var barOptions = {
        chart: {
            title: 'Performance',
        },
        width: 900,
        height: 500
    };
    // Set chart options
    var lineOptions = {
        chart: {
            title: 'Sales History'
        },
        width: 900,
        height: 500
    };


    var barChart = new google.charts.Bar(document.getElementById('bar_chart'));
    google.visualization.events.addOneTimeListener(barChart, 'ready', function () {
        var lineChart = new google.charts.Line(document.getElementById('line_chart'));
        lineChart.draw(lineData, lineOptions);
    });
    barChart.draw(barData, barOptions);
};
<script src="https://www.google.com/jsapi" type="text/javascript"></script>
<div id="bar_chart"></div>
<div id="line_chart"></div>

Upvotes: 8

Valentin Podkamennyi
Valentin Podkamennyi

Reputation: 7359

Works with setTimeout:

// Instantiate and draw our chart, passing in some options.
var BarChart = new google.charts.Bar(document.getElementById(
    'bar_chart'));
setTimeout(function() {
  BarChart.draw(BarData, BarOptions);
}, 0);
var LineChart = new google.charts.Line(document.getElementById(
    'line_chart'));
setTimeout(function() {
  LineChart.draw(LineData, LineOptions);
}, 1e3);

Updated JSFiddle

Upvotes: 2

CodeToad
CodeToad

Reputation: 4724

The code below works by creating the second chart inside of setTimeout. I don't know what is causing the problem, but at least you have a workaround.

fiddle

<script type="text/javascript">
    // Load the Visualization API and the chart packages.
    google.load('visualization', '1.1', {
        packages: ['line', 'bar', 'corechart']
    });
    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);
    // Callback that creates and populates a data table,
    // instantiates the charts, passes in the data and
    // draws them.
    function drawChart() {
        // Create the data table.
        var BarData = new google.visualization.arrayToDataTable([
            ['', 'Customer', 'Segment Avg'],
            ['TTM Sales', 4, 2],
            ['TTM Orders', 5, 3],
            ['TTM Categories', 7, 4]
        ]);
        // Create the data table.
        var LineData = new google.visualization.arrayToDataTable([
            ['Year', 'Customer', 'Segment Avg'],
            ['2011', 4, 5],
            ['2012', 5, 3],
            ['2013', 4, 2]
        ]);
        // Set chart options
        var BarOptions = {
            chart: {
                title: 'Performance',
            },
            width: 900,
            height: 500
        };
        // Set chart options
        var LineOptions = {
            chart: {
                title: 'Sales History'
            },
            width: 900,
            height: 500
        };
        // Instantiate and draw our chart, passing in some options.
        var BarChart = new google.charts.Bar(document.getElementById(
            'bar_chart'));

        var LineChart = new google.charts.Line(document.getElementById(
            'line_chart'));
        LineChart.draw(LineData, LineOptions);
        setTimeout(function(){


        BarChart.draw(BarData, BarOptions);
    },50);

        };
    </script>
    <body>
        <!--Divs that will hold the charts-->
        <div id="bar_chart"></div>
        <div id="line_chart"></div>
    </body>

Upvotes: 1

Related Questions