nwalsh
nwalsh

Reputation: 471

Getting Type of Google Chart

I was looking through the API and couldn't find anything about getting the type of chart.

I have this code:

<script type="text/javascript">
var chart;
function drawTest( dataTable ) {
    google.load("visualization", "1", {packages:["corechart"]});
    var options = {
        title: 'test chart',
        hAxis: {title: 'Day', titleTextStyle: {color: 'black'},
        animation: {
            duration: 1000,
            easing: 'out',
        }}
    };
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'String');
    data.addColumn('number', 'Number1');
    data.addColumn('number', 'Number2');
    dataTable.forEach( function(row) {
        data.addRow( row );
    } );

    chart = new google.visualization.ColumnChart( document.getElementById( 'chart' ) );
    chart.draw( data, options );
}

What I want to do is instead of setting chart to a new chart in the bottom I want to check if it's already set. I'm not sure if this is in the API.

if( chart.getType() == "ColumnChart" ){
   chart.draw( data, options );
}

However, I have multiple charts so I can't just check if chart is initialized.

Any help about completing this task is appreciated.

Upvotes: 1

Views: 212

Answers (1)

juvian
juvian

Reputation: 16068

If you use a chartWrapper instead, there is a getChartType method that returns the class name of the wrapped chart. You can view more info about chartWrapper class here:

https://developers.google.com/chart/interactive/docs/reference#chartwrapperobject

Here is an example using google visualization playground:

function drawVisualization() {
  // Create and populate the data table.
    var wrapper = new google.visualization.ChartWrapper({
      chartType: 'ColumnChart',
      dataTable: [['', 'Germany', 'USA', 'Brazil', 'Canada', 'France', 'RU'],
                  ['', 700, 300, 400, 500, 600, 800]],
      options: {'title': 'Countries'},
      containerId: 'visualization'
    });
    wrapper.draw();
    console.log(wrapper.getChartType()) // ColumnChart
}

Upvotes: 1

Related Questions