0

I have a composite chart in dc.js and trying to add a line calculated against existing groups:

Basically I'm trying to get the diff of Actuals vs Forecast (rDiffPct) as a percentage as follows: (Actuals - Forecast) / Forecast but getting either Nan or in,fin,ity.

var dateDim = ndx.dimension(function (d) { return d.date; });
var rHistByDate = dateDim.group().reduceSum(function (d) { return d.Hist ; }) // Forecast
var rFcstByDate = dateDim.group().reduceSum(function (d) { return d.Fcst ; }) // Actuals
var rDiff = dateDim.group().reduceSum(function (d) { return d.Hist - d.Fcst; }) // Difference From Forecast

var rDiffPct = dateDim.group().reduceSum(function (d) { return Math.round((d.Hist - d.Fcst) / d.Fcst); });

Both rHistByDate and rFcstByDate render ok.

The Chart renders fine with other results but not with rDiffPct. Chart uses the following:

.dimension(dateDim)
.group(rHistByDate)
dc.lineChart(rViz_Main2b).group(rDiffPct,"Variation - hist-fcst").useRightYAxis(false).colors('#2d00b3').dashStyle([5,5]).renderDataPoints({radius: 3,fillOpacity: 0.8,strokeOpacity: 0.8})

I'm obviously missing something or going about it the wrong way.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
stonk
  • 305
  • 2
  • 3
  • 15
  • What is it doing wrong? – Adam Apr 06 '17 at 16:14
  • It plots the line and shows across the xUnits ok but the values are all "In,fin,ity". If I change the last part of the equation from **"/ d.Fcst**" to **"/ 100"** then this shows the correct values divided by 100. It doesn't like me using the ***d.Fcst*** – stonk Apr 07 '17 at 08:19
  • I'm not sure what good a sum of percentages will do you. If you are literally getting the string `"In,fin,ity"` that sounds like some extreme string/number typing problems (the string Infinity being formatted as a decimal number, I guess). I guess this is possible if `d.Fcst` is not present or zero, but you say it's working in the other case. Kind of hard to guess what's happening without seeing a running example, sorry! – Gordon Apr 07 '17 at 19:33
  • I think you probably want to do the division last, outside of crossfilter, anyway. The math would make more sense. – Gordon Apr 08 '17 at 10:55
  • Thanks Gordon, I'll set up a fiddle...... – stonk Apr 08 '17 at 21:24
  • Please see this [codepen](http://codepen.io/stutray/pen/VpoMJj?editors=0010) - See line ***89***. if I use "**((d.Hist - d.Fcst) / d.Fcst)**" then I have zeros but if I do "**((d.Hist - d.Fcst) / 100)**" then I get the Hist-Fcst correctly divided by 100 – stonk Apr 10 '17 at 09:31

0 Answers0