Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 749
bboysupaman Avatar asked Oct 14 '15 14:10

bboysupaman


People also ask

Is Google Charts free to use?

Google chart tools are powerful, simple to use, and free. Try out our rich gallery of interactive charts and data tools.

Is Google Charts free for commercial use?

Yes, it is free. It is written in the official page: Completely free for all uses: commercial, governmental, personal or educational.


2 Answers

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>
like image 155
Vadim Gremyachev Avatar answered Oct 13 '22 05:10

Vadim Gremyachev


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

like image 45
Valentin Podkamennyi Avatar answered Oct 13 '22 05:10

Valentin Podkamennyi