我開始與dc,我轉過身來幾天關於我不能做的一件簡單的事情。我有一個這種結構的經典數據樣本: {date:「2011-11-14T16:17:54Z」,quantity:2,total:190,tips:100,type:「tab」} 只是想顯示不同種類的付款的數量。有3個。我可以在控制檯中顯示它,但不能顯示在圖表中。我嘗試了很多不同的東西,但都沒有工作。我有一些與reduceSum一起工作,但不與reduceCount,對象的結構似乎是不同的。 感謝您的幫助簡單號碼顯示從reduceCount與dc.js
這是我的代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1"/>
<title>Crossfilter</title>
<script src="crossfilter.js"></script>
<script src="d3.js"></script>
<script src="dc.js"></script>
</head>
<body>
<div class="container">
My count :
<div id="category-count"></div>
<script type="text/javascript">
var payments = crossfilter([
{date: "2011-11-14T16:17:54Z", quantity: 2, total: 190, tip: 100, type: "tab"},
{date: "2011-11-14T16:20:19Z", quantity: 2, total: 190, tip: 100, type: "tab"},
{date: "2011-11-14T16:28:54Z", quantity: 1, total: 300, tip: 200, type: "visa"},
{date: "2011-11-14T16:30:43Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T16:48:46Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T16:53:41Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T16:54:06Z", quantity: 1, total: 100, tip: 0, type: "cash"},
{date: "2011-11-14T16:58:03Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T17:07:21Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T17:22:59Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T17:25:45Z", quantity: 2, total: 200, tip: 0, type: "cash"},
{date: "2011-11-14T17:29:52Z", quantity: 1, total: 200, tip: 100, type: "visa"}
]);
var paymentsByType = payments.dimension(function(d) { return d.type; });
var countType = paymentsByType.group().reduceCount();
console.log(countType.size());
dc.numberDisplay('#category-count')
.formatNumber(d3.format("d"))
.group(countType)
.valueAccessor(function (d) { return d.size(); });
</script>
</div>
</body>
</html>
非常感謝戈登。它以這種方式工作。這不是最直觀的,但它使工作。順便說一下,也請感謝scala-like synthax x => x,而不是詳細的java-like類型。我很喜歡。 –