Skip to content

Instantly share code, notes, and snippets.

@nolanamy
Last active January 3, 2016 04:29
Show Gist options
  • Save nolanamy/8409051 to your computer and use it in GitHub Desktop.
Save nolanamy/8409051 to your computer and use it in GitHub Desktop.
Graph time-based data in irb
#example data
data = [
{
:time => Time.now - 2.days,
:value => 2321.65
},
{
:time => Time.now - 1.days,
:value => 1307.98
},
{
:time => Time.now,
:value => 2576
}
]
#graph it. the maximum gets 100 dots
max = data.max_by{|a| a[:value]}[:value]
data.each do |a|
puts (a[:time].to_formatted_s(:short) + ' ' + '%9.1f' % a[:value] + ' ' + '.'*(a[:value].to_f/max*100))
end
@nolanamy
Copy link
Author

Grabbing data to graph:

data = []
(0..90).each do |i|
  data.push({
    :time => Time.now - (i+1).days,
    :value => ModelName.where(:created_at => (Time.now - (i+1).days)..(Time.now - i.days)).count
  })
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment