Recent

Author Topic: [SOLVED] Sorting an Array for a lineSeries  (Read 2956 times)

donnie

  • Jr. Member
  • **
  • Posts: 72
[SOLVED] Sorting an Array for a lineSeries
« on: January 25, 2017, 01:22:06 pm »
Hi there again,
I have an array with two fields x,y which I call it tpnt.
Code: Pascal  [Select][+][-]
  1. type
  2.    tpnt = record
  3.       x : TFloat;
  4.       y: TFloat;
  5.    end;      
and as I put some values in it is not sorted. The thing is that I must sort it to the x field.
the code I use is simple and after that I draw it as well

Code: Pascal  [Select][+][-]
  1. n := High(PositionArray);
  2.   repeat
  3.     newn := 0;
  4.     for i := 1 to n   do
  5.       begin
  6.         if PositionArray[i-1].x > PositionArray[i].x then
  7.           begin
  8.             tempx := PositionArray[i-1].x;
  9.             tempy := PositionArray[i-1].y;
  10.             PositionArray[i-1].x := PositionArray[i].x;
  11.             PositionArray[i-1].y := PositionArray[i].y;
  12.             PositionArray[i].x:= tempx;
  13.             PositionArray[i].y:= tempy;
  14.             newn := i;
  15.           end;
  16.       end ;
  17.     n := newn;
  18.   until n = 0;
  19.  
  20.  
  21.   for i := Low(PositionArray) to High(PositionArray) do
  22.   begin
  23.     xaxis := PositionArray[i].x;
  24.     yaxis := PositionArray[i].y;
  25.     newlineXTHsorted.AddXY(xaxis, yaxis);
  26.   end;        
  27.  

Is there something in order not to sort the array myself and leave this sorting for the lineseries?
I mean if this can hapen with the tachart itself.
« Last Edit: January 26, 2017, 11:03:08 am by donnie »

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Sorting an Array for a lineSeries
« Reply #1 on: January 25, 2017, 07:33:26 pm »
If you don't want to sort the primary data array it would be best to add all x,y pair to the built-in ListSource of the series. And this can be sorted:

Code: Pascal  [Select][+][-]
  1. type
  2.   TPnt = record x,y: TFloat;
  3. end;
  4.  
  5. var
  6.   PositionArray: array of TPnt;
  7.  
  8. // after you have all your data and when you want to create a plot:
  9. for i:=0 to High(PositionArray) do
  10.   Chart1LineSeries1.AddXY(PositionArray[i].x, PositionArray[i].y);
  11. Chart1LineSeries1.ListSource.Sort;

The attached demo creates x values in random order and adds them to a lineseries. Once the series is sorted (Checkbox "Sorted") these random values are sorted automatically into the right location.

donnie

  • Jr. Member
  • **
  • Posts: 72
Re: Sorting an Array for a lineSeries
« Reply #2 on: January 26, 2017, 10:36:25 am »
Yeap,
That's what I mean.
Fast and easy.
Thanks once again wp.

 

TinyPortal © 2005-2018