Stacked Bar Chart Issue

Hi!

So I’m creating a stacked bar chart using plotly.js, everything is displaying correctly except that my foreach statement is not working correctly.

For some reason, it’s just showing the second value “SegmentTwo”

What am I doing wrong? I think I’ve been staring at this for too long and I can’t figure it out. Any help is very much appreciated it!

Code:

function getQuarterData(fiscalYear, itemTypeID, divID) {
//Draws Stacked Bar chart
return $.ajax({
    type: "GET",
    url: window.applicationBaseUrl + 'api/QuarterReport/GetChartingData',
    datatype: JSON,
    data: {
        fiscalYear: fiscalYear,
        itemTypeID: itemTypeID
    },
    error: function (textStatus, errorThrown) {
        console.log("There was an issue loading the data. - " + 
errorThrown);
        console.log(textStatus);
        alert("Looks like there was an issue loading the data!");
    },
    success: function (result) {
        var fiscalYear = result.Data.FiscalYear % 2000;
        var stringFormat = result.Data.StringFormat;
        //Here it loops throughout the segment list..
        result.Data.SegmentList.forEach(function (segment) {  // ---> The 
        //issue is here..it's displaying only the one segment and not the 
        //two other remaining ones          
            var xAxis = [];
            var yAxis = [];
            segment.PreviousFY.forEach(function (quarter) {          
                xAxis.push("FY" + (fiscalYear - 1) + "-Q" + 
quarter.Quarter);
                yAxis.push(quarter.Value);
            });

            segment.CurrentFY.forEach(function (quarter) {
                xAxis.push("FY" + fiscalYear + "-Q" + quarter.Quarter);
                yAxis.push(quarter.Value);
        });


            // Create array of formatted values for text (not taken care 
 by tickformatting)
            var labels = [];
            yAxis.forEach(function (yValue) {
                labels.push(d3FormatConvert(stringFormat, yValue));
            });

            var trace1 = {
                x: xAxis,
                y: yAxis,
                type: 'bar',
                name: segment.SegmentName,
                text: labels,
                textposition: 'auto',
                hoverinfo: 'none',
                marker: {
                    color: segment.PrimaryColor
                }
            };


            var trace2 = {
               x: xAxis,
                y: yAxis,
                type: 'bar',
                name: segment.SegmentName,
                text: labels,
                textposition: 'auto',
                hoverinfo: 'none',
                marker: {
                    color: segment.PrimaryColor
                }
            };

            var trace3 = {
                x: xAxis,
                y: yAxis,
                type: 'bar',
                name: segment.SegmentName,
                text: labels,
                textposition: 'auto',
                hoverinfo: 'none',
                marker: {
                    color: segment.PrimaryColor}
            };

            // traces
            data = [trace1, trace2, trace3];

            var layout = {
                title: segment.SegmentName,
                //showlegend: false,
                height: 200,
                margin: {
                    l: 50,
                    r: 10,
                    b: 30,
                    t: 40,
                    pad: 10
                },
                xaxis: {
                    tickfont: {
                        size: 11,
                        color: 'black'
                    }
                },
                yaxis: {
                    tickformat: d3Format(stringFormat)
                },
                barmode: 'stack',
                annotations: [{
                    x: " ",
                    y: segment.BlueGoal,
                    xref: 'x',
                    yref: 'y',
                    text: d3FormatConvert(stringFormat, 
segment.BlueGoal),
                    showarrow: true,
                    arrowcolor: "red",
                    font: {
                        size: 11
                    },
                    arrowhead: 2,
                    ax: 30,
                    ay: 0
                }]
            };

            // Plot chart
            Plotly.newPlot(divID, data, layout, { displayModeBar: false 
});
        });

    }
});

}


Output:

Desire Output: