I just added more data in the main array and received the messed up output. But then I added the header columns i.e
$pdf->Cell(30, 5, 'Number of men:');
$pdf->Cell(15, 5, $data['Men'], 0, 0, 'R');
I added above set of code for newly added array values. and then the graph and legends were all good.