The magnetocrystalline anisotropy energy (MAE) for ferromagnetic and antiferromagnetic freestanding monowires of 4d transition metals is investigated on the basis of first-principles calculations. Across the 4d series, the easy axis of the magnetization oscillates between two directions: perpendicular and along the wire axis. The largest values of the MAE occur at the end of the series. Giant values of 30-60 meV/atom can be obtained upon stretching Ru or Rh wires. Ru and Pd chains change the magnetization direction upon wire stretching, opening new perspectives in controlling the spin-dependent ballistic conductance in these structures.