2013-05-27 57 views

回答

21

在僞SQL來說,你正在嘗試做的是:

SELECT COUNT(book) 
GROUP BY author, month 

我走近這類問題的辦法是「組」的字段一起到一個單一的維度。所以在你的情況下,我會將月份和作者信息連接在一起,形成一個維度。

讓這成爲我們的測試數據:

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); 
    return 'month='+thisDate.getMonth()+';author='+d.author; 
}); 

而現在我們只要簡單地做一個減少數來計算多少書有每作者,每月(即每個維度單位):

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

並且結果如下:

{"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

我沒有找到接受的答案都有幫助。

我用下面的代替。

我第一次做了一個鍵組(在你的情況下本月)

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

接下來,我用的地圖減輕鍵數據集的方法來計算平均

分組輔助功能:

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

地圖-降低功能:

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

我想更新舊的答案與周圍描述的新作:https://github.com/dc-js/dc.js/pull/91

這一業績尚未在大型數據集測試

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() 

結果:

[ { 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 } ] 
相關問題