如何设置工具提示以显示与 Google 可视化折线图中的轴匹配的百分比?
可以使用以下代码将工具提示设置为显示百分比:
var formatter = new google.visualization.NumberFormat({ fractionDigits: 2, suffix: '%' }); formatter.format(data, 1); // Apply formatter to first column.
NumberFormat 有没有办法将每个元素乘以 100?否则工具提示将显示为 0.50%。
我使用的是 vAxis.format = "format:'#%' "
它确实乘以 100。因此 0.5 在垂直轴上显示为 50%。
根据文档(icu-project.org/apiref),这可以通过将 % 用单引号括起来来覆盖,但这不起作用。
最终结果是工具提示与轴不匹配。最好的方法是什么?
The tooltips can be set to display percentages using the following code:
var formatter = new google.visualization.NumberFormat({ fractionDigits: 2, suffix: '%' }); formatter.format(data, 1); // Apply formatter to first column.
Is there a way for NumberFormat to multiply each element by 100? Otherwise the tooltip appears as .50%.
I am using vAxis.format = "format:'#%' "
which does multiply by 100. So .5 is displayed as 50% on the vertical axis.
According to the documentation(icu-project.org/apiref), this can be overwritten by enclosing the % in single quotes, but this did not work.
The net result is that the tooltips do not match the axis. What is the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我通过完全按照您的方式指定格式化程序来实现此目的:
最后一次调用中的
1
表示第二列,其中我有浮点值。然后,我在图表选项中指定轴的格式,转义百分号,如文档和其他人指出的那样:
然后绘制图表:
这将呈现一个左侧轴,其值如
10%
、20%
等等。工具提示看起来与默认提示类似,但百分比如10.10%
、20.20%
等。如果您还想在左侧轴中显示两个小数位,请在图表选项中使用此格式:
I got this working by specifying a formatter exactly as you do:
The
1
in the last call means the second column, in which I have float values.Then I specify a format for the axis in the chart options, escaping the percentage sign as pointed out by documentation and others here:
I then draw the chart:
This renders a left side axis with values like
10%
,20%
and so on. And the tooltips looks like the default one but with a percentage like10.10%
,20.20%
and so on.If you want two fraction digits in the left side axis as well, use this as format in the chart options instead:
感谢 http://groups.google.com/group/google-visualization-api/
Thanks to http://groups.google.com/group/google-visualization-api/
必须用单引号将百分号 (%) 本身引起来。
我用来执行此操作的行如下所示:
options['vAxis'] = {'format': "#,###'%'"};
将其与上面的格式化程序结合起来,您可以可以使垂直轴有一个百分比符号,也可以使工具提示也包含它。
You must surround the percent (%) symbol itself in single quotes.
The line I used to do this looks like this:
options['vAxis'] = {'format': "#,###'%'"};
Combining this with your formatter above, you can make the vertical axis have a percent symbol and also make the tooltip include it too.
好吧...所以这有点晚了。我承认七年前我不需要这个。尽管如此,这对我有用。
就我而言,我使用四列,其中两列以百分比分配给右轴。我希望这些列的工具提示能够反映正确的百分比而不是小数表示形式。
这是 Google 文档的链接:
我希望这可以帮助一些随机的陌生人寻找它。 ;)
Ok... So this is a little late. I admit I didn't need this seven years ago. Nevertheless, this worked for me.
In my case, I am using four columns, two of which are assigned to the right axis with percentages. I wanted those columns' tooltips to reflect the proper percentage rather than the decimal representation.
Here is a link to the Google docs:
I hope this helps some random stranger looking for it. ;)