柱形图的构建器。如需了解详情,请参阅 Google 图表文档。
此示例展示了如何使用数据表中的数据创建柱形图。
var sampleData = Charts.newDataTable() .addColumn(Charts.ColumnType.STRING, "Year") .addColumn(Charts.ColumnType.NUMBER, "Sales") .addColumn(Charts.ColumnType.NUMBER, "Expenses") .addRow(["2004", 1000, 400]) .addRow(["2005", 1170, 460]) .addRow(["2006", 660, 1120]) .addRow(["2007", 1030, 540]) .addRow(["2008", 800, 600]) .addRow(["2009", 943, 678]) .addRow(["2010", 1020, 550]) .addRow(["2011", 910, 700]) .addRow(["2012", 1230, 840]) .build(); var chart = Charts.newColumnChart() .setTitle('Sales & Expenses') .setXAxisTitle('Year') .setYAxisTitle('Amount (USD)') .setDimensions(600, 500) .setDataTable(sampleData) .build();
方法
详细文档
build()
reverseCategories()
反转域轴上的序列绘制。对于垂直范围图表(例如折线图、面积图或柱形图),这意味着横轴从右向左绘制。对于水平范围图表(例如条形图),这意味着纵轴从上到下绘制。对于饼图,这意味着各切片按逆时针方向绘制。
// Creates a pie chart builder and sets drawing of the slices in a counter-clockwise manner. var builder = Charts.newPieChart(); builder.reverseCategories();
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setBackgroundColor(cssValue)
设置图表的背景颜色。
// Creates a line chart builder and sets the background color to gray var builder = Charts.newLineChart(); builder.setBackgroundColor("gray");
参数
名称 | 类型 | 说明 |
---|---|---|
cssValue | String | 颜色的 CSS 值(例如 "blue" 或 "#00f" )。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setColors(cssValues)
设置图表中线条的颜色。
// 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"]);
参数
名称 | 类型 | 说明 |
---|---|---|
cssValues | String[] | 颜色 CSS 值数组,例如 ["red", "#acf"] 。数组中的第 n 个元素表示图表中第 n 条线的颜色。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setDataSourceUrl(url)
设置数据源网址,用于从外部来源(如 Google 表格)拉取数据。如果提供了数据源网址和 DataTable,系统会忽略数据源网址。
如需详细了解如何查询数据源,请参阅 Google 图表文档。
参数
名称 | 类型 | 说明 |
---|---|---|
url | String | 数据源网址,包括任何查询参数。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setDataTable(tableBuilder)
使用 DataTableBuilder 设置要用于图表的数据表。这是一种无需调用 build()
即可设置数据表的便捷方法。
参数
名称 | 类型 | 说明 |
---|---|---|
tableBuilder | DataTableBuilder | 数据表构建器。在此调用中,系统会立即创建新的数据表,因此对构建器的任何后续更新都不会反映在图表中。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setDataTable(table)
设置数据表,其中包含图表的线条以及 X 轴标签。第一列应为字符串,并包含横轴标签。后面可跟随任意数量的列,且所有列都必须是数字。每一列都显示为单独的一行。
参数
名称 | 类型 | 说明 |
---|---|---|
table | DataTableSource | 要用于图表的数据表。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setDataViewDefinition(dataViewDefinition)
设置要用于图表的数据视图定义。
参数
名称 | 类型 | 说明 |
---|---|---|
dataViewDefinition | DataViewDefinition | 一个数据视图定义对象,用于定义应从图表绘图的给定数据源派生的视图。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setDimensions(width, height)
设置图表的尺寸。
参数
名称 | 类型 | 说明 |
---|---|---|
width | Integer | 图表的宽度(以像素为单位)。 |
height | Integer | 图表的高度(以像素为单位)。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setLegendPosition(position)
设置图例相对于图表的位置。默认情况下,不显示图例。
// Creates a line chart builder and sets the legend position to right. var builder = Charts.newLineChart(); builder.setLegendPosition(Charts.Position.RIGHT);
参数
名称 | 类型 | 说明 |
---|---|---|
position | Position | 图例的位置。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setLegendTextStyle(textStyle)
设置图表图例的文本样式。
// 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);
参数
名称 | 类型 | 说明 |
---|---|---|
textStyle | TextStyle | 用于图表图例的文本样式。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setOption(option, value)
设置此图表的高级选项。请参阅此图表的可用选项。如果指定的选项无效,此方法不会产生任何影响。
// Build a column chart with a 1-second animation duration. var builder = Charts.newColumnChart(); builder.setOption('animation.duration', 1000); var chart = builder.build();
参数
名称 | 类型 | 说明 |
---|---|---|
option | String | 要设置的选项。 |
value | Object | 要设置的值。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setRange(start, end)
设置图表的范围。
如果有任何数据点不在该范围内,则会扩展该范围以包含这些数据点。
参数
名称 | 类型 | 说明 |
---|---|---|
start | Number | 范围轴最低网格线的值。 |
end | Number | 范围轴最高网格线的值。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setStacked()
setTitle(chartTitle)
设置图表的标题。标题会显示在图表上方的中心位置。
// Creates a line chart builder and title to 'My Line Chart'. var builder = Charts.newLineChart(); builder.setTitle('My Line Chart')
参数
名称 | 类型 | 说明 |
---|---|---|
chartTitle | String | 图表标题。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setTitleTextStyle(textStyle)
设置图表标题的文本样式。
// 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);
参数
名称 | 类型 | 说明 |
---|---|---|
textStyle | TextStyle | 用于图表标题的文本样式。您可以通过调用 Charts.newTextStyle() 来创建 TextStyleBuilder 对象。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setXAxisTextStyle(textStyle)
设置横轴文本样式。
// 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);
参数
名称 | 类型 | 说明 |
---|---|---|
textStyle | TextStyle | 用于横轴标题的文本样式。您可以通过调用 Charts.newTextStyle() 来创建 TextStyleBuilder 对象。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setXAxisTitle(title)
向横轴添加标题。标题居中显示于轴值标签下方。
// Creates a line chart builder and sets the X-axis title. var builder = Charts.newLineChart(); builder.setTitle('X-axis Title')
参数
名称 | 类型 | 说明 |
---|---|---|
title | String | X 轴的标题。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setXAxisTitleTextStyle(textStyle)
设置横轴标题文本样式。
// 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);
参数
名称 | 类型 | 说明 |
---|---|---|
textStyle | TextStyle | 用于横轴标题的文本样式。您可以通过调用 Charts.newTextStyle() 来创建 TextStyleBuilder 对象。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setYAxisTextStyle(textStyle)
设置纵轴文本样式。
// 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);
参数
名称 | 类型 | 说明 |
---|---|---|
textStyle | TextStyle | 用于横轴标题的文本样式。您可以通过调用 Charts.newTextStyle() 来创建 TextStyleBuilder 对象。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setYAxisTitle(title)
为纵轴添加标题。标题居中显示于值标签的左侧。
// Creates a line chart builder and sets the Y-axis title. var builder = Charts.newLineChart(); builder.setYAxisTitle('Y-axis Title')
参数
名称 | 类型 | 说明 |
---|---|---|
title | String | Y 轴的标题。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
setYAxisTitleTextStyle(textStyle)
设置纵轴标题文本样式。
// 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);
参数
名称 | 类型 | 说明 |
---|---|---|
textStyle | TextStyle | 用于横轴标题的文本样式。您可以通过调用 Charts.newTextStyle() 来创建 TextStyleBuilder 对象。 |
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。
useLogScale()
将范围轴设为对数刻度(要求所有值均为正数)。对于垂直图表(例如折线图、面积或柱形图),范围轴是纵轴;对于水平图表(例如条形图),范围轴是水平轴。
弃踢回攻
ColumnChartBuilder
- 此构建器,可用于链接。