Google Visualization API Reference

  • Thegoogle.visualization.* namespace provides objects and methods for creating and managing visualizations.

  • Key objects includeDataTable for data representation,DataView for filtered views,ChartWrapper for simplified chart management, andChartEditor for interactive chart customization.

  • The API includes methods for data manipulation (group,join), formatting data for display (Formatters), querying data sources, and handling events.

  • Visualizations are expected to expose standard methods likedraw,getSelection, andsetSelection.

  • Utility methods are available for converting arrays toDataTable and drawing charts with a simple JSON configuration.

This page lists the objects exposed by the Google Visualization API, and the standard methods exposed by all visualizations.

Note: The Google Visualization API namespace isgoogle.visualization.*

A Note on Arrays

Some browsers don't properly handle trailing commas in JavaScript arrays, so don't use them. Empty values in the middle of an array are fine. So, for example:

data = ['a','b','c', ,]; // BAD
data = ['a','b','c'];   // OK
data = ['a','b', ,'d']; // Also OK. The third value is undefined.

DataTable Class

Represents a two-dimensional, mutable table of values. To make a read-only copy of aDataTable (optionally filtered to show specific values, rows, or columns), create aDataView.

Each column is assigned a data type, plus several optional properties including an ID, label, and pattern string.

In addition, you can assign custom properties (name/value pairs) to any cell, row, column, or the entire table. Some visualizations support specific custom properties; for example theTable visualization supports a cell property called 'style', which lets you assign an inline CSS style string to the rendered table cell. A visualization should describe in its documentation any custom properties that it supports.

See also:QueryResponse.getDataTable

Constructor

Syntax

DataTable(opt_data,opt_version)

opt_data
[Optional] Data used to initialize the table. This can either be the JSON returned by callingDataTable.toJSON() on a populated table, or a JavaScript object containing data used to initialize the table. The structure of the JavaScript literal object is describedhere. If this parameter is not supplied, a new, empty data table will be returned.
opt_version
[Optional] A numeric value specifying the version of the wire protocol used. This is only used byChart Tools Datasource implementors.The current version is 0.6.

Details

TheDataTable object is used to hold the data passed into a visualization. ADataTable is a basic two-dimensional table. All data in each column must have the same data type. Each column has a descriptor that includes its data type, a label for that column (which might be displayed by a visualization), and an ID, which can be used to refer to a specific column (as an alternative to using column indexes). TheDataTable object also supports a map of arbitrary properties assigned to a specific value, a row, a column, or the wholeDataTable. Visualizations can use these to support additional features; for example, theTable visualization uses custom properties to let you assign arbitrary class names or styles to individual cells.

Each cell in the table holds a value. Cells can have a null value, or a value of the type specified by its column. Cells optionally can take a "formatted" version of the data; this is a string version of the data, formatted for display by a visualization. A visualization can (but is not required to) use the formatted version for display, but will always use the data itself for any sorting or calculations that it makes (such as determining where on a graph to place a point). An example might be assigning the values "low" "medium", and "high" as formatted values to numeric cell values of 1, 2, and 3.

To add data rows after calling the constructor, you can call eitheraddRow() for a single row, oraddRows() for multiple rows. You can add columns as well by calling theaddColumn() methods. There are removal methods for rows and columns as well, but rather than removing rows or columns, consider creating aDataView that is a selective view of theDataTable.

If you change values in aDataTable after it is passed into a visualization'sdraw() method, the changes will not immediately change the chart. You must calldraw() again to reflect any changes.

Note: Google Charts does not perform anyvalidation on datatables. (If it did, charts would be slower torender.) If you provide a number where your column is expecting astring, or vice versa, Google Charts will do its level best tointerpret the value in a way that makes sense, but will not flagmistakes.

Examples

The following example demonstrates instantiating and populating aDataTable with a literal string, with the same data as shown in the JavaScript example above:

var dt = new google.visualization.DataTable({    cols: [{id: 'task', label: 'Task', type: 'string'},           {id: 'hours', label: 'Hours per Day', type: 'number'}],    rows: [{c:[{v: 'Work'}, {v: 11}]},           {c:[{v: 'Eat'}, {v: 2}]},           {c:[{v: 'Commute'}, {v: 2}]},           {c:[{v: 'Watch TV'}, {v:2}]},           {c:[{v: 'Sleep'}, {v:7, f:'7.000'}]}]    }, 0.6);

The following example creates a new, emptyDataTable and then populates it manually with the same data as above:

var data = new google.visualization.DataTable();data.addColumn('string', 'Task');data.addColumn('number', 'Hours per Day');data.addRows([  ['Work', 11],  ['Eat', 2],  ['Commute', 2],  ['Watch TV', 2],  ['Sleep', {v:7, f:'7.000'}]]);
Should I create my DataTable in JavaScript or object literal notation?

You can create aDataTable either by calling the constructor without parameters and then adding values by calling theaddColumn()/addRows() methods listed below, or by passing in a populated JavaScript literal object to initialize it. Both methods are described below. Which one should you use?

  • Creating and populating a table in JavaScript by callingaddColumn(),addRow(), andaddRows() is very readable code. This method is useful when you'll be entering code by hand. It is slower than using object literal notation (described next), but in smaller tables (say, 1,000 cells) you probably won't notice much difference.
  • Direct declaration of theDataTable object using object-literal notation is considerably faster in large tables. However, it can be a tricky syntax to get right; use this if you can generate the object literal syntax in code, which reduces possibility of errors.

 

Methods

MethodReturn ValueDescription

addColumn(type, opt_label, opt_id)

OR

addColumn(description_object)

Number

Adds a new column to the data table, and returns the index of the new column. All the cells of the new column are assigned anull value. This method has two signatures:

First signature has the following parameters:

  • type - A string with the data type of the values of the column. The type can be one of the following:'string', 'number', 'boolean', 'date', 'datetime', and'timeofday'.
  • opt_label - [Optional] A string with the label of the column. The column label is typically displayed as part of the visualization, for example as a column header in a table, or as a legend label in a pie chart. If no value is specified, an empty string is assigned.
  • opt_id - [Optional] A string with a unique identifier for the column. If no value is specified, an empty string is assigned.

Second signature has a single object parameter with the following members:

  • type - A string describing the column data type. Same values astype above.
  • label - [Optional, string] A label for the column.
  • id - [Optional, string] An ID for the column.
  • role - [Optional, string] Arole for the column.
  • pattern - [Optional, string] A number (or date) format string specifying how to display the column value.

See also:getColumnId,getColumnLabel,getColumnType,insertColumn,getColumnRole

addRow(opt_cellArray)Number

Adds a new row to the data table, and returns the index of the new row.

  • opt_cellArray [optional] A row object, in JavaScript notation, specifying the data for the new row. If this parameter is not included, this method will simply add a new, empty row to the end of the table. This parameter is an array of cell values: if you only want to specify a value for a cell, just give the cell value (e.g. 55 or 'hello'); if you want to specify a formatted value and/or properties for the cell, use acell object (e.g., {v:55, f:'Fifty-five'}). You can mix simple values and cell objects in the same method call). Usenull or an empty array entry for an empty cell.

Examples:

data.addRow();  // Add an empty rowdata.addRow(['Hermione', new Date(1999,0,1)]); // Add a row with a string and a date value.// Add a row with two cells, the second with a formatted value.data.addRow(['Hermione', {v: new Date(1999,0,1),                          f: 'January First, Nineteen ninety-nine'}]);data.addRow(['Col1Val', null, 'Col3Val']); // Second column is undefined.data.addRow(['Col1Val', , 'Col3Val']);     // Same as previous.
addRows(numOrArray)Number

Adds new rows to the data table, and returns the index of the last added row. You can call this method to create new empty rows, or with data used to populate the rows, as described below.

  • numOrArray - Either a number or an array:
    • Number - A number specifying how many new, unpopulated rows to add.
    • Array - An array ofrow objects used to populate a set of new rows. Each row is an object as described inaddRow(). Use null or an empty array entry for an empty cell.

Example:

