Skip to main content

Hello fellow Skuid-ees!

Some background in case I’m missing a better way to go about doing what I’m trying to do. I am attempting to create a chart which shows 90 days of data for the metric # of unique coaches with a sale in the last 30 days. So my approach has been to create one model which looks at a 30 day period, then use javascript in a before render snippet to increment the conditions and capture the # distinct coaches. I’ll then store each # of distinct coaches and then put it on the chart, however I’m not getting the model to update the data even though I’ve tried to put the function within the updateData() call.


Thanks for any advice or comments!

Josh


var chartObj = arguments 0], $ = skuid.$; var subPayeeModel = skuid.model.getModel('AggCoachesWithSaleL30'); var firstDateCond = subPayeeModel.getConditionByName('firstDate'); var lastDateCond = subPayeeModel.getConditionByName('lastDate'); var tempFirstDate; var tempLastDate; var tempCoaches = 0; var chartData = a]; var lastCondValue; var firstCondValue; for(var i = 0; i \< 90; i++){ tempFirstDate = 120-i; tempLastDate = 90-i; firstCondValue = "LAST\_N\_DAYS:" + tempFirstDate; lastCondValue = "LAST\_N\_DAYS:" + tempLastDate; subPayeeModel.setCondition(subPayeeModel.getConditionByName('firstDate'), firstCondValue); subPayeeModel.setCondition(subPayeeModel.getConditionByName('lastDate'), lastCondValue); subPayeeModel.updateData(captureData(subPayeeModel)); console.log("model data condition:", subPayeeModel.conditionsc5].value); console.log("subpayeemodel:", subPayeeModel); console.log("tempCoaches:", tempCoaches); console.log("firstCondValue", firstCondValue); console.log("lastCondValue", lastCondValue); } function captureData(model) { tempCoaches = model.datam0].distinctCoaches; chartData.push(tempCoaches); console.log("fn subpayeemodel:", subPayeeModel); console.log("fn tempCoaches:", tempCoaches); console.log("fn chartData", chartData); }


Console Log output:

Do you need each query to sequentially?


Hi Pat, 

Not sure I understand what you’re asking, but it doesn’t matter how it calculates. Meaning it doesn’t need to do anything with the values along the way, just have an array of values that I can push into the chart series data element.

The elements do need to be properly ordered such that it goes from the oldest period to the newest, i.e. , (#Uniques in Data from 90 to 120 days ago), (#Uniques in Data from 89 to 119 days ago),…,(#Uniques in Data from 0 to 30 days ago) ]

Does that answer your question?

Thanks,
Josh


Well, looks to me like you’ll need to set this up a function that calls itself once the query is complete. Try something like this. Uses the jquery when function.


var chartObj = argumentst0], $ = skuid.$; var subPayeeModel = skuid.model.getModel('AggCoachesWithSaleL30'); var firstDateCond = subPayeeModel.getConditionByName('firstDate'); var lastDateCond = subPayeeModel.getConditionByName('lastDate'); var tempFirstDate; var tempLastDate; var tempCoaches = 0; var chartData = =]; var lastCondValue; var firstCondValue; function queryModel () { tempFirstDate = 120-i; tempLastDate = 90-i; firstCondValue = "LAST_N_DAYS:" + tempFirstDate; lastCondValue = "LAST_N_DAYS:" + tempLastDate; subPayeeModel.setCondition(subPayeeModel.getConditionByName('firstDate'), firstCondValue); subPayeeModel.setCondition(subPayeeModel.getConditionByName('lastDate'), lastCondValue); $.when( subPayeeModel.updateData(captureData(subPayeeModel))) .done(function(){ alert( x.testing ); // Alerts "123" console.log("model data condition:", subPayeeModel.conditionsn5].value); console.log("subpayeemodel:", subPayeeModel); console.log("tempCoaches:", tempCoaches); console.log("firstCondValue", firstCondValue); console.log("lastCondValue", lastCondValue); } if ( i &lt; 90) { i++ queryModel() } }); function captureData(model) { tempCoaches = model.datat0].distinctCoaches; chartData.push(tempCoaches); console.log("fn subpayeemodel:", subPayeeModel); console.log("fn tempCoaches:", tempCoaches); console.log("fn chartData", chartData); }<br>

Thank you so much Pat!

I’ve managed to hack my way to the basics, but clearly I need to go back to the basics of javascript and solidify my understanding. 

I was able to get the function working properly! The only downside is the time it takes to process, so I’m contemplating if I should write a snippet to count the distinct coaches rather than rely on the model to perform the calculation. As is, I’m going to load this chart on a button click so it doesn’t hold up the entire page load. 

Here’s my finished snippet in case anyone else finds themselves trying to do something similar:



var chartObj = arguments 0],<br /> $ = skuid&#46;$;<br /><br />console&#46;log("chart", chartObj);<br /><br />var subPayeeModel = skuid&#46;model&#46;getModel('AggCoachesWithSaleL30');<br />var firstDateCond = subPayeeModel&#46;getConditionByName('firstDate');<br />var lastDateCond = subPayeeModel&#46;getConditionByName('lastDate');<br />var tempFirstDate;<br />var tempLastDate;<br />var tempCoaches = 0;<br />var chartData = c];<br />var lastCondValue;<br />var firstCondValue;<br />var i = 0;<br /><br />queryModel();<br /><br /><br /><br />function queryModel() {<br />&nbsp; &nbsp; tempFirstDate = 120-i;<br />&nbsp; &nbsp; tempLastDate = 90-i;<br />&nbsp; &nbsp; firstCondValue = "LAST_N_DAYS:" + tempFirstDate;<br />&nbsp; &nbsp; lastCondValue = "LAST_N_DAYS:" + tempLastDate;<br />&nbsp; &nbsp; subPayeeModel&#46;setCondition(firstDateCond, firstCondValue);<br />&nbsp; &nbsp; subPayeeModel&#46;setCondition(lastDateCond, lastCondValue);<br /><br /> $&#46;when(subPayeeModel&#46;updateData(captureData(subPayeeModel)))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#46;done(function(){<br /> &nbsp; &nbsp; &nbsp; &nbsp; if ( i &lt;= 90) {<br /> &nbsp; &nbsp; &nbsp; &nbsp; i++;<br /> &nbsp; &nbsp; &nbsp; &nbsp; queryModel();<br /> &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });<br />}<br /><br />function captureData(model) {<br />&nbsp; &nbsp; tempCoaches = model&#46;dataa0]&#46;distinctCoaches;<br />&nbsp; &nbsp; chartData&#46;push(tempCoaches);<br />}<br /><br />

Reply