Comments (8)
I'd love to help, but I'm not sure I understand what you're trying to do. group_by_month
should give you a time like 2012-01-01 00:00:00 UTC
, and from that, you should be able to parse out the month and the year with Time.parse
. Does that help at all?
from chartkick.
Looking to produce something like:
The other problem is about the ordering of dates within when dates are Hash Keys, and Adding missing dates. I have meeting looking for a gem but have had no luck.
For missing dates i am talking about imagine you had Jan, Feb, April, May, as data. But when you graph it you want to have march in as a value of 0.
I know i can handle missing dates by hand and sorting by hand, which I have been doing up to this point, but was just thinking there may be a gem already created for this type of manipulation.
from chartkick.
The groupdate gem has the ability to fill in 0's if that's what you're using to generate the chart data https://github.com/ankane/groupdate#show-me-the-series-moneybag
from chartkick.
I am using MongoDB at the moment. according to ankane/groupdate#5 , mongo is not supported atm.
from chartkick.
@ankane Can you pass data manually into groupdate?
mmmm @ankane my problem seems to be because i am using MongoDB.
I am using the Aggregate Framework in Mongo to provide me with the counts of time series data such as the number of occurrences in a month:
def analyzeRestaurantCreationDateCount
restaurantCreationDateCount = @coll.aggregate([
{ "$match" => {fs_fcr_date: {"$ne" => nil}}},
{ "$project" => {createdate_month: {"$month" => "$fs_fcr_date"},createdate_year: {"$year" => "$fs_fcr_date"}}},
{ "$group" => {_id: {year:"$createdate_year", month:"$createdate_month"},number: { "$sum" => 1 }}},
# { "$sort" => {"_id.year" => 1}},
])
dateFormatChangeHash={}
restaurantCreationDateCount.each do |x|
temp = Date.strptime(Date::MONTHNAMES[x["_id"]["month"]] + " " + x["_id"]["year"].to_s, '%B %Y')
dateFormatChangeHash[temp] = x["number"]
end
So when i get my data from mongo it already looks like:
{Mon, 01 Oct 2012=>53, Sat, 01 Dec 2012=>58, Mon, 01 Aug 2011=>8, Thu, 01 Nov 2012=>75, Thu, 01 Sep 2011=>58, Sun, 01 Sep 2013=>67, Sat, 01 Jun 2013=>561, Sat, 01 Sep 2012=>102, Wed, 01 May 2013=>609, Sun, 01 Jul 2012=>84, Wed, 01 Dec 2010=>1, Mon, 01 Jul 2013=>595, Fri, 01 Mar 2013=>253, Fri, 01 Jun 2012=>129, Wed, 01 Feb 2012=>95, Sun, 01 Apr 2012=>50, Thu, 01 Aug 2013=>736, Tue, 01 Jan 2013=>242, Tue, 01 Nov 2011=>46, Thu, 01 Mar 2012=>68, Sat, 01 Oct 2011=>48, Sun, 01 Jan 2012=>70, Wed, 01 Aug 2012=>70, Fri, 01 Feb 2013=>266, Thu, 01 Dec 2011=>47, Mon, 01 Apr 2013=>449, Tue, 01 May 2012=>77}
from chartkick.
You won't be able to use groupdate for this.
However, I don't see where your code is failing. What happens when you pass the data you get from mongo into the line_chart
helper?
from chartkick.
@ankane my data is presented correctly. BUT I want to show months that do not have values with a value of 0. So my understanding is that i need to add those months into the array.
I wrote up a loop to accomplish this:
Sample input data:
{Sun, 01 Jan 2012=>58, Sat, 01 Sep 2012=>53, Sat, 01 Dec 2012=>58}
and code snippet:
def addMissingMonths (datesHash)
count = 0
datesHash.keys.each do |x|
result = []
if x != datesHash.keys.last
(x+1.month).upto(datesHash.keys[count+1]-1.month) do |a|
result << [a.at_beginning_of_month]
end
puts result.uniq.to_s
end
count += 1
end
end
Thoughts?
Output at the moment for testing purposes looks like:
[[Wed, 01 Feb 2012], [Thu, 01 Mar 2012], [Sun, 01 Apr 2012], [Tue, 01 May 2012], [Fri, 01 Jun 2012], [Sun, 01 Jul 2012], [Wed, 01 Aug 2012]]
[[Mon, 01 Oct 2012], [Thu, 01 Nov 2012]]
And while going into prod the dates would be combined into the original hash with values of 0 and resorted
Thoughts?
https://gist.github.com/StephenOTT/6646927
from chartkick.
Here's how groupdate does it: https://github.com/ankane/groupdate/blob/master/lib/groupdate/series.rb#L68
Essentially, it sorts the dates to find the earliest and latest. Then starting from the earliest, it adds months to an array until it hits the latest date.
from chartkick.
Related Issues (20)
- Chart HOT 1
- More formatting options on chart legends HOT 3
- Overlapping issue HOT 1
- Make chart download link image visible at all times HOT 2
- Better adding of plugins to Chart.js with Chartkick HOT 9
- Charts stuck at "Loading..." [example included] HOT 4
- CSP compliance breaks when used with Turbo Drive w/ and random nonce generator HOT 3
- Add support for chart.js 4.x HOT 3
- Add combo bar/line charts HOT 2
- Jruby Support HOT 1
- Support for Apex Charts HOT 1
- Add support for limiting the serie to a certain time frame HOT 1
- Support for polar (spiderweb) charts HOT 1
- Demo not rendering correctly in Safari HOT 2
- `createChart` is not fired after Turbo Frame renders the view HOT 8
- Comparisons in Two Date Ranges HOT 1
- Error importing Chartkick object HOT 2
- Provide ES module for JS with default exports HOT 1
- Rails 7 Setup Additional Step Needed HOT 4
- Canvas accessibility options like aria-label HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chartkick.