问题
I've got a question concerning measures that can't be aggregated over time, for example stock value. In the example below, I've created a time line for my total stock value, which works well. Additionally I would like to see the most current stock level per company. This should be last month of my time selection. I can't get this to work as the stock value is being summed over the entire period, where I would like to see the stock value of the last period (month).
Whenever I change my selection in the chartMonth, the stock value in the chartCompany should adjust accordingly. I think I need to explicitly define a filter on my dimCompany or grpCompant, but I really don't have a clue how to proceed.
<script type="text/javascript">
var data = [{date:"201501",company:"A",stock:1200}
, {date:"201502",company:"A",stock:1400}
, {date:"201503",company:"A",stock:1300}
, {date:"201504",company:"A",stock:1100}
, {date:"201501",company:"B",stock:1000}
, {date:"201502",company:"B",stock:1100}
, {date:"201503",company:"B",stock:900}
, {date:"201504",company:"B",stock:1200}];
var dateFormat = d3.time.format('%Y%m');
data.forEach(function (d) {
d.dd = dateFormat.parse(d.date);
d.year = d3.time.year(d.dd);
d.month = d3.time.month(d.dd);
});
var ndx = crossfilter(data)
, dimMonth = ndx.dimension(function(d){return d.month})
, dimCompany = ndx.dimension(function(d){return d.company;});
var grpMonth = dimMonth.group().reduceSum(function(d){return d.stock;})
, grpCompany = dimCompany.group().reduceSum(function(d){return d.stock;});
var chartCompany = dc.barChart('#chartCompany');
var chartMonth = dc.lineChart('#chartMonth');
chartCompany
.width(400)
.height(400)
.margins({top: 50, right: 50, bottom: 50, left: 50})
.dimension(dimCompany)
.group(grpCompany)
.x(d3.scale.ordinal())
.xUnits(dc.units.ordinal)
.elasticY(true);
chartMonth
.width(400)
.height(400)
.margins({top: 50, right: 50, bottom: 50, left: 50})
.dimension(dimMonth)
.group(grpMonth)
.x(d3.time.scale().domain([new Date(2015, 5, 10, 6), new Date(2015, 5, 10, 18)]))
.xUnits(d3.time.months)
.elasticX(true);
dc.renderAll();
</script>
来源:https://stackoverflow.com/questions/32472189/dc-js-get-last-month-value-as-filter