2013-05-27 13 views

Antwort

21

In Pseudo-SQL-Bedingungen, was Sie versuchen zu tun:

SELECT COUNT(book) 
GROUP BY author, month 

So wie ich diese Art von Problem Ansatz ist, die Felder in einer einzigen Dimension ‚Gruppe‘. In diesem Fall würde ich den Monat verketten und Informationen zusammen in eine Dimension schreiben.

Lassen Sie diese unsere Testdaten sein:

var cf = crossfilter([ 
{ date:"1 jan 2014", author: "Mr X", book: "Book 1" }, 
{ date:"2 jan 2014", author: "Mr X", book: "Book 2" }, 
{ date:"3 feb 2014", author: "Mr X", book: "Book 3" }, 
{ date:"1 mar 2014", author: "Mr X", book: "Book 4" }, 
{ date:"2 apr 2014", author: "Mr X", book: "Book 5" }, 
{ date:"3 apr 2014", author: "Mr X", book: "Book 6"}, 
{ date:"1 jan 2014", author: "Ms Y", book: "Book 7" }, 
{ date:"2 jan 2014", author: "Ms Y", book: "Book 8" }, 
{ date:"3 jan 2014", author: "Ms Y", book: "Book 9" }, 
{ date:"1 mar 2014", author: "Ms Y", book: "Book 10" }, 
{ date:"2 mar 2014", author: "Ms Y", book: "Book 11" }, 
{ date:"3 mar 2014", author: "Ms Y", book: "Book 12" }, 
{ date:"4 apr 2014", author: "Ms Y", book: "Book 13" } 
]); 

Die Dimension wie folgt definiert ist:

var dimensionMonthAuthor = cf.dimension(function (d) { 
    var thisDate = new Date(d.date); 
    return 'month='+thisDate.getMonth()+';author='+d.author; 
}); 

Und jetzt können wir einfach nur noch eine Zählung reduzieren zu berechnen, wie viele Bücher gibt es pro Autor, pro Monat (dh pro Dimensionseinheit):

var monthAuthorCount = dimensionMonthAuthor.group().reduceCount(function (d) { return d.book; }).all(); 

Und die Ergebnisse lauten wie folgt:

{"key":"month=0;author=Mr X","value":2} 
{"key":"month=0;author=Ms Y","value":3} 
{"key":"month=1;author=Mr X","value":1} 
{"key":"month=2;author=Mr X","value":1} 
{"key":"month=2;author=Ms Y","value":3} 
{"key":"month=3;author=Mr X","value":2} 
{"key":"month=3;author=Ms Y","value":1} 
4

Ich fand die akzeptierte Antwort nicht alle hilfreich.

Ich habe stattdessen das folgende verwendet.

ich zum ersten Mal eine Schlüsselgruppe (in Ihrem Fall Monat)

var authors = cf.dimension(function (d) { 
    return +d['month']; 
    }) 

Als nächstes gemacht habe ich eine Karte Methode auf dem verschlüsselten Datensatz reduzierte die Mittelwert

Die Gruppierung Hilfsfunktion zu berechnen:

var monthsAvg = authors.group().reduce(reduceAddbooks, reduceRemovebooks, reduceInitialbooks).all(); 

Die Karten reduzieren Funktionen:

function reduceAddbooks(p, v) { 
    p.author = v['author']; 
    p.books = +v['books']; 
    return p; 
} 

function reduceRemovebooks(p, v) { 
    p.author = v['author']; 
    p.books = +v['books']; 
    return p; 
} 

function reduceInitialbooks() { 
    return { 
     author:0, 
     books:0 
    }; 
} 
3

Ich mag eine alte Antwort mit einer neuen Arbeit aktualisieren um beschrieben in: https://github.com/dc-js/dc.js/pull/91

Diese Leistung bei großen Daten-Sets

var cf = crossfilter([ 
    { date:"1 jan 2014", author: "Mr X", book: "Book 1" }, 
    { date:"2 jan 2014", author: "Mr X", book: "Book 2" }, 
    { date:"3 feb 2014", author: "Mr X", book: "Book 3" }, 
    { date:"1 mar 2014", author: "Mr X", book: "Book 4" }, 
    { date:"2 apr 2014", author: "Mr X", book: "Book 5" }, 
    { date:"3 apr 2014", author: "Mr X", book: "Book 6"}, 
    { date:"1 jan 2014", author: "Ms Y", book: "Book 7" }, 
    { date:"2 jan 2014", author: "Ms Y", book: "Book 8" }, 
    { date:"3 jan 2014", author: "Ms Y", book: "Book 9" }, 
    { date:"1 mar 2014", author: "Ms Y", book: "Book 10" }, 
    { date:"2 mar 2014", author: "Ms Y", book: "Book 11" }, 
    { date:"3 mar 2014", author: "Ms Y", book: "Book 12" }, 
    { date:"4 apr 2014", author: "Ms Y", book: "Book 13" } 
    ]); 

    var dimensionMonthAuthor = cf.dimension(function (d) { 
    var thisDate = new Date(d.date); 
    //stringify() and later, parse() to get keyed objects 
    return JSON.stringify ({ date: thisDate.getMonth() , author: d.author }) ; 
    }); 

    group = dimensionMonthAuthor.group(); 
    //this forEach method could be very expensive on write. 
    group.all().forEach(function(d) { 
    //parse the json string created above 
    d.key = JSON.parse(d.key); 
    }); 

    return group.all() 

Ergebnisse wurde nicht geprüft:

[ { key: { date: 0, author: 'Mr X' }, 
    value: 2 }, 
    { key: { date: 0, author: 'Ms Y' }, 
    value: 3 }, 
    { key: { date: 1, author: 'Mr X' }, 
    value: 1 }, 
    { key: { date: 2, author: 'Mr X' }, 
    value: 1 }, 
    { key: { date: 2, author: 'Ms Y' }, 
    value: 3 }, 
    { key: { date: 3, author: 'Mr X' }, 
    value: 2 }, 
    { key: { date: 3, author: 'Ms Y' }, 
    value: 1 } ] 
Verwandte Themen