You should convert your YValue from axis values to pixels with the axis CalcPosValue
function.
If you want to draw a line at a constant YValue, it would be an horizontal line, not a vertical line.
In the following example I'm drawing an horizontal line at YValue=10.
Note the drawing functions should be called at OnAfterDraw
event or similar to make sure your custom drawings are done after every repaint.
To use OnAfterDraw
event on RAD Studio, select the chart at design-time, navigate to the Events tab at the Object Inspector and double-click on the white cell next to OnAfterDraw
.
This action should open the code view with the cursor inside a new and empty OnAfterDraw function.
Then you can add what you want to do there. Ie, drawing an horizontal line within the ChartRect
, at YValue=10:
void __fastcall TForm1::Chart1AfterDraw(TObject *Sender)
{
Chart1->Canvas->Pen->Color = clRed;
int X0Pos = Chart1->ChartRect.Left;
int X1Pos = X0Pos + Chart1->ChartRect.Width();
double YVal = 10;
int YPos = Chart1->Axes->Left->CalcPosValue(YVal);
Chart1->Canvas->DoHorizLine(X0Pos, X1Pos, YPos);
}