Page 1 of 1

Finding minimum or maximum point on series

Posted: Sun Nov 25, 2007 7:48 am
by 9347662
Hi,

I would like to find the maximum/minimum point in a line series within the stated range. For example, if there are 818 samples in my line and I would only like to find the maximum in the first 409 samples, how do I do it? Ive tried:

Code: Select all

Chart.Series[1].MinYValue;
or

Code: Select all

Chart.Series[1].MaxYValue;
Problem with them is they find the whole line series. Please help. Thank you

David[/code]

Posted: Mon Nov 26, 2007 9:39 am
by yeray
Hi mygetho,

You are right. MinYValue and MaxYValue return the minimum and maximum values of the entire series respectively. If you want to calculate the min and max for an interval, you have to calculate it manually creating your own functions.

One solution could be the following:

Code: Select all

function TForm1.Max(line: TLineSeries; indexfrom: Integer; indexto: Integer): Integer;
var i: Integer;
begin
  result := -1;

  if (((line.Count-1) >= indexfrom) and ((line.Count-1) >= indexto) and (indexfrom <= indexto)) then
  begin
    result := indexfrom;

    for i := indexfrom to indexto do
    begin
      if line.YValue[i] > line.YValue[result] then
        result := i;
    end;
  end;
end;

function TForm1.Min(line: TLineSeries; indexfrom, indexto: Integer): Integer;
var i: Integer;
begin
  result := -1;

  if (((line.Count-1) >= indexfrom) and ((line.Count-1) >= indexto) and (indexfrom <= indexto)) then
  begin
    result := indexfrom;

    for i := indexfrom to indexto do
    begin
      if line.YValue[i] < line.YValue[result] then
        result := i;
    end;
  end;
end;
In this example, we return the index of the point we've found.

Posted: Wed Nov 28, 2007 5:27 am
by 9347662
Hi,
I have tried your code and it works but the problem I am having now is that my line series has points that increase and decrease like a roller coaster so basically it will keep detecting the min or max up til the end as long as the if statement is true. Is there a way to do it so that it will find the absolute max value or min value within a specified range like the MinYValue or MaxYValue. Thanks.
David

Posted: Wed Nov 28, 2007 10:02 am
by yeray
Hi mygetho,

If I understand well, you want a function which, with a minimum and a maximum Y values given, gives the minimum point of the series (and another one that gives the maximum).

In other words, as you can see in the image, you set an interval of Y values and you search for the maximum and minimum of this interval.

Image
Click on the image to enlarge.

Posted: Wed Nov 28, 2007 10:33 am
by yeray
Hi mygetho,

Here there are two functions that will achieve what I understood:

Code: Select all

function TForm1.MaxBetween(line: TLineSeries; MinYValue, MaxYValue: Double): Integer;
var i: Integer;
begin
  result := -1;

  if (MinYValue <= MaxYValue) then
    for i := 0 to line.Count-1 do
      if (line.YValue[i] <= MaxYValue) and (line.YValue[i] >= MinYValue) then
        if (result = -1) then
          result := i
        else
          if (line.YValue[i] > line.YValue[result]) then
            result := i;
end;

function TForm1.MinBetween(line: TLineSeries; MinYValue, MaxYValue: Double): Integer;
var i: Integer;
begin
  result := -1;

  if (MinYValue <= MaxYValue) then
    for i := 0 to line.Count-1 do
      if (line.YValue[i] <= MaxYValue) and (line.YValue[i] >= MinYValue) then
        if (result = -1) then
          result := i
        else
          if (line.YValue[i] < line.YValue[result]) then
            result := i;
end;