如何为 Google 表格图表设置水平(或垂直)轴数字格式?
Posted
技术标签:
【中文标题】如何为 Google 表格图表设置水平(或垂直)轴数字格式?【英文标题】:How to set the horizontal (or vertical) axis number format for a Google Sheets chart? 【发布时间】:2021-09-29 00:22:10 【问题描述】:我正在生成一个电子表格、几个数据透视表和图表以可视化结果。我一切正常,除了我还没有找到一种方法来设置图表水平/垂直轴的数字格式。我希望轴显示整数,而无需更改支持数据的数字格式。
很遗憾,Google 提供的文档并未涵盖此功能:https://developers.google.com/apps-script/chart-configuration-options#line-chart-configuration-options。
可以set the number format manually,但是,由于我要生成大量图表,所以对所有图表都这样做太麻烦了。
以下是用于生成我的图表的 Apps 脚本代码示例:
embeddedChartBuilder.setChartType(Charts.ChartType.LINE)
.setPosition(chartRow, chartColumn, 0, 0)
.setNumHeaders(1)
.setOption('backgroundColor', '#222222')
.setOption('height', chartHeightPx)
.setOption('width', chartWidthPx)
.setOption('series', [
color:'#4ebcbb', pointSize:6, lineWidth:4,
color:'#cccccc', pointSize:6, lineWidth:4,
color:'#666666', pointSize:6, lineWidth:4,
color:'#34a853', pointSize:6, lineWidth:4])
.setOption('applyAggregateData', 0)
.setOption('vAxis.gridlines.color', '#434343')
.setOption('vAxis.minValue', 0)
.setOption('vAxis.maxValue', 5)
.setOption('vAxis.textStyle', color: '#efefef', fontName: 'Arial', fontSize: 12, bold: false, italic: false )
.setOption('hAxis.textStyle', color: '#efefef', fontName: 'Arial', fontSize: 12, bold: false, italic: false );
【问题讨论】:
【参考方案1】:在Configuration Options 下的Line Chart 页面上,hAxis.format
具有以下描述:
A format string for numeric or date axis labels.
For number axis labels, this is a subset of the decimal formatting ICU pattern set . For instance, format:'#,###%' will display values "1,000%", "750%", and "50%" for values 10, 7.5, and 0.5. You can also supply any of the following:
format: 'none': displays numbers with no formatting (e.g., 8000000)
format: 'decimal': displays numbers with thousands separators (e.g., 8,000,000)
format: 'scientific': displays numbers in scientific notation (e.g., 8e6)
format: 'currency': displays numbers in the local currency (e.g., $8,000,000.00)
format: 'percent': displays numbers as percentages (e.g., 800,000,000%)
format: 'short': displays abbreviated numbers (e.g., 8M)
format: 'long': displays numbers as full words (e.g., 8 million)
For date axis labels, this is a subset of the date formatting ICU pattern set . For instance, format:'MMM d, y' will display the value "Jul 1, 2011" for the date of July first in 2011.
The actual formatting applied to the label is derived from the locale the API has been loaded with. For more details, see loading charts with a specific locale .
In computing tick values and gridlines, several alternative combinations of all the relevant gridline options will be considered and alternatives will be rejected if the formatted tick labels would be duplicated or overlap. So you can specify format:"#" if you want to only show integer tick values, but be aware that if no alternative satisfies this condition, no gridlines or ticks will be shown.
This option is only supported for a continuous axis.
Type: string
Default: auto
还有一个vAxis.format
选项,格式如下:
A format string for numeric axis labels. This is a subset of the ICU pattern set . For instance, format:'#,###%' will display values "1,000%", "750%", and "50%" for values 10, 7.5, and 0.5. You can also supply any of the following:
format: 'none': displays numbers with no formatting (e.g., 8000000)
format: 'decimal': displays numbers with thousands separators (e.g., 8,000,000)
format: 'scientific': displays numbers in scientific notation (e.g., 8e6)
format: 'currency': displays numbers in the local currency (e.g., $8,000,000.00)
format: 'percent': displays numbers as percentages (e.g., 800,000,000%)
format: 'short': displays abbreviated numbers (e.g., 8M)
format: 'long': displays numbers as full words (e.g., 8 million)
The actual formatting applied to the label is derived from the locale the API has been loaded with. For more details, see loading charts with a specific locale .
In computing tick values and gridlines, several alternative combinations of all the relevant gridline options will be considered and alternatives will be rejected if the formatted tick labels would be duplicated or overlap. So you can specify format:"#" if you want to only show integer tick values, but be aware that if no alternative satisfies this condition, no gridlines or ticks will be shown.
Type: string
Default: auto
【讨论】:
嗨,凯西,感谢您的回复!我尝试在上面的代码中添加:setOption('vAxis.format', format: '#')
和:setOption('vAxis.format', format: 'none')
。在生成新图表或更新现有图表时,这两个选项都不起作用。这可能是 Google API 的错误吗?
嗨,你能用你尝试过的问题更新问题吗,如果可能的话,用模拟数据的表格来尝试重现它?如果代码按照文档所述正确应用,则可能是一个问题,可以报告here
好主意 Kessy,我会按照你的建议去做。以上是关于如何为 Google 表格图表设置水平(或垂直)轴数字格式?的主要内容,如果未能解决你的问题,请参考以下文章