d3.js - How can I group data to be filtered without losing crossfilter functionality using dc.js? -
i'm trying learn d3 via dc.js , i'm quite stuck trying figure out how group line chart w15sec, w30sec,...,w1hr, names , values. when filter applied, i'd show max value workouts within filter parameters. here jsfiddle.
i've got flat cycling data looks this:
var data = [{"timestamp":"2017-09-06t12:32:04.183","duration":3459.518,"distance":10261,"activityid":175508086,"avgpower":305.5419317525,"w15sec":499.2666666667,"w30sec":479.3333333333,"w1min":470.2666666667,"w2min":441.9416666667,"w5min":417.5166666667,"w10min":410.5616666667,"w20min":342.3141666667,"w40min":306.2033333333,"w1hr":0.0},{"timestamp":"2017-09-08t12:07:27.033","duration":2106.755,"distance":3152,"activityid":175647595,"avgpower":168.8485158649,"w15sec":375.8666666667,"w30sec":327.7333333333,"w1min":271.1833333333,"w2min":261.6083333333,"w5min":0.0,"w10min":0.0,"w20min":0.0,"w40min":0.0,"w1hr":0.0},{"timestamp":"2017-09-07t17:11:51.577","duration":1792.025,"distance":4245,"activityid":175670859,"avgpower":244.2495803022,"w15sec":365.2,"w30sec":342.1333333333,"w1min":328.0333333333,"w2min":290.975,"w5min":276.0566666667,"w10min":268.8316666667,"w20min":246.8858333333,"w40min":0.0,"w1hr":0.0},{"timestamp":"2017-09-09t10:31:21.107","duration":15927.885,"distance":39408,"activityid":175971583,"avgpower":255.0849193803,"w15sec":405.0666666667,"w30sec":389.8666666667,"w1min":367.6666666667,"w2min":350.3916666667,"w5min":318.93,"w10min":300.345,"w20min":281.9883333333,"w40min":259.4733333333,"w1hr":0.0}];
the goal have chart on right populated names of categories (w15sec, w30sec,...,w1hr) dimensions , values max found in activities each category. looks line chart going high values (w15sec) lower values (w1hr).
it should image.
thanks help!
i think best way approach use reductio's multi-value group , maximum reducer calculate maximum each window on power curve in single bucket, create fake group make appear each of these windows own group "bucket".
you start defining dimension, helper maps (you need onto linear scale, need convert windows numbers of seconds), , helper dimension can use filtering in event want this:
var rmmdim = facts.dimension(function(d) { return true; }); var timemap = { "w15sec": 15, "w30sec": 30, "w1min": 60, "w2min": 120, "w5min": 300, "w10min": 600, "w20min": 1200, "w40min": 2400, "w1hr": 3600 } var timearray = ["w15sec","w30sec","w1min","w2min","w5min","w10min","w20min","w40min","w1hr"].map((d) => timemap[d]) var rmmfilterdim = facts.dimension(function(d) { return timearray; }, true)
you create group using reductio, calculating max each window:
var rmmgroup = rmmdim.group(); var reducer = reductio() reducer.value('w15sec') .max((d) => { return d.w15sec; }) reducer.value('w30sec') .max((d) => { return d.w30sec; }) reducer.value('w1min') .max((d) => { return d.w1min; }) reducer.value('w2min') .max((d) => { return d.w2min; }) reducer.value('w5min') .max((d) => { return d.w5min; }) reducer.value('w10min') .max((d) => { return d.w10min; }) reducer.value('w20min') .max((d) => { return d.w20min; }) reducer.value('w40min') .max((d) => { return d.w40min; }) reducer.value('w1hr') .max((d) => { return d.w1hr; }) reducer(rmmgroup)
and create fake group. need both top
, all
because line chart requires them reason:
var fakegroup = { all: function() { return ["w15sec","w30sec","w1min","w2min","w5min","w10min","w20min","w40min","w1hr"].map((d) => { return { key: timemap[d], value: rmmgroup.top(infinity)[0].value[d] } }) }, top: function() { return ["w15sec","w30sec","w1min","w2min","w5min","w10min","w20min","w40min","w1hr"].map((d) => { return { key: timemap[d], value: rmmgroup.top(infinity)[0].value[d] } }) } }
then can use fake group in power distribution chart:
pwrdistchart .width(960) .height(150) .margins({ top: 10, right: 10, bottom: 20, left: 40 }) .dimension(rmmfilterdim) .group(fakegroup) .valueaccessor((d) => { return d.value.max }) .transitionduration(500) .x(d3.scale.linear().domain([0,3600])) .elasticy(true)
here updated version of fiddle of working: http://jsfiddle.net/fb3wsyck/5/
Comments
Post a Comment