Recent

Author Topic: plot discontinuous line?  (Read 1227 times)

DMStevenson

  • New member
  • *
  • Posts: 7
plot discontinuous line?
« on: June 04, 2025, 05:11:28 pm »
Hi - I was wondering if anyone has had to plot a discontinuous line. That is, for example, plot points 1,2,3 as a line, then no line between points 3,4, then a line again between points 4,5,6. I have tried various combinations of ShowPoints, and LinePen.Styles, but cannot find a way to do this. I could, of course, use multiple lineSeries, but my data sets I am plotting are *very* large, so this seems unworkable. Has anyone else found a need for this?

Thaddy

  • Hero Member
  • *****
  • Posts: 18306
  • Here stood a man who saw the Elbe and jumped it.
Re: plot discontinuous line?
« Reply #1 on: June 04, 2025, 05:43:47 pm »
If I understand you correctly, you want to do a kind of collision detection?
I.e. if a line crosses another then it should not progress drawing any further?
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

440bx

  • Hero Member
  • *****
  • Posts: 5809
Re: plot discontinuous line?
« Reply #2 on: June 04, 2025, 05:44:15 pm »
TTBOMK, there is no function (in Windows at least) that will output a number of disconnected line segments.

OTH, even with a large dataset, it takes very little code to implement that.  for instance:

Step 1. MoveTo(InitialPoint)
Step 2. Draw line to end point
Step 3. if there are more segments, advance to next segment, go to step 1.

Either a "for" or "while" loop could implement that.  It looks like it would be less than 10 lines of code.

HTH.

FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

wp

  • Hero Member
  • *****
  • Posts: 13207
Re: plot discontinuous line?
« Reply #3 on: June 04, 2025, 06:01:27 pm »
Add a NaN ("not a number", defined in math) as y value to the series at an intermediate x point (https://wiki.lazarus.freepascal.org/TAChart_documentation#Skipping_source_items).

Code: Pascal  [Select][+][-]
  1. uses
  2.   math;
  3.  
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. var
  6.   i: Integer;
  7. begin
  8.   for i := 0 to 10 do
  9.     Chart1LineSeries1.AddXY(i, random);
  10.   Chart1Lineseries1.AddXY(10.5, NaN);
  11.   for i := 11 to 20 do
  12.     Chart1lineseries1.AddXY(i, random + 1);
  13. end;

But be warned: It is not allowed to do any calculations with NaN, even "if x = NaN" will crash your application (use "if IsNaN(x)" instead). Usage of Nan is in TAChart for more than 10 years, and I hope that all such occurrencies now have been eliminated, but you never can tell...
« Last Edit: June 04, 2025, 06:05:19 pm by wp »

DMStevenson

  • New member
  • *
  • Posts: 7
Re: plot discontinuous line?
« Reply #4 on: June 04, 2025, 06:47:27 pm »
Thank you wp! This will solve my problem exactly, and also thanks for the reference in the lit. (did not find that passage). I need this as data output from one of our instruments occasionally contains 'impossible' values (usually 0), and yet we need to graph the results - skipping the untenable values. This will work beautifully!

VisualLab

  • Hero Member
  • *****
  • Posts: 693
Re: plot discontinuous line?
« Reply #5 on: June 05, 2025, 02:25:01 pm »
Continuing the topic of discontinuity of functions - is it possible to draw graphs of functions (as one series) using TAChart:
  • having a jump discontinuity,
  • having a point of discontinuity,
  • having asymptotes.
I mean examples such as those presented in this presentation.

wp

  • Hero Member
  • *****
  • Posts: 13207
Re: plot discontinuous line?
« Reply #6 on: June 05, 2025, 06:12:12 pm »
Yes, of course. Since a few version you can even do it in design mode:
  • Add a TExpressionSeries to the chart
  • Define the ranges for the x and y axes: Chart.LeftAxis.Range.XMin = -10, .XMax = 10, .UseMin = true, .UseMax = 10. The same with the BottomAxis. (You can also enter this information in the Extent property of the ExpressionSeries, but this is more work when you want to add serveral series).
  • Type the math expression into the property Expression, e.g. 1/x^2 --> You see the function in the chart!
  • But at x = 0 there is a vertical line representing the discontinuity: In property Domain tell the parser where the function is not defined - enter "x <> 0" (without quotes) - the vertical line disappears.
  • To see the formula in the legend, set Chart.Legend.Visible = true. Enter the function expression in the series' Title. You can get proper superscript for the exponent by using a HTML expression: "y = 1/x<sup>2</sup>" (no quotes). In order to activate HTML, set Chart.Legend.TextFormat to tfHTML.
But be careful: save often. As I noticed while preparing this, the parser is not very tolerant regarding typos, and this will easily freeze the IDE when you do this at designtime.

VisualLab

  • Hero Member
  • *****
  • Posts: 693
Re: plot discontinuous line?
« Reply #7 on: June 06, 2025, 07:04:07 pm »
wp, thanks for the tips on creating expression-based function graphs.

It's nice that there is a description of how to use the TAChart parser: How To Use TFPExpressionParser.

 

TinyPortal © 2005-2018