score:0

Accepted answer

There can be many ways of doing this. One of the simplest is to have multiple color sets (with different number of colors)... and depending on the number of data items (sectors in pie), change the color set. Here is a quick example... change the length of data to test.

DEMO

var colorSet1 = ["#98abc5", "#8a89a6", "#7b6888", "#6b486b"];
var colorSet2 = ["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56"];

var colorSet = data.length%2 == 0 ? colorSet1 : colorSet2;

Code for above taken from D3 examples: https://bl.ocks.org/mbostock/3887235

score:0

You can use something like this to color your slices.

color = d3.scale.ordinal()
               .domain([0,1,2,3])
               .range(["red", "green", "blue", "pink"]);;
color(0);//red
color(1);//green
color(2);//blue    
color(3);//pink
color(4);//red
color(5);//green
...so on //so the slice index can decide the color, using the scale.

EDIT

Make a function like this to ensure no adjacent colors are same.

Here sliceNumber is the index of slice in pie chart. Here maxSlices is the maximum slices in the pie chart

function getMyColor(sliceNumber,maxSlices){
   if ((sliceNumber == maxSlices) && (color(sliceNumber) == color(maxSlices))){
       return color(sliceNumber + 1)
   } else {
       return color(sliceNumber)
   }
}

Related Query

More Query from same tag