Rails chartkick: only integer values ​​for axes are required. Use discrete or something else?

Say I have the following code

<% data = [ [1,1],[2,3],[3,5],[4,8],[6,4],[7,2] ] %> <%= line_chart data, {discrete: true, library: {width: 600} }%> 

Using chartkick you get the following chart

Description

I want the vertical axis to be labeled with integers. (not decimal numbers) I thought the discrete option should have done this, but for this example, all that was done was to 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} }%> 

produces it

Yuck

)

So my question is: what exactly does discrete do, except for the change dates, which are actually numbers numbers. How can I use it to create numbers on integers of a vertical axis? (Or, if it cannot be used for this, what can I use?)

+7
source share
2 answers

The discrete parameter applies only to the "main axis" and refers to the discrete axis. There is a difference between discrete and continuous axes that you should read.

And I just read the configuration options. Apparently, you can pass the ticks option for each axis. And ticks are markers. You can get the minimum and maximum value for each range from your data, and then distribute it using 1 integer interval.

So the following should work for you:

 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}) 

For more information, see Google chart line chart configuration options .

+5
source

Just try this one. library: { yAxis: {allowDecimals: false } }

Source: https://github.com/ankane/chartkick/issues/269

0
source

Source: https://habr.com/ru/post/980002/


All Articles