title: crossfilter.js, how to deal with artificial duplicates on a date dimension
body: <p>Im working on a rails project. The data structure looks like the following. a session can have many events.</p>
  
  <pre><code>[{"id":102,"user_id":"user_2","date":"2015-10-08","count":2,"duration":0,"events":[{"event_type":"Event1","reference_id":"23","duration":2},{"event_type":"Event2","reference_id":"22","duration":3}]}
  </code></pre>
  
  <p>Given the above datastructure i can generate graphs based on the session_date and so on. But i do need to generate piecharts based on data inside the nested events array (by event_type or reference_id for instance).</p>
  
  <p>Now i did some reading and it seems that crossfilter works best on a a flat data structure. So i flatend the structure so that every event gets it own row. </p>
  
  <pre><code>[{"id":102,"date":"2015-10-08","count":2,"duration":0,"event_type":"Event1","reference_id":"23","user_id":"user_1","event_duration":2},{"id":102,"date":"2015-10-08","count":2,"duration":0,"event_type":"Event2","reference_id":"22","user_id":"user_2","duration":3}]
  </code></pre>
  
  <p>I no longer have problems using the data of the former event jsonarray. 
  But now im having the following problem, i need a bar chart showing the number of sessions per day. In the above example i would have one Session with two events at 2015/10/8.</p>
  
  <p>I created the date dimension like this:</p>
  
  <pre><code>var dateDimension = ndx.dimension(function(d) {
               return d.date;
            });
  </code></pre>
  
  <p>And used to group like this:</p>
  
  <pre><code>var dateGroup = dateDimension.group();
  </code></pre>
  
  <p>I initialize the charts like this:</p>
  
  <pre><code>visitorBarChart.width(800)
                    .height(250)
                    .dimension(dateDimension).group(dateGroup)
                    .x(d3.time.scale().domain([minDate,maxDate]))
                    .xAxisLabel("Date")
                    .yAxisLabel("Visitors");
  </code></pre>
  
  <p>But obviously, since i have duplicate dates in the record this does not work any longer. its pretty much counting the number of events by date.</p>
  
  <p>I need something like a group(date,id).count.</p>
  
  <p>Im completely new to crossfilter and dc.js, i hope i described the problem accurately. I searched the web for hours now but i cant figure this one out.</p>
  
  <p>Im guessing i have to do some work with reduceAdd/reduceRemove but i cant figure it out</p>
  
  <p>Any help would be appreciated!</p>
  
  <p>Alright i might have found an solution myself. Im calling reduce with the following functions on my group. </p>
  
  <pre><code>function reduceInitial() {
                return {
                    unique: d3.map(),
                    count: 0
                };
            }
            function reduceAdd(p, v) {
                var id = v.id;
                var date = v.date;
                //is there a date for the current id?
                var check = p.unique.get(id);
                // nopes add to count, add date to id
                if(!check){
                    ++p.count;
                    p.unique.set(id,date);
                }else{
                    //mhhh that should not be neccessary
                    if(check != date){
                        ++p.count;
                        p.unique.set(id,date);
                    }
                }
                //console.log(v)
                //console.log("---")
                //console.log(p)
                return p;
            }
  
            function reduceRemove(p, v) {
                var id = v.id;
                var date = v.date;
                var check = p.unique.get(id);
                if(check){
                    if(check == date){
                        p.unique.remove(id);
                        --p.count;
                    }
  
                }
                return p;
            }
  </code></pre>
  
  <p>I guess i could just use an array instead of d3.map, but seems to work for now</p>