Right now I am using the renderlet function of one of my charts on the page to calculate summary totals of the filtered data. in this PEN Year Over Year Charts
saleschart.renderlet(function (chart) {
// rotate x-axis labels
chart.selectAll('g.x text')
.attr('transform', 'translate(10,10) rotate(-45)')
.style('font-weight',"bold")
.style('font-size',"12px")
.style('text-anchor',"end")
console.log("renderlet start")
currentData = branchDimension.top(Infinity)
// console.log(currentData)
totals = alasql("select sum(sales) as sales,sum(grossprofit) as gp,sum(variableExp) as varexp,sum(personnelExp) as persexp,sum(semiFixedExp) as sfexp,sum(fixedExpense) as fixedexp, sum(np) as np from ?",[currentData])
console.log(totals[0])
$("#salestotal").html( "<span>Sales :</span><span>"+dollarformat(totals[0].sales)+"</span>")
$("#gptotal").html( "<span>gp :</span><span>"+dollarformat(totals[0].gp)+"</span>")
$("#varexptotal").html( "<span>varExp :</span><span>"+dollarformat(totals[0].varexp)+"</span>")
$("#persexptotal").html( "<span>persExp :</span><span>"+dollarformat(totals[0].persexp)+"</span>")
$("#sfexptotal").html( "<span>sfExp :</span><span>"+dollarformat(totals[0].sfexp)+"</span>")
$("#fixedexptotal").html("<span>fixedExp :</span><span>"+dollarformat(totals[0].fixedexp)+"</span>")
$("#nptotal").html( "<span>Net profit:</span><span>"+dollarformat(totals[0].np)+"</span>")
})
versus something like this Is there a way to attach callback what fires whenever a crossfilter dimension filter changes?
which I suppose would be a better way to go if I was actually using the filtering ability on the charts which I am not doing (Yet).
The question is how do i keep from calculating my totals more than once (aka not on ever chart filtering only the first) using the second method
Is there a better way to do this where I can easily get the current filtered data (i plan to rewrite in typescript eventually) and only calculate the totals once?
I know I could do all of this with ALASQL but I like the fact that I can attach crossfilter dimensions to dropdowns and the data is just filtered and I don't have to start building sql strings on the fly (not that it is hard I have done it pleny of times but crossfilter has the capacity to eliminate one level of that pretty easily)
so basically the real question is when and where is it safe to do something like this
currentData = branchDimension.top(Infinity)
and know that I have the filtered data. From experimenting it seems to already be filtered in the renderlet function of the chart and the onChange of the dropdown but I can't tell if I am just lucky or my small number of rows is causing the behavior (gets done filtering before code in on change executes async).