GithubHelp home page GithubHelp logo

Comments (8)

ankane avatar ankane commented on May 2, 2024

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.

StephenOTT avatar StephenOTT commented on May 2, 2024

Looking to produce something like:
2er9v

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.

ankane avatar ankane commented on May 2, 2024

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.

StephenOTT avatar StephenOTT commented on May 2, 2024

I am using MongoDB at the moment. according to ankane/groupdate#5 , mongo is not supported atm.

from chartkick.

StephenOTT avatar StephenOTT commented on May 2, 2024

@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.

ankane avatar ankane commented on May 2, 2024

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.

StephenOTT avatar StephenOTT commented on May 2, 2024

@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.

ankane avatar ankane commented on May 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.