Class EmbeddedLineChartBuilder

EmbeddedLineChartBuilder

Builder for line charts. For more details, see the Gviz documentation.

Methods

MethodReturn typeBrief description
addRange(range)EmbeddedChartBuilderAdds a Range to the chart represented by this builder.
asAreaChart()EmbeddedAreaChartBuilderSets the chart type to AreaChart and returns an EmbeddedAreaChartBuilder.
asBarChart()EmbeddedBarChartBuilderSets the chart type to BarChart and returns an EmbeddedBarChartBuilder.
asColumnChart()EmbeddedColumnChartBuilderSets the chart type to ColumnChart and returns an EmbeddedColumnChartBuilder.
asComboChart()EmbeddedComboChartBuilderSets the chart type to ComboChart and returns an EmbeddedComboChartBuilder.
asHistogramChart()EmbeddedHistogramChartBuilderSets the chart type to HistogramChart and returns an EmbeddedHistogramChartBuilder.
asLineChart()EmbeddedLineChartBuilderSets the chart type to LineChart and returns an EmbeddedLineChartBuilder.
asPieChart()EmbeddedPieChartBuilderSets the chart type to PieChart and returns an EmbeddedPieChartBuilder.
asScatterChart()EmbeddedScatterChartBuilderSets the chart type to ScatterChart and returns an EmbeddedScatterChartBuilder.
asTableChart()EmbeddedTableChartBuilderSets the chart type to TableChart and returns an EmbeddedTableChartBuilder.
build()EmbeddedChartBuilds the chart to reflect all changes made to it.
getChartType()ChartTypeReturns the current chart type.
getContainer()ContainerInfoReturn the ContainerInfo, which encapsulates where the chart appears on the sheet.
getRanges()Range[]Returns a copy of the list of ranges currently providing data for this chart.
removeRange(range)EmbeddedChartBuilderRemoves the specified Range from the chart represented by this builder.
reverseCategories()EmbeddedLineChartBuilderReverses the drawing of series in the domain axis.
setBackgroundColor(cssValue)EmbeddedLineChartBuilderSets the background color for the chart.
setChartType(type)EmbeddedChartBuilderChanges the type of chart.
setColors(cssValues)EmbeddedLineChartBuilderSets the colors for the lines in the chart.
setCurveStyle(style)EmbeddedLineChartBuilderSets the style to use for curves in the chart.
setLegendPosition(position)EmbeddedLineChartBuilderSets the position of the legend with respect to the chart.
setLegendTextStyle(textStyle)EmbeddedLineChartBuilderSets the text style of the chart legend.
setOption(option, value)EmbeddedChartBuilderSets advanced options for this chart.
setPointStyle(style)EmbeddedLineChartBuilderSets the style for points in the line.
setPosition(anchorRowPos, anchorColPos, offsetX, offsetY)EmbeddedChartBuilderSets the position, changing where the chart appears on the sheet.
setRange(start, end)EmbeddedLineChartBuilderSets the range for the chart.
setTitle(chartTitle)EmbeddedLineChartBuilderSets the title of the chart.
setTitleTextStyle(textStyle)EmbeddedLineChartBuilderSets the text style of the chart title.
setXAxisTextStyle(textStyle)EmbeddedLineChartBuilderSets the horizontal axis text style.
setXAxisTitle(title)EmbeddedLineChartBuilderAdds a title to the horizontal axis.
setXAxisTitleTextStyle(textStyle)EmbeddedLineChartBuilderSets the horizontal axis title text style.
setYAxisTextStyle(textStyle)EmbeddedLineChartBuilderSets the vertical axis text style.
setYAxisTitle(title)EmbeddedLineChartBuilderAdds a title to the vertical axis.
setYAxisTitleTextStyle(textStyle)EmbeddedLineChartBuilderSets the vertical axis title text style.
useLogScale()EmbeddedLineChartBuilderMakes the range axis into a logarithmic scale (requires all values to be positive).

Detailed documentation

addRange(range)

Adds a Range to the chart represented by this builder. Will not add the Range if it has already been added to the chart.


 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];

 var chart = sheet.newChart()
     .setChartType(Charts.ChartType.BAR)
     .addRange(sheet.getRange("A1:B8"))
     .setPosition(5, 5, 0, 0)
     .build();

 sheet.insertChart(chart);
 

Parameters

NameTypeDescription
rangeRangethe range to add

Return

EmbeddedChartBuilder — the builder for method chaining


asAreaChart()

Sets the chart type to AreaChart and returns an EmbeddedAreaChartBuilder.

Return

EmbeddedAreaChartBuilder — a builder for an area chart


asBarChart()

Sets the chart type to BarChart and returns an EmbeddedBarChartBuilder.

Return

EmbeddedBarChartBuilder — a builder for a bar chart


asColumnChart()

Sets the chart type to ColumnChart and returns an EmbeddedColumnChartBuilder.

Return

EmbeddedColumnChartBuilder — a builder for a column chart


asComboChart()

Sets the chart type to ComboChart and returns an EmbeddedComboChartBuilder.

Return

EmbeddedComboChartBuilder — a builder for a combo chart


asHistogramChart()

Sets the chart type to HistogramChart and returns an EmbeddedHistogramChartBuilder.

Return

EmbeddedHistogramChartBuilder — a builder for a histogram chart


asLineChart()

Sets the chart type to LineChart and returns an EmbeddedLineChartBuilder.

Return

EmbeddedLineChartBuilder — a builder for a line chart


asPieChart()

Sets the chart type to PieChart and returns an EmbeddedPieChartBuilder.

Return

EmbeddedPieChartBuilder — a builder for a pie chart


asScatterChart()

Sets the chart type to ScatterChart and returns an EmbeddedScatterChartBuilder.

Return

EmbeddedScatterChartBuilder — a builder for a scatter chart


asTableChart()

Sets the chart type to TableChart and returns an EmbeddedTableChartBuilder.

Return

EmbeddedTableChartBuilder — a builder for a table chart


build()

Builds the chart to reflect all changes made to it.

This method will 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);


 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];

 var range = sheet.getRange("A1:B5");
 var chart = sheet.newChart()
     .setChartType(Charts.ChartType.BAR)
     .addRange(range)
     .setPosition(5, 5, 0, 0)
     .build()

 sheet.insertChart(chart);
 

Return

EmbeddedChart — the created chart, which must still be added to the spreadsheet


getChartType()

Returns the current chart type.

Return

ChartType — the chart type


getContainer()

Return the ContainerInfo, which encapsulates where the chart appears on the sheet.


 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];

 var chartBuilder = 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()
 var containerInfo = chartBuilder.getContainer();

 // Logs the values we 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

ContainerInfo — an object containing the chart container's position


getRanges()

Returns a copy of the list of ranges currently providing data for this chart. Use addRange and removeRange to modify this list.


 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];

 var chartBuilder = sheet.newChart()
     .setChartType(Charts.ChartType.BAR)
     .addRange(sheet.getRange("A1:B8"))
     .setPosition(5, 5, 0, 0)

 var ranges = chartBuilder.getRanges();

 // There's only one range as a data source for this chart,
 // so this logs "A1:B8"
 for (var i in ranges) {
   var range = 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 represented by this builder. Will not throw an error if the Range is not in this chart.

The range removed must match up with a range added via addRange(range), or it will not be removed, and it will not throw an exception. This method cannot be used to partially remove values from a range.


 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];

 var firstRange = sheet.getRange("A1:B5");
 var secondRange = sheet.getRange("A6:B8");

 var chartBuilder = sheet.newChart()
     .setChartType(Charts.ChartType.BAR)
     .addRange(firstRange)
     // This range will render 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
 // will not be removed, and will not throw an exception
 chartBuilder.removeRange(firstRange);
 chartBuilder.removeRange(sheet.getRange("A6:B8"));

 var chart = chartBuilder.build();

 sheet.insertChart(chart);
 

Parameters

NameTypeDescription
rangeRangethe range to remove

Return

EmbeddedChartBuilder — the builder for method 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.
 var builder = Charts.newPieChart();
 builder.reverseCategories();
 

Return

EmbeddedLineChartBuilder — 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
 var builder = Charts.newLineChart();
 builder.setBackgroundColor("gray");
 

Parameters

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

Return

EmbeddedLineChartBuilder — this builder, useful for chaining


