There are probably a half-dozen or more ways to do this. Can you supply us with whatever code you have and some sample data?
Have a look at
for some suggestions.