multiple line charts rendering

42 Views Asked by At

The following array, as generated by the controller,

2023-06-14
12.0
14.0
10.0
1.0
2023-06-13
8.0
11.0
17.0
2.0
2023-06-12
4.0
6.0
20.0
0.0
2023-06-11
5.0
5.0
10.0
0.0

is, at best, only rendering the date on the x-axis and the next data point on the Y axis. The goal is to have multiple lines on the same chart, as data is proximate.

<%= line_chart land_based_stats_path, title: 'Land based' %> is invoked on the view. However there is no immediately documented way to define the overall grouping @land-based instance variable and sub-components @one, @two (etc.) which are arrays of date/value. a

NoMethodError (undefined method `chart_json'

is hit in the controller, when respecting the gem ReadMe suggestion: For multiple series, add chart_json at the end. removing chart_json avoids the error, but does not render as expected. The controller defines:

    @land_based = LandBased.order(date: :desc).where('date in (?) AND typology_id = ?', uniq_dates, 1).all
    @one = @land_based.pluck(:date, :attribute_one)
    @two = @land_based.pluck(:date, :attribute_two)
    render json: @land_based.chart_json

The console does issue the full response

Object { id: 2941, typology_id: 1, date: "2023-06-14", … }
id: 2941
typology_id:    1
date:   "2023-06-14"
attribute_one:      "12.0"
attribute_two:      "14.0"
attribute_three:    "10.0"
attribute_four:     "1.0"

thus I suspect a syntactic variance is required to render properly

How can this multiple line chart be rendered (with distinct colours for each line?)

1

There are 1 best solutions below

0
Jerome On

The controller-defined instance variable is fine.
Create the array of hashes & set some options.

  <%= line_chart [
                 { name: "one", data: @land_based.pluck(:date, :attribute_one) },
                 { name: "two", data: @land_lossess.pluck(:date, :attribute_one) },
               ], xtitle: "Date", ytitle: "Count"  %>

chartkick picks up the ball from there and carries you to the finish line.