Search code examples
c++teechartdiagramming

Using Tchart in c++ to draw a vertical line


I got some values that i need to draw as a vertical line. The line should be from begin till the end of diagramm field.

I use VCLTee.Chart.hpp in Embarcadero. As I know it is the Tchart, that is actually used more for Delphi.

However:

I use this function:

DlgMainWindow->ChartTemperatureCurve->Canvas->DoVertLine(XValue,YValue,ZValue);

i can´t find the description. As I see DoVertLine works with Pixel of the diagramm. But if my YValue = 10 , and should be always parallel to x for whole distance.


Solution

  • 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);
    }