Class PieChartBuilder

  • PieChartBuilder is used to create pie charts and offers various methods to customize their appearance and data source.

  • You can set the title, dimensions, background color, and colors of the pie chart slices.

  • Data can be imported from external sources like Google Sheets usingsetDataSourceUrl.

  • The appearance of the legend and title can be customized using specific text style methods.

  • Thebuild() method is used to finalize the configuration and create theChart object.

PieChartBuilder

A builder for pie charts. For more details, see theGoogle Charts documentation.

Here is an example that shows how to build a pie chart. The data isimported from a Google spreadsheet.

// Get sample data from a spreadsheet.constdataSourceUrl='https://docs.google.com/spreadsheet/tq?range=A1%3AB8'+'&key=0Aq4s9w_HxMs7dHpfX05JdmVSb1FpT21sbXd4NVE3UEE&gid=3&headers=-1';constchartBuilder=Charts.newPieChart().setTitle('World Population by Continent').setDimensions(600,500).set3D().setDataSourceUrl(dataSourceUrl);constchart=chartBuilder.build();

Methods

MethodReturn typeBrief description
build()ChartBuilds the chart.
reverseCategories()PieChartBuilderReverses the drawing of series in the domain axis.
set3D()PieChartBuilderSets the chart to be three-dimensional.
setBackgroundColor(cssValue)PieChartBuilderSets the background color for the chart.
setColors(cssValues)PieChartBuilderSets the colors for the lines in the chart.
setDataSourceUrl(url)PieChartBuilderSets the data source URL that is used to pull data in from an external source, such as GoogleSheets.
setDataTable(tableBuilder)PieChartBuilderSets the data table to use for the chart using a DataTableBuilder.
setDataTable(table)PieChartBuilderSets the data table which contains the lines for the chart, as well as the X-axis labels.
setDataViewDefinition(dataViewDefinition)PieChartBuilderSets the data view definition to use for the chart.
setDimensions(width, height)PieChartBuilderSets the dimensions for the chart.
setLegendPosition(position)PieChartBuilderSets the position of the legend with respect to the chart.
setLegendTextStyle(textStyle)PieChartBuilderSets the text style of the chart legend.
setOption(option, value)PieChartBuilderSets advanced options for this chart.
setTitle(chartTitle)PieChartBuilderSets the title of the chart.
setTitleTextStyle(textStyle)PieChartBuilderSets 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.


reverseCategories()

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. Forhorizontal-range charts (such as bar charts), this means the vertical axis is drawn from top tobottom. 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.constbuilder=Charts.newPieChart();builder.reverseCategories();

Return

PieChartBuilder — This builder, useful for chaining.


set3D()

Sets the chart to be three-dimensional.

Return

PieChartBuilder — This builder, useful for chaining.


setBackgroundColor(cssValue)

Sets the background color for the chart.

// Creates a line chart builder and sets the background color to grayconstbuilder=Charts.newLineChart();builder.setBackgroundColor('gray');

Parameters

NameTypeDescription
cssValueStringThe CSS value for the color (such as"blue" or"#00f").

Return

PieChartBuilder — This builder, useful for chaining.


setColors(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.constbuilder=Charts.newLineChart();builder.setColors(['green','red']);

Parameters

NameTypeDescription
cssValuesString[]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

PieChartBuilder — This builder, useful for chaining.


setDataSourceUrl(url)

Sets the data source URL that is used to pull data in from an external source, such as GoogleSheets. 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 theGoogle Charts documentation.

Parameters

NameTypeDescription
urlStringThe data source URL, including any query parameters.

Return

PieChartBuilder — This builder, useful for chaining.


setDataTable(tableBuilder)

Sets the data table to use for the chart using a DataTableBuilder. This is a convenience methodfor setting the data table without needing to callbuild().

Parameters

NameTypeDescription
tableBuilderDataTableBuilderA 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

PieChartBuilder — This builder, useful for chaining.


setDataTable(table)

Sets the data table which contains the lines for the chart, as well as the X-axis labels. Thefirst column should be a string, and contain the horizontal axis labels. Any number of columnscan follow, all must be numeric. Each column is displayed as a separate line.

Parameters

NameTypeDescription
tableDataTableSourceThe data table to use for the chart.

Return

PieChartBuilder — This builder, useful for chaining.


setDataViewDefinition(dataViewDefinition)

Sets the data view definition to use for the chart.

Parameters

NameTypeDescription
dataViewDefinitionDataViewDefinitionA data view definition object that defines the view that should be derived from the given data source for the chart drawing.

Return

PieChartBuilder — This builder, useful for chaining.


setDimensions(width, height)

Sets the dimensions for the chart.

Parameters

NameTypeDescription
widthIntegerThe width of the chart, in pixels.
heightIntegerThe height of the chart, in pixels.

Return

PieChartBuilder — This builder, useful for chaining.


setLegendPosition(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.constbuilder=Charts.newLineChart();builder.setLegendPosition(Charts.Position.RIGHT);

Parameters

NameTypeDescription
positionPositionThe position of the legend.

Return

PieChartBuilder — This builder, useful for chaining.


setLegendTextStyle(textStyle)

Sets the text style of the chart legend.

// Creates a line chart builder and sets it up for a  blue, 26-point legend.consttextStyleBuilder=Charts.newTextStyle().setColor('#0000FF').setFontSize(26);conststyle=textStyleBuilder.build();constbuilder=Charts.newLineChart();builder.setLegendTextStyle(style);

Parameters

NameTypeDescription
textStyleTextStyleThe text style to use for the chart legend.

Return

PieChartBuilder — This builder, useful for chaining.


setOption(option, value)

Sets advanced options for this chart. Seethe available options forthis chart. This method has no effect if the given option is invalid.

// Build a pie chart with a pretty legend.constbuilder=Charts.newPieChart();builder.setOption('legend',{textStyle:{color:'blue',fontSize:16}});constchart=builder.build();

Parameters

NameTypeDescription
optionStringThe option to set.
valueObjectThe value to set.

Return

PieChartBuilder — This builder, useful for chaining.


setTitle(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'.constbuilder=Charts.newLineChart();builder.setTitle('My Line Chart');

Parameters

NameTypeDescription
chartTitleStringthe chart title.

Return

PieChartBuilder — This builder, useful for chaining.


setTitleTextStyle(textStyle)

Sets the text style of the chart title.

// Creates a line chart builder and sets it up for a  blue, 26-point title.consttextStyleBuilder=Charts.newTextStyle().setColor('#0000FF').setFontSize(26);conststyle=textStyleBuilder.build();constbuilder=Charts.newLineChart();builder.setTitleTextStyle(style);

Parameters

NameTypeDescription
textStyleTextStyleThe text style to use for the chart title. You can create aTextStyleBuilder object by callingCharts.newTextStyle().

Return

PieChartBuilder — This builder, useful for chaining.

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 2025-12-11 UTC.