从支付示例crossfilter(https://github.com/square/crossfilter/wiki/API-Reference)开始,我们如何为每种支付类型(标签,签证,现金)创建一个带有一个折线图的复合图表?
我假设您想显示每次付款的一段totals时间内的付款(date维度)type。
var payments = crossfilter([...]);
var dateDimension = payments.dimension(function(d) { return new Date(d.date); });
Run Code Online (Sandbox Code Playgroud)
为每种付款类型(tab、visa、现金)创建一组付款总额
var totalForType = function(type) {
return function(d) {
return d.type === type ? d.total : null;
};
};
var tabTotalsGroup = dateDimension.group().reduceSum(totalForType('tab'));
var visaTotalsGroup = dateDimension.group().reduceSum(totalForType('visa'));
var cashTotalsGroup = dateDimension.group().reduceSum(totalForType('cash'));
Run Code Online (Sandbox Code Playgroud)
定义复合图表并使用组定义 3 个折线图作为复合图表的一部分。
var compositeChart = dc.compositeChart('#composite-chart');
compositeChart
...
.x(d3.time.scale().domain([new Date("2011-11-14T16:15:00Z"), new Date("2011-11-14T17:45:00Z")]))
.dimension(dateDimension)
.compose([
dc.lineChart(compositeChart).group(tabTotalsGroup, 'tab').colors(['#ffaa00']),
dc.lineChart(compositeChart).group(visaTotalsGroup, 'visa').colors(['#aa00ff']),
dc.lineChart(compositeChart).group(cashTotalsGroup, 'cash').colors(['#00aaff'])
]);
dc.renderAll();
Run Code Online (Sandbox Code Playgroud)
完整示例:http://plnkr.co/edit/rhDURrDfeSvVqEnQR9L1 ?p=preview