This example shows the how to create a legend for vector layers.
The js is not minified so it is readable. See vector-legend.js.