data.addRows([  ['Ivan', new Date(1977,2,28)],  ['Igor', new Date(1962,7,5)],  ['Felix', new Date(1983,11,17)],  ['Bob', null] // No date set for Bob.]);

See also:insertRows

clone()DataTable Returns a clone of the data table. The result is a deep copy of the data table except for thecell properties,row properties,table properties andcolumn properties, which are shallow copies; this means that non-primitive properties are copied by reference, but primitive properties are copied by value.
getColumnId(columnIndex)String Returns the identifier of a given column specified by the column index in the underlying table.
For data tables that are retrieved by queries, the column identifier is set by the data source, and can be used to refer to columns when using thequery language.
See also:Query.setQuery
getColumnIndex(columnIdentifier)String, Number Returns the index of a given column specified by the column index, id, or label if it exists in this table, otherwise -1. WhencolumnIdentifier is a string, it is used to find the column by its id first, then by its label.
See also:getColumnId,getColumnLabel
getColumnLabel(columnIndex)String Returns the label of a given column specified by the column index in the underlying table.
The column label is typically displayed as part of the visualization. For example the column label can be displayed as a column header in a table, or as the legend label in a pie chart.
For data tables that are retrieved by queries, the column label is set by the data source, or by thelabel clause of thequery language.
See also:setColumnLabel
getColumnPattern(columnIndex)String

Returns the formatting pattern used to format the values of the specified column.

  • columnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method.

For data tables that are retrieved by queries, The column pattern is set by the data source, or by theformat clause of the query language. An example of a pattern is'#,##0.00'. For more on patterns see thequery language reference.

getColumnProperties(columnIndex)Object

Returns a map of all properties for the specified column. Note that the properties object is returned by reference, so changing values in the retrieved object changes them in theDataTable.

  • columnIndex is the numeric index of the column to retrieve properties for.
getColumnProperty(columnIndex, name)Auto

Returns the value of a named property, ornull if no such property is set for the specified column. The return type varies, depending on the property.

  • columnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method.
  • name is the property name, as a string.

See also:setColumnPropertysetColumnProperties

getColumnRange(columnIndex)Object

Returns the minimal and maximal values of values in a specified column. The returned object has propertiesmin andmax. If the range has no values,min andmax will containnull.

columnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method.

getColumnRole(columnIndex)StringReturns therole of the specified column.
getColumnType(columnIndex)String

Returns the type of a given column specified by the column index.

  • columnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method.

The returned column type can be one of the following:'string', 'number', 'boolean', 'date', 'datetime', and'timeofday'

getDistinctValues(columnIndex)Array of objects

Returns the unique values in a certain column, in ascending order.

  • columnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method.

The type of the returned objects is the same as that returned by thegetValue method.

getFilteredRows(filters)Array of objects

Returns the row indexes for rows that match all of the given filters. The indexes are returned in ascending order. The output of this method can be used as input toDataView.setRows() to change the displayed set of rows in a visualization.

filters - An array of objects that describe an acceptable cell value. A row index is returned by this method if it matchesall of the given filters. Each filter is an object with a numericcolumn property that specifies the index of the column in the row to assess, plus one of the following:

  • Avalue property with a value that must be matched exactly by the cell in the specified column. The value must be the same type as the column;or
  • One or both of the following properties, the same type as the column being filtered:
    • minValue - A minimum value for the cell. The cell value in the specified column must be greater than or equal to this value.
    • maxValue - A maximum value for the cell. The cell value in the specified column must be less than or equal to this value.
    A null or undefined value forminValue (ormaxValue) means that the lower (or upper) bound of the range will not be enforced.

Another optional property,test, specifies a function to be combined with value or range filtering. The function is called with the cell value, the row and column indices, and the datatable. It should return false if the row should be excluded from the result.

Example:getFilteredRows([{column: 3, value: 42}, {column: 2, minValue: 'bar', maxValue: 'foo'}, {column: 1, test: (value, rowId, columnId, datatable) => { return value == "baz"; }}]) returns an array containing, in ascending order, the indexes of all rows for which the fourth column (column index 3) is exactly 42, and the third column (column index 2) is between 'bar' and 'foo' (inclusive).

getFormattedValue(rowIndex, columnIndex)String

Returns the formatted value of the cell at the given row and column indexes.

  • rowIndex should be a number greater than or equal to zero, and less than the number of rows as returned by thegetNumberOfRows() method.
  • ColumnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method.

For more on formatting column values see thequery language reference.

See also:setFormattedValue

getNumberOfColumns()NumberReturns the number of columns in the table.
getNumberOfRows()NumberReturns the number of rows in the table.
getProperties(rowIndex, columnIndex)Object

Returns a map of all the properties for the specified cell. Note that the properties object is returned by reference, so changing values in the retrieved object changes them in theDataTable.

  • rowIndex is the cell's row index.
  • columnIndex is the cell's column index.
getProperty(rowIndex, columnIndex, name)Auto

Returns the value of a named property, ornull if no such property is set for the specified cell. The return type varies, depending on the property.

  • rowIndex should be a number greater than or equal to zero, and less than the number of rows as returned by thegetNumberOfRows() method.
  • columnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method.
  • name is a string with the property name.

See also:setCellsetPropertiessetProperty

getRowProperties(rowIndex)Object

Returns a map of all properties for the specified row. Note that the properties object is returned by reference, so changing values in the retrieved object changes them in theDataTable.

  • rowIndex is the index of the row to retrieve properties for.
getRowProperty(rowIndex, name)Auto

Returns the value of a named property, ornull if no such property is set for the specified row. The return type varies, depending on the property.

  • rowIndex should be a number greater than or equal to zero, and less than the number of rows as returned by thegetNumberOfRows() method.
  • name is a string with the property name.

See also:setRowPropertysetRowProperties

getSortedRows(sortColumns)Array of numbers

Returns a sorted version of the table without modifying the order of the underlying data. To permanently sort the underlying data, callsort(). You can specify sorting in a number of ways, depending on the type you pass in to thesortColumns parameter:

  • A single number specifies the index of the column to sort by. Sorting will be in ascending order.Example:sortColumns(3) will sort by the 4th column, in ascending order.
  • A single object that contains the number of the column index to sort by, and an optional boolean propertydesc. Ifdescis set to true, the specific column will be sorted in descending order; otherwise, sorting is in ascending order.Examples:sortColumns({column: 3}) will sort by the 4th column, in ascending order;sortColumns({column: 3, desc: true}) will sort by the 4th column, in descending order.
  • An array of numbers of the column indexes by which to sort. The first number is the primary column by which to sort, the second one is the secondary, and so on. This means that when two values in the first column are equal, the values in the next column are compared, and so on.Example:sortColumns([3, 1, 6]) will sort first by the 4th column (in ascending order), then by the 2nd column (in ascending order), and then by the 7th column (in ascending order).
  • An array of objects, each one with the number of the column index to sort by, and an optional boolean propertydesc. Ifdescis set to true, the specific column will be sorted in descending order (the default is ascending order). The first object is the primary column by which to sort, the second one is the secondary, and so on. This means that when two values in the first column are equal, the values in the next column are compared, and so on.Example:sortColumn([{column: 3}, {column: 1, desc: true}, {column: 6, desc: true}]) will sort first by the 4th column (in ascending order), then column 2 in descending order, and then column 7 in descending order.

The returned value is an array of numbers, each number is an index of aDataTable row. Iterating on theDataTable rows by the order of the returned array will result in rows ordered by the specifiedsortColumns. The output can be used as input toDataView.setRows() to quickly change the displayed set of rows in a visualization.

Note that the sorting is guaranteed to be stable: this means that if you sort on equal values of two rows, the same sort will return the rows in the same order every time.
See also:sort

Example: To iterate on rows ordered by the third column, use:

var rowInds = data.getSortedRows([{column: 2}]);for (var i = 0; i < rowInds.length; i++) {  var v = data.getValue(rowInds[i], 2);}
getTablePropertiesObjectReturns a map of all properties for the table.
getTableProperty(name)Auto

Returns the value of a named property, ornull if no such property is set for the table. The return type varies, depending on the property.

  • name is a string with the property name.

See also:setTablePropertiessetTableProperty

getValue(rowIndex, columnIndex)Object

Returns the value of the cell at the given row and column indexes.

  • rowIndex should be a number greater than or equal to zero, and less than the number of rows as returned by thegetNumberOfRows() method.
  • columnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method.

The type of the returned value depends on the column type (seegetColumnType):

  • If the column type is 'string', the value is a string.
  • If the column type is 'number', the value is a number.
  • If the column type is 'boolean', the value is a boolean.
  • If the column type is 'date' or 'datetime', the value is a Date object.
  • If the column type is 'timeofday', the value is an array of four numbers: [hour, minute, second, milliseconds].
  • If the cell value is a null value, it returnsnull.
insertColumn(columnIndex, type [,label [,id]])None

Inserts a new column to the data table, at the specifid index. All existing columns at or after the specified index are shifted to a higher index.

  • columnIndex is a number with the required index of the new column.
  • type should be a string with the data type of the values of the column. The type can be one of the following:'string', 'number', 'boolean', 'date', 'datetime', and'timeofday'.
  • label should be a string with the label of the column. The column label is typically displayed as part of the visualization, for example as a column header in a table, or as a legend label in a pie chart. If no value is specified, an empty string is assigned.
  • id should be a string with a unique identifier for the column. If no value is specified, an empty string is assigned.

See also:addColumn

insertRows(rowIndex, numberOrArray)None

Insert the specified number of rows at the specified row index.

  • rowIndex is the index number where to insert the new row(s). Rows will be added, starting at the index number specified.
  • numberOrArray is either a number of new, empty rows to add, or an array of one or more populated rows to add at the index. SeeaddRows() for the syntax for adding an array of row objects.

See also:addRows

removeColumn(columnIndex)None

Removes the column at the specified index.

  • columnIndex should be a number with a valid column index.

See also:removeColumns

removeColumns(columnIndex, numberOfColumns)None

Removes the specified number of columns starting from the column at the specified index.

  • numberOfColumns is the number of columns to remove.
  • columnIndex should be a number with a valid column index.

See also:removeColumn

removeRow(rowIndex)None

Removes the row at the specified index.

  • rowIndex should be a number with a valid row index.

See also:removeRows

removeRows(rowIndex, numberOfRows)None

Removes the specified number of rows starting from the row at the specified index.

  • numberOfRows is the number of rows to remove.
  • rowIndex should be a number with a valid row index.

See also:removeRow

setCell(rowIndex, columnIndex [, value [, formattedValue [, properties]]])None

Sets the value, formatted value, and/or properties, of a cell.

  • rowIndex should be a number greater than or equal to zero, and less than the number of rows as returned by thegetNumberOfRows() method.
  • columnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method.
  • value [Optional] is the value assigned to the specified cell. To avoid overwriting this value, set this parameter toundefined; to clear this value, set it tonull. The type of the value depends on the column type (seegetColumnType()):
    • If the column type is 'string', the value should be a string.
    • If the column type is 'number', the value should be a number.
    • If the column type is 'boolean', the value should be a boolean.
    • If the column type is 'date' or 'datetime', the value should be a Date object.
    • If the column type is 'timeofday', the value should be an array of four numbers: [hour, minute, second, milliseconds].
  • formattedValue [Optional] is a string with the value formatted as a string. To avoid overwriting this value, set this parameter toundefined; to clear this value and have the API apply default formatting tovalue as needed, set it tonull; to explicitly set an empty formatted value, set it to an empty string. The formatted value is typically used by visualizations to display value labels. For example the formatted value can appear as a label text within a pie chart.
  • properties [Optional] is anObject (a name/value map) with additional properties for this cell. To avoid overwriting this value, set this parameter toundefined; to clear this value, set it tonull. Some visualizations support row, column, or cell properties to modify their display or behavior; see the visualization documentation to see what properties are supported.

See also:setValue(),setFormattedValue(),setProperty(),setProperties().

setColumnLabel(columnIndex, label)None

Sets the label of a column.

  • columnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method.
  • label is a string with the label to assign to the column. The column label is typically displayed as part of the visualization. For example the column label can be displayed as a column header in a table, or as the legend label in a pie chart.

See also:getColumnLabel

setColumnProperty(columnIndex, name, value)None

Sets a single column property. Some visualizations support row, column, or cell properties to modify their display or behavior; see the visualization documentation to see what properties are supported.

  • columnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method.
  • name is a string with the property name.
  • value is a value of any type to assign to the specified named property of the specified column.

See also:setColumnPropertiesgetColumnProperty

setColumnProperties(columnIndex, properties)None

Sets multiple column properties. Some visualizations support row, column, or cell properties to modify their display or behavior; see the visualization documentation to see what properties are supported.

  • columnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method.
  • properties is anObject (name/value map) with additional properties for this column. Ifnull is specified, all additional properties of the column will be removed.

See also:setColumnPropertygetColumnProperty

setFormattedValue(rowIndex, columnIndex, formattedValue)None

Sets the formatted value of a cell.

  • rowIndex should be a number greater than or equal to zero, and less than the number of rows as returned by thegetNumberOfRows() method.
  • columnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method.
  • formattedValue is a string with the value formatted for display. To clear this value and have the API apply default formatting to the cell value as needed, set itformattedValuenull; to explicitly set an empty formatted value, set it to an empty string.

See also:getFormattedValue

setProperty(rowIndex, columnIndex, name, value)None

Sets a cell property. Some visualizations support row, column, or cell properties to modify their display or behavior; see the visualization documentation to see what properties are supported.

  • rowIndex should be a number greater than or equal to zero, and less than the number of rows as returned by thegetNumberOfRows() method.
  • columnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method.
  • name is a string with the property name.
  • value is a value of any type to assign to the specified named property of the specified cell.

See also:setCellsetPropertiesgetProperty

setProperties(rowIndex, columnIndex, properties)None

Sets multiple cell properties. Some visualizations support row, column, or cell properties to modify their display or behavior; see the visualization documentation to see what properties are supported.

  • rowIndex should be a number greater than or equal to zero, and less than the number of rows as returned by thegetNumberOfRows() method.
  • columnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method.
  • properties is anObject (name/value map) with additional properties for this cell. Ifnull is specified, all additional properties of the cell will be removed.

See also:setCellsetPropertygetProperty

setRowProperty(rowIndex, name, value)None

Sets a row property. Some visualizations support row, column, or cell properties to modify their display or behavior; see the visualization documentation to see what properties are supported.

  • rowIndex should be a number greater than or equal to zero, and less than the number of rows as returned by thegetNumberOfRows() method.
  • name is a string with the property name.
  • value is a value of any type to assign to the specified named property of the specified row.

See also:setRowPropertiesgetRowProperty

setRowProperties(rowIndex, properties)None

Sets multiple row properties. Some visualizations support row, column, or cell properties to modify their display or behavior; see the visualization documentation to see what properties are supported.

  • rowIndex should be a number greater than or equal to zero, and less than the number of rows as returned by thegetNumberOfRows() method.
  • properties is anObject (name/value map) with additional properties for this row. Ifnull is specified, all additional properties of the row will be removed.

See also:setRowPropertygetRowProperty

setTableProperty(name, value)None

Sets a single table property. Some visualizations support table, row, column, or cell properties to modify their display or behavior; see the visualization documentation to see what properties are supported.

  • name is a string with the property name.
  • value is a value of any type to assign to the specified named property of the table.

See also:setTablePropertiesgetTableProperty

setTableProperties(properties)None

Sets multiple table properties. Some visualizations support table, row, column, or cell properties to modify their display or behavior; see the visualization documentation to see what properties are supported.

  • properties is anObject (name/value map) with additional properties for the table. Ifnull is specified, all additional properties of the table will be removed.

See also:setTablePropertygetTableProperty

setValue(rowIndex, columnIndex, value)None

Sets the value of a cell. In addition to overwriting any existing cell value, this method will also clear out any formatted value and properties for the cell.

  • rowIndex should be a number greater than or equal to zero, and less than the number of rows as returned by thegetNumberOfRows() method.
  • columnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method. This method does not let you set a formatted value for this cell; to do that, callsetFormattedValue().
  • value is the value assigned to the specified cell. The type of the returned value depends on the column type (seegetColumnType):
    • If the column type is 'string', the value should be a string.
    • If the column type is 'number', the value should be a number.
    • If the column type is 'boolean', the value should be a boolean.
    • If the column type is 'date' or 'datetime', the value should be a Date object.
    • If the column type is 'timeofday', the value should be an array of four numbers: [hour, minute, second, milliseconds].
    • For any column type, the value can be set tonull.

See also:setCell,setFormattedValue, setProperty,setProperties

sort(sortColumns)None Sorts the rows, according to the specified sort columns. TheDataTable is modified by this method. SeegetSortedRows() for a description of the sorting details. This method does not return the sorted data.
See also:getSortedRows
Example: To sort by the third column and then by the second column, use:data.sort([{column: 2}, {column: 1}]);
toJSON()String Returns a JSON representation of theDataTable that can be passed into theDataTableconstructor. For example:
{"cols":[{"id":"Col1","label":"","type":"date"}], "rows":[   {"c":[{"v":"a"},{"v":"Date(2010,10,6)"}]},   {"c":[{"v":"b"},{"v":"Date(2010,10,7)"}]} ]}

Format of the Constructor's JavaScript LiteraldataParameter

You can initialize aDataTable by passing a JavaScript string literal object into thedata parameter. We'll call this object thedata object. You can code this object by hand, according to the description below, or you can use ahelper Python library if you know how to use Python, and your site can use it. However, if you want to construct the object by hand, this section will describe the syntax.

First, let's show an example of a simple JavaScript object describing a table with three rows and three columns (String, Number, and Date types):

{  cols: [{id: 'A', label: 'NEW A', type: 'string'},         {id: 'B', label: 'B-label', type: 'number'},         {id: 'C', label: 'C-label', type: 'date'}  ],  rows: [{c:[{v: 'a'},             {v: 1.0, f: 'One'},             {v: new Date(2008, 1, 28, 0, 31, 26), f: '2/28/08 12:31 AM'}        ]},         {c:[{v: 'b'},             {v: 2.0, f: 'Two'},             {v: new Date(2008, 2, 30, 0, 31, 26), f: '3/30/08 12:31 AM'}        ]},         {c:[{v: 'c'},             {v: 3.0, f: 'Three'},             {v: new Date(2008, 3, 30, 0, 31, 26), f: '4/30/08 12:31 AM'}        ]}  ],  p: {foo: 'hello', bar: 'world!'}}

Now let's describe the syntax:

Thedataobject consists of two required top-level properties,cols androws, and an optionalp property that is a map of arbitrary values.

Note: All property names and string constants shown are case-sensitive. Also, properties described as taking a string value should have their value enclosed in quotation marks. For example, if you wish to specify the type property as being number, it would be expressed like this:type: 'number' but the value itself, as numeric, would be expressed like this:v: 42

cols Property

cols is an array of objects describing the ID and type of each column. Each property is an object with the following properties (case-sensitive):

  • type [Required] Data type of the data in the column. Supports the following string values (examples include the v: property, described later):
    • 'boolean' - JavaScript boolean value ('true' or 'false').Example value:v:'true'
    • 'number' - JavaScript number value.Example values:v:7 ,v:3.14,v:-55
    • 'string' - JavaScript string value.Example value:v:'hello'
    • 'date' - JavaScript Date object (zero-based month), with the time truncated.Example value:v:new Date(2008, 0, 15)
    • 'datetime' - JavaScript Date object including the time.Example value:v:new Date(2008, 0, 15, 14, 30, 45)
    • 'timeofday' - Array of three numbers and an optional fourth, representing hour (0 indicates midnight), minute, second, and optional millisecond.Example values:v:[8, 15, 0],v: [6, 12, 1, 144]
  • id [Optional] String ID of the column. Must be unique in the table. Use basic alphanumeric characters, so the host page does not require fancy escapes to access the column in JavaScript. Be careful not to choose a JavaScript keyword.Example:id:'col_1'
  • label [Optional] String value that some visualizations display for this column.Example:label:'Height'
  • pattern [Optional] String pattern that was used by a data source to format numeric, date, or time column values. This is for reference only; you probably won't need to read the pattern, and it isn't required to exist. The Google Visualization client does not use this value (it reads the cell's formatted value). If theDataTable has come from a data source in response to a query with aformat clause, the pattern you specified in that clause will probably be returned in this value. The recommended pattern standards are the ICU DecimalFormat and SimpleDateFormat.
  • p[Optional] An object that is a map of custom values applied to the cell. These values can be of any JavaScript type. If your visualization supports any cell-level properties, it will describe them; otherwise, this property will be ignored. Example:p:{style: 'border: 1px solid green;'}.

cols Example

cols: [{id: 'A', label: 'NEW A', type: 'string'},       {id: 'B', label: 'B-label', type: 'number'},       {id: 'C', label: 'C-label', type: 'date'}]

rows Property

Therows property holds an array of row objects.

Each row object has one required property calledc, which is an array of cells in that row. It also has an optionalp property that defines a map of arbitrary custom values to assign to the whole row. If your visualization supports any row-level properties it will describe them; otherwise, this property will be ignored.

Cell Objects

Each cell in the table is described by an object with the following properties:

  • v [Optional] The cell value. The data type should match the column data type. If the cell is null, thev property should be null, though it can still havef andp properties.
  • f [Optional] A string version of thev value, formatted for display. Typically the values will match, though they do not need to, so if you specifyDate(2008, 0, 1) forv, you should specify "January 1, 2008" or some such string for this property. This value is not checked against thev value. The visualization will not use this value for calculation, only as a label for display. If omitted, a string version ofv will be automatically generated using the default formatter. Thef values can be modified using your own formatter, or set withsetFormattedValue() orsetCell(), or retrieved withgetFormattedValue().
  • p[Optional] An object that is a map of custom values applied to the cell. These values can be of any JavaScript type. If your visualization supports any cell-level properties, it will describe them. These properties can be retrieved by thegetProperty() andgetProperties() methods. Example:p:{style: 'border: 1px solid green;'}.

Cells in the row array should be in the same order as their column descriptions incols. To indicate a null cell, you can specifynull, leave a blank for a cell in an array, or omit trailing array members. So, to indicate a row with null for the first two cells, you could specify[ , , {cell_val}] or[null, null, {cell_val}].

Here is a sample table object with three columns, filled with three rows of data:

{  cols: [{id: 'A', label: 'NEW A', type: 'string'},         {id: 'B', label: 'B-label', type: 'number'},         {id: 'C', label: 'C-label', type: 'date'}  ],  rows: [{c:[{v: 'a'},             {v: 1.0, f: 'One'},             {v: new Date(2008, 1, 28, 0, 31, 26), f: '2/28/08 12:31 AM'}        ]},         {c:[{v: 'b'},             {v: 2.0, f: 'Two'},             {v: new Date(2008, 2, 30, 0, 31, 26), f: '3/30/08 12:31 AM'}        ]},         {c:[{v: 'c'},             {v: 3.0, f: 'Three'},             {v: new Date(2008, 3, 30, 0, 31, 26), f: '4/30/08 12:31 AM'}        ]}  ]}

p Property

The table-levelp property is a map of custom values applied to the wholeDataTable. These values can be of any JavaScript type. If your visualization supports any datatable-level properties, it will describe them; otherwise, this property will be available for application use. Example:p:{className: 'myDataTable'}.

DataView Class

A read-only view of an underlyingDataTable. ADataView allows selection of only a subset of the columns and/or rows. It also allows reordering columns/rows, and duplicating columns/rows.

A view is a live window on the underlyingDataTable, not a static snapshot of data. However, you still must be careful when changing thestructure of the table itself, as described here:

  • Adding or removingcolumns from the underlying table will not be reflected by the view, and might cause unexpected behavior in the view; you will have to create a newDataView from theDataTable to pick up these changes.
  • Adding or removingrows from the underlying table is safe and changes will be propagated to the view immediately (but you must calldraw() on any visualizations after this change to have the new row set rendered). Note that if your view has filtered out rows by calling one of thesetRows() or hideRows() methods, and you add or remove rows from the underlying table, the behavior is unexpected; you must create a newDataView to reflect the new table.
  • Changingcell values in existing cells is safe, and changes are immediately propagated to theDataView (but you must calldraw() on any visualizations after this change to have the new cell values rendered).

It is also possible to create aDataView from anotherDataView. Note that whenever anunderlying table or view is mentioned, it refers to the level immediately below this level. In other words, it refers to the data object used to construct thisDataView.

DataView also supports calculated columns; these are columns whose value is calculated on the fly using a function that you supply. So, for example, you can include a column that is a sum of two preceding columns, or a column that calculates and shows the calendar quarter of a date from another column. SeesetColumns() for more details.

When you modify aDataView by hiding or showing rows or columns, the visualization will not be affected until you calldraw() on the visualization again.

You can combineDataView.getFilteredRows() withDataView.setRows() to create aDataView with an interesting subset of data, as shown here:

var data = new google.visualization.DataTable();data.addColumn('string', 'Employee Name');data.addColumn('date', 'Start Date');data.addRows(6);data.setCell(0, 0, 'Mike');data.setCell(0, 1, new Date(2008, 1, 28));data.setCell(1, 0, 'Bob');data.setCell(1, 1, new Date(2007, 5, 1));data.setCell(2, 0, 'Alice');data.setCell(2, 1, new Date(2006, 7, 16));data.setCell(3, 0, 'Frank');data.setCell(3, 1, new Date(2007, 11, 28));data.setCell(4, 0, 'Floyd');data.setCell(4, 1, new Date(2005, 3, 13));data.setCell(5, 0, 'Fritz');data.setCell(5, 1, new Date(2007, 9, 2));// Create a view that shows everyone hired since 2007.var view = new google.visualization.DataView(data);view.setRows(view.getFilteredRows([{column: 1, minValue: new Date(2007, 0, 1)}]));var table = new google.visualization.Table(document.getElementById('test_dataview'));table.draw(view, {sortColumn: 1});

Constructors

There are two ways to create a newDataView instance:

Constructor 1

var myView = new google.visualization.DataView(data)
data
ADataTable orDataView used to initialize the view. By default, the view contains all the columns and rows in the underlying data table or view, in the original order. To hide or show rows or columns in this view, call the appropriateset...() orhide...() methods.

See also:

setColumns(),hideColumns(),setRows(),hideRows().

 

Constructor 2

This constructor creates a newDataView by assigning a serializedDataView to aDataTable. It helps you recreate theDataView that you serialized usingDataView.toJSON().

var myView = google.visualization.DataView.fromJSON(data,viewAsJson)
data
TheDataTable object that you used to create theDataView, on which you calledDataView.toJSON(). If this table is any different from the original table, you will get unpredictable results.
viewAsJson
The JSON string returned byDataView.toJSON(). This is a description of which rows to show or hide from thedata DataTable.

Methods

MethodReturn ValueDescription
See descriptions inDataTable. Same as the equivalentDataTable methods, except that row/column indexes refer to the index in the view and not in the underlying table/view.
getTableColumnIndex(viewColumnIndex)Number

Returns the index in the underlying table (or view) of a given column specified by its index in this view.viewColumnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method. Returns -1 if this is agenerated column.

Example: IfsetColumns([3, 1, 4]) was previously called, thengetTableColumnIndex(2) will return4.

getTableRowIndex(viewRowIndex)Number

Returns the index in the underlying table (or view) of a given row specified by its index in this view.viewRowIndex should be a number greater than or equal to zero, and less than the number of rows as returned by thegetNumberOfRows() method.

Example: IfsetRows([3, 1, 4]) was previously called, thengetTableRowIndex(2) will return4.

getViewColumnIndex(tableColumnIndex)Number

Returns the index in this view that maps to a given column specified by its index in the underlying table (or view). If more than one such index exists, returns the first (smallest) one. If no such index exists (the specified column is not in the view), returns -1.tableColumnIndex should be a number greater than or equal to zero, and less than the number of columns as returned by thegetNumberOfColumns() method of the underlying table/view.

Example: IfsetColumns([3, 1, 4]) was previously called, thengetViewColumnIndex(4) will return2.

getViewColumns()Array of numbers

Returns the columns in this view, in order. That is, if you callsetColumns with some array, and then callgetViewColumns() you should get an identical array.

getViewRowIndex(tableRowIndex)Number

Returns the index in this view that maps to a given row specified by its index in the underlying table (or view). If more than one such index exists, returns the first (smallest) one. If no such index exists (the specified row is not in the view), returns -1.tableRowIndex should be a number greater than or equal to zero, and less than the number of rows as returned by thegetNumberOfRows() method of the underlying table/view.

Example: IfsetRows([3, 1, 4]) was previously called, thengetViewRowIndex(4) will return2.

getViewRows()Array of numbers

Returns the rows in this view, in order. That is, if you callsetRows with some array, and then callgetViewRows() you should get an identical array.

hideColumns(columnIndexes)none

Hides the specified columns from the current view.columnIndexes is an array of numbers representing the indexes of the columns to hide. These indexes are the index numbers in theunderlying table/view. The numbers incolumnIndexes do not have to be in order (that is, [3,4,1] is fine). The remaining columns retain their index order when you iterate through them. Entering an index number for a column already hidden is not an error, but entering an index that does not exist in the underlying table/view will throw an error. To unhide columns, callsetColumns().

Example: If you have a table with 10 columns, and you callsetColumns([2,7,1,7,9]), and thenhideColumns([7,9]), the columns in the view will then be [2,1].

hideRows(min, max)None

Hides all rows with indexes that lie between min and max (inclusive) from the current view. This is a convenience syntax forhideRows(rowIndexes) above. For example,hideRows(5, 10) is equivalent tohideRows([5, 6, 7, 8, 9, 10]).

hideRows(rowIndexes)None

Hides the specified rows from the current view.rowIndexes is an array of numbers representing the indexes of the rows to hide. These indexes are the index numbers in theunderlying table/view. The numbers inrowIndexes do not have to be in order (that is, [3,4,1] is fine). The remaining rows retain their index order. Entering an index number for a row already hidden is not an error, but entering an index that does not exist in the underlying table/view will throw an error. To unhide rows, callsetRows().

Example: If you have a table with 10 rows, and you callsetRows([2,7,1,7,9]), and thenhideRows([7,9]), the rows in the view will then be [2,1].

setColumns(columnIndexes)None

Specifies which columns are visible in this view. Any columns not specified will be hidden. This is an array of column indexes in the underlying table/view, or calculated columns. If you don't call this method, the default is to show all columns. The array can also contain duplicates, to show the same column multiple times. Columns will be shown in the order specified.

  • columnIndexes - An array of numbers and/or objects (can be mixed):
    • Numbers specify the index of the source data column to include in the view. The data is brought through unmodified. If you need to explicitly define a role or additional column properties, specify an object with asourceColumn property.
    • Objects specify acalculated column. A calculated column creates a value on the fly for each row and adds it to the view. The object must have the following properties:
      • calc [function] - A function that will be called for each row in the column to calculate a value for that cell. The function signature isfunc(dataTable,row), wheredataTable is the sourceDataTable, androw is the index of the source data row. The function should return a single value of the type specified bytype.
      • type [string] - The JavaScript type of the value that thecalc function returns.
      • label [Optional,string] - An optional label to assign to this generated column. If not specified, the view column will have no label.
      • id [Optional,string] - An optional ID to assign to this generated column.
      • sourceColumn - [Optional,number] The source column to use as a value; if specified, do not specify thecalc or thetype property. This is similar to passing in a number instead of an object, but enables you to specify a role and properties for the new column.
      • properties [Optional,object] - An object containing any arbitrary properties to assign to this column. If not specified, the view column will have no properties.
      • role [Optional,string] - Arole to assign to this column. If not specified, the existing role will not be imported.

Examples:

// Show some columns directly from the underlying data.// Shows column 3 twice.view.setColumns([3, 4, 3, 2]);// Underlying table has a column specifying a value in centimeters.// The view imports this directly, and creates a calculated column// that converts the value into inches.view.setColumns([1,{calc:cmToInches, type:'number', label:'Height in Inches'}]);function cmToInches(dataTable, rowNum){  return Math.floor(dataTable.getValue(rowNum, 1) / 2.54);}
setRows(min, max)None

Sets the rows in this view to be all indexes (in the underlying table/view) that lie between min and max (inclusive). This is a convenience syntax forsetRows(rowIndexes) below. For example,setRows(5, 10) is equivalent tosetRows([5, 6, 7, 8, 9, 10]).

setRows(rowIndexes)None

Sets the visible rows in this view, based on index numbers from the underlying table/view.rowIndexes should be an array of index numbers specifying which rows to show in the view. The array specifies the order in which to show the rows, and rows can be duplicated. Note thatonly the rows specified inrowIndexes will be shown; this method clears all other rows from the view. The array can also contain duplicates, effectively duplicating the specified row in this view (for example,setRows([3, 4, 3, 2]) will cause row3 to appear twice in this view). The array thus provides a mapping of the rows from the underlying table/view to this view. You can usegetFilteredRows() orgetSortedRows() to generate input for this method.

Example: To create a view with rows three and zero of an underlying table/view:view.setRows([3, 0])

toDataTable()DataTable Returns aDataTable object populated with the visible rows and columns of theDataView.
toJSON()string Returns a string representation of thisDataView. Thisstring does not contain the actual data; it only contains theDataView-specific settings such as visible rows and columns. You can store this string and pass it to the staticDataView.fromJSON() constructor to recreate this view. This won't includegenerated columns.

ChartWrapper Class

AChartWrapper class is used to wrap your chart and handle all loading, drawing, and Datasource querying for your chart. The class exposes convenience methods for setting values on the chart and drawing it. This class simplifies reading from a data source, because you do not have to create a query callback handler. You can also use it to save a chart easily for reuse.

Another bonus of usingChartWrapper is that you can reduce the number of library loads by using dynamic loading. Additionally, you don't need to load the packages explicitly sinceChartWrapper will handle looking up and loading the chart packages for you. See the examples below for details.

However,ChartWrapper currently only propagates a subset of events thrown by charts: select, ready, and error. Other events are not transmitted through the ChartWrapper instance; to get other events, you must callgetChart() and subscribe to events directly on the chart handle, as shown here:

var wrapper;function drawVisualization() {  // Draw a column chart  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'  });  // Never called.  google.visualization.events.addListener(wrapper, 'onmouseover', uselessHandler);  // Must wait for the ready event in order to  // request the chart and subscribe to 'onmouseover'.  google.visualization.events.addListener(wrapper, 'ready', onReady);  wrapper.draw();  // Never called  function uselessHandler() {    alert("I am never called!");  }  function onReady() {    google.visualization.events.addListener(wrapper.getChart(), 'onmouseover', usefulHandler);  }  // Called  function usefulHandler() {    alert("Mouseover event!");  }}

Constructor

ChartWrapper(opt_spec)
opt_spec
[Optional] - Either a JSON object defining the chart, or a serialized string version of that object. The format of this object is shown in thedrawChart() documentation. If not specified, you must set all the appropriate properties using theset... methods exposed by this object.

Methods

ChartWrapper exposes the following additional methods:

MethodReturn TypeDescription
draw(opt_container_ref)None

Draws the chart. You must call this method after any changes that you make to the chart or data to show the changes.

  • opt_container_ref [Optional] - A reference to a valid container element on the page. If specified, the chart will be drawn there. If not, the chart will be drawn in the element with ID specified bycontainerId.
toJSON()StringReturns a string version of the JSON representation of the chart.
clone()ChartWrapperReturns a deep copy of the chart wrapper.
getDataSourceUrl()String If this chart gets its data from adata source, returns the URL for this data source. Otherwise, returns null.
getDataTable()google.visualization.DataTable

If this chart gets its data from a locally-definedDataTable, will return a reference to the chart'sDataTable. If this chart gets its data from a data source, it will return null.

Any changes that you make to the returned object will be reflected by the chart the next time you callChartWrapper.draw().

getChartType()String The class name of the wrapped chart. If this is a Google chart, the name will not be qualified withgoogle.visualization. So, for example, if this were a Treemap chart, it would return "Treemap" rather than "google.visualization.treemap".
getChartName()StringReturns the chart name assigned bysetChartName().
getChart()Chart object reference Returns a reference to the chart created by this ChartWrapper, for example a google.visualization.BarChart or a google.visualization.ColumnChart. This will return null until after you have calleddraw() on the ChartWrapper object, and it throws a ready event. Methods called on the returned object will be reflected on the page.
getContainerId()StringThe ID of the chart's DOM container element.
getQuery()StringThe query string for this chart, if it has one and queries a data source.
getRefreshInterval()Number Anyrefresh interval for this chart, if it queries a data source. Zero indicates no refresh.
getOption(key,opt_default_val)Any type

Returns the specified chart option value

  • key - The name of the option to retrieve. May be a qualified name, such as'vAxis.title'.
  • opt_default_value [Optional] - If the specified value is undefined or null, this value will be returned.
getOptions()ObjectReturns the options object for this chart.
getView()Object OR Array Returns theDataView initializer object, in the same format asdataview.toJSON(), or an array of such objects.
setDataSourceUrl(url)None Sets the URL of adata source to use for this chart. If you also set a data table for this object, the data source URL will be ignored.
setDataTable(table)None Sets the DataTable for the chart. Pass in one of the following: null; a DataTable object; a JSON representation of a DataTable; or an array following the syntax ofarrayToDataTable().
setChartType(type)None Sets the chart type. Pass in the class name of the wrapped chart. If this is a Google chart, do not qualify it withgoogle.visualization. So, for example, for a pie chart, pass in "PieChart".
setChartName(name)None Sets an arbitrary name for the chart. This is not shown anywhere on the chart, unless a custom chart is explicitly designed to use it.
setContainerId(id)NoneSets the ID of the containing DOM element for the chart.
setQuery(query_string)None Sets a query string, if this chart queries a data source. You must also set the data source URL if specifying this value.
setRefreshInterval(interval)None Sets therefresh interval for this chart, if it queries a data source. You must also set a data source URL if specifying this value. Zero indicates no refresh.
setOption(key,value)None Sets a single chart option value, wherekey is the option name andvalue is the value. To unset an option, pass in null for the value. Note thatkey may be a qualified name, such as'vAxis.title'.
setOptions(options_obj)NoneSets a complete options object for a chart.
setView(view_spec)None Sets aDataView initializer object, which acts as a filter over the underlying data. The chart wrapper must have underlying data from a DataTable or a data source to apply this view to. You can pass in either a string orDataView initializer object, like that returned bydataview.toJSON(). You can also pass in an array ofDataView initializer objects, in which case the firstDataView in the array is applied to the underlying data to create a new data table, and the secondDataView is applied to the data table resulting from application of the firstDataView, and so on.

Events

The ChartWrapper object throws the following events. Note that you must callChartWrapper.draw() before any events will be thrown.

NameDescriptionProperties
errorFired when an error occurs when attempting to render the chart.id, message
ready The chart is ready for external method calls. If you want to interact with the chart, and call methods after you draw it, you should set up a listener for this eventbefore you call thedraw method, and call them only after the event was fired.None
select Fired when the user clicks a bar or legend. When a chart element is selected, the corresponding cell in the data table is selected; when a legend is selected, the corresponding column in the data table is selected. To learn what has been selected, callChartWrapper.getChart().getSelection(). Note that this will only be thrown when the underlying chart type throws a selection event.None

Example

The following two snippets create an equivalentline chart. The first example uses JSON literal notation to define the chart; the second uses ChartWrapper methods to set these values.

<!DOCTYPEhtml><html><head><metahttp-equiv="content-type"content="text/html; charset=utf-8"/><title>GoogleVisualizationAPISample</title><!--Onescripttagloadsalltherequiredlibraries!--><scripttype="text/javascript"src='https://www.gstatic.com/charts/loader.js'></script><script>google.charts.load('current);google.charts.setOnLoadCallback(drawVisualization);functiondrawVisualization(){varwrap=newgoogle.visualization.ChartWrapper({'chartType':'LineChart','dataSourceUrl':'http://spreadsheets.google.com/tq?key=pCQbetd-CptGXxxQIG7VFIQ&pub=1','containerId':'visualization','query':'SELECT A,D WHERE D > 100 ORDER BY D','options':{'title':'Population Density (people/km^2)','legend':'none'}});wrap.draw();}</script></head><body><divid="visualization"style="height: 400px; width: 400px;"></div></body></html>

Same chart, now using setter methods:

<!DOCTYPEhtml><html><head><metahttp-equiv='content-type'content='text/html; charset=utf-8'/><title>GoogleVisualizationAPISample</title><!--Onescripttagloadsalltherequiredlibraries!--><scripttype="text/javascript"src='https://www.gstatic.com/charts/loader.js'></script><scripttype="text/javascript">google.charts.load('current');google.charts.setOnLoadCallback(drawVisualization);functiondrawVisualization(){//Definethechartusingsetters:varwrap=newgoogle.visualization.ChartWrapper();wrap.setChartType('LineChart');wrap.setDataSourceUrl('http://spreadsheets.google.com/tq?key=pCQbetd-CptGXxxQIG7VFIQ&pub=1');wrap.setContainerId('visualization');wrap.setQuery('SELECT A,D WHERE D > 100 ORDER BY D');wrap.setOptions({'title':'Population Density (people/km^2)','legend':'none'});wrap.draw();}</script></head><body><divid='visualization'style='height: 400px; width: 400px;'></div></body></html>

ChartEditor Class

TheChartEditor class is used to open an in-page dialog box that enables a user to customize a visualization on the fly.

To use ChartEditor:

  1. Load thecharteditor package. Ingoogle.charts.load(), load the package 'charteditor'. You do not need to load the packages for the chart type that you render in the editor; the chart editor will load any package for you as needed.
  2. Create aChartWrapper object that defines the chart for the user to customize. This chart will be shown in the dialog, and the user uses the editor to redesign the chart, change chart types, or even change the source data.
  3. Create a new ChartEditor instance, and register to listen for the "ok" event. This event is thrown when the user clicks the "OK" button on the dialog. When received, you should callChartEditor.getChartWrapper() to retrieve the user-modified chart.
  4. CallChartEditor.openDialog(), passing in theChartWrapper. This opens the dialog. The dialog buttons enable the user to close the editor. TheChartEditor instance is available as long as it is in scope; it is not automatically destroyed after the user closes the dialog.
  5. To update the chart in code, callsetChartWrapper().

Methods

MethodReturn ValueDescription
openDialog(chartWrapper,opt_options)null

Opens the chart editor as an embedded dialog box on the page. The function returns immediately; it does not wait for the dialog to be closed. If you do not lose scope of the instance, you can callopenDialog() again to reopen the dialog, although you must pass in a ChartWrapper object again.

  • chartWrapper - AChartWrapper object defining the initial chart to render in the window. The chart must either have a populatedDataTable, or be connected to a valid data source. This wrapper is copied internally to the chart editor, so any later changes that you make to your ChartWrapper handle will not be reflected in the chart editor's copy.
  • opt_options - [Optional] An object containing any options for the chart editor. See the options table below.
getChartWrapper()ChartWrapperReturns aChartWrapper representing the chart, with user modifications.
setChartWrapper(chartWrapper)null

Use this method to update the rendered chart on the editor.

chartWrapper - AChartWrapper object representing the new chart to render. The chart must either have a populatedDataTable, or be connected to a valid data source.

closeDialog()nullCloses the chart editor dialog box.

Options

The chart editor supports the following options:

NameTypeDefaultDescription
dataSourceInputElement handle or 'urlbox'null

Use this to enable the user to specify a data source for the chart. This property can be one of two values:

  • 'urlbox' - Show the chart's data source URL on the dialog in an editable textbox. The user can modify this, and the chart will be redrawn, based on the new data source.
  • DOM element - Enables you to provide a custom HTML element to use to select a data source. Pass in a handle to an HTML element, either one created in code or copied from the page. This element will be displayed on the dialog. Use this as a way to let the user choose the chart's data source. For example, create a listbox containing several data source URLs, or user-friendly names that the user can choose from. The element must implement a selection handler and use it to change the chart's data source: for example, either change the underlyingDataTable, or modify the chart'sdataSourceUrl field.

Events

The chart editor throws the following events:

NameDescriptionProperties
ok Fired when the user clicks the "OK" button on the dialog. After receiving this method, you should callgetChartWrapper() to retrieve the user-configured chart.none
cancelFired when the user clicks the "Cancel" button on the dialog.none

Example

The following example code opens a chart editor dialog with a populated line chart. If the user clicks "OK", the edited chart will be saved to the specified<div> on the page.

<!DOCTYPEhtml><html><head><metahttp-equiv="content-type"content="text/html; charset=utf-8"/><title>GoogleVisualizationAPISample</title><scripttype="text/javascript"src="https://www.gstatic.com/charts/loader.js"></script><scripttype="text/javascript">google.charts.load('current',{packages:['charteditor']});</script><scripttype="text/javascript">google.charts.setOnLoadCallback(loadEditor);varchartEditor=null;functionloadEditor(){//Createthecharttoedit.varwrapper=newgoogle.visualization.ChartWrapper({'chartType':'LineChart','dataSourceUrl':'http://spreadsheets.google.com/tq?key=pCQbetd-CptGXxxQIG7VFIQ&pub=1','query':'SELECT A,D WHERE D > 100 ORDER BY D','options':{'title':'Population Density (people/km^2)','legend':'none'}});chartEditor=newgoogle.visualization.ChartEditor();google.visualization.events.addListener(chartEditor,'ok',redrawChart);chartEditor.openDialog(wrapper,{});}//On"OK"savethecharttoa<div>onthepage.functionredrawChart(){chartEditor.getChartWrapper().draw(document.getElementById('vis_div'));}</script></head><body><divid="vis_div"style="height: 400px; width: 600px;"></div></body></html>

Data Manipulation Methods

Thegoogle.visualization.data namespace holds static methods to perform SQL-like operations onDataTable objects, for example joining them or grouping by column value.

Thegoogle.visualization.data namespace exposes the following methods:

MethodDescription
google.visualization.data.group Performs a SQL GROUP BY action to return a table grouped by values in specified columns.
google.visualization.data.joinJoins two data tables on one or more key columns.

group()

Takes a populatedDataTable object and performs a SQL-like GROUP BY operation, returning a table with rows grouped by the specified column values. Note that this does not modify the inputDataTable.

The returned table includes one row for each combination of values in the specified key columns. Each row includes the key columns, plus one column with an aggregated column value over all rows that match the key combination (for example, a sum or count of all values in the specified column).

Thegoogle.visualization.data namespace includes several useful aggregation values (for example,sum andcount), but you can define your own (for example, standardDeviation or secondHighest). Instructions on how to define your own aggregator are given after the method description.

Syntax

google.visualization.data.group(data_table,keys,columns)
data_table
The inputDataTable. This will not be modified by callinggroup().
keys
An array of numbers and/or objects specifying which columns to group by. The result table includes every column in this array, as well as every column incolumns. If a number, this is a column index of the inputDataTable to group by. If an object, it will include a function that can modify the specified column (for example, add 1 to the value in that column). The object must have the following properties:
  • column - A number that is a column index fromdt to apply the transformation to.
  • modifier - A function that accepts one value (the cell value in that column for each row), and returns the modified value. This function is used to modify the column value to assist in the grouping: for example, by calling a whichQuarter function that calculates a quarter from a date column, so the API can group rows by quarter. The calculated value is displayed in the key columns in the returned table. This function can be declared inline inside this object, or it can be a function that you define elsewhere in your code (it must be within the calling scope). The API providesone simple modifier function;here are instructions on how to create your own, more useful functions. You must know the data type that this function can accept, and call it only on columns of that type. You must also know the return type of this function, and declare it in thetype property described next.
  • type - The type returned by the functionmodifier. This should be a JavaScript string type name, for example: 'number' or 'boolean'.
  • label - [Optional] A string label to assign this column in the returnedDataTable.
  • id - [Optional] A string ID to assign this column in the returnedDataTable.

Examples:[0],[0,2],[0,{column:1, modifier:myPlusOneFunc, type:'number'},2]
columns
[Optional] Lets you specify which columns, in addition to key columns, to include in the output table. Because all rows in the row group are compressed into a single output row, you must determine what value to display for that row group. For example, you could choose to show the column value from the first row in the set, or an average of all rows in that group.columns is an array of objects, with the following properties:
  • column - A number specifying the index of the column to show.
  • aggregation - A function that accepts an array of all values of this column in this row group and returns a single value to display in the result row. The return value must be of the type specified by the object'stype property. Details on creating your own aggregation function are given below. You must know what data types this method accepts and only call it on columns of the appropriate type. The API provides several useful aggregation functions. SeeProvided Aggregation Functions below for a list, orCreating an aggregation function to learn how to write your own aggregation function.
  • type - The return type of the aggregation function. This should be a JavaScript string type name, for example: 'number' or 'boolean'.
  • label - [Optional] A string label to apply to this column in the returned table.
  • id - [Optional] A string ID to apply to this column in the returned table.

Return Value

ADataTable with one column for each column listed inkeys and one column for each column listed incolumns. The table is sorted by key rows, from left to right.

Example

// This call will group the table by column 0 values.// It will also show column 3, which will be a sum of// values in that column for that row group.var result = google.visualization.data.group(  dt,  [0],  [{'column': 3, 'aggregation': google.visualization.data.sum, 'type': 'number'}]);*Input table*1  'john'  'doe'            101  'jane'  'doe'           1003  'jill'  'jones'          503  'jack'  'jones'          755  'al'    'weisenheimer'  500*Output table*1  1103  1255  500

Provided Modifier Functions

The API provides the following modifier functions that you can pass into thekeys.modifier parameter to customize grouping behavior.

FunctionInput Array TypeReturn TypeDescription
google.visualization.data.monthDatenumberGiven a date, it will return the zero-based month value (0, 1, 2, and so on).

Provided Aggregation Functions

The API provides the following aggregation functions that you can pass into thecolumns.aggregation parameter array.

FunctionInput Array TypeReturn TypeDescription
google.visualization.data.avgnumbernumberThe average value of the array passed in.
google.visualization.data.countany typenumberThe count of rows in the group. Null and duplicate values are counted.
google.visualization.data.maxnumber, string, Datenumber, string, Date, null The maximum value in the array. For strings, this is the first item in an lexicographically sorted list; for Date values, it is the latest date. Nulls are ignored. Returns null if there is no maximum.
google.visualization.data.min number, string, Datenumber, string, Date, null The minimum value in the array. For strings, this is the last item in an lexicographically sorted list; for Date values, it is the earliest date. Nulls are ignored. Returns null if if there is no minimum.
google.visualization.data.sumnumbernumberThe sum of all values in the array.

Creating a modifier function

You can create a modifier function to perform a simple transformation onkey values before thegroup() function groups your rows. This function takes a single cell value, performs an action on it (for example, adds 1 to the value), and returns it. The input and return types need not be the same type, but the caller must know the input and output types. Here's an example of a function that accepts a date and returns the quarter:

// Input type: Date// Return type: number (1-4)function getQuarter(someDate) {  return Math.floor(someDate.getMonth()/3) + 1;}

Creating an aggregation function

You can create an aggregation function that accepts a set of column values in a row group and returns a single number: for example, returning a count or average of values. Here is an implementation of the provided count aggregation function, which returns a count of how many rows are in the row group:

// Input type: Array of any type// Return type: numberfunction count(values) {  return values.length;}

join()

This method joins two data tables (DataTable orDataView objects) into a single results table, similar to a SQL JOIN statement. You specify one or more column pairs (key columns) between the two tables, and the output table includes the rows according to a join method that you specify: only rows where both keys match; all rows from one table; or all rows from both tables, whether or not the keys match. The results table includes only the key columns, plus any additional columns that you specify. Note thatdt2 cannot have duplicate keys, butdt1 can. The term "key" means the combination of all key column values, not a specific key column value; so if a row has cell values A | B | C and columns 0 and 1 are key columns, then the key for that row is AB.

Syntax

google.visualization.data.join(dt1,dt2,joinMethod,keys,dt1Columns,dt2Columns);
dt1
A populatedDataTable to join withdt2.
dt2
A populatedDataTable to join withdt1. This table cannot have multiple identical keys (where a key is a combination of key column values).
joinMethod
A string specifying the join type. Ifdt1 has multiple rows that match adt2 row, the output table will include all matchingdt1 rows. Choose from the following values:
  • 'full' - The output table includes all rows from both tables, regardless of whether keys match. Unmatched rows will have null cell entries; matched rows are joined.
  • 'inner' - The full join filtered to include only rows where the keys match.
  • 'left' - The output table includes all rows fromdt1, whether or not there are any matching rows fromdt2.
  • 'right' - The output table includes all rows fromdt2, whether or not there are any matching rows fromdt1.
keys
An array of key columns to compare from both tables. Each pair is a two element array, the first is a key indt1, the second is a key indt2. This array can specify columns by their index, id, or label, seegetColumnIndex.
Columns must be the same type in both tables. All specified keys must match according to the rule given byjoinMethod in order to include a row from the table. Key columns are always included in the output table. Onlydt1, the left-hand table, can include duplicate keys; keys indt2 must be unique. The term "key" here means a unique set of key columns, not individual column values. For example, if your key columns were A and B, the following table would have only unique key values (and could thus be used asdt2):
AB
Jen Red
JenBlue
Fred Red
Example:[[0,0], [2,1]] compares values from the first column in both tables as well as the third column fromdt1 with the second column fromdt2.
dt1Columns
An array of columns fromdt1 to include in the output table, in addition todt1's key columns. This array can specify columns by their index, id, or label, seegetColumnIndex.
dt2Columns
An array of columns fromdt2 to include in the output table, in addition todt2's key columns. This array can specify columns by their index, id, or label, seegetColumnIndex.

Return Value

ADataTable with the key columns,dt1Columns, anddt2Columns. This table is sorted by the key columns, from left to right. WhenjoinMethod is 'inner', all key cells should be populated. For other join methods, if no matching key is found, the table will have a null for any unmatched key cells.

Examples

*Tables*dt1dt2bob  | 111 | red           bob   | 111 | pointbob  | 111 | green         ellyn | 222 | squarebob  | 333 | orange        jane  | 555 | circlefred | 555 | blue          jane  | 777 | trianglejane | 777 | yellow        fred  | 666 | dodecahedron* Note that right table has duplicate Jane entries, but the key we will use is* columns 0 and 1. The left table has duplicate key values, but that is* allowed.*Inner join* google.visualization.data.join(dt1, dt2, 'inner', [[0,0],[1,1]], [2], [2]);bob  | 111 | red    | pointbob  | 111 | green  | pointjane | 777 | yellow | triangle* Note that both rows from dt1 are included and matched to* the equivalent dt2 row.*Full join* google.visualization.data.join(dt1, dt2, 'full', [[0,0],[1,1]], [2], [2]);bob   | 111 | red    | pointbob   | 111 | green  | pointbob   | 333 | orange |nullellyn | 222 |null | squarefred  | 555 | blue   |nullfred  | 666 |null | dodecahedronjane  | 555 |null | circlejane  | 777 | yellow | triangle*Left join*  google.visualization.data.join(dt1, dt2, 'left', [[0,0],[1,1]], [2], [2]);bob  | 111 | red | pointbob  | 111 | green | pointbob  | 333 | orange |nullfred | 555 | blue |nulljane | 777 | yellow | triangle*Right join*  google.visualization.data.join(dt1, dt2, 'right', [[0,0],[1,1]], [2], [2]);bob   | 111 | red | pointbob   | 111 | green | pointellyn | 222 |null | squarefred  | 666 |null | dodecahedronjane  | 555 |null | circlejane  | 777 | yellow | triangle

Formatters

The Google Visualization API provides formatters that can be used to reformat data in a visualization. These formatters change the formatted value of the specified column in all rows. Note that:

  • Formatters modify only the formatted values, not the underlying values. For example, the displayed value would be "$1,000.00" but the underlying value would still be "1000".
  • Formatters only affect one column at a time; to reformat multiple columns, apply a formatter to each column that you want to change.
  • If you also use user-defined formatters, certain Google Visualization formatters will override all user-defined formatters.
  • The actual formatting applied to the data is derived from the locale the API has been loaded with. For more details, see loading charts with a specific locale.

    Important: Formatters can only be used with aDataTable; they cannot be used with aDataView (DataView objects are read-only).

    Here are the general steps for using a formatter:

    1. Get your populatedDataTable object.
    2. For each column that you want to reformat:
      1. Create an object that specifies all the options for your formatter. This is a basic JavaScript object with a set of properties and values. Look at your formatter's documentation to see what properties are supported. (Optionally, you can pass in an object literal notation object specifying your options.)
      2. Create your formatter, passing in your options object.
      3. Callformatter.format(table,colIndex), passing in theDataTable and the (zero-based) column number of the data to reformat. Note that you can only apply a single formatter to each column; applying a second formatter will simply overwrite the effects of the first.
        Important: Many formatters require HTML tags to display special formatting; if your visualization supports anallowHtml option, you should set it to true.

    Here is an example of changing the formatted date values of a date column to use a long date format ("January 1, 2009"):

    vardata=newgoogle.visualization.DataTable();data.addColumn('string','Employee Name');data.addColumn('date','Start Date');data.addRows(3);data.setCell(0,0,'Mike');data.setCell(0,1,newDate(2008,1,28));data.setCell(1,0,'Bob');data.setCell(1,1,newDate(2007,5,1));data.setCell(2,0,'Alice');data.setCell(2,1,newDate(2006,7,16));//Createaformatter.//Thisexampleusesobjectliteralnotationtodefinetheoptions.varformatter=newgoogle.visualization.DateFormat({formatType:'long'});//Reformatourdata.formatter.format(data,1);//Drawourdatavartable=newgoogle.visualization.Table(document.getElementById('dateformat_div'));table.draw(data,{showRowNumber:true});

    Most formatters expose the following two methods:

    MethodDescription
    google.visualization.formatter_name(options)

    Constructor, whereformatter_name is a specfic formatterclass name.

    • options - A generic JavaScript object that specifies the options for that formatter. This object is a generic object with property/value pairs with properties specific to that formatter. See the documentationfor your specific formatter to learn what options are supported. Here are two example ways to call the constructor for the DateFormat object, passing in two properties:
    // Object literal techniquevar formatter = new google.visualization.DateFormat({formatType: 'long', timeZone: -5});// Equivalent property setting techniquevar options = new Object();options['formatType'] = 'long';options['timeZone'] = -5;var formatter = new google.visualization.DateFormat(options);
    format(data,colIndex)

    Reformats the data in the specified column.

    • data - ADataTable containing the data to reformat. You cannot use a DataView here.
    • colIndex - The zero-based index of the column to format. To format multiple columns, you must call this method multiple times, with different colIndex values.

     

    The Google Visualization API provides the following formatters:

    Formatter NameDescription
    ArrowFormat Adds an up or down arrow, indicating whether the cell value is above or below a specified value.
    BarFormat Adds a colored bar, the direction and color of which indicates whether the cell value is above or below a specified value.
    ColorFormatColors a cell according to whether the values fall within a specified range.
    DateFormat Formats a Date or DateTime value in several different ways, including "January 1, 2009," "1/1/09" and "Jan 1, 2009."
    NumberFormatFormats various aspects of numeric values.
    PatternFormat Concatenates cell values on the same row into a specified cell, along with arbitrary text.

    ArrowFormat

    Adds an up or down arrow to a numeric cell, depending on whether the value is above or below a specified base value. If equal to the base value, no arrow is shown.

    Options

    ArrowFormat supports the following options, passed in to the constructor:

    OptionDescription
    base

    A number indicating the base value, used to compare against the cell value. If the cell value is higher, the cell will include a green up arrow; if the cell value is lower, it will include a red down arrow; if the same, no arrow.

    Sample code

    vardata=newgoogle.visualization.DataTable();data.addColumn('string','Department');data.addColumn('number','Revenues Change');data.addRows([['Shoes',{v:12,f:'12.0%'}],['Sports',{v:-7.3,f:'-7.3%'}],['Toys',{v:0,f:'0%'}],['Electronics',{v:-2.1,f:'-2.1%'}],['Food',{v:22,f:'22.0%'}]]);vartable=newgoogle.visualization.Table(document.getElementById('arrowformat_div'));varformatter=newgoogle.visualization.ArrowFormat();formatter.format(data,1);//Applyformattertosecondcolumntable.draw(data,{allowHtml:true,showRowNumber:true});

    BarFormat

    Adds a colored bar to a numeric cell indicating whether the cell value is above or below a specified base value.

    Options

    BarFormat supports the following options, passed in to the constructor:

    Option

    Example

    Description
    base A number that is the base value to compare the cell value against. If the cell value is higher, it will be drawn to the right of the base; if lower, it will be drawn to the left. Default value is 0.
    colorNegative A string indicating the negative value section of bars. Possible values are 'red', 'green' and 'blue'; default value is 'red'.
    colorPositive A string indicating the color of the positive value section of bars. Possible values are 'red', 'green' and 'blue'. Default is 'blue'.
    drawZeroLine A boolean indicating if to draw a 1 pixel dark base line when negative values are present. The dark line is there to enhance visual scanning of the bars. Default value is 'false'.
    max The maximum number value for the bar range. Default value is the highest value in the table.
    min The minimum number value for the bar range. Default value is the lowest value in the table.
    showValueIf true, shows values and bars; if false, shows only bars. Default value is true.
    width Thickness of each bar, in pixels. Default value is 100.

    Sample code

    vardata=newgoogle.visualization.DataTable();data.addColumn('string','Department');data.addColumn('number','Revenues');data.addRows([['Shoes',10700],['Sports',-15400],['Toys',12500],['Electronics',-2100],['Food',22600],['Art',1100]]);vartable=newgoogle.visualization.Table(document.getElementById('barformat_div'));varformatter=newgoogle.visualization.BarFormat({width:120});formatter.format(data,1);//Applyformattertosecondcolumntable.draw(data,{allowHtml:true,showRowNumber:true,width:'100%',height:'100%'});

    ColorFormat

    Assigns colors to the foreground or background of a numeric cell, depending on the cell value. This formatter is an unusual, in that it doesn't take its options in the constructor. Instead, you should calladdRange() oraddGradientRange() as many times as you want, to add color ranges, before callingformat(). Colors can be specified in any acceptable HTML format, for example "black", "#000000", or "#000".

    Methods

    ColorFormat supports the following methods:

    MethodDescription
    google.visualization.ColorFormat()Constructor. Takes no arguments.
    addRange(from,to,color,bgcolor)

    Specifies a foreground color and/or background color to a cell, depending on the cell value. Any cell with a value in the specifiedfromto range will be assignedcolor andbgcolor. It is important to realize that the range is non-inclusive, because creating a range from 1—1,000 and a second from 1,000— 2,000 will not cover the value 1,000!

    • from - [String, Number, Date, DateTime, or TimeOfDay] The lower boundary (inclusive) of the range, or null. If null, it will match -∞. String boundaries are compared alphabetically against string values.
    • to - [String, Number, Date, DateTime, or TimeOfDay] The high boundary (non-inclusive) of the range, or null. If null, it will match +∞. String boundaries are compared alphabetically against string values.
    • color - The color to apply to text in matching cells. Values can be either '#RRGGBB' values or defined color constants, (example: '#FF0000' or 'red').
    • bgcolor - The color to apply to the background of matching cells. Values can be either '#RRGGBB' values or defined color constants, (example: '#FF0000' or 'red').
    addGradientRange(from,to,color,fromBgColor,toBgColor)

    Assigns a background color from a range, according to the cell value. The color is scaled to match the cell's value within a range from a lower boundary color to an upper boundary color. Note that this method cannot compare string values, asaddRange() can.Tip: Color ranges are often hard for viewers to gauge accurately; the simplest and easiest to read range is from a fully saturated color to white (e.g., #FF0000—FFFFFF).

    • from - [Number, Date, DateTime, or TimeOfDay] The lower boundary (inclusive) of the range, or null. If null, it will match -∞.
    • to - [Number, Date, DateTime, or TimeOfDay] The higher boundary (non-inclusive) of the range, or null. If null, it will match +∞.
    • color - The color to apply to text in matching cells. This color is the same for all cells, no matter what their value.
    • fromBgColor - The background color for cells holding values at the low end of the gradient. Values can be either '#RRGGBB' values or defined color constants, (example: '#FF0000' or 'red').
    • toBgColor - The background color for cells holding values at the high end of the gradient. Values can be either '#RRGGBB' values or defined color constants, (example: '#FF0000' or 'red').

     

    format(dataTable,columnIndex)The standardformat() method to apply formatting to the specified column.

    Sample code

    vardata=newgoogle.visualization.DataTable();data.addColumn('string','Department');data.addColumn('number','Revenues');data.addRows([['Shoes',10700],['Sports',-15400],['Toys',12500],['Electronics',-2100],['Food',22600],['Art',1100]]);vartable=newgoogle.visualization.Table(document.getElementById('colorformat_div'));varformatter=newgoogle.visualization.ColorFormat();formatter.addRange(-20000,0,'white','orange');formatter.addRange(20000,null,'red','#33ff33');formatter.format(data,1);//Applyformattertosecondcolumntable.draw(data,{allowHtml:true,showRowNumber:true,width:'100%',height:'100%'});

    DateFormat

    Formats a JavaScriptDate value in a variety of ways, including "January 1, 2016," "1/1/16" and "Jan 1, 2016".

    Options

    DateFormat supports the following options, passed in to the constructor:

    OptionDescription
    formatType

    A quick formatting option for the date. The following string values are supported, reformatting the date February 28, 2016 as shown:

    • 'short' - Short format: e.g., "2/28/16"
    • 'medium' - Medium format: e.g., "Feb 28, 2016"
    • 'long' - Long format: e.g., "February 28, 2016"

    You cannot specify bothformatType andpattern.

    pattern

    A custom format pattern to apply to the value, similar to theICU date and time format. For example:var formatter3 = new google.visualization.DateFormat({pattern: "EEE, MMM d, ''yy"});

    You cannot specify bothformatType andpattern. You can read more details about patterns in the next section.

    timeZone The time zone in which to display the date value. This is a numeric value, indicating GMT + this number of time zones (can be negative). Date object are created by default with the assumed time zone of the computer on which they are created; this option is used to display that value in a different time zone. For example, if you created a Date object of 5pm noon on a computer located in Greenwich, England, and specified timeZone to be -5 (options['timeZone'] = -5, or Eastern Pacific Time in the US), the value displayed would be 12 noon.

    Methods

    DateFormat supports the following methods:

    MethodDescription
    google.visualization.DateFormat(options)

    Constructor. See the options section above for more info.

    format(dataTable,columnIndex)The standardformat() method to apply formatting to the specified column.
    formatValue(value)

    Returns the formatted value of a given value. This method does not require aDataTable.

    Sample code

    functiondrawDateFormatTable(){vardata=newgoogle.visualization.DataTable();data.addColumn('string','Employee Name');data.addColumn('date','Start Date (Long)');data.addColumn('date','Start Date (Medium)');data.addColumn('date','Start Date (Short)');data.addRows([['Mike',newDate(2008,1,28,0,31,26),newDate(2008,1,28,0,31,26),newDate(2008,1,28,0,31,26)],['Bob',newDate(2007,5,1,0),newDate(2007,5,1,0),newDate(2007,5,1,0)],['Alice',newDate(2006,7,16),newDate(2006,7,16),newDate(2006,7,16)]]);//Createthreeformattersinthreestyles.varformatter_long=newgoogle.visualization.DateFormat({formatType:'long'});varformatter_medium=newgoogle.visualization.DateFormat({formatType:'medium'});varformatter_short=newgoogle.visualization.DateFormat({formatType:'short'});//Reformatourdata.formatter_long.format(data,1);formatter_medium.format(data,2);formatter_short.format(data,3);//Drawourdatavartable=newgoogle.visualization.Table(document.getElementById('dateformat_div'));table.draw(data,{showRowNumber:true,width:'100%',height:'100%'});}

    More About Date Patterns

    Here are some more details on what patterns are supported:

    The patterns are similar to theICU date and time format, but the following patterns are not yet supported: A e D F g Y u w W. To avoid collision with patterns, any literal text you want to appear in the output should be surrounded by single quotes, except for the single quote, which should be doubled: e.g.,"K 'o''clock.'".

    PatternDescriptionExample Output
    GG Era designator."AD"
    yy or yyyy year. 1996
    M

    Month in year. For January:

    • M produces 1
    • MM produces 01
    • MMM produces Jan
    • MMMM produces January

    "July"

    "07"

    d Day in month. Extra 'd' values will add leading zeros.10
    h Hour in 12 hour scale. Extra 'h' values will add leading zeros.12
    H Hour in 24 hour scale. Extra Hk' values will add leading zeros.0
    m Minute in hour. Extra 'M' values will add leading zeros.30
    s Second in minute. Extra 's' values will add leading zeros.55
    SFractional second. Extra 'S' values will be padded on the right with zeros.978
    E

    Day of week. Following outputs for "Tuesday":

    • E produces T
    • EE or EEE Produce Tu or Tues
    • EEEE Produces Tuesday

    "Tues"

    "Tuesday"

    aa AM/PM"PM"
    kHour in day (1~24). Extra 'k' values will add leading zeros.24
    KHour in AM/PM (0~11). Extra 'k' values will add leading zeros.0
    z

    Time zone. For time zone 5, produces "UTC+5"

    "UTC+5"
    Z

    Time zone in RFC 822 format. For time zone -5:

    Z, ZZ, ZZZ produce -0500

    ZZZZ and more produce "GMT -05:00"

    "-0800"

    "GMT -05:00"

    v

    Time zone (generic).

    "Etc/GMT-5"
    'escape for text'Date='
    ''single quote''yy

    NumberFormat

    Describes how numeric columns should be formatted. Formatting options include specifying a prefix symbol (such as a dollar sign) or the punctuation to use as a thousands marker.

    Options

    NumberFormat supports the following options, passed in to the constructor:

    OptionDescription
    decimalSymbol

    A character to use as the decimal marker. The default is a dot (.).

    fractionDigits

    A number specifying how many digits to display after the decimal. The default is 2. If you specify more digits than the number contains, it will display zeros for the smaller values. Truncated values will be rounded (5 rounded up).

    groupingSymbol A character to be used to group digits to the left of the decimal into sets of three. Default is a comma (,).
    negativeColor The text color for negative values. No default value. Values can be any acceptable HTML color value, such as "red" or "#FF0000".
    negativeParens A boolean, where true indicates that negative values should be surrounded by parentheses. Default is true.
    pattern

    A format string. When provided, all other options are ignored, exceptnegativeColor.

    The format string is a subset of the ICU pattern set. For instance,{pattern:'#,###%'} will result in output values "1,000%", "750%", and "50%" for values 10, 7.5, and 0.5.

    prefixA string prefix for the value, for example "$".
    suffixA string suffix for the value, for example "%".

    Methods

    NumberFormat supports the following methods:

    MethodDescription
    google.visualization.NumberFormat(options)

    Constructor. See the options section above for more info.

    format(dataTable,columnIndex)The standardformat() method to apply formatting to the specified column.
    formatValue(value)

    Returns the formatted value of a given value. This method does not require aDataTable.

    Sample code

    vardata=newgoogle.visualization.DataTable();data.addColumn('string','Department');data.addColumn('number','Revenues');data.addRows([['Shoes',10700],['Sports',-15400],['Toys',12500],['Electronics',-2100],['Food',22600],['Art',1100]]);vartable=newgoogle.visualization.Table(document.getElementById('numberformat_div'));varformatter=newgoogle.visualization.NumberFormat({prefix:'$',negativeColor:'red',negativeParens:true});formatter.format(data,1);//Applyformattertosecondcolumntable.draw(data,{allowHtml:true,showRowNumber:true,width:'100%',height:'100%'});

    PatternFormat

    Enables you to merge the values of designated columns into a single column, along with arbitrary text. So, for example, if you had a column for first name and a column for last name, you could populate a third column with {last name}, {first name}. This formatter does not follow the conventions for the constructor and theformat() method. See the Methods section below for instructions.

    Methods

    PatternFormat supports the following methods:

    MethodDescription
    google.visualization.PatternFormat(pattern)

    Constructor. Does not take an options object. Instead, it takes a stringpattern parameter. This is a string that describes which column values to put into the destination column, along with any arbitrary text. Embed placeholders in your string to indicate a value from another column to embed. The placeholders are{#}, where # is a the index of a source column to use. The index is an index in thesrcColumnIndices array from theformat() method below. To include a literal { or } character, escape it like this: \{ or \}. To include a literal \ mark, escape it as \\.

    Sample code

    The following example demonstrates a constructor for a pattern that creates an anchor element, with the first and second elements taken from theformat() method:

    var formatter = new google.visualization.PatternFormat(  '<a href="mailto:{1}">{0}</a>');
    format(dataTable,srcColumnIndices,opt_dstColumnIndex)

    The standard formatting call, with a few additional parameters:

    • dataTable - The DataTable on which to operate.
    • srcColumnIndices - An array of one or more (zero-based) column indices to pull as the sources from the underlying DataTable. This will be used as a data source for thepattern parameter in the constructor. The column numbers donot have to be in sorted order.
    • opt_dstColumnIndex - [optional] The destination column to place the output of thepattern manipulation. If not specified, the first element insrcColumIndices will be used as the destination.

    See the formatting examples after the table.

    Here are a few example inputs and outputs for a four-column table.

    Row before formatting (4 columns, last is blank):John  |  Paul  |  Jones  |[empty]var formatter = new google.visualization.PatternFormat("{0} {1} {2}");formatter.format(data, [0,1,2], 3);Output:  John  |  Paul  |  Jones  |  John Paul Jonesvar formatter = new google.visualization.PatternFormat("{1}, {0}");formatter.format(data, [0,2], 3);Output:  John  |  Paul  |  Jones  |  Jones, John

    Sample code

    The following example demonstrates how to combine data from two columns to create an email address. It uses aDataView object to hide the original source columns:

    vardata=newgoogle.visualization.DataTable();data.addColumn('string','Name');data.addColumn('string','Email');data.addRows([['John Lennon','john@beatles.co.uk'],['Paul McCartney','paul@beatles.co.uk'],['George Harrison','george@beatles.co.uk'],['Ringo Starr','ringo@beatles.co.uk']]);vartable=newgoogle.visualization.Table(document.getElementById('patternformat_div'));varformatter=newgoogle.visualization.PatternFormat('<a href="mailto:{1}">{0}</a>');//Applyformatterandsettheformattedvalueofthefirstcolumn.formatter.format(data,[0,1]);varview=newgoogle.visualization.DataView(data);view.setColumns([0]);//Createaviewwiththefirstcolumnonly.table.draw(view,{allowHtml:true,showRowNumber:true,width:'100%',height:'100%'});

    GadgetHelper

    A helper class to simplify writingGadgets that use the Google Visualization API.

    Constructor

    google.visualization.GadgetHelper()

    Methods

    MethodReturn ValueDescription
    createQueryFromPrefs(prefs)google.visualization.Query Static. Create a new instance ofgoogle.visualization.Query and set its properties according to values from the gadget preferences. The type of parameterprefs is_IG_Prefs
    1. Preference_table_query_url is used to set the Query data source URL.
    2. Preference_table_query_refresh_interval is used to set the Query refresh interval (in seconds).
    validateResponse(response)Boolean Static. Parameterresponse is of typegoogle.visualization.QueryResponse. Returnstrue if the response contains data. Returnsfalse if the query execution failed and the response does not contain data. If an error occurred, this method displays an error message.

    Query Classes

    The following objects are available to send queries for data to an external data source, such as Google Spreadsheets.

    • Query - Wraps the outgoing data request.
    • QueryResponse - Handles the response from the data source.

    Query

    Represents a query that is sent to a data source.

    Constructor

    google.visualization.Query(dataSourceUrl,opt_options)

    Parameters

    dataSourceUrl
    [Required,String] URL to send the query to. See theCharts and Spreadsheets documentation for Google Spreadsheets.
    opt_options
    [Optional, Object] A map of options for the request.Note:If you are accessing a restricted data source, you should not use this parameter. Here are the supported properties:
    • sendMethod - [Optional, String] Specifies the method to use to send the query. Choose one of the following string values:
      • 'xhr' - Send the query usingXmlHttpRequest.
      • 'scriptInjection' - Send the query using script injection.
      • 'makeRequest' - [Available only for gadgets, which are deprecated] Send the query using the Gadget API  makeRequest() method. If specified, you should also specifymakeRequestParams.
      • 'auto' - Use the method specified by thetqrt URL parameter from the data source URL.tqrt can have the following values: 'xhr', 'scriptInjection', or 'makeRequest'. Iftqrt is missing or has an invalid value, the default is 'xhr' for same-domain requests and 'scriptInjection' for cross-domain requests.
    • makeRequestParams - [Object] A map of parameters for amakeRequest() query. Used and required only ifsendMethod is 'makeRequest'.

    Methods

    MethodReturn ValueDescription
    abort()None Stops the automated query sending that was started withsetRefreshInterval().
    setRefreshInterval(seconds)None

    Sets the query to automatically call thesend method every specified duration (number of seconds), starting from the first explicit call tosend.seconds is a number greater than or equal to zero.

    If you use this method, you should call it before calling thesend method.

    Cancel this method either by calling it again with zero (the default), or by callingabort().

    setTimeout(seconds)None Sets the number of seconds to wait for the data source to respond before raising a timeout error.seconds is a number greater than zero.
    The default timeout is 30 seconds. This method, if used, should be called before calling thesend method.
    setQuery(string)None Sets the query string. The value of thestring parameter should be a valid query.
    This method, if used, should be called before calling thesend method.Learn more about the Query language.
    send(callback)None Sends the query to the data source.callback should be a function that will be called when the data source responds. The callback function will receive a single parameter of typegoogle.visualization.QueryResponse.

    QueryResponse

    Represents a response of a query execution as received from the data source. An instance of this class is passed as an argument to the callback function that was set whenQuery.send was called.

    Methods

    MethodReturn ValueDescription
    getDataTable()DataTable Returns the data table as returned by the data source. Returnsnull if the query execution failed and no data was returned.
    getDetailedMessage()String Returns a detailed error message for queries that failed. If the query execution was successful, this method returns an empty string. The message returned is a message that is intended for developers, and may contain technical information, for example 'Column {salary} does not exist'.
    getMessage()String Returns a short error message for queries that failed. If the query execution was successful, this method returns an empty string. The message returned is a short message that is intended for end users, for example 'Invalid Query' or 'Access Denied'.
    getReasons()Array of strings Returns an array of zero of more entries. Each entry is a short string with an error or warning code that was raised while executing the query. Possible codes:
    • access_denied The user does not have permissions to access the data source.
    • invalid_query The specified query has a syntax error.
    • data_truncated One or more data rows that match the query selection were not returned due to output size limits. (warning).
    • timeout The query did not respond within the expected time.
    hasWarning()BooleanReturnstrue if the query execution has any warning messages.
    isError()Boolean Returnstrue if the query execution failed, and the response does not contain any data table. Returns <false> if the query execution was successful and the response contains a data table.

    Error Display

    The API provides several functions to help you display custom error messages to your users. To use these functions, provide a container element on the page (typically a<div>), into which the API will draw a formatted error message. This container can be either the visualization container element, or a container just for errors. If you specify the visualization containe element, the error message will be displayed above the visualization. Then call the appropriate function below to show, or remove, the error message.

    All functions are static functions in the namespacegoogle.visualization.errors.

    Many visualizations can throw an error event; see error event below to learn more about that.

    You can see an example custom error in theQuery Wrapper Example.

    FunctionReturn ValueDescription
    addError(container,message,opt_detailedMessage,opt_options) String ID value that identifies the error object created. This is a unique value on the page, and can be used to remove the error or find its containing element.

    Adds an error display block to the specified page element, with specified text and formatting.

    • container - The DOM element into which to insert the error message. If the container cannot be found, the function will throw a JavaScript error.
    • message - A string message to display.
    • opt_detailedMessage - An optional detailed message string. By default, this is mouseover text, but that can be changed in theopt_options.showInToolTip property described below.
    • opt_options - An optional object with properties that set various display options for the message. The following options are supported:
      • showInTooltip - A boolean value where true shows the detailed message only as tooltip text, and false shows the detailed message in the container body after the short message. Default value is true.
      • type - A string describing the error type, which determines which css styles should be applied to this message. The supported values are 'error' and 'warning'. Default value is 'error'.
      • style - A style string for the error message. This style will override any styles applied to the warning type (opt_options.type). Example:'background-color: #33ff99; padding: 2px;' Default value is an empty string.
      • removable - A boolean value, where true means that the message can be closed by a mouse click from the user. Default value is false.
    addErrorFromQueryResponse(container,response)

    String ID value that identifies the error object created, or null if the response didn't indicate an error. This is a unique value on the page, and can be used to remove the error or find its containing element.

    Pass a query response and error message container to this method: if the query response indicates a query error, displays an error message in the specified page element. If the query response is null, the method will throw a JavaScript error. Pass yourQueryResponse received in your query handler to this message to display an error. It will also set the style of the display appropriate to the type (error or warning, similar toaddError(opt_options.type))

    • container - The DOM element into which to insert the error message. If the container cannot be found, the function will throw a JavaScript error.
    • response - AQueryResponse object received by your query handler in response to aquery. If this is null, the method will throw a JavaScript error.
    removeError(id)Boolean: true if the error was removed; false otherwise.

    Removes the error specified by ID from the page.

    • id - The string ID of an error created usingaddError() oraddErrorFromQueryResponse().
    removeAll(container)None

    Removes all error blocks from a specified container. If the specified container does not exist, this will throw an error.

    • container - The DOM element holding the error strings to remove. If the container cannot be found, the function will throw a JavaScript error.
    getContainer(errorId)Handle to a DOM element holding the error specified, or null if it could not be found.

    Retrieves a handle to the container element holding the error specified byerrorID.

    • errorId - String ID of an error created usingaddError() oraddErrorFromQueryResponse().

    Events

    Most visualizations fire events to indicate something has occurred. As a user of the chart, you would often want to listen to these events. If youcode your own visualization, you might also want to trigger such events on your own.

    The following methods enable developers to listen to events, remove existing event handlers or trigger events from inside a visualization.

    addListener()

    Call this method to register to receive events fired by a visualization hosted on your page. You should document what event arguments, if any, will be passed to the handling function.

    google.visualization.events.addListener(source_visualization,event_name,handling_function)
    source_visualization
    A handle to the source visualization instance.
    event_name
    The string name of the event to listen for. A visualization should document which events it throws.
    handling_function
    The name of the local JavaScript function to call when source_visualization fires the event_name event. The handling function will be passed any event arguments as parameters.

    Returns

    A listener handler for the new listener. The handler can be used to later remove this listener if needed by callinggoogle.visualization.events.removeListener().

    Example

    Here is an example of registering to receive the selection event

    var table = new google.visualization.Table(document.getElementById('table_div'));table.draw(data, options);google.visualization.events.addListener(table, 'select', selectHandler);function selectHandler() {  alert('A table row was selected');}

    addOneTimeListener()

    This is identical toaddListener(), but is intended for events that should only be listened to once. Subsequent throws of the event will not invoke the handling function.

    An example of when this is useful: every draw causes aready event to be thrown. If you want only the firstready to execute your code, you'll wantaddOneTimeListener rather thanaddListener.

    removeListener()

    Call this method to unregister an existing event listener.

    google.visualization.events.removeListener(listener_handler)
    listener_handler
    The listener handler to remove, as returned bygoogle.visualization.events.addListener().

    removeAllListeners()

    Call this method to unregister all event listeners of a specific visualization instance.

    google.visualization.events.removeAllListeners(source_visualization)
    source_visualization
    A handle to the source visualization instance from which all event listeners should be removed.

    trigger()

    Called by visualizationimplementers. Call this method from your visualization to fire an event with an arbitrary name and set of values.

    google.visualization.events.trigger(source_visualization,event_name,event_args)
    source_visualization
    A handle to the source visualization instance. If you are calling this function from within a method defined by the sending visualization, you can simply pass in thethis keyword.
    event_name
    A string name to call the event. You can choose any string value that you want.
    event_args
    [optional] A map of name/value pairs to pass to the receiving method. For example: {message: "Hello there!", score: 10, name: "Fred"}. You can pass null if no events are needed; the receiver should be prepared to accept null for this parameter.

    Example

    Here is an example of a visualization that throws a method named "select" when its onclick method is called. It does not pass back any values.

    MyVisualization.prototype.onclick = function(rowIndex) {  this.highlightRow(this.selectedRow, false); // Clear previous selection  this.highlightRow(rowIndex, true); // Highlight new selection  // Save the selected row index in case getSelection is called.  this.selectedRow = rowIndex;  // Trigger a select event.  google.visualization.events.trigger(this, 'select', null);}

    Standard Visualization Methods and Properties

    Every visualizationshould expose the following set of required and optional methods and properties. However, note that there is no type checking to enforce these standards, so you should read the documentation for each visualization.

    Note:These methods are in the namespace of the visualization,not the google.visualization namespace.

    Constructor

    The constructor should have the name of your visualization class, and return an instance of that class.

    visualization_class_name(dom_element)
    dom_element
    A pointer to a DOM element where the visualization should be embedded.

    Example

    var org = new google.visualization.OrgChart(document.getElementById('org_div'));

    draw()

    Draws the visualization on the page. Behind the scenes this can be fetching a graphic from a server or creating the graphic on the page using the linked visualization code. You should call this method every time the data or options change. The object should be drawn inside the DOM element passed into the constructor.

    draw(data[, options])
    data
    ADataTable orDataView holding the data to use to draw the chart. There is no standard method for extracting aDataTable from a chart.
    options
    [Optional] A map of name/value pairs of custom options. Examples include height and width, background colors, and captions. The visualization documentation should list which options are available, and should support default options if you do not specify this parameter. You can use the JavaScript object literal syntax to pass in an options map: e.g.,{x:100, y:200, title:'An Example'}

    Example

    chart.draw(myData, {width: 400, height: 240, is3D: true, title: 'My Daily Activities'});

    getAction()

    This is optionally exposed by visualizations that havetooltips and allowtooltip actions.

    Returns the tooltip action object with the requestedactionID.

    Example:

    // Returns the action object with the ID 'alertAction'.chart.getAction('alertAction');

    getSelection()

    This is optionally exposed by visualizations that want to let you access the currently selected data in the graphic.

    selection_array getSelection()

    Returns

    selection_array   An array of selected objects, each one describing a data element in the underlying table used to create the visualization (aDataView or aDataTable). Each object has propertiesrow and/orcolumn, with the index of the row and/or column of the selected item in the underlyingDataTable. If therow property is null, then the selection is a column; if thecolumn property is null, then the selection is a row; if both are non-null, then it is a specific data item. You can call theDataTable.getValue() method to get the value of the selected item. The retrieved array can be passed intosetSelection().

    Example

    function myClickHandler(){  var selection = myVis.getSelection();  var message = '';  for (var i = 0; i < selection.length; i++) {    var item = selection[i];    if (item.row != null && item.column != null) {      message += '{row:' + item.row + ',column:' + item.column + '}';    } else if (item.row != null) {      message += '{row:' + item.row + '}';    } else if (item.column != null) {      message += '{column:' + item.column + '}';    }  }  if (message == '') {    message = 'nothing';  }  alert('You selected ' + message);}

    removeAction()

    This is optionally exposed by visualizations that havetooltips and allowtooltip actions.

    Removes the tooltip action object with the requestedactionID from your chart.

    Example:

    // Removes an action from chart with the ID of 'alertAction'.chart.removeAction('alertAction');

    setAction()

    This is optionally exposed by visualizations that havetooltips and allowtooltip actions. It works only for core charts (bar, column, line, area, scatter, combo, bubble, pie, donut, candlestick, histogram, stepped area).

    Sets a tooltip action to be executed when the user clicks on the action text.

    setAction(action object)

    ThesetAction method takes an object as its action parameter. This object should specify 3 properties:id— the ID of the action being set,text —the text that should appear in the tooltip for the action, andaction — the function that should be run when a user clicks on the action text.

    Any and all tooltip actions should be set prior to calling the chart'sdraw() method.

    Example:

    // Sets a tooltip action which will pop an alert box on the screen when triggered.chart.setAction({  id: 'alertAction',  text: 'Trigger Alert',  action: function() {    alert('You have triggered an alert');  }});

    ThesetAction method can also define two additional properties:visible andenabled. These properties should be functions that returnboolean values indicating if the tooltip action will be visible and/or enabled.

    Example:

    // The visible/enabled functions can contain any logic to determine their state// as long as they return boolean values.chart.setAction({  id: 'alertAction',  text: 'Trigger Alert',  action: function() {    alert('You have triggered an alert');  },  visible: function() {    return true;  },  enabled: function() {    return true;  }});

    setSelection()

    Optionally selects a data entry in the visualization—for example, a point in an area chart, or a bar in a bar chart. When this method is called, the visualization should visually indicate what the new selection is. The implementation ofsetSelection()should not fire a "select" event. Visualizations may ignore part of the selection. For example, a table that can show only selected rows may ignore cell or column elements in itssetSelection() implementation, or it can select the entire row.

    Every time this method is called, all selected items are deselected, and the new selection list passed in should be applied. There is no explicit way to deselect individual items; to deselect individual items, callsetSelection() with the items toremain selected; to deselect all elements, callsetSelection(),setSelection(null), orsetSelection([]).

    setSelection(selection_array)
    selection_array
    An array of objects, each with a numeric row and/or column property.row andcolumn are the zero-based row or column number of an item in the data table to select. To select a whole column, setrow to null; to select a whole row, setcolumn to null.Example:setSelection([{row:0,column:1},{row:1, column:null}]) selects the cell at (0,1) and the entire row 1.

    Assorted Static Methods

    This section contains various useful methods exposed in thegoogle.visualization namespace.

    arrayToDataTable()

    This method takes in a two-dimensional array and converts it to a DataTable.

    The column data types are determined automatically by the data provided. Column data types can also be specified using the object literal notation in the first row (the column header row) of the array (i.e.{label: 'Start Date', type: 'date'}).Optional data roles may be used as well, but they must be defined explicitly using object literal notation. Object literal notation may also be used for any cell, allowing you to defineCell Objects).

    Syntax

    google.visualization.arrayToDataTable(twoDArray,opt_firstRowIsData)
    twoDArray
    A two-dimensional array, where each row represents a row in the data table. Ifopt_firstRowIsData is false (the default), the first row will be interpreted as header labels. The data types of each column are interpreted automatically from the data given. If a cell has no value, specify a null or empty value as appropriate.
    opt_firstRowIsData
    Whether the first row defines a header row or not. If true, all rows are assumed to be data. If false, the first row is assumed to be a header row, and the values are assigned as column labels.Default is false.

    Returns

    A newDataTable.

    Examples

    The following code demonstrates three ways to create the sameDataTable object:

    // Version 1: arrayToDataTable methodvardata2=google.visualization.arrayToDataTable([[{label:'Country',type:'string'},{label:'Population',type:'number'},{label:'Area',type:'number'},{type:'string',role:'annotation'}],['CN',1324,9640821,'Annotated'],['IN',1133,3287263,'Annotated'],['US',304,9629091,'Annotated'],['ID',232,1904569,'Annotated'],['BR',187,8514877,'Annotated']]);// Version 2: DataTable.addRowsvardata3=newgoogle.visualization.DataTable();data3.addColumn('string','Country');data3.addColumn('number','Population');data3.addColumn('number','Area');data3.addRows([['CN',1324,9640821],['IN',1133,3287263],['US',304,9629091],['ID',232,1904569],['BR',187,8514877]]);// Version 3: DataTable.setValuevardata=newgoogle.visualization.DataTable();data.addColumn('string','Country');data.addColumn('number','Population');data.addColumn('number','Area');data.addRows(5);data.setValue(0,0,'CN');data.setValue(0,1,1324);data.setValue(0,2,9640821);data.setValue(1,0,'IN');data.setValue(1,1,1133);data.setValue(1,2,3287263);data.setValue(2,0,'US');data.setValue(2,1,304);data.setValue(2,2,9629091);data.setValue(3,0,'ID');data.setValue(3,1,232);data.setValue(3,2,1904569);data.setValue(4,0,'BR');data.setValue(4,1,187);data.setValue(4,2,8514877);

    drawChart()

    This method creates a chart in a single call. The advantage of using this method is that it requires slightly less code, and you can serialize and save visualizations as text strings for reuse. This method does not return a handle to the created chart, so you cannot assign method listeners to catch chart events.

    Syntax

    google.visualization.drawChart(chart_JSON_or_object)
    chart_JSON_or_object
    Either aJSON literal string or a JavaScript object, with the following properties (case-sensitive):
    PropertyTypeRequiredDefaultDescription
    chartTypeStringRequirednone The class name of the visualization. Thegoogle.visualization package name can be omitted for Google charts. If the appropriate visualization library has not already been loaded, this method will load the library for you if this is a Google visualization; you must load third party visualizations explicitly.Examples:Table,PieChart,example.com.CrazyChart.
    containerIdStringRequirednoneThe ID of the DOM element on your page that will host the visualization.
    optionsObjectOptionalnone An object describing the options for the visualization. You can use either JavaScript literal notation, or provide a handle to the object.Example:"options": {"width": 400, "height": 240, "is3D": true, "title": "Company Performance"}
    dataTableObjectOptionalNone ADataTable used to populate the visualization. This can be a literal JSON string representation of a DataTable, as describedabove, or a handle to a populatedgoogle.visualization.DataTable object, or a 2-dimensional array like that accepted by arrayToDataTable(opt_firstRowIsData=false). You must specify either this property or thedataSourceUrl property.
    dataSourceUrlStringOptionalNone A data source query to populate the chart data (for example, aGoogle Spreadsheet). You must specify either this property or thedataTable property.
    queryStringOptionalNone If specifyingdataSourceUrl, you can optionally specify a SQL-like query string using theVisualization query language to filter or manipulate the data.
    refreshIntervalNumberOptionalNone How often, in seconds, the visualization should refresh its query source. Use this only when specifyingdataSourceUrl.
    viewObject OR ArrayOptionalNone Sets aDataView initializer object, which acts as a filter over the underlying data, as defined by either thedataTable ordataSourceUrl parameter. You can pass in either a string orDataView initializer object, like that returned bydataview.toJSON().Example:"view": {"columns": [1, 2]} You can also pass in an array ofDataView initializer objects, in which case the firstDataView in the array is applied to the underlying data to create a new data table, and the secondDataView is applied to the data table resulting from application of the firstDataView, and so on.

    Examples

    Creates a table chart based on a spreadsheet data source, and includes the query SELECT A,D WHERE D > 100 ORDER BY D

    <scripttype="text/javascript">google.charts.load('current');// Note: No need to specify chart packages.functiondrawVisualization(){google.visualization.drawChart({"containerId":"visualization_div","dataSourceUrl":"https://spreadsheets.google.com/a/google.com/tq?key=pCQbetd-CptGXxxQIG7VFIQ&pub=1","query":"SELECT A,D WHERE D > 100 ORDER BY D","refreshInterval":5,"chartType":"Table","options":{"alternatingRowStyle":true,"showRowNumber":true}});}google.charts.setOnLoadCallback(drawVisualization);</script>

    This next example creates the same table, but creates aDataTable locally:

    <scripttype='text/javascript'>google.charts.load('current');functiondrawVisualization(){vardataTable=[["Country","Population Density"],["Indonesia",117],["China",137],["Nigeria",142],["Pakistan",198],["India",336],["Japan",339],["Bangladesh",1045]];google.visualization.drawChart({"containerId":"visualization_div","dataTable":dataTable,"refreshInterval":5,"chartType":"Table","options":{"alternatingRowStyle":true,"showRowNumber":true,}});}google.charts.setOnLoadCallback(drawVisualization);</script>

    This example passes in a JSON string representation of the chart, which you might have loaded from a file:

    <scripttype="text/javascript">google.charts.load('current');varmyStoredString='{"containerId": "visualization_div",'+'"dataSourceUrl": "https://spreadsheets.google.com/a/google.com/tq?key=pCQbetd-CptGXxxQIG7VFIQ&pub=1",'+'"query":"SELECT A,D WHERE D > 100 ORDER BY D",'+'"refreshInterval": 5,'+'"chartType": "Table",'+'"options": {'+'   "alternatingRowStyle": true,'+'   "showRowNumber" : true'+'}'+'}';functiondrawVisualization(){google.visualization.drawChart(myStoredString);}google.charts.setOnLoadCallback(drawVisualization);</script>

    drawToolbar()

    This is the constructor for the toolbar element that can be attached to many visualizations. This toolbar enables the user to export the visualization data into different formats, or to provide an embeddable version of the visualization for use in different places. See thetoolbar page for more information and a code example.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-07-10 UTC.