update excluding multiples
Example of a set of graphs where one of the attribute is required not to be summed all the time.
by raino01r
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js"></script>
<script src="https://dc-js.github.io/dc.js/js/crossfilter.js"></script>
<script src="https://dc-js.github.io/dc.js/js/dc.js"></script>
<link rel="stylesheet" href="https://dc-js.github.io/dc.js/css/dc.css">
</br>
<div id="brush">Select time</div>
</br>
<div id="bymonth">Month</div>
</br>
<div id="byroom">Room</div>
</br>
<div id="bytype">Type</div>
CSS
#brush g.y { /*Hide y axis on top bar-brush*/
display: none;
}
JavaScript
var dateFormat = d3.time.format('%Y-%m');
function check_done_array(p, v){
return p.done.indexOf(v.room + '_' + dateFormat(v.month));
}
// The three functions needed for the custom `.reduce` block.
function reduce_add_rate_computation(p, v){
var index = check_done_array(p, v);
if (index==-1) {
p.done.push(v.room + '_' + dateFormat(v.month));
}
var mach_count_to_sum = (index==-1 || true)? v.machineCount:0;
p.mach_count += mach_count_to_sum;
p.f_count += v.failCount;
p.rate = (p.mach_count==0) ? 0 : p.f_count * 1000 / p.mach_count;
return p;
}
function reduce_remove_rate_computation(p, v){
var index = check_done_array(p, v);
if (index==-1) {
p.done.splice(index, 1);
}
var mach_count_to_subtract = (index==-1 || true)? v.machineCount:0;
p.mach_count -= mach_count_to_subtract;
p.f_count -= v.failCount;
p.rate = (p.mach_count==0) ? 0 : p.f_count * 1000 / p.mach_count;
return p;
}
function reduce_initial_rate_computation(){
return {rate: 0, mach_count:0, f_count:0, done: new Array()};
}
function group_reduce_wrapper(dim){
return dim.group()
.reduce(
reduce_add_rate_computation,
reduce_remove_rate_computation,
reduce_initial_rate_computation
)
.order(function(p){return p.rate;});
}
var data = [
{"machineCount": 9, "failCount": 5, "month": "2016-12", "room": "1", "type": "A"},
{"machineCount": 7, "failCount": 2, "month": "2016-12", "room": "2", "type": "A"},
{"machineCount": 2, "failCount": 2, "month": "2016-12", "room": "3", "type": "A"},
{"machineCount": 9, "failCount": 1, "month": "2016-12", "room": "1", "type": "B"},
{"machineCount": 7, "failCount": 5, "month": "2016-12", "room": "2", "type": "B"},
{"machineCount": 2, "failCount": 4, "month": "2016-12", "room": "3", "type": "B"},
{"machineCount": 8, "failCount": 3, "month": "2017-01", "room": "1",...