Generalized histogram function.