When the magnitude and direction of a force varies with position, the work done by such a force for an infinite simal displacement is given by

The total work done in going from A to B is W

Area under force displacement curve with proper algebraic sign represents work done by the force.