Rails: Chartkick cummulative user graph
If I understand your question, you need to manipulate the hash of data.
In controller do somehing like this:
@data = User.group_by_week(:created_at).count
accumulator = 0
@data.transform_values! do |val|
val += accumulator
accumulator = val
end
Then show the chart in view as <%= line_chart @data %>
If you inspect the hash <%= User.group_by_week(:created_at).count.inspect %>
it becomes clear.
In order to use accumulator over any hash, could be useful to add a custom method to Class hash.
module HashPatch
def accumulate_values!
accumulator = 0
transform_values! do |val|
val+= accumulator
accumulator = val
end
end
end
Hash.include HashPatch
points = {'x1' => 0, 'x2' => 10, 'x3' => 10, 'x4' => 10.1}
points.accumulate_values! #=> {"x1"=>0, "x2"=>10, "x3"=>20, "x4"=>30.1}
transform_values!
since Ruby v4.2.1
Rails: Using groupdate & chartkick to create a cumulative user graph
Martin's answer was close, but I ended up using:
User.group_by_week(:created_at).order("week asc").count.map { |x,y| { x => (sum += y)} }.reduce({}, :merge)
To get weekly - notice the order("week asc") - it's what fixed it...
Chartkick cumulative chart by date never descending
I think it is not possible for cumulative data if you only have first occurred timestamp and current status.
- If your errors will never be deleted and the only action is resolve (Change from active -> resolved status), you could add a new field, indicate when the error was resolved.
The error will be considered active
on a day if they are created before or on that day, and not resolved or resolved after that day. So you query would be:
# Don't filter by status here
errors = Error.where('first_occurrence_date <= ?', Time.zone.today - 30.days)
day_data = (29.days.ago.to_date..Time.zone.today).to_h { |day| [day, 0] }
errors.find_each do |error|
resolved_date = error.resolved_at&.to_date || Time.zone.tomorrow
(error.first_occurrence_date..resolved_date).each do |day|
day_data[day] += 1
end
end
- If the error can be reopened/resolved multiple times, can be deleted, or having multiple statuses, this approach will be invalid though. You may consider storing the history amount for each day in a new model via a daily cronjob or something like that, and just query those amounts. For example
Cronjob: run at the end of the day
# first_occurrence_date doesn't matter, we only count the errors still active by the end of the day
DayReport.create(day: Time.zone.today, active_errors_count: Error.where(status: "active").count)
Then simply query for the chart
line_chart DayReport.pluck(:day, :active_errors_count).to_h
Cumulative Chartkick Chart & Acts_As_Taggable_On
Alright guys, I finally worked this out!
Most answers around PG conflicts refer to .group() whereas I was looking at .group_by_day(:created_at). I fixed the ambiguous error by simply using:
= line_chart @items.group_by_day('items.created_at').order("day asc").sum(:result).map { |x,y| { x => (@sum += y)} }.reduce({}, :merge)
The only difference being passing 'items.created_at' as a string to group_by_day in order to define which created_at I was referring to.
Chartkick graph keeps loading
Figured it out, just needed to add .count
<%= line_chart Client.group_by_day(:created_at).count, id: random_chartkick_id %>
Rails Chartkick Lines not drawing in multi-series line_chart
As previous answers have mentioned, this seems to be due to the data points not sharing x-axis values. However, Highcharts handles such irregular data just fine, and it's supported by chartkick. Just source Highcharts (and jQuery as a dependency) instead of Google charts, e.g.
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="path/to/chartkick.js"></script>
For a demo, check out: http://www.highcharts.com/demo/spline-irregular-time
Chartkick - Stacked vertical graph in percentage
So the trick is to transform the data a bit.
@data = [
{
name: "Fantasy & Sci Fi",
data: [["2010", 10], ["2020", 16], ["2030", 28]]
},
{
name: "Romance",
data: [["2010", 24], ["2020", 22], ["2030", 19]]
},
{
name: "Mystery/Crime",
data: [["2010", 20], ["2020", 23], ["2030", 29]]
}
]
And this works like a charm then:
<%= bar_chart @data, stacked: true %>
I have found the answer in an older question over here.
However, if you want to do the percentage stack, you need to use the library hash for that.
<%= bar_chart @data, adapter: 'google', library: { isStacked: 'percent' } %>
P.s. Notice that it should be either true or 'percent' for the isStacked. More information here.
Related Topics
Import CSV in Batches of Lines in Rails
How to Convert Character Code to What I Want
How to Ignore File Types in a Web Crawler
Ruby - Convert Integer to String
New Way of Creating Hashes in Ruby 2.2.0
Why Rails Can Use 'If' as Hash Key But Not in Ruby
Why Non-Explicit Splat Param Plus Default Param Is Wrong Syntax for Method Definition in Ruby 1.9
Capture Webcam's Image with Ruby
Ruby- Delete a Value from Sorted (Unique) Array at O(Log N) Runtime
How to Use String Methods on Utf-8 Characters
Errno::Enoent No Such File or Directory Rails 4
How to Access Text Field in an Iframe
How to Stop Bundler from Adding Ruby Version to Gemfile.Lock
How to Force To_Yaml to Output Long Strings in Literal Block Style
Active Resource Complaining About Expects an Hash
While Executing Gem ... (Argumenterror) Unknown Encoding Name - Cp720
How to Fix Ruby Script Which Fails with Encoding Error: "\Xd8" on Us-Ascii