Percent of filtered records having dimension value x

204 views
Skip to first unread message

Pete Huang

unread,
Apr 5, 2015, 7:27:35 PM4/5/15
to dc-js-us...@googlegroups.com
Hi,

I'm trying to create a chart that shows the percent of the filtered records that have "correct" or "incorrect" as a value for a particular dimension. So far, I'm able to get the initial display correctly using the following value accessor:

.valueAccessor(function(p){
 
return (p.value / ndx.groupAll().value()) * 100;
})

The only problem is that if I try to filter on the "correct" or "incorrect" dimension itself, the numbers would clearly get a little weird. Specifically, incorrect goes to around 21% if I filter on correct, and correct goes to around 465% if I filter on incorrect.

How would I get the correct/incorrect filtering working along with the percentage calculation?

Thanks!
Pete

Blair Nilsson

unread,
Apr 8, 2015, 3:42:41 AM4/8/15
to dc-js-us...@googlegroups.com
What does your data and the reduce you are using look like?

you could try 
.valueAccessor(function(p){
 console.log(p.value, ndx.groupAll().value())
 
return (p.value / ndx.groupAll().value()) * 100;
})

to see what it is thinking while you do this.

Pete Huang

unread,
Apr 8, 2015, 12:12:23 PM4/8/15
to dc-js-us...@googlegroups.com
Hi Blair,

Here's the JSON for one entry:

{
     
"difficulty": "Easy",
     
"id": 486,
     
"others_pace": 84,
     
"provider": "Magoosh",
     
"result": "Correct",
     
"section": "Data Sufficiency",
     
"session_id": 39,
     
"subject": "Word Problems",
     
"timestamp": [
       
"2015-04-01",
       
"14:43:58"
     
],
     
"user_pace": 111
   
}

I'm still using the default reduce. 

I added the console print. It confirms what I'm suspecting.

Let's say 100 entries are "Correct" and 20 are "Incorrect. Upon first render, it'll spit out 100 120 and 20 120. This is correct.

Once I filter on "Correct", it'll spit out 100 100 and 20 100. This is how the numbers are getting messed up. Either the p.value needs to update on the filter (so that the filtered bar becomes 100% and the other bar becomes 0%), or it needs to function like the other graphs, where clicking on a bar only highlights it.

Gordon Woodhull

unread,
Apr 8, 2015, 4:43:37 PM4/8/15
to dc-js-us...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "dc-js user group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dc-js-user-gro...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dc-js-user-group/8203bba3-29cb-46f2-b2d1-6e164d7c1ed2%40googlegroups.com.

Blair Nilsson

unread,
Apr 9, 2015, 12:30:11 AM4/9/15
to dc-js-us...@googlegroups.com
That looks almost exactly like what I was going to suggest.

It should look something like....

function init() {
  return {
    count:0,
    correct:0,
    correctPercent:0,
    failPercent:1
  }
}

function add(p, v) {
  p.count = p.count + 1;
  p.correct = p.correct + (v.result =='Correct')?1:0
  p.correctPercent = p.count ? p.correct/p.count : 0;
  p.failPercent = 1 - p.correctPercent;
  return p;
}

function remove(p, v) {
  p.count = p.count - 1;
  p.correct = p.correct - (v.result =='Correct')?1:0
  p.correctPercent = p.count ? p.correct/p.count : 0;
  p.failPercent = 1 - p.correctPercent;
  return p;
}

dim.group().reduce(add,remove,init)

Pete Huang

unread,
Apr 9, 2015, 2:54:03 AM4/9/15
to dc-js-us...@googlegroups.com
Thanks for both your responses. I implemented the change and it turns out that this works great using a numberDisplay but not a graph, which requires a dimension. I think this is saying that there is one reduce for each value in the dimension.

{key: "Correct",
 value
: {
  correct
: 107,
  correctPercent
: 1,
  count
: 107,
  incorrectPercent
: 0
 
}
},
{key: "Incorrect",
 value
: {
  correct
: 0,
  correctPercent
: 0,
  count
: 23,
  incorrectPercent
: 1
 
}
}
Reply all
Reply to author
Forward
0 new messages