### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook (preview only)

### Author Topic: Basic code for simple scatterplot  (Read 360 times)

#### rnfpc

• Jr. Member
• Posts: 91
##### Basic code for simple scatterplot
« on: July 21, 2019, 02:22:10 pm »
I have a list of points with x,y values as follows:

Code: Pascal  [Select]
1. x: array[0..4] of integer=(1,2,3,4,5);
2. y: array[0..4] of integer=(10,25,30,55,75);

Hence the points are (1,10), (2,25), (3,30), (4,55) and (5,75).

I have to plot a simple scatterplot. I know it can be done with Lazarus using visual programming, but what is the basic code to draw such a chart? I believe, it will use TAchart :

https://wiki.freepascal.org/TAChart

https://wiki.lazarus.freepascal.org/TAChart_documentation#Point_series

I have gone through above references but could not find essential code to plot a simple scatterplot. Any help on this will be much appreciated.

« Last Edit: July 21, 2019, 02:26:44 pm by rnfpc »

#### wp

• Hero Member
• Posts: 6499
##### Re: Basic code for simple scatterplot
« Reply #1 on: July 21, 2019, 02:36:35 pm »
Follow the steps in the "getting started" tutorial, but prepare only a single series. When you reached the section "Adding data" just apply your own data instead of the sin and cos values (I assume that the series has kept its default name, Chart1LineSeries1):
Code: Pascal  [Select]
1. var
2.   x: array of integer=(1,2,3,4,5);
3.   y: array of integer=(10,25,30,55,75);
4.
5. procedure TForm1.FormCreate(Sender: TObject);
6. var
7.   i: Integer;
8. begin
9.   for i:=0 to High(x) do begin
11.   end;
12. end;

If you want only scattered points, without the connecting lines, do this:
• Select the series in the Object Inspector.
• Set its "ShowLines" to false. Note that older versions of Lazarus do not yet have this property; in this case set "LineType" to "ltNone". This turns off the connecting lines.
• Set the property "ShowPoints" to true to turn on the point symbols.
• Adjust the appearance of the point symbols by the "Pointer" sub-properties. They should be self-explaining.

If you are less interested in designtime actions but mor in actual code you should have a look at https://wiki.freepascal.org/TAChart_Runtime_FAQ. Moreover I recommend studying the example in folder (lazarus)/components/tachart/demo/runtime/plotunit, unit uplot. It contains code to plot x,y arrays with a single function call plot(...).
« Last Edit: July 21, 2019, 02:43:37 pm by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

#### rnfpc

• Jr. Member
• Posts: 91
##### Re: Basic code for simple scatterplot
« Reply #2 on: July 21, 2019, 04:00:21 pm »
Thanks for a well explained answer. I am going through the link and will try this code.

I am using Lazarus 1.6.2 (on Debian Stable) and in /usr/lib/lazarus/1.6.2/components/tachart/demo (or its any subfolders) there is no file named plotunit.

Are there any other charting libraries also for Lazarus / Freepascal?
« Last Edit: July 21, 2019, 04:05:10 pm by rnfpc »

#### rnfpc

• Jr. Member
• Posts: 91
##### Re: Basic code for simple scatterplot
« Reply #3 on: July 21, 2019, 05:32:05 pm »
I tried following unit file:

Code: Pascal  [Select]
1. unit rnUscatter;
2.
3. {\$mode objfpc}{\$H+}
4.
5. interface
6.
7. uses
8.   Classes, SysUtils, FileUtil, TAGraph, TASeries, Forms, Controls, Graphics,
9.   Dialogs;
10.
11. type
12.
13.   { TForm1 }
14.
15.   TForm1 = class(TForm)
16.     Chart1: TChart;
17.     Chart1LineSeries1: TLineSeries;
18.   private
19.     { private declarations }
20.   public
21.     { public declarations }
22.     procedure FormCreate(Sender: TObject);
23.   end;
24.
25. var
26.   Form1: TForm1;
27.   x: array[0..4] of integer=(1,2,3,4,5);
28.   y: array[0..4] of integer=(10,25,30,55,75);
29.
30. implementation
31.
32. {\$R *.lfm}
33.
34. procedure TForm1.FormCreate(Sender: TObject);
35. var
36.   i: Integer;
37. begin
38.   for i:=0 to High(x) do begin
40.     end;
41. end;
42.
43. end.

The project compiles and builds without any error or even warning. However, on running it, only a blank graph (axes and grid) is shown - no points/line are/is plotted.

Where is the problem. Do I need to call procedure formCreate?

#### wp

• Hero Member
• Posts: 6499
##### Re: Basic code for simple scatterplot
« Reply #4 on: July 21, 2019, 06:09:44 pm »
How did you create the method FormCreate? Did you simply type the code? In this case you still must link your code to the event OnCreate of the form. The first issue is that the Object Inspector cannot find your event handler when you declare it as "public", you must put it into the published section, for forms this is the part of the form declaration above "private" where "Chart1" and "Chart1LineSeries1" already are found:

Code: Pascal  [Select]
1.   TForm1 = class(TForm)
2.     Chart1: TChart;
3.     Chart1LineSeries1: TLineSeries;
4.     procedure FormCreate(Sender: TObject);  // moved up to here to make the method visible in the Obj Inspector.
5.   private
6.     { private declarations }
7.   public
8.     { public declarations }
9.   end;

Next, in order to assign your FormCreate code to the event, select the chart in the form designer, go to the Object Inspector, open page "Events", click on the down arrow next to "OnCreate" and select "FormCreate" from the list (without doing the previous step the method would not be listed here). Now it should work.

The next time when you create an event handler in Lazarus do it the easy way: Select the event in the Object Inspector, page "Events" again, and double-click on the right column of the event name (or single-click on the '...' button). The IDE then inserts an empty method body for the event handler and immediately assigns it to the event. You only must go to the code editor and type in your code.

As for the "plotunit" project, you can download the files separately from https://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/components/tachart/demo/runtime/plotunit/?root=lazarus&sortby=rev. I tried with Laz 1.6.4 (I don't have 1.6.2 any more) - trying to compile with you Lazarus you will notice two issues which you can fix easily:

(1) In function Plot() of unit uplot, the compiler will complain somewhere near line 132 about "; expected, identifier BRUSH found" --> just comment out the four lines beginning with "Pointer.", they affect TFitSeries, but are not essentially needed for the rest of the demo to work.

(2) The next issue is in unit main, declaration "const SYMBOLS =...", after "psDiamond": "Identifier not found: psDownTriangle". This is because these enumeration elements did not yet exist in Laz 1.6 series. Just close the bracket after "psDiamond and reduce the upper array index to 3 (instead of 5).

Then the demo will compile.

Of course, you should consider upgrading to a more recent version of Lazarus, the current release is v2.02.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

#### rnfpc

• Jr. Member
• Posts: 91
##### Re: Basic code for simple scatterplot
« Reply #5 on: July 21, 2019, 06:33:58 pm »
Thanks to your clear instructions, I have been able to get a proper scatterplot.

I have also been able to build and run plotunit project.  I am now going through plotunit files.
« Last Edit: July 21, 2019, 06:44:05 pm by rnfpc »