setChartType(type)

Changes the type of chart. Not all embedded chart types are currently supported. See ChartType.


 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];

 var range = sheet.getRange("A1:B5");
 var chart = sheet.newChart()
     .setChartType(Charts.ChartType.BAR)
     .addRange(range)
     .setPosition(5, 5, 0, 0)
     .build()

 sheet.insertChart(chart);
 

Parameters

NameTypeDescription
typeChartTypea chart type

Return

EmbeddedChartBuilder — the builder for method 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.
 var builder = 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

EmbeddedLineChartBuilder — this builder, useful for chaining


setCurveStyle(style)

Sets the style to use for curves in the chart. See CurveStyle for allowed curve styles.


 // Creates a line chart builder and curves the lines in the chart.
 var builder = Charts.newLineChart();
 builder.setCurveStyle(Charts.CurveStyle.SMOOTH)
 

Parameters

NameTypeDescription
styleCurveStylethe style for curves in the chart

Return

EmbeddedLineChartBuilder — this builder, useful for chaining

See also


setLegendPosition(position)

Sets the position of the legend with respect to the chart. By default, there will be no legend.


 // Creates a line chart builder and sets the legend position to right.
 var builder = Charts.newLineChart();
 builder.setLegendPosition(Charts.Position.RIGHT);
 

Parameters

NameTypeDescription
positionPositionthe position of the legend

Return

EmbeddedLineChartBuilder — 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.
 var textStyleBuilder = Charts.newTextStyle().setColor('#0000FF').setFontSize(26);
 var style = textStyleBuilder.build();
 var builder = Charts.newLineChart();
 builder.setLegendTextStyle(style);
 

Parameters

NameTypeDescription
textStyleTextStylethe text style to use for the chart legend.

Return

EmbeddedLineChartBuilder — this builder, useful for chaining


setOption(option, value)

Sets advanced options for this chart. Lookup the chart type in the Charts API Chart Gallery for a listing of the available options. For example, the options for an area chart are listed here.

This method will NOT 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 animation duration to 1 second and set a legend.


 builder.setOption('title', 'Earnings projections');
 builder.setOption('animation.duration', 1000);
 builder.setOption('legend', {position: 'top', textStyle: {color: 'blue', fontSize: 16}});
 

Parameters

NameTypeDescription
optionStringthe name of the option
valueObjectthe value of the option

Return

EmbeddedChartBuilder — the builder for method chaining


setPointStyle(style)

Sets the style for points in the line. By default, points will have no particular styles, and only the line will be visible.


 // Creates a line chart builder and sets large point style.
 var builder = Charts.newLineChart();
 builder.setPointStyle(Charts.PointStyle.LARGE);
 

Parameters

NameTypeDescription
stylePointStylethe style to use for points in the line

Return

EmbeddedLineChartBuilder — this builder, useful for chaining

See also


setPosition(anchorRowPos, anchorColPos, offsetX, offsetY)

Sets the position, changing where the chart appears on the sheet. AnchorRowPos and AnchorColPos are 1-indexed.


 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];

 var range = sheet.getRange("A1:B5");
 var chart = sheet.newChart()
     .setChartType(Charts.ChartType.BAR)
     .addRange(range)
     .setPosition(5, 5, 0, 0)
     .build()

 sheet.insertChart(chart);
 

Parameters

NameTypeDescription
anchorRowPosIntegerthe chart's top side will be anchored in this row
anchorColPosIntegerthe chart's left side will be anchored in this column
offsetXIntegerthe chart's upper right-hand corner will be offset by this many pixels
offsetYIntegerthe chart's lower left-hand corner will be offset by this many pixels

Return

EmbeddedChartBuilder — the builder for method chaining


setRange(start, end)

Sets the range for the chart.

If any data points fall outside the range, the range will be expanded to include those data points.

Parameters

NameTypeDescription
startNumbervalue for the lowest grid line of the range axis
endNumbervalue for the highest grid line of the range axis

Return

EmbeddedLineChartBuilder — this builder, useful for chaining


setTitle(chartTitle)

Sets the title of the chart. The title will be displayed centered above the chart.


 // Creates a line chart builder and title to 'My Line Chart'.
 var builder = Charts.newLineChart();
 builder.setTitle('My Line Chart')
 

Parameters

NameTypeDescription
chartTitleStringthe chart title.

Return

EmbeddedLineChartBuilder — 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.
 var textStyleBuilder = Charts.newTextStyle().setColor('#0000FF').setFontSize(26);
 var style = textStyleBuilder.build();
 var builder = Charts.newLineChart();
 builder.setTitleTextStyle(style);
 

Parameters

NameTypeDescription
textStyleTextStylethe text style to use for the chart title. TextStyleBuilder object can be created by calling Charts.newTextStyle().

Return

EmbeddedLineChartBuilder — this builder, useful for chaining


setXAxisTextStyle(textStyle)

Sets the horizontal axis text style.


 // Creates a line chart builder and sets the X-axis text style to blue, 18-point font.
 var textStyle = Charts.newTextStyle().setColor('blue').setFontSize(18).build();
 var builder = Charts.newLineChart();
 builder.setXAxisTextStyle(textStyle);
 

Parameters

NameTypeDescription
textStyleTextStylethe text style to use for the horizontal axis title. TextStyleBuilder object can be created by calling Charts.newTextStyle().

Return

EmbeddedLineChartBuilder — this builder, useful for chaining


setXAxisTitle(title)

Adds a title to the horizontal axis. The title will be centered and will appear below the axis value labels.


 // Creates a line chart builder and sets the X-axis title.
 var builder = Charts.newLineChart();
 builder.setTitle('X-axis Title')
 

Parameters

NameTypeDescription
titleStringthe title for the X-axis

Return

EmbeddedLineChartBuilder — this builder, useful for chaining


setXAxisTitleTextStyle(textStyle)

Sets the horizontal axis title text style.


 // Creates a line chart builder and sets the X-axis title text style to blue, 18-point font.
 var textStyle = Charts.newTextStyle().setColor('blue').setFontSize(18).build();
 var builder = Charts.newLineChart();
 builder.setXAxisTitleTextStyle(textStyle);
 

Parameters

NameTypeDescription
textStyleTextStylethe text style to use for the horizontal axis title. TextStyleBuilder object can be created by calling Charts.newTextStyle().

Return

EmbeddedLineChartBuilder — this builder, useful for chaining


setYAxisTextStyle(textStyle)

Sets the vertical axis text style.


 // Creates a line chart builder and sets the Y-axis text style to blue, 18-point font.
 var textStyle = Charts.newTextStyle().setColor('blue').setFontSize(18).build();
 var builder = Charts.newLineChart();
 builder.setYAxisTextStyle(textStyle);
 

Parameters

NameTypeDescription
textStyleTextStylethe text style to use for the horizontal axis title. TextStyleBuilder object can be created by calling Charts.newTextStyle().

Return

EmbeddedLineChartBuilder — this builder, useful for chaining


setYAxisTitle(title)

Adds a title to the vertical axis. The title will be centered and will appear to the left of the value labels.


 // Creates a line chart builder and sets the Y-axis title.
 var builder = Charts.newLineChart();
 builder.setYAxisTitle('Y-axis Title')
 

Parameters

NameTypeDescription
titleStringthe title for the Y-axis

Return

EmbeddedLineChartBuilder — this builder, useful for chaining


setYAxisTitleTextStyle(textStyle)

Sets the vertical axis title text style.


 // Creates a line chart builder and sets the Y-axis title text style to blue, 18-point font.
 var textStyle = Charts.newTextStyle().setColor('blue').setFontSize(18).build();
 var builder = Charts.newLineChart();
 builder.setYAxisTitleTextStyle(textStyle);
 

Parameters

NameTypeDescription
textStyleTextStylethe text style to use for the horizontal axis title. TextStyleBuilder object can be created by calling Charts.newTextStyle().

Return

EmbeddedLineChartBuilder — this builder, useful for chaining


useLogScale()

Makes the range axis into a logarithmic scale (requires all values to be positive). The range axis will be the vertical axis for vertical charts (line, area, column, etc.) and the horizontal axis for horizontal charts (bar, etc.)

Return

EmbeddedLineChartBuilder — this builder, useful for chaining

Send feedback about...

Apps Script
Apps Script