mardi 14 juin 2016

Multi-line chart groupped per day (SQL & ChartJS)

I'm trying to show the number of usages of terms/tags (per day) and plot them in a line chart using ChartJS. The data is queried using Propel and returned to the client side using AJAX.

This is the big old query (removed some unnecessary parts):

$query = RDSUserTermQuery::create('UserTerm')
    ->joinRDSUserAction('UserAction')
    ->withColumn('UserTerm.TermID', 'TermID')
    ->withColumn('DATE_FORMAT(UserTerm.AssociatedAt, "%d")', 'Day')
    ->withColumn('DATE_FORMAT(UserTerm.AssociatedAt, "%a %d")', 'Date')
    ->withColumn('COUNT(UserTerm.TermID)', 'Frequency')
    ->withColumn('COUNT(UserTerm.TermID) * UserAction.Weight', 'Weight')
    ->select(['TermID', 'Day', 'Date', 'Frequency', 'Weight'])
    ->orderBy('Day', Criteria::ASC)
    ->groupBy(['TermID', 'Day']);

Which currently returns data in the following JSON format:

[
    {TermID: "11379", Day: "07", Date: "Tue 07", Frequency: "2", Weight: "3.0", Name: "candy"}
    {TermID: "10814", Day: "07", Date: "Tue 07", Frequency: "2", Weight: "3.0", Name: "toys"}
    {TermID: "9240", Day: "07", Date: "Tue 07", Frequency: "2", Weight: "3.0", Name: "person"}
    {TermID: "11328", Day: "07", Date: "Tue 07", Frequency: "1", Weight: "1.5", Name: "test"}
    {TermID: "11379", Day: "08", Date: "Wed 08", Frequency: "4", Weight: "8.0", Name: "candy"}
    {TermID: "10814", Day: "08", Date: "Wed 08", Frequency: "1", Weight: "2.0", Name: "toys"}
    {TermID: "9240", Day: "08", Date: "Wed 08", Frequency: "1", Weight: "2.0", Name: "person"}
    {TermID: "11328", Day: "08", Date: "Wed 08", Frequency: "3", Weight: "4.0", Name: "test"}
    // etc...
]

I want to plot the line chart just as Google Trends does but I'm pretty confused at this point. How would I set up the data and plot the chart?

Edit: This is what I would like to achieve (rough sketch): enter image description here

Thanks!

Aucun commentaire:

Enregistrer un commentaire