The rules say
You must move at least [...] and may move up to [...] directly toward the designated opponent. [...]
[...] You must move to the closest space from which you can attack the opponent.
(Movement During a Charge)
Ride-by Attack doesn’t change this—Ride-by Attack allows you to continue moving after a charge, but you still have to execute the charge by following all the usual rules first, and that includes this one.
There is a bit of ambiguity on what “directly toward” means here, but my view here is that the later rules about moving to the closest square from which you can attack the opponent is the rules’ definition of “directly toward.” It’s possible to use other definitions, but 1. that involves speculation, since we don’t have any more to go on, and 2. the most “obvious” definition available, in my mind, makes Ride-by Attack useless,1 which explains why they felt the need to “expand upon” that phrase with the more detailed rule in the next paragraph. So I basically rule that as long as the “closest square” rule is obeyed, you are moving “directly toward” and we don’t have to worry about that any longer.
Which means that with the cavalier starting at \$\text S\$, and the enemy being at \$\text X\$, there is a limited number of squares the cavalier can move to and still have it count as a charge—he must choose the closest square from which he can reach the target (or choose one of the closest squares, if there is a tie). The square \$\text A\$ in your diagram is not that square.
The lance has 10-ft. reach, so the cavalier must move to the square 10 feet from \$\text X\$ that is closest to \$\text S\$. In this diagram, the top number is the distance the cavalier travels, while the bottom number is the distance from \$\text X\$. Importantly, 10-ft. reach is slightly different from 10-ft. movement, so the bottom number is going to be in terms of reach, not movement. So we need the square with a 10 on the bottom and the lowest number on top:
\begin{array}{c|c}
\phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} \\ \hline
\text S & ^{\phantom{0}5}_{30} & ^{10}_{25} & ^{15}_{20} & ^{20}_{10\rlap{^*}} & ^{25}_{10} & ^{30}_{10} \\ \hline
^{\phantom{0}5}_{30} & ^{\phantom{0}5}_{25} & ^{10}_{20} & ^{15}_{15} & ^{20}_{10} & ^{25}_{\phantom{0}5} & ^{30}_{\phantom{0}5} \\ \hline
^{10}_{30} & ^{10}_{25} & ^{15}_{20} & ^{20}_{15} & ^{25}_{10} & ^{30}_{\phantom{0}5} & \text X \\ \hline
\\
\end{array}
\$^*\$ indicates the square that is different for a reach weapon than movement.
As you can see from this, there are five squares from which you can attack \$\text X\$ with a 10-ft. reach weapon:
\begin{array}{c|c}
\phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} \\ \hline
\text S &&&& ^{20}_{10\rlap{^*}} & ^{25}_{10} & ^{30}_{10} \\ \hline
&&&& ^{20}_{10} \\ \hline
&&&& ^{25}_{10} && \text X \\ \hline
\\
\end{array}
And two of these are tied for being closest to \$\text S\$:
\begin{array}{c|c}
\phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} \\ \hline
\text S &&&& ^{20}_{10\rlap{^*}} & & \text A \\ \hline
&&&& ^{20}_{10} \\ \hline
&&&&&& \text X \\ \hline
\\
\end{array}
So the cavalier could not have attacked from \$\text A\$, which was 30 feet away from his initial position, since he had options that were 20 feet away. He can, however, choose either of these squares, which means the upper one—two squares to the left of \$\text A\$—was an option, which would have allowed him to continue moving, in a straight line, to \$\text E\$, like so:
\begin{array}{c|c}
\phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} & \phantom{\rightarrow} \\ \hline
\text S & \rightarrow & \rightarrow & \rightarrow & \text A' & \rightarrow & \rightarrow ^\llap{\text A\;} & \rightarrow & \rightarrow & \rightarrow & \text E \\ \hline
\\ \hline
&&&&&& \text X \\ \hline
\\
\end{array}
Where \$\text A'\$ is where the cavalier must attack from instead of \$\text A\$, which he now passes through on his way to \$\text{E}\$, as indicated by \$\rightarrow^\llap{\text{A}\;}\$.
In other words, this was very slightly off, but the final result was one totally allowed by the rules. (Unless there was some special reason why attacking from \$\text A'\$ would be different from attacking from \$\text A\$—an area effect that includes one but not the other and enhances or interferes with the attack, maybe?)
- Pathfinder inherits these rules from D&D 3.5e, which used identical wording, and the question of how these rules work was raised in the D&D 3.5e FAQ. Please note the myriad problems with that FAQ, but nonetheless, that entry may be worth considering. Unfortunately, in my view, it’s a very weird answer—the question asserts that, with these rules, “Ride-By Attack is now pretty much useless,” to which the response is “you’ve got it about right.” The answer goes on to reiterate the rules—avoiding the phrase “directly toward” and emphasizing the closest square, much as I have—which seems like a strange thing to do if your conclusion is that those rules are useless. But it’s stranger still to answer a question asserting something is useless, and not emphatically contradict that if you don’t think it’s useless. Anyway, my primary take-away from it is that the result itself—that Ride-by Attack is “pretty much useless”—is a strong sign that the questioner’s interpretation of the rules isn’t the best one. Hence choosing to interpret “directly toward” as simply “in the manner we describe below.”