Class EmbeddedPieChartBuilder
Stay organized with collections
Save and categorize content based on your preferences.
AI-generated Key Takeaways
-
EmbeddedPieChartBuilder is a builder specifically for pie charts.
-
You can add or remove ranges of data for the chart using
addRange()andremoveRange(). -
The builder includes methods to change the chart type to various other chart types like AreaChart, BarChart, ColumnChart, and more.
-
Several methods allow customization of the pie chart's appearance, including setting it to 3D, changing background color, and setting colors for slices.
-
The
build()method is used to finalize the chart based on the applied configurations before inserting or updating it on a sheet.
Builder for pie charts. For more details, see the Gviz documentation.
Methods
| Method | Return type | Brief description |
|---|---|---|
add | Embedded | Adds a range to the chart this builder modifies. |
as | Embedded | Sets the chart type to AreaChart and returns an Embedded. |
as | Embedded | Sets the chart type to BarChart and returns an Embedded. |
as | Embedded | Sets the chart type to ColumnChart and returns an Embedded. |
as | Embedded | Sets the chart type to ComboChart and returns an Embedded. |
as | Embedded | Sets the chart type to HistogramChart and returns an Embedded. |
as | Embedded | Sets the chart type to LineChart and returns an Embedded. |
as | Embedded | Sets the chart type to PieChart and returns an Embedded. |
as | Embedded | Sets the chart type to ScatterChart and returns an Embedded. |
as | Embedded | Sets the chart type to TableChart and returns an Embedded. |
build() | Embedded | Builds the chart to reflect all changes made to it. |
clear | Embedded | Removes all ranges from the chart this builder modifies. |
get | Chart | Returns the current chart type. |
get | Container | Return the chart Container, which encapsulates where the chart appears on the
sheet. |
get | Range[] | Returns a copy of the list of ranges currently providing data for this chart. |
remove | Embedded | Removes the specified range from the chart this builder modifies. |
reverse | Embedded | Reverses the drawing of series in the domain axis. |
set3D() | Embedded | Sets the chart to be three-dimensional. |
set | Embedded | Sets the background color for the chart. |
set | Embedded | Changes the type of chart. |
set | Embedded | Sets the colors for the lines in the chart. |
set | Embedded | Sets the strategy to use for hidden rows and columns. |
set | Embedded | Sets the position of the legend with respect to the chart. |
set | Embedded | Sets the text style of the chart legend. |
set | Embedded | Sets the merge strategy to use when more than one range exists. |
set | Embedded | Sets the number of rows or columns of the range that should be treated as headers. |
set | Embedded | Sets advanced options for this chart. |
set | Embedded | Sets the position, changing where the chart appears on the sheet. |
set | Embedded | Sets the title of the chart. |
set | Embedded | Sets the text style of the chart title. |
set | Embedded | Sets whether the chart's rows and columns are transposed. |
Detailed documentation
addRange(range)
Adds a range to the chart this builder modifies. Does not add the range if it has already been added to the chart.
constss=SpreadsheetApp.getActiveSpreadsheet(); constsheet=ss.getSheets()[0]; constchart=sheet.newChart() .setChartType(Charts.ChartType.BAR) .addRange(sheet.getRange('A1:B8')) .setPosition(5,5,0,0) .build(); sheet.insertChart(chart);
Parameters
| Name | Type | Description |
|---|---|---|
range | Range | The range to add. |
Return
Embedded — This builder, for chaining.
asAreaChart()
Sets the chart type to AreaChart and returns an Embedded.
Return
Embedded — A builder for an area chart.
asBarChart()
Sets the chart type to BarChart and returns an Embedded.
Return
Embedded — A builder for a bar chart.
asColumnChart()
Sets the chart type to ColumnChart and returns an Embedded.
Return
Embedded — A builder for a column chart.
asComboChart()
Sets the chart type to ComboChart and returns an Embedded.
Return
Embedded — A builder for a combo chart.
asHistogramChart()
Sets the chart type to HistogramChart and returns an Embedded.
Return
Embedded — A builder for a histogram chart.
asLineChart()
Sets the chart type to LineChart and returns an Embedded.
Return
Embedded — A builder for a line chart.
asPieChart()
Sets the chart type to PieChart and returns an Embedded.
Return
Embedded — A builder for a pie chart.
asScatterChart()
Sets the chart type to ScatterChart and returns an Embedded.
Return
Embedded — A builder for a scatter chart.
asTableChart()
Sets the chart type to TableChart and returns an Embedded.
Return
Embedded — A builder for a table chart.
build()
Builds the chart to reflect all changes made to it.
This method does not automatically draw the chart on top of the spreadsheet. A new chart
must be inserted via sheet.insertChart(chart), and an existing chart should be updated
via sheet.updateChart(chart).
constss=SpreadsheetApp.getActiveSpreadsheet(); constsheet=ss.getSheets()[0]; constrange=sheet.getRange('A1:B5'); constchart=sheet.newChart() .setChartType(Charts.ChartType.BAR) .addRange(range) .setPosition(5,5,0,0) .build(); sheet.insertChart(chart);
Return
Embedded — The created chart, which must still be added to the spreadsheet.
clearRanges()
Removes all ranges from the chart this builder modifies.
constss=SpreadsheetApp.getActiveSpreadsheet(); constsheet=ss.getSheets()[0]; // This code updates the chart to use only the new ranges while preserving the // existing formatting of the chart. constchart=sheet.getCharts()[0]; constnewChart=chart.modify() .clearRanges() .addRange(sheet.getRange('A1:A5')) .addRange(sheet.getRange('B1:B5')) .build(); sheet.updateChart(newChart);
Return
Embedded — This builder, for chaining.
getChartType()
getContainer()
Return the chart Container, which encapsulates where the chart appears on the
sheet.
constss=SpreadsheetApp.getActiveSpreadsheet(); constsheet=ss.getSheets()[0]; constchartBuilder=sheet.newChart() .setChartType(Charts.ChartType.BAR) .addRange(sheet.getRange('A1:B8')) .setPosition(5,5,0,0); // This method returns the exact same data as Chart#getContainerInfo() constcontainerInfo=chartBuilder.getContainer(); // Logs the values used in setPosition() Logger.log( 'Anchor Column: %s\r\nAnchor Row %s\r\nOffset X %s\r\nOffset Y %s', containerInfo.getAnchorColumn(), containerInfo.getAnchorRow(), containerInfo.getOffsetX(), containerInfo.getOffsetY(), );
Return
Container — An object containing the chart container's position.
getRanges()
Returns a copy of the list of ranges currently providing data for this chart. Use add and remove to modify this list.
constss=SpreadsheetApp.getActiveSpreadsheet(); constsheet=ss.getSheets()[0]; constchartBuilder=sheet.newChart() .setChartType(Charts.ChartType.BAR) .addRange(sheet.getRange('A1:B8')) .setPosition(5,5,0,0); constranges=chartBuilder.getRanges(); // There's only one range as a data source for this chart, // so this logs "A1:B8" for(constiinranges){ constrange=ranges[i]; Logger.log(range.getA1Notation()); }
Return
Range[] — An array of ranges that serve as the chart to be built's data source.
removeRange(range)
Removes the specified range from the chart this builder modifies. Does not throw an error if the range is not in this chart.
The range removed must match up with a range added via add;
otherwise no change is made to the chart. This method cannot be used to partially remove values
from a range.
constss=SpreadsheetApp.getActiveSpreadsheet(); constsheet=ss.getSheets()[0]; constfirstRange=sheet.getRange('A1:B5'); constsecondRange=sheet.getRange('A6:B8'); constchartBuilder=sheet.newChart() .setChartType(Charts.ChartType.BAR) .addRange(firstRange) // This range renders in a different color .addRange(secondRange) .setPosition(5,5,0,0); // Note that you can use either of these two formats, but the range // MUST match up with a range that was added via addRange(), or it // is not removed, and does not throw an exception chartBuilder.removeRange(firstRange); chartBuilder.removeRange(sheet.getRange('A6:B8')); constchart=chartBuilder.build(); sheet.insertChart(chart);
Parameters
| Name | Type | Description |
|---|---|---|
range | Range | The range to remove. |
Return
Embedded — This builder, for chaining.
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. 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. constbuilder=Charts.newPieChart(); builder.reverseCategories();
Return
Embedded — This builder, useful for chaining.
set3D()
Sets the chart to be three-dimensional.
Return
Embedded — 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 gray constbuilder=Charts.newLineChart(); builder.setBackgroundColor('gray');
Parameters
| Name | Type | Description |
|---|---|---|
css | String | The CSS value for the color (such as "blue" or "#00f"). |
Return
Embedded — This builder, useful for chaining.
setChartType(type)
Changes the type of chart. Not all embedded chart types are currently supported. See Chart.
constss=SpreadsheetApp.getActiveSpreadsheet(); constsheet=ss.getSheets()[0]; constrange=sheet.getRange('A1:B5'); constchart=sheet.newChart() .setChartType(Charts.ChartType.BAR) .addRange(range) .setPosition(5,5,0,0) .build(); sheet.insertChart(chart);
Parameters
| Name | Type | Description |
|---|---|---|
type | Chart | The type to change this chart into. |
Return
Embedded — This builder, 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
| 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
Embedded — This builder, useful for chaining.
setHiddenDimensionStrategy(strategy)
Sets the strategy to use for hidden rows and columns. Defaults to IGNORE_ROWS .
constss=SpreadsheetApp.getActiveSpreadsheet(); constsheet=ss.getSheets()[0]; constrange=sheet.getRange('A1:B5'); constchart=sheet.newChart() .setChartType(Charts.ChartType.BAR) .addRange(range) .setHiddenDimensionStrategy( Charts.ChartHiddenDimensionStrategy.IGNORE_COLUMNS, ) .setPosition(5,5,0,0) .build(); sheet.insertChart(chart);
Parameters
| Name | Type | Description |
|---|---|---|
strategy | Chart | The strategy to use for hidden rows and columns. |
Return
Embedded — This builder, 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
| Name | Type | Description |
|---|---|---|
position | Position | The position of the legend. |
Return
Embedded — 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
| Name | Type | Description |
|---|---|---|
text | Text | The text style to use for the chart legend. |
Return
Embedded — This builder, useful for chaining.
setMergeStrategy(mergeStrategy)
Sets the merge strategy to use when more than one range exists. If MERGE_ROWS , rows are merged; if MERGE_COLUMNS , columns are merged. Defaults to MERGE_COLUMNS .
constss=SpreadsheetApp.getActiveSpreadsheet(); constsheet=ss.getSheets()[0]; constrange=sheet.getRange('A1:B10'); constrange2=sheet.getRange('C:C10'); constchart=sheet.newChart() .setChartType(Charts.ChartType.BAR) .addRange(range) .addRange(range2) .setMergeStrategy(Charts.ChartMergeStrategy.MERGE_ROWS) .setPosition(5,5,0,0) .build(); sheet.insertChart(chart);
Parameters
| Name | Type | Description |
|---|---|---|
merge | Chart | The merge strategy to use. |
Return
Embedded — This builder, for chaining.
setNumHeaders(headers)
Sets the number of rows or columns of the range that should be treated as headers.
constss=SpreadsheetApp.getActiveSpreadsheet(); constsheet=ss.getSheets()[0]; constrange=sheet.getRange('A1:B5'); constchart=sheet.newChart() .setChartType(Charts.ChartType.BAR) .addRange(range) .setNumHeaders(1) .setPosition(5,5,0,0) .build(); sheet.insertChart(chart);
Parameters
| Name | Type | Description |
|---|---|---|
headers | Integer | The number of rows or columns to treat as headers. Negative values cause headers to be auto-detected. |
Return
Embedded — This builder, for chaining.
setOption(option, value)
Sets advanced options for this chart. To view a list of the available options, see Chart configuration options.
This method doesn't validate the option you specify is valid for this chart type nor if the value is of the correct format/structure.
This example shows how to change the title and set a legend.
constspreadsheet=SpreadsheetApp.getActiveSpreadsheet(); constsheet=spreadsheet.getSheets()[0]; constchart=sheet.newChart() .setOption('title','Earnings projections') .setOption('legend',{ position:'top', textStyle:{color:'blue',fontSize:16}, }).build();
Parameters
| Name | Type | Description |
|---|---|---|
option | String | The name of the option. |
value | Object | The value of the option. |
Return
Embedded — This builder, for chaining.
setPosition(anchorRowPos, anchorColPos, offsetX, offsetY)
Sets the position, changing where the chart appears on the sheet. anchor and
anchor are 1-indexed.
constss=SpreadsheetApp.getActiveSpreadsheet(); constsheet=ss.getSheets()[0]; constrange=sheet.getRange('A1:B5'); constchart=sheet.newChart() .setChartType(Charts.ChartType.BAR) .addRange(range) .setPosition(5,5,0,0) .build(); sheet.insertChart(chart);
Parameters
| Name | Type | Description |
|---|---|---|
anchor | Integer | The chart's top side is anchored in this row. |
anchor | Integer | The chart's left side is anchored in this column. |
offsetX | Integer | The chart's upper right-hand corner is offset by this many pixels. |
offsetY | Integer | The chart's lower left-hand corner is offset by this many pixels. |
Return
Embedded — This builder, 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
| Name | Type | Description |
|---|---|---|
chart | String | the chart title. |
Return
Embedded — 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
| 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
Embedded — This builder, useful for chaining.
setTransposeRowsAndColumns(transpose)
Sets whether the chart's rows and columns are transposed. If set to true, the rows and
columns are switched. Defaults to false.
constss=SpreadsheetApp.getActiveSpreadsheet(); constsheet=ss.getSheets()[0]; constrange=sheet.getRange('A1:B5'); constchart=sheet.newChart() .setChartType(Charts.ChartType.BAR) .addRange(range) .setTransposeRowsAndColumns(true) .setPosition(5,5,0,0) .build(); sheet.insertChart(chart);
Parameters
| Name | Type | Description |
|---|---|---|
transpose | Boolean | If true, the rows and columns used to construct the chart are
transposed. |
Return
Embedded — This builder, for chaining.