Mostly a fork from richardolsson's answer but this is a more generic form for people who want to achieve this in general and fixes the bug if your line goes straight down (ie start and end point have the same x). The function will stand-alone.
//import flash.geom.Point;
//import flash.display.Shape;
var sp:Point=new Point(10,10); //some starting point - can be anywhere
var ep:Point=new Point(250,250);// some end point - can be anywhere
var myLine:Shape=gradientLine(sp,ep,0xFF0000,0x0000FF,0x00FF00,0xFFFF00); // put in as many colours as you want, the function will evenly space them out
addChild(myLine);
function gradientLine(startPoint:Point,endPoint:Point,...colours):Shape
/*GRADIENT LINE - returns a line from startPoint to endPoint with even gradient of colours*/
{
/*Create matrix - gradient box*/
var d:Point=startPoint.subtract(endPoint);
d.x=Math.abs(d.x);
d.y=Math.abs(d.y);
if(d.x==0)d.x=1; /*corrects for lines going straight down*/
var matrix:Matrix=new Matrix;
matrix.createGradientBox(d.x,d.y,Math.atan2(d.y,d.x),startPoint.x,startPoint.y);
/*Create/populate array of ratios and alphas*/
var l:int=colours.length;
var alphas:Array=new Array();
var ratios:Array=new Array();
for(var i:int=0;i<l;i++)
{
alphas.push(1);
ratios.push((0xFF/l)*i+1); /*evenly spreads ratios of chosen colours*/
}
/*Create shape*/
var s:Shape=new Shape;
s.graphics.lineStyle(2);
s.graphics.lineGradientStyle(GradientType.LINEAR,colours,alphas,ratios,matrix);
s.graphics.moveTo(startPoint.x,startPoint.y);
s.graphics.lineTo(endPoint.x,endPoint.y);
return(s);
}