如何在图表 API x/y 轴标签中仅显示整数(无小数)
我正在使用带有 Google Chart JS Api 的柱形图。我正在显示一些只能表示为整数的值(按天计算的总订单数)。
一切都运行良好,除了当我显示的其中一个图表的值太低(例如 1 或 2)时,它开始在 y 轴上显示小数。小数看起来很愚蠢,因为不可能有“一半”订单(这就是我正在计算的),如果可能的话,我想隐藏它。
I'm using a column chart with the Google Chart JS Api. I'm displaying some values (total orders by day) that can only be represented as integers.
Everything is working great, except that when one of the charts I'm displaying has values that are too low, like 1 or 2, it starts to show decimals on the y-axis. The decimals look silly b/c it's impossible to have "half" an order (which is what I'm counting), and I'd like to hide this if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
使用该选项
,如
use the option
as in
我没有足够的代表来直接回复 Ian Hunter 的帖子,但它的作用是格式化标签,但不会将网格线设置为与标签相同的值,这可能会产生不良结果。假设您有这些网格线:
10
7.5
5
2.5
0
当您将标签格式设置为仅显示整数时,它会显示以下内容:
10
8
5
3
0
但是图表上的数据与 y 尺度标签不相符。例如,如果您的值为 3,它实际上会显示在“3”标签上方,因为“3”标签实际上是 2.5
不幸的是,我能想到的唯一解决方案是监控最小/最大范围图表中显示的数据,然后应用一些逻辑根据我有多少个 y 尺度标签来设置最小/最大,这样它们就可以除以没有余数。
因此,在上面的情况下,我将其设置为“
没有小数的网格线
这将给出12
8
6
4
0
,并且数据与图表上的标签不相关也没有问题。
I don't have enough rep to reply to Ian Hunter's post directly, but what that does is format the label, but doesn't set the grid lines to the same value as the label, which can have undesirable results. So let's say you have these grid lines:
10
7.5
5
2.5
0
When you format the label to only show integers it shows this:
10
8
5
3
0
But then your data on the graph doesn't tie-in with the y-scale labels. E.g If you have a value of 3 it actually shows above the '3' labels because the '3' label is actually 2.5
Unfortunately, the only solution I could think of was to monitor the min/max ranges of the data shown in the graph, and then apply some logic to set the min/max according to how many y-scale labels I have, such that they would divide with no remainders.
So in the case above, I'd set it to
This would give grid lines of
12
8
6
4
0
No decimals, and no problems with the data not correlating with the labels on the graph.
添加 {format : 0} 会将浮点刻度转换为整数,但条形图上的实际值显示不正确。
主要问题是 Google Charts API 假设您主要需要 5 条网格线,这可能无法为您提供整数刻度值。
添加这个给了我很好的圆形刻度值 -
来源 - https: //groups.google.com/forum/#!topic/google-visualization-api/4qCeUgE-OmU
Adding {format : 0} converts the float ticks to integers but the actual values on the bars appear incorrectly.
The main issue is Google Charts API assumes you want 5 gridlines mostly which may not work out to give you integer tick values.
Adding this gave me nice round tick values -
source - https://groups.google.com/forum/#!topic/google-visualization-api/4qCeUgE-OmU
如果您只想显示整数值,则应考虑到 google 图表希望向您显示至少 5 条网格线。假设所有这些都应该是整数,给出下图:
if you want only the integer values to be shown, you should take to account that google charts wants to show you at least 5 gridlines. Presuming all of them should be integer give the graph following:
最简单的方法是编辑左侧垂直轴,将 Min 设置为 0,将 Max 设置为 5 的倍数(5、10、15 等),以适合您的预期最大值。
The easiest way is to edit the left vertical axis, with Min as 0, and Max as a multiple of 5 (5, 10, 15 etc) as appropriate to your expected maximum.
如果您使用
vAxis: {gridlines: { count: 4}}
,此问题将得到解决This issue will be resolved if you use
vAxis: {gridlines: { count: 4}}
在我的例子中,我的值范围为 0 到 600。
这样您只显示“整数”。
当然,您需要动态生成 JavaScript。
代码示例:
In my case I have values ranging from 0 to 600.
In this way you only display 'whole numbers'.
You'll need to dynamically generate the javascript off course.
Code Example:
我使用以下代码:
只需计算最大值,然后将 gridlines.count 设置为该值
I'm using the following code:
Just calculate your maximum value and then set gridlines.count to this value
我使用 Guava Multiset 作为我的数据。 Cagy79 的技巧是我唯一能够使用的技巧。我还尝试使用 IntStream 生成自己的线,但某些图形仍然有小数。如上所述,只需设置格式就会创建一个图形,其中悬停值和网格线不同。
多重集项 = TreeMultiset.create();
使用 .add 和 .addAll 添加项目
确保项目已排序:
occurrences = Multisets.copyHighestCountFirst(items);
字符串选项=“hAxis:{标题:'计数',网格线:{计数:”+
((max >=12) ? 4 : max+1)
+ } }")
I'm using the Guava Multiset as my data. Cagy79's trick is the only one that I was able to get working. I also tried generating my own lines using an IntStream, but still had fractional numbers for some of the graphs. As mentioned above, just setting the format will create a graph where the hover over value and the gridline differ.
Multiset items = TreeMultiset.create();
Items added using .add and .addAll
Make sure the items are sorted:
occurrences = Multisets.copyHighestCountFirst(items);
String option = "hAxis : { title : 'Count', gridlines : { count:" +
((max >=12) ? 4 : max+1)
+ } }")
我更喜欢
gridlines: { count: -1 }
选项,但您始终可以使用ticks
显式指定所需的值。很酷的一点是它们甚至不必均匀分布 - 如果您愿意,您可以执行 [1, 10, 100, 200, 500, 1000]:options: { vAxis: { ticks: [1, 2,3,4,5,6,7,8,9,10] } }
根据您的数据,您可能需要对其进行硬编码或使用阈值。
I prefer the
gridlines: { count: -1 }
option, but you can always explicitly specify the values you want withticks
. Cool thing about this is they don't even have to be evenly spaced - you could do [1, 10, 100, 200, 500, 1000] if you wanted:options: { vAxis: { ticks: [1,2,3,4,5,6,7,8,9,10] } }
Depending upon your data you may want to hardcode this or use thresholds.
对于也在寻找答案的人,您可以在此处查看此问题的答案,该答案使用选项格式。
无法将 Google Charts Axis 设置为十进制格式
For anyone that are also searching for the answer, you can see the answer to this question here, which use options format.
Can't get Google Charts Axis to format in decimal