Ok, I got it.
Calculate the number of categories:
Categories = NItems(Associative Array (dt:CategoriesColumn));
Set up how many legend items you want:
LegendLevels = 3;
Construct a list that has the same number of elements as how many categories you have, fill it with consecutive numbers for the levels you want to show, and "-1" for the rest:
p = {};
For(i=0, i < Categories, i+=1,
if(i <= (LegendLevels-1), p = Insert(p, i), p = Insert(p, -1))
);
The use this list when scripting your Graph Builder, inside SendToReport():
Dispatch(
{},
"400",
LegendBox,
{Set Title( "Category" ), Legend Position( {5, Matrix(p)} ),
Position( Matrix(p) )}
)