条形图的构建器。如需了解详情,请参阅 Google 图表文档。
以下示例展示了如何构建条形图。数据从 Google 电子表格导入。
// Get sample data from a spreadsheet. const dataSourceUrl = 'https://docs.google.com/spreadsheet/tq?range=B1%3AC11' + '&key=0Aq4s9w_HxMs7dHpfX05JdmVSb1FpT21sbXd4NVE3UEE&gid=0&headers=-1'; const chartBuilder = Charts.newBarChart() .setTitle('Top Grossing Films in US and Canada') .setXAxisTitle('USD') .setYAxisTitle('Film') .setDimensions(600, 500) .setLegendPosition(Charts.Position.BOTTOM) .setDataSourceUrl(dataSourceUrl); const chart = chartBuilder.build();
方法
详细文档
build()
reverseCategories()
反转网域轴中序列的绘制顺序。对于垂直范围图表(例如折线图、面积图或柱状图),这意味着水平轴是从右向左绘制的。对于水平范围图表(例如条形图),这意味着垂直轴是从上到下绘制的。对于饼图,这意味着扇形是按逆时针方向绘制的。
// Creates a pie chart builder and sets drawing of the slices in a // counter-clockwise manner. const builder = Charts.newPieChart(); builder.reverseCategories();
返回
BarChartBuilder - 此构建器,适用于链式调用。
reverseDirection()
setBackgroundColor(cssValue)
设置图表的背景颜色。
// Creates a line chart builder and sets the background color to gray const builder = Charts.newLineChart(); builder.setBackgroundColor('gray');
参数
| 名称 | 类型 | 说明 |
|---|---|---|
css | String | 颜色的 CSS 值(例如 "blue" 或 "#00f")。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setColors(cssValues)
设置图表中线条的颜色。
// 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']);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
css | String[] | 颜色 CSS 值数组,例如 ["red", "#acf"]。数组中的第 n 个元素表示图表中第 n 条线的颜色。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setDataSourceUrl(url)
设置用于从外部来源(例如 Google 表格)提取数据的数据源网址。如果同时提供了数据源网址和 DataTable,系统会忽略数据源网址。
如需详细了解如何查询数据源,请参阅 Google 图表文档。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
url | String | 数据源网址,包括所有查询参数。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setDataTable(tableBuilder)
使用 DataTableBuilder 设置图表要使用的数据表。这是一种用于设置数据表的便捷方法,无需调用 build()。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
table | Data | 数据表构建器。在此调用过程中,系统会立即创建一个新的数据表,因此对构建器的任何进一步更新都不会反映在图表中。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setDataTable(table)
设置包含图表线条以及 X 轴标签的数据表。第一列应为字符串,并包含横轴标签。后面可以跟任意数量的列,但所有列都必须是数值列。每个列都显示为单独的一行。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
table | Data | 用于图表的数据表。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setDataViewDefinition(dataViewDefinition)
设置要用于图表的数据视图定义。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
data | Data | 一个数据视图定义对象,用于定义应从给定数据源派生的视图,以用于绘制图表。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setDimensions(width, height)
设置图表的尺寸。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
width | Integer | 图表的宽度(以像素为单位)。 |
height | Integer | 图表的高度,以像素为单位。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setLegendPosition(position)
设置图例相对于图表的位置。默认情况下,没有图例。
// Creates a line chart builder and sets the legend position to right. const builder = Charts.newLineChart(); builder.setLegendPosition(Charts.Position.RIGHT);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
position | Position | 图例的位置。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setLegendTextStyle(textStyle)
设置图表图例的文本样式。
// 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);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
text | Text | 图表图例所用的文字样式。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setOption(option, value)
为此图表设置高级选项。请参阅此图表的可用选项。如果给定的选项无效,则此方法不会产生任何影响。
// Build a bar chart with a 1-second animation duration. const builder = Charts.newBarChart(); builder.setOption('animation.duration', 1000); const chart = builder.build();
参数
| 名称 | 类型 | 说明 |
|---|---|---|
option | String | 要设置的选项。 |
value | Object | 要设置的值。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setRange(start, end)
设置图表的范围。
如果有任何数据点超出该范围,系统会扩大范围以纳入这些数据点。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
start | Number | 范围轴上最低网格线的值。 |
end | Number | 范围轴上最高网格线的值。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setStacked()
setTitle(chartTitle)
设置图表的标题。标题显示在图表上方的中心位置。
// Creates a line chart builder and title to 'My Line Chart'. const builder = Charts.newLineChart(); builder.setTitle('My Line Chart');
参数
| 名称 | 类型 | 说明 |
|---|---|---|
chart | String | 图表标题。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setTitleTextStyle(textStyle)
设置图表标题的文字样式。
// 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);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
text | Text | 图表标题所用的文字样式。您可以通过调用 Charts.newTextStyle() 来创建 Text 对象。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setXAxisTextStyle(textStyle)
设置横轴文本样式。
// Creates a line chart builder and sets the X-axis text style to blue, 18-point // font. const textStyle = Charts.newTextStyle().setColor('blue').setFontSize(18).build(); const builder = Charts.newLineChart(); builder.setXAxisTextStyle(textStyle);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
text | Text | 用于横轴标题的文字样式。您可以通过调用 Charts.newTextStyle() 来创建 Text 对象。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setXAxisTitle(title)
为横轴添加标题。标题居中显示,并显示在轴值标签下方。
// Creates a line chart builder and sets the X-axis title. const builder = Charts.newLineChart(); builder.setTitle('X-axis Title');
参数
| 名称 | 类型 | 说明 |
|---|---|---|
title | String | X 轴的标题。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setXAxisTitleTextStyle(textStyle)
设置横轴标题文本样式。
// Creates a line chart builder and sets the X-axis title text style to blue, // 18-point font. const textStyle = Charts.newTextStyle().setColor('blue').setFontSize(18).build(); const builder = Charts.newLineChart(); builder.setXAxisTitleTextStyle(textStyle);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
text | Text | 用于横轴标题的文字样式。您可以通过调用 Charts.newTextStyle() 来创建 Text 对象。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setYAxisTextStyle(textStyle)
设置纵轴文本样式。
// Creates a line chart builder and sets the Y-axis text style to blue, 18-point // font. const textStyle = Charts.newTextStyle().setColor('blue').setFontSize(18).build(); const builder = Charts.newLineChart(); builder.setYAxisTextStyle(textStyle);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
text | Text | 用于横轴标题的文字样式。您可以通过调用 Charts.newTextStyle() 来创建 Text 对象。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setYAxisTitle(title)
为纵轴添加标题。标题居中显示,并位于值标签的左侧。
// Creates a line chart builder and sets the Y-axis title. const builder = Charts.newLineChart(); builder.setYAxisTitle('Y-axis Title');
参数
| 名称 | 类型 | 说明 |
|---|---|---|
title | String | Y 轴的标题。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
setYAxisTitleTextStyle(textStyle)
设置纵轴标题文本样式。
// Creates a line chart builder and sets the Y-axis title text style to blue, // 18-point font. const textStyle = Charts.newTextStyle().setColor('blue').setFontSize(18).build(); const builder = Charts.newLineChart(); builder.setYAxisTitleTextStyle(textStyle);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
text | Text | 用于横轴标题的文字样式。您可以通过调用 Charts.newTextStyle() 来创建 Text 对象。 |
返回
BarChartBuilder - 此构建器,适用于链式调用。
useLogScale()
将范围轴设置为对数刻度(要求所有值均为正数)。范围轴是垂直图表(例如折线图、面积图或柱状图)的纵轴,也是水平图表(例如条形图)的横轴。
返回
BarChartBuilder - 此构建器,适用于链式调用。