Rails chartkick: setting up individual curves in a multi-line line chart?

I use chartkick to create a multi-series ruler with the following code:

<%= line_chart [ {name: @track.name, data: records_of_track(@track).map{|record| [record.time_entered, record.entry]}}, {name: "Goal", data: [[@track.goal_by, @track.goal]]}, {name: "Goal trend line", data: [[most_recent_record(@track).time_entered, most_recent_record(@track).entry], [@track.goal_by, @track.goal]]} ]%> 

And here is what it looks like:

Chart, not modified

The main idea is that blue is the material entered by the user, red is their goal, yellow connects the most recent user record with the goal in the form of a “trend line”. I wanted to set up a trend line so that I don’t have endpoints ( pointSize: 0 ) and be broken ( lineDashStyle: [5,5] ), but not the other two. I tried to do it

 <%= line_chart [ {name: @track.name, data: records_of_track(@track).map{|record| [record.time_entered, record.entry]}}, {name: "Goal", data: [[@track.goal_by, @track.goal]]}, {name: "Goal trend line", data: [[most_recent_record(@track).time_entered, most_recent_record(@track).entry], [@track.goal_by, @track.goal]] , library: {pointSize: 0, lineDashStyle: [5,5]}} ]%> 

but it didn’t work, as I got the same result as before and tried to do it

 <%= line_chart [ {name: @track.name, data: records_of_track(@track).map{|record| [record.time_entered, record.entry]}}, {name: "Goal", data: [[@track.goal_by, @track.goal]]}, {name: "Goal trend line", data: [[most_recent_record(@track).time_entered, most_recent_record(@track).entry], [@track.goal_by, @track.goal]]} ] , library: {pointSize: 0, lineDashStyle: [5,5]} %> 

but it made ALL lines dotted and all points disappeared as expected:

Chart, but EVERYTHING got modified (as expected)

So, how do I make these attributes apply only to one curve, but not to the other two? If there is no direct implementation in the map market, it would also be useful to know how I could do this using only Google charts. While I get functionality!

-

EDIT 1: After looking at this , I tried to use the series parameter to change some parameter (here: lineWidth , since it was the easiest to type), but this also did not work, I received an error message. This was my code:

 <%= line_chart [ {data: [[most_recent_record(@track).time_entered, most_recent_record(@track).entry], [@track.goal_by, @track.goal]]}, {name: @track.name, data: records_of_track(@track).map{|record| [record.time_entered, record.entry]}}, {name: "Goal", data: [[@track.goal_by, @track.goal]]} ] , library: library_settings %> 

from

 <% library_settings = { width: 600, vAxis: {ticks: choose_ticks(@track)}, colors: ['ff9900', '3366cc', 'dc3912'], series: { 0: {lineWidth: 1}, 1: {lineWidth: 2}, 2: {lineWidth: 10} } } %> 

but i got an error

 ....html.erb:16: syntax error, unexpected ':', expecting => 0: {lineWidth: 1}, ^ ....html.erb:16: syntax error, unexpected ',', expecting keyword_end ....html.erb:17: syntax error, unexpected ',', expecting keyword_end ....html.erb:19: syntax error, unexpected '}', expecting keyword_end 
+5
source share
1 answer

You seem to be on the right track right now. The syntax error you are getting now should be fixed as follows:

 <% library_settings = { width: 600, vAxis: {ticks: choose_ticks(@track)}, colors: ['ff9900', '3366cc', 'dc3912'], series: { 0 => {lineWidth: 1}, 1 => {lineWidth: 2}, 2 => {lineWidth: 10} } } %> 

The library_settings variable is a ruby ​​hash, and numeric keys are allowed for hashes in ruby, but they should use the hashrocket syntax, not the new colon syntax. To clarify:

 2.1.2 :001 > { 0: {lineWith: 1} } SyntaxError: (irb):1: syntax error, unexpected ':', expecting => { 0: {lineWith: 1} } ^ (irb):1: syntax error, unexpected '}', expecting end-of-input 

But

 2.1.2 :002 > { 0 => {lineWith: 1} } => {0=>{:lineWith=>1}} 
+3
source

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


All Articles