Search code examples
lineteechartseries

Line pieces in Tee chart lite XE5


I'm trying to create this type of graph in Delphi XE5 with teechart lite:

So I have points (x1,y1),(x2,y2), ....,(xk,yk).

Points (x1,y1), (x2,y2) form line piece no. 1.

The second line piece is (x3,y3), (x4,y4) and so on. Note that (x2,y2) is different than (x3,y3) they are not the same point.

With the tee chart lite is it possible to create such a graph in XE5?


Solution

  • There are different options to do this with TeeChart.

    • One series and null points. You can add a null point after each segment so the lines in your series won't be connected. Ie:

      uses Series;
      
      procedure TForm1.FormCreate(Sender: TObject);
      
        procedure AddSegment(Series: TChartSeries; X0, Y0, X1, Y1: Double);
        begin
          Series.AddXY(X0, Y0);
          Series.AddXY(X1, Y1);
          Series.AddNullXY(X1, Y1);
        end;
      
      var fastLine1: TFastLineSeries;
      begin
        Chart1.View3D:=false;
      
        fastLine1:=Chart1.AddSeries(TFastLineSeries) as TFastLineSeries;
        fastLine1.TreatNulls:=tnDontPaint;
      
        AddSegment(fastLine1, 0, 1, 1, 2);
        AddSegment(fastLine1, 2, 1, 3, 0);
        AddSegment(fastLine1, 4, 2, 5, 3);
      end;
      
    • Multiple series.. You can have a series for each segment. Ie:

      uses Series;
      
      procedure TForm1.FormCreate(Sender: TObject);
      
        procedure AddSegment(Chart: TChart; X0, Y0, X1, Y1: Double);
        begin
          with Chart.AddSeries(TFastLineSeries) do
          begin
            AddXY(X0, Y0);
            AddXY(X1, Y1);
      
            Color:=Chart[0].Color;
          end;
        end;
      
      begin
        Chart1.View3D:=false;
      
        AddSegment(Chart1, 0, 1, 1, 2);
        AddSegment(Chart1, 2, 1, 3, 0);
        AddSegment(Chart1, 4, 2, 5, 3);
      end;
      
    • A DrawLineTool. This tool allows you to draw line segments directly with the mouse, or add them by code. Ie:

      uses Series, TeeTools;
      
      procedure TForm1.FormCreate(Sender: TObject);
      
        procedure AddSegment(tool: TDrawLineTool; X0, Y0, X1, Y1: Double);
        begin
          tool.Lines.AddLine(X0, Y0, X1, Y1);
        end;
      
      var drawLineTool1: TDrawLineTool;
      begin
        Chart1.View3D:=false;
      
        Chart1.Axes.Bottom.SetMinMax(0, 5);
        Chart1.Axes.Left.SetMinMax(0, 3);
        Chart1.AddSeries(TFastLineSeries);
      
        drawLineTool1:=Chart1.Tools.Add(TDrawLineTool) as TDrawLineTool;
        AddSegment(drawLineTool1, 0, 1, 1, 2);
        AddSegment(drawLineTool1, 2, 1, 3, 0);
        AddSegment(drawLineTool1, 4, 2, 5, 3);
      end;