11393

dc.js calculate averages with multi column rowchart

Question:

I am using working with the code from <a href="https://stackoverflow.com/questions/24737277/dc-js-how-to-create-a-row-chart-from-multiple-columns" rel="nofollow">How to create a row chart from multiple columns</a>.

However I need to get the averages of the data. I normally do this via value accessor but am unsure of how to do this with this functionality.

function regroup(dim, cols) { var _groupAll = dim.groupAll().reduce( function(p, v) { // add cols.forEach(function(c) { p[c] += v[c]; }); return p; }, function(p, v) { // remove cols.forEach(function(c) { p[c] -= v[c]; }); return p; }, function() { // init var p = {}; cols.forEach(function(c) { p[c] = 0; }); return p; }); return { all: function() { // or _.pairs, anything to turn the object into an array return d3.map(_groupAll.value()).entries(); } }; }

I just need to be able to get the current sum of each column and divide it by the count of the rows based on the current filter state.

The code is similar to the one in this fiddle <a href="http://jsfiddle.net/gordonwoodhull/37uET/6/" rel="nofollow">multi column fiddle</a>

Answer1:

It seems that the easiest way to get the count of records is to create another crossfilter.groupAll() with the default reduction:

var _recordCount = dim.groupAll();

Then you can divide each sum by the current count when rotating the results:

all: function() { var count = _recordCount.value(); // or _.pairs, anything to turn the object into an array return d3.map(_groupAll.value()) .entries().map(function(kv) { return {key: kv.key, value: kv.value / count}; }); }

Here's the updated fiddle: <a href="http://jsfiddle.net/37uET/32/" rel="nofollow">http://jsfiddle.net/37uET/32/</a>

Recommend

  • How can I use 2 range sliders at the same time?
  • Referring to individual variables in … with dplyr quos
  • How to insert an Image in WORD after a bookmark using OpenXML
  • Hadoop shuffle uses which protocol?
  • finding greatest prime factor using recursion in c
  • Refresh other frame, from another frame. Jquery
  • Imageloader not loading image on real device
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • XSLT foreach repeating nodes to flat
  • Symfony 2. CSRF token is invalid
  • Make new pandas columns based on pipe-delimited column with possible repeats
  • List images(01.png) and descriptions(01.txt) from directory
  • How to assign byte[] as a pointer in C#
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Appending Character to Character Array In C
  • PHP CURL timing out but CLI CURL works
  • Declaring variable dynamically in VB.net
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • Jackson Parser: ignore deserializing for type mismatch
  • D3 nodes and links from JSON with nested arrays of children
  • Initializer list vs. initialization method
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Date difference with leap year
  • Adding custom controls to a full screen movie
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Benchmarking RAM performance - UWP and C#
  • using HTMLImports.whenReady not working in chrome
  • Angular 2 constructor injection vs direct access
  • -fvisibility=hidden not passed by compiler for Debug builds
  • How do I configure my settings file to work with unit tests?
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • How can I remove ASP.NET Designer.cs files?
  • Is it possible to post an object from jquery to bottle.py?
  • EntityFramework adding new object to nested object collection
  • Sorting a 2D array using the second column C++
  • UserPrincipal.Current returns apppool on IIS