Reduce function in dc.js

I am new to the dc.js library and wanted to do the crossfilter calculation below on group method of my geochoropleth chart. I am pretty sure there is some function I could pass to the reduce method of group .

I have the following data in DATA.csv (the first row contains column names):

BUDGET,GID,MDIS,USPRO,TYPE,FILEURL,RVID,VERDATE,VERSTAT,SCORE
10428,ALI-G-006,Aliabad,Kunduz,Hard,/uploadedfiles/reports/SIKA North/136-SIKA-North-ALI-G-006.pdf,0,19-08-2014,2,0
24853,ALI-G-008,Aliabad,Kunduz,Hard,/uploadedfiles/reports/SIKA North/561-SIKA-North-ALI-G-008.pdf,0,19-08-2014,0
24831,ALI-G-019,Aliabad,Kunduz,Hard,/uploadedfiles/reports/SIKA North/987-SIKA-North-ALI-G-019.pdf,0,18-08-2014,2,0
24771,IMA-G-017,Imam Sahib,Kunduz,Hard,/uploadedfiles/reports/SIKA North/557-SIKA-North- IMA-G-017.pdf,0,28-08-2014,2,1
21818,IMA-G-019,Imam Sahib,Kunduz,Hard,/uploadedfiles/reports/SIKA North/992-SIKA-North-IMA-G-019.pdf,0,27-08-2014,2,0
12266,KHA-G-007,Khanabad,Kunduz,Hard,/uploadedfiles/reports/SIKA North/583-SIKA-North - KHA-G-007.pdf,0,7/9/2014,1,0
23148,KUN-G-002,Kunduz,Kunduz,Hard,/uploadedfiles/reports/SIKA North/909-SIKA-North - KUN-G-002.pdf,0,1/9/2014,2,0
54584,KUN-G-004,Kunduz,Kunduz,Hard,/uploadedfiles/reports/SIKA North/702-SIKA-North - KUN-G-004 20140709.pdf,0,9/7/2014,1,0
24544,PUL-G-001,Pul-e Khumri,Baghlan,Hard,/uploadedfiles/reports/SIKA North/599-SIKA-North - PUL-G-001 - 20140623.pdf,0,6/7/2014,2,1
40149,SSKDAG046,Arghandab (1),Kandahar,Hard,/uploadedfiles/reports/SIKA South/239-SIKA-South-SSKDAG046.pdf,0,12/9/2014,0,0.625
39452,0003 LGR MAG,Muhammad Aghah,Logar,Hard,/uploadedfiles/reports/SIKA East/792-SIKA-East - 0003 LGR MAG - 20140610.pdf,0,10/6/2014,2,0.7
58298,0013 LGR MAG,Muhammad Aghah,Logar,Hard,/uploadedfiles/reports/SIKA East/591-SIKA-East - 0013 LGR MAG 20140601.pdf,0,1/6/2014,2,0]        

Below is the dimension and group for my chart:

var facts = crossfilter(data);
var scoref = facts.dimension(function (d) { return d.district;});

var scoreg = scoref.group().reduceSum(function(d){return d.score;});

The d.score field's value is calculated using the code below with PHP:

$tempsql = $dbase->query('select "VERMDIS", COUNT(*) AS TOTAL, SUM("VERSTAT") AS SAM FROM mt_fver GROUP BY "VERMDIS"');

while ($r = pg_fetch_array($tempsql)) {
    $dist = $r['VERMDIS'];
    $score = $r[2] / (2 * $r[1]);   
    $disxx[$dist] = $score;
}

What I would like to achieve is to do the same calculation using group().reduce(function (p,v) { /* ... */ }) from the dc.js library while grouping the values by district names.


What results/errors are you getting? Looks like pretty much the right idea, but take into account:

  • if you're using d3.csv() , it will return the values as strings, so you'll need either to preprocess your data, or use +d.score to convert the values while reading them
  • the field may come out as d.SCORE depending how you are reading the CSV in.
  • you may need to adapt the reduce function to suit your calculation
  • If you put a line break in the function you pass to reduce, you can set a breakpoint there using the browser's debugger and experiment on the console to figure out what expression works for what you need.

    链接地址: http://www.djcxy.com/p/32734.html

    上一篇: 在流星中制作dc.js图

    下一篇: 减少dc.js中的函数