zygimantus
zygimantus

Reputation: 3777

How to list all Google chart types in a page?

I am using Google chart tools and I am wondering if it is possible to list all all Google chart types in a web page using JS?

Note: I tried using this in Chrome console:

google.visualization

but this gave not only types but also NumberFormat, PatternFormat objects which I do not need.

Upvotes: 0

Views: 58

Answers (1)

davidkonrad
davidkonrad

Reputation: 85558

There is no function such a "getLoadedChartTypes()" or similar. But it is not so difficult to create a function like that. All chart functions begins with a capital letter and ends with Chart. There is no other functions that follow that scheme, so all we have to do is to extract functions following that scheme and filtering the base function CoreChart out :

Here is a function that populates a <select> box with all available (loaded) google visualization chart types :

function populate() {
    var option,
        select = document.getElementById('chartTypes');
    for (var element in google.visualization) {
        if (/[A-Z]/.test(element[0]) && //begins with capital letter
            element.match('Chart$') &&  //ends with Chart
            element != 'CoreChart') {   //is not CoreChart
           option = document.createElement("option");
           option.text = element;
           select.add(option);
        }
    }
}

demo -> http://jsfiddle.net/6dgkvojj/

Upvotes: 2

Related Questions