refactored the code to avoid repetition in the plotting component