Is there a correct way to add an operation to chained instructions?

I would like to be able to add some code between the .data and the .entry.

I want to look the data generated by the pie() function, to check if the startAngle and endAngle are greater than 45 degrees and less than 135 degrees, so they start and end can be swapped so the arcs are draw reversed, so the text is not upside down when it draws to the arcs path.

I can do it in a very hack way like I did here when I added centroid code to the attribute ‘id’ code. But the code gets messy / confusing to read.

I hope that it’s a simple as adding a function to the .data()

const g = pieGroup.selectAll('.arc')
			.data(pie(teTracks[trackNumber].sectors))							// iterates over the number of sectors in each track

      // add class names to define quadrant and zone.
			.attr('class',  (d, idx) => `${teTracks[trackNumber].sectorsQuadrant[idx]} ${zoneName[teTracks[trackNumber].zone]}`) 
			.attr('id', (d, idx) => {		 											  // add an id to each sector so we can address it.
             // >>  "hacky mc hack face", way to get the center point of each sector 
 				return `sector${idx}_${trackNumber}`

I kind of rubber ducked this bug by asking the question.

Yes. .data() can have a function

  .data(d => {
        let flippy = pie(teTracks[trackNumber].sectors)
        if(textRings.includes(trackNumber))  {
          let temp = flippy[1].startAngle
          flippy[1].startAngle = flippy[1].endAngle
          flippy[1].endAngle = temp

          temp = flippy[2].startAngle
          flippy[2].startAngle = flippy[2].endAngle
          flippy[2].endAngle = temp
        return flippy

I didn't need to check angles as I am only dealing with quarters.

The function argument for .data() is intended for groups (nested selections):

The data is specified for each group in the selection. If the selection has multiple groups (such as d3.selectAll followed by selection.selectAll), then data should typically be specified as a function. This function will be evaluated for each group in order, being passed the group’s parent datum (d , which may be undefined), the group index (i ), and the selection’s parent nodes (nodes ), with this as the group’s parent element.

You’re not using any of the arguments that are passed to the callback, so there isn’t much of a point in calling data() this way. I recommend you preprocess your data in a prior step, and then pass the cleaned up data in, e.g.:

    .filter(/* remove some items */)
    .map(/* change some items */)

The original .data was .data(pie(teTracks[trackNumber].sectors)
And I needed to flip some of the results of that is why I wrote it like that.
yes, I should have written .data( () => {}) as I was not using that variable.

I didn’t think of moving the pie() function into the data preprocessing.

Thanks for the useful link and tip.