Rails chartkick:只需要轴上的整数值。使用离散还是别的? [英] Rails chartkick: want only integer values on axes. Use discrete or something else?

查看:561
本文介绍了Rails chartkick:只需要轴上的整数值。使用离散还是别的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下代码

<% data = [
[1,1],[2,3],[3,5],[4,8],[6,4],[7,2]
] %>

<%= line_chart data,  {discrete: true, library: {width: 600} }%>

使用chartkick,生成以下图形

Using chartkick, this produces the following graph

我想垂直轴使用整数标记。 (不是小数)我认为离散选项应该这样做,但对于这个例子,它所做的是改变横向轴上元素的格式从时间到数字(即以下代码

I want the vertical axis to be labeled using integers. (not decimals) I thought that the discrete option was supposed to do this but for this example all it did was change the format of the elements on the horizontal axis from time to number (i.e. the following code

<%= line_chart data,  {library: {width: 600} }%>

产生

)。

所以我的问题是: discrete 究竟是什么,除了更改实际上是数字的日期。我如何使用它来使数字在纵轴上的整数? (或者,如果不能使用它,我可以使用什么?)

So my question is: what exactly does discrete do, other than change dates that were actually numbers to numbers. How can I use it to make the numbers on the vertical axis integers? (Or, if it can't be used to do this, what can I use?)

推荐答案

discrete 选项仅适用于主轴,并且用于离散轴。

The discrete option applies only to the 'major axis' and is for a discrete axis. There's a difference between discrete and continuous axises that you should read up on.

我只读了配置选项。显然你可以通过 ticks 选项到每个轴。蜱是标记。您可以从数据中获取每个范围的最小值和最大值,然后以1个整数间隔展开。

And I just read the configuration options. Apparently you can pass a ticks option to each axis. And the ticks are markers. You could get the minimum and maximum value for each range from your data and then spread it out with a 1 integer interval.

因此,以下应该为你工作:

data = [[1,1],[2,3],[3,5],[4,8],[6,4],[7,2]]

x_values = data.map(&:first)
x_range = (x_values.min)..(x_values.max)

y_values = data.map(&:last)
y_range = (y_values.min)..(y_values.max)

library_options = {
  width: 600,
  hAxis: {ticks: x_range.to_a},
  vAxis: {ticks: y_range.to_a}
  # to_a because I don't know if Range is acceptable input
}

line_chart(data, {library: library_options})

有关更多选项,请参阅 Google图表折线图的配置选项

For more options, take a look at Google Chart's configuration options for line charts.

这篇关于Rails chartkick:只需要轴上的整数值。使用离散还是别的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