How to sort Opps in Chart by StageName as per order in Sales Process

Hi Pat, I’ve tried adding this snippet to a chart that’s using an Opportunity Line Item aggregate model. I get the error

  1. There was a problem rendering a component of type skuidvis__chart: Cannot read property ‘picklistEntries’ of undefined

Here is the javascript, can you see anything wrong with what I’ve written?

// this snippet has been created in order to sort the series in the chart by the stage order
// in the sales process assigned to the Record Type

var chartObj = arguments[0],
    Opportunities = skuid.$M(‘PopularProductsThisFY’),
StageName,
sortedSeries= ,
$ = skuid.$;

// to get the sort order, we need to find the Stage field metadata in the Opportunities models list of fields
$.each(Opportunities.fields, function(f,field){
    if (field.id === “OpportunityStageName”){
        StageName = field;
        return false;
    }
});

// now use this field to create a temporary array to replace the current series
// loop through entries in order to then find the corresponding series by the same name
// in order to add it to the temporary array
$.each(StageName.picklistEntries, function(p,entry){
    $.each(chartObj.series, function(s,series){
        if (series.name === entry.label){
            sortedSeries.push(series);
        }
    });
});

// empty original series
chartObj.series.length = 0;

// insert sorted chart
$.each(sortedSeries,function(ss,sseries){
    chartObj.series.push(sseries);
});