Create programatically a diagram

For cross reference, this has been answered here, too.