Bookstore

Recent

Author Topic: [SOLVED] How to display a complete curve based on dbChartsource?  (Read 236 times)

SNPI

  • New member
  • *
  • Posts: 5
The program is used to draw the curve of two fields in the SQlite3 database 'data.db'. However, the chart cannot display lineseries' curve after the program is running (as shown in the first figure).After Clicking the left mouse button dozens of times on chart area, the complete curve can be displayed(as shown in the second figure).
   I have repeatedly modified various attributes of the chart, but still can't display the complete curve after formcreated.
   Thanks a lot!

lazarus-2.0.6-fpc-3.0.4-win32
Tested in win10
« Last Edit: February 19, 2020, 02:21:47 pm by SNPI »

SNPI

  • New member
  • *
  • Posts: 5
Re: How to display a complete curve based on dbChartsource?
« Reply #1 on: February 19, 2020, 12:12:15 pm »
Program

wp

  • Hero Member
  • *****
  • Posts: 6819
Re: How to display a complete curve based on dbChartsource?
« Reply #2 on: February 19, 2020, 12:44:55 pm »
It seems that SQLite optimizes the query to deliver only a subset of the entire table. I can see this also when I attach simply a DBGrid to the DataSource and scroll down - whenever I get close to the end of the scrollbar new data are added againd and again. Increasing the PacketRecords loads more records at once. When I set it to 1000 I see the chart fully populated.

But I would not use a TDbChartSource at all. Whenever a datapoint is needed this chart source iterates over all records of the dataset until the data point is found. For the next data point it repeats the entire procedure - again and again until the entire series is populated. This is extremely inefficient - and you'll notice a short delay until the series appears. And when a DBGrid is hooked to the same Datasource you see the active cell moving to the end for example when the chart is zoomed.

It is better to manually iterate over all records once to collect the data points and store them in a TListChartSource - the built-in source is fine (use series.AddXY).

Delete the DbChartSource and put this code into the query's OnAfterOpen event:
Code: Pascal  [Select]
  1. procedure TForm1.SQLQuery1AfterOpen(DataSet: TDataSet);
  2. var
  3.   TimeField, IrrField: TField;
  4. begin
  5.   TimeField := SQLQuery1.FieldByName('Time');
  6.   IrrField := SQLQuery1.FieldByName('Irr');
  7.   Chart1LineSeries1.BeginUpdate;
  8.   try
  9.     Chart1LineSeries1.Clear;
  10.     while not SQLQuery1.EoF do
  11.     begin
  12.       Chart1LineSeries1.AddXY(TimeField.AsDateTime, IrrField.AsFloat);
  13.       SQLQuery1.Next;
  14.     end;
  15.   finally
  16.     Chart1LineSeries1.EndUpdate;
  17.   end;
  18. end;
  19.  
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

SNPI

  • New member
  • *
  • Posts: 5
Re: How to display a complete curve based on dbChartsource?
« Reply #3 on: February 19, 2020, 02:09:10 pm »
WP,thanks a lot for your perfect solution!