Recent

Author Topic: Getting a live TAChart display "just right"  (Read 434 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 2422
Getting a live TAChart display "just right"
« on: January 29, 2021, 10:54:15 pm »
As mentioned in another thread, using one of @wp's examples I've got a TAChart live display of a multimeter signal working using code like this:

Code: Pascal  [Select][+][-]
  1. (* Output the values read from the meter in binary form for the chart etc.
  2.   This is called, via Synchronize(), by the background communications thread.
  3. *)
  4. procedure TMs2115bForm.ReadingBin(timestamp: TDateTime; value: double);
  5.  
  6. begin
  7.   while Chart1LineSeries1.Count > 100 do
  8.     Chart1LineSeries1.Delete(0);
  9.   Chart1LineSeries1.AddXY(timestamp, value)
  10. end { TMs2115bForm.ReadingBin } ;
  11.  

However, I have a couple of questions: one fairly serious and the other much less so.

The serious one is based on the observation that when the program is running and accumulating data (paced by what the meter sends, with a reading typically arriving a couple of times a second), the right-hand margin of the control dances around on roughly a 2-second (4-reading) cycle. Is there some way of locking this down to be less visually obtrusive?

The less serious one is that before any data has been accumulated the timestamps on the X axis cover a 24-hour period from 23:20:00 (I forget the date) onwards... is there an easy way to preset this to all zeros or something innocuous?

I'm hoping to reuse this program for a pulse oximeter as soon as the hardware arrives. In that case I won't need the X-azis timestamps at all, but I definitely don't want the edge of the control to be dancing around or the grid to be moving: if my understanding of what you can get off the sensor is correct I just want a rolling display like you get on a hospital's patient monitor (I'm not expecting that sort of resolution which would be from an ECG, but I am moderately confident that the pulse will be detectable).

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

wp

  • Hero Member
  • *****
  • Posts: 8316
Re: Getting a live TAChart display "just right"
« Reply #1 on: January 30, 2021, 12:37:30 am »
A better example is in the attachment.

At first it sets the viewport width to a given value, 10 seconds in the example. This way the chart starts at the left and slowly fills the viewport. With every data point arriving the range of the data accumulated is calculated (LineSeries.Extent). When the range is wider than the 10 seconds the left end of the viewport is forced to be 10 seconds before the right edge. This way old data run out of the viewport. Of course you can delete too old values (having x < left edge of view port) if you don't need them (but I think it's a pity to discard measurement values)

The jitter of the right edge of the viewport is caused by the last axis label somtimes reaching beyond the right viewpoirt edge - the chart shrinks to avoid truncating that label. As you may notice there is much less jitter at the left edge of the chart because there are the labels of the y axis which provide enough space to avoid clipping of the first x label. Maybe there is a better way but I added a right y axis since it has a property LabelSize which fixes the distance to the right component edge (not quite exact, study the drawing in https://wiki.lazarus.freepascal.org/TAChart_documentation#Axis_ranges) and thus provides some buffer for the last label. The labels of the right axis are the same as those of the left axis - you can keep them or turn them off with Marks.Visible = false.

Since the 10 seconds horizontal interval is fixed I decided to use a fixed date/time format in the DateTimeChartSoure.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

MarkMLl

  • Hero Member
  • *****
  • Posts: 2422
Re: Getting a live TAChart display "just right"
« Reply #2 on: January 30, 2021, 10:35:54 am »
Thanks, investigating. Is the drawing the one by the "Graphical explanation" heading? The comment about its being iterative reminds me the Delta Blue constraint solver which is available for Delphi... never tried to port it to FPC/Lazarus but I think it was used by Apple for this sort of thing.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

wp

  • Hero Member
  • *****
  • Posts: 8316
Re: Getting a live TAChart display "just right"
« Reply #3 on: January 30, 2021, 11:27:29 am »
Sorry, I don't understand what you mean...
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

MarkMLl

  • Hero Member
  • *****
  • Posts: 2422
Re: Getting a live TAChart display "just right"
« Reply #4 on: January 30, 2021, 11:56:53 am »
The documentation is commenting about it being iterative. I've not checked... does it use something like Deltablue?

Anyway, I've added a right axis as you've suggested, with non-zero labelsize and marks set non-visible. I note that the labelsize can be kept fairly small provided that the text on the bottom axis is rotated about its right-hand end.

I'm continuing to trim old data fairly aggressively and haven't attempted to stop the grid crawling, since that gives the sort of behaviour I'd expect. If anybody wants logged data it can be pulled from the text display... potentially this could log to a database etc. I've preloaded the graphical display with a sequence of zeroes at startup which makes the bottom axis predictable and prevents the grid jumping around too much.

Thanks very much for the help, I think I've got something usable there and could improve it beyond Mastech's supplied Windows-only program with minimal effort (dump graphic and accumulated readings to file, log to file etc.).

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018