filter on year, but keep the data of other years

20 views
Skip to first unread message

Arjen Haayman

unread,
Feb 2, 2017, 8:57:52 AM2/2/17
to dc-js user group
Hi,

I've got this dataset with the fields areaId, year, someOtherValues. There's a dropdown of all the years and I always filter only one year to be displayed.
in short:

var ndx = crossfilter(data);
var yearDim = ndx.dimension((d) => d.year);
var years = yearDim.group().all().map( (d)=> d.key)
yearDim
.filter( d.max(years));
var dropdown = createDropdown( years )
dropdown
.on('change', (v) => {
   yearDim
.filter(d);
})
var yearGroup = yearDim.group().reduceCount();



I have this map that displays all areas using dimension areaId. The areas should display the *increase* of the count compared to the previous year.
I can't figure out how to do this.
I've done charts displaying increase before using the year Dimension by taking the group

/**
 * get the data of the next year
 * @param nextYear
 * @returns {*}
 */
var getNextPeriod = function( year ) {
   
var yearValues = yearGroup.all();
   
var curIndex = findIndex( yearValues, function(d){
       
return d.key == year;
   
});
   
var nextIndex;
   
if( (nextIndex = curIndex + 1) <= yearValues.length ) {
       
return yearValues[ nextIndex ];
   
}
   
return null;
}

var getIncrease = function( d ) {
   
var curValue = d.value;
   
var nextPeriod, nextValue;
   
if( nextPeriod = getNextPeriod(d.key )) {
       
       
if( nextValue = nextPeriod.value ) {
           
return (nextValue - curValue);
       
}
   
}
   
return 0;
}

chart
.valueAccessor( getIncrease );


This only works in a chart that uses the same yearDimension that's also used for filtering the year.

In the case of my map I have to use areaId as dimension. When I try to get the data of the previous year I find those are zero, because the have been reduced out.

I've got 2 lines of thought:
- maybe I can write some kind of reducer that maintains the values per year? The problem here seems to be that I don't know when I should or should not add/reduce the counts. If the filter is on someOtherValues I should add/reduce, but if the filter is on Year I should not.
- maybe I can use 2 different crossfilters and synchronize the filtering on both, except the yearFilter?



Reply all
Reply to author
Forward
0 new messages