Search code examples
delphiteechart

Draw a marker on each new point on teechart


I am developing an application on Embarcadero XE where i receive real time data from the ethernet port and display on a teechart chart on the screen.

The application works like an osciloscope, that is, there is a time window (10 seconds for example) of data that the chart displays, and each new incoming point overwrites what already is on screen.

I would like your help to make a code that puts a marker on only the newest point added, so the user can keep track of which of the points on the screen is the most recent point. I don´t want all of the points with a marker, i want only the newest.

The series being used is a fastline.

Here is the code i'm using to add data to the chart:

//Delete already existing point
if (Oscilografia.Series[0].Count>1) then
begin
Oscilografia.Series[0].Delete(cont);
end;
//Write point
Oscilografia.Series[0].addxy(cont,data, '', clblue);

Solution

  • You have several options. The simplest is to make a new TPointSeries to display the current point. If you wish to not show this series in the legend then simply set :

     Oscilografia.Series[n].ShowInLegend := false;
    

    where n is the index of the series you wish to exclude from the legend.

    Alternatively, you can custom-draw any relevant items in the OnAfterDraw handler. For example :

    procedure TForm1.Chart1AfterDraw(Sender: TObject);
    var
      xPos, yPos : integer;
    begin
      Chart1.Canvas.Pen.Color := clRed;
      Chart1.Canvas.Pen.Style := psSolid;
      Chart1.Canvas.Pen.Width := 1;
      Chart1.Canvas.Pen.Mode := pmCopy;
    
      xPos := Chart1.BottomAxis.CalcPosValue(CurrentXValue);
      yPos := Chart1.LeftAxis.CalcPosValue(CurrentYValue);
    
      // Parameters are 
      //  X-Coord, Y-Coord, X-Radius, Y-Radius, Start Angle, End Angle, Hole%
      Chart1.Canvas.Donut(xPos, yPos, 3, 3, 0, 360, 0);
    end;
    

    This produces, for example :

    enter image description here

    Custom drawing lets you do other things also, like add markers, etc. For example :

    procedure TForm1.Chart1AfterDraw(Sender: TObject);
    var
      xPos, yPos : integer;
      yMax, yMin : integer;
    begin
      Chart1.Canvas.Pen.Color := clRed;
      Chart1.Canvas.Pen.Style := psSolid;
      Chart1.Canvas.Pen.Width := 1;
      Chart1.Canvas.Pen.Mode := pmCopy;
    
      xPos := Chart1.BottomAxis.CalcPosValue(CurrentXValue);
      yPos := Chart1.LeftAxis.CalcPosValue(CurrentYValue);
    
      Chart1.Canvas.Donut(xPos, yPos, 3, 3, 0, 360, 0);
    
      Chart1.Canvas.Pen.Color := clGreen;
      Chart1.Canvas.Pen.Style := psDash;
    
      yMax := Chart1.LeftAxis.CalcPosValue(Chart1.LeftAxis.Maximum);
      yMin := Chart1.LeftAxis.CalcPosValue(Chart1.LeftAxis.Minimum);
      Chart1.Canvas.DoVertLine(xPos, yMax, yMin);
    end;
    

    Which gives a dashed vertical line that follows the current point :

    enter image description here

    Note that the CalcPosValue function is exposed by the chart axes and allows you to translate a point in the axis-space to an integer (screen) coordinate in the chart's canvas space.