Recent

Author Topic: Get number of shown points  (Read 3096 times)

kapibara

  • Hero Member
  • *****
  • Posts: 610
Get number of shown points
« on: February 03, 2017, 10:45:55 pm »
Lets say 500 days of data is loaded into a TChartSourceList. A TLineSeries is showing this data on the chart. If zooming in, how to know how many days, or points, are now being plotted?

Also, how to tell the series or chart to show only 200 days/points instead of all 500?
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Get number of shown points
« Reply #1 on: February 03, 2017, 11:17:48 pm »
Lets say 500 days of data is loaded into a TChartSourceList. A TLineSeries is showing this data on the chart. If zooming in, how to know how many days, or points, are now being plotted?
I guess you must count them. Call Chart.LogicalExtent to get the visible axis range (let's name it "ext"); the x axis starts at ext.a.x and ends at ext.b.x. Then you iterate through all data points and increment a counter if the x value of a data point is between ext.a.x and ext.b.x

Code: Pascal  [Select][+][-]
  1. uses
  2.   Math;
  3. var
  4.   ext: TDoubleRect;
  5.   i: Integer;
  6.   count: Integer;
  7. begin
  8.   count := 0;
  9.   ext := Chart1.LogicalExtent;
  10.   for i:=0 to Chart1LineSeries1.Count-1 do
  11.     if InRange(Chart1LineSeries1.XValue[i], ext.a.x, ext.b.x) then inc(count);
  12. end;

Also, how to tell the series or chart to show only 200 days/points instead of all 500?
Also by code. Declare a variable ext: TDoubleRect. Then decide at which data point index the 200 data points should start. Write its x value in ext.a.x. Then add 199 (= 200-1) to the index, get the x value of this data point and write it to ext.b.x. If you want the y axis to scale to the new data range you must iterate through all data points between the first and last index of the zoomed range and find the minimum and maximum of the data y values. The minimum goes to ext.a.y, the maximum to ext.b.y. Finally assign the Chart1.Logicalextent to ext.

Code: Pascal  [Select][+][-]
  1. uses
  2.   math;
  3. var
  4.   istart, iend: Integer;
  5.   ext: TDoubleRect;
  6. begin  
  7.   istart := ....  // index of left-most datapoint
  8.   iend := istart + 199;
  9.   // Make sure to stay inside the data range
  10.   if iend >= Chart1LineSeries1.Count then begin
  11.     iend := chart1LineSeries1.Count - 1;
  12.     istart := iend - 199;
  13.   end;
  14.   ext.a.x := Chart1LineSeries1.XValue[istart];
  15.   ext.b.x := Chart1LineSeries1.XValue[iend];
  16.   ext.a.y := 1E308;
  17.   ext.b.y := -1E308;
  18.   for i:= istart to iend do begin
  19.     ext.a.y := Min(ext.a.y, Chart1LineSeries1.YValue[i]);
  20.     ext.b.y := Max(ext.b.y, Chart1LineSeries1.YValue[i]);
  21.   end;
  22.   Chart1.LogicalExtent := ext;
  23. end;

NOTE:
I assume that the chartsource of the lineseries is sorted (by x). And I assume that no axis transformations are involved.
« Last Edit: February 03, 2017, 11:22:20 pm by wp »

kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: Get number of shown points
« Reply #2 on: February 04, 2017, 07:37:36 am »
That immediately worked. :-) I'll try it out some more when I'm back in later today.
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

 

TinyPortal © 2005-2018