A builder for pie charts. For more details, see the Google Charts documentation .
Here is an example that shows how to build a pie chart. The data is imported from a Google spreadsheet .
// Get sample data from a spreadsheet. const dataSourceUrl = 'https://docs.google.com/spreadsheet/tq?range=A1%3AB8' + '&key=0Aq4s9w_HxMs7dHpfX05JdmVSb1FpT21sbXd4NVE3UEE&gid=3&headers=-1' ; const chartBuilder = Charts . newPieChart () . setTitle ( 'World Population by Continent' ) . setDimensions ( 600 , 500 ) . set3D () . setDataSourceUrl ( dataSourceUrl ); const chart = chartBuilder . build ();
Methods
Method | Return type | Brief description |
---|---|---|
Chart
|
Builds the chart. | |
Pie
|
Reverses the drawing of series in the domain axis. | |
Pie
|
Sets the chart to be three-dimensional. | |
Pie
|
Sets the background color for the chart. | |
Pie
|
Sets the colors for the lines in the chart. | |
Pie
|
Sets the data source URL that is used to pull data in from an external source, such as Google Sheets. | |
Pie
|
Sets the data table to use for the chart using a DataTableBuilder. | |
Pie
|
Sets the data table which contains the lines for the chart, as well as the X-axis labels. | |
Pie
|
Sets the data view definition to use for the chart. | |
Pie
|
Sets the dimensions for the chart. | |
Pie
|
Sets the position of the legend with respect to the chart. | |
Pie
|
Sets the text style of the chart legend. | |
Pie
|
Sets advanced options for this chart. | |
Pie
|
Sets the title of the chart. | |
Pie
|
Sets the text style of the chart title. |
Detailed documentation
build()
Builds the chart.
Return
Chart
— A Chart object, which can be embedded into documents, UI elements, or used as a static
image.
reverse
Categories()
Reverses the drawing of series in the domain axis. For vertical-range charts (such as line, area or column charts), this means the horizontal axis is drawn from right to left. For horizontal-range charts (such as bar charts), this means the vertical axis is drawn from top to bottom. For pie charts, this means the slices are drawn counterclockwise.
// Creates a pie chart builder and sets drawing of the slices in a // counter-clockwise manner. const builder = Charts . newPieChart (); builder . reverseCategories ();
Return
Pie
— This builder, useful for chaining.
set3D()
Sets the chart to be three-dimensional.
Return
Pie
— This builder, useful for chaining.
set
Background
Color(cssValue)
Sets the background color for the chart.
// Creates a line chart builder and sets the background color to gray const builder = Charts . newLineChart (); builder . setBackgroundColor ( 'gray' );
Parameters
Name | Type | Description |
---|---|---|
css
|
String
|
The CSS value for the color (such as "blue"
or "#00f"
). |
Return
Pie
— This builder, useful for chaining.
set
Colors(cssValues)
Sets the colors for the lines in the chart.
// Creates a line chart builder and sets the first two lines to be drawn in // green and red, respectively. const builder = Charts . newLineChart (); builder . setColors ([ 'green' , 'red' ]);
Parameters
Name | Type | Description |
---|---|---|
css
|
String[]
|
An array of color CSS values, such as ["red", "#acf"]
. The nth element
in the array represents the color of the nth line in the chart. |
Return
Pie
— This builder, useful for chaining.
set
Data
Source
Url(url)
Sets the data source URL that is used to pull data in from an external source, such as Google Sheets. If a data source URL and a DataTable are provided, the data source URL is ignored.
For more information about querying data sources, check out the Google Charts documentation .
Parameters
Name | Type | Description |
---|---|---|
url
|
String
|
The data source URL, including any query parameters. |
Return
Pie
— This builder, useful for chaining.
set
Data
Table(tableBuilder)
Sets the data table to use for the chart using a DataTableBuilder. This is a convenience method
for setting the data table without needing to call build()
.
Parameters
Name | Type | Description |
---|---|---|
table
|
Data
|
A data table builder. A new data table is created instantly as part of this call, so any further updates to the builder won't be reflected in the chart. |
Return
Pie
— This builder, useful for chaining.
set
Data
Table(table)
Sets the data table which contains the lines for the chart, as well as the X-axis labels. The first column should be a string, and contain the horizontal axis labels. Any number of columns can follow, all must be numeric. Each column is displayed as a separate line.
Parameters
Name | Type | Description |
---|---|---|
table
|
Data
|
The data table to use for the chart. |
Return
Pie
— This builder, useful for chaining.
set
Data
View
Definition(dataViewDefinition)
Sets the data view definition to use for the chart.
Parameters
Name | Type | Description |
---|---|---|
data
|
Data
|
A data view definition object that defines the view that should be derived from the given data source for the chart drawing. |
Return
Pie
— This builder, useful for chaining.
set
Dimensions(width, height)
Sets the dimensions for the chart.
Parameters
Name | Type | Description |
---|---|---|
width
|
Integer
|
The width of the chart, in pixels. |
height
|
Integer
|
The height of the chart, in pixels. |
Return
Pie
— This builder, useful for chaining.
set
Legend
Position(position)
Sets the position of the legend with respect to the chart. By default, there is no legend.
// Creates a line chart builder and sets the legend position to right. const builder = Charts . newLineChart (); builder . setLegendPosition ( Charts . Position . RIGHT );
Parameters
Name | Type | Description |
---|---|---|
position
|
Position
|
The position of the legend. |
Return
Pie
— This builder, useful for chaining.
set
Legend
Text
Style(textStyle)
Sets the text style of the chart legend.
// Creates a line chart builder and sets it up for a blue, 26-point legend. const textStyleBuilder = Charts . newTextStyle (). setColor ( '#0000FF' ). setFontSize ( 26 ); const style = textStyleBuilder . build (); const builder = Charts . newLineChart (); builder . setLegendTextStyle ( style );
Parameters
Name | Type | Description |
---|---|---|
text
|
Text
|
The text style to use for the chart legend. |
Return
Pie
— This builder, useful for chaining.
set
Option(option, value)
Sets advanced options for this chart. See the available options for this chart . This method has no effect if the given option is invalid.
// Build a pie chart with a pretty legend. const builder = Charts . newPieChart (); builder . setOption ( 'legend' , { textStyle : { color : 'blue' , fontSize : 16 }}); const chart = builder . build ();
Parameters
Name | Type | Description |
---|---|---|
option
|
String
|
The option to set. |
value
|
Object
|
The value to set. |
Return
Pie
— This builder, useful for chaining.
set
Title(chartTitle)
Sets the title of the chart. The title is displayed centered above the chart.
// Creates a line chart builder and title to 'My Line Chart'. const builder = Charts . newLineChart (); builder . setTitle ( 'My Line Chart' );
Parameters
Name | Type | Description |
---|---|---|
chart
|
String
|
the chart title. |
Return
Pie
— This builder, useful for chaining.
set
Title
Text
Style(textStyle)
Sets the text style of the chart title.
// Creates a line chart builder and sets it up for a blue, 26-point title. const textStyleBuilder = Charts . newTextStyle (). setColor ( '#0000FF' ). setFontSize ( 26 ); const style = textStyleBuilder . build (); const builder = Charts . newLineChart (); builder . setTitleTextStyle ( style );
Parameters
Name | Type | Description |
---|---|---|
text
|
Text
|
The text style to use for the chart title. You can create a Text
object by calling Charts.newTextStyle()
. |
Return
Pie
— This builder, useful for chaining.