### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: TListChartSource Y count  (Read 2698 times)

#### PAsNap

• Newbie
• Posts: 2
##### TListChartSource Y count
« on: September 08, 2021, 03:41:58 pm »
Hi everyone,
this is my first post here so please be patient with me.

First of all I am quite a noob with Lazarus and I apologise if mine is kinda of stupid question.

I am working on a software whom read a csv file and plot it using a TChart component.
I managed to read the file and put all the data in a TListChartSource component. The TListChartSource has x (time data) column and 4 y columns (actual data).

At this point I got stuck... My intention is to plot only one Y at time (for example x and y2).
In my software I have a TLineSeries which has as source my TListChartSource component. I correctly see the source ycount is 4 but when plotting my TLineSeries it plots all 4 columns of TListChartSource.

Where is the error? There is any way to plot only one colum of TListChartSource despite the fact the ycount property is 4?

#### wp

• Hero Member
• Posts: 9164
##### Re: TListChartSource Y count
« Reply #1 on: September 08, 2021, 07:00:52 pm »
Using multiply y values was primarily motivated by stacked bar and area series. Application to line series came in later, but I see from your question that it does not seem to be complete...

Anyway, I'm going to show you three solutions how you could solve your problem. Suppose you have a csv file with one x column and 5 y columns (y1, ..., y5).

The simplest solution would be to add 5 series, each of them with YCount=1, and store (x,y1) in the first series, (x,y2) in the second, (x,y3) in the third etc. This, in fact, gives you the greatest flexibility, and you can even use a TChartListbox to display any combination of the series (--> project 1)

Of course, x is stored mulitple times in this solution. Although this multiple internal storage of a value very probably won't let you run out of memory, I agree it is not very elegant... So let's return to YCount=5; now each data point consists of a shared x and 5 y values here, and you need only a single series. In order to hav an individual color for each "sub-series" you must work with TChartStyles. Add a TChartStyles component to the chart and link it to the Styles property of the lineseries. Then add five Style elements (one for each series) and set the Pen.Color as you need for each sub-series. For all these lines which you do not want to see set the Pen.Style to psClear. The lines that you do want to see will have psSolid as Pen.Style (---> project2)

The disadvantage of this solution is that the hidden sub-series are still evaluated for calculating the chart extent. So, if one column of your csv file contains only very large values, and another column only very small values the sub-series with the small numbers will be almost flat.

This limitation is overcome by the third solution. There is no need to store the data of the csv file in a ChartlistSource at all, you can use an ordinary array of x-y1-y2-y3-y4-y5 records instead and use a TUserDefinedChartSource to assign the record element to the series. For this purpose you must write a handler for the OnGetChartDataItem event (---> project 3)
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### PAsNap

• Newbie
• Posts: 2
##### Re: TListChartSource Y count
« Reply #2 on: September 09, 2021, 09:10:40 am »
Thanks also for the brief introduction to ycount reason for sure it will be useful bit of information to know before start my next project.

The simplest solution would be to add 5 series, each of them with YCount=1, and store (x,y1) in the first series, (x,y2) in the second, (x,y3) in the third etc. This, in fact, gives you the greatest flexibility, and you can even use a TChartListbox to display any combination of the series (--> project 1)

At the moment I am working with your solution number 1. As you stated it is the simplest and the only one who come up in my mind. The issue with this solution is I am working with large amount of data... recorded time signals sampled at 4Mhz with a length of 15 to 20 seconds.
In this specific case having the x duplicated is a problem that's the reason I was looking for a smart way to store the minimum amount of data.

Of course, x is stored mulitple times in this solution. Although this multiple internal storage of a value very probably won't let you run out of memory, I agree it is not very elegant... So let's return to YCount=5; now each data point consists of a shared x and 5 y values here, and you need only a single series. In order to hav an individual color for each "sub-series" you must work with TChartStyles. Add a TChartStyles component to the chart and link it to the Styles property of the lineseries. Then add five Style elements (one for each series) and set the Pen.Color as you need for each sub-series. For all these lines which you do not want to see set the Pen.Style to psClear. The lines that you do want to see will have psSolid as Pen.Style (---> project2)

For the same reason (but just my guess) solution 2 could be potentially slow down user intercation with TChart. Having so much data (even if hidden data) could slow down drag and zoom operations.

This limitation is overcome by the third solution. There is no need to store the data of the csv file in a ChartlistSource at all, you can use an ordinary array of x-y1-y2-y3-y4-y5 records instead and use a TUserDefinedChartSource to assign the record element to the series. For this purpose you must write a handler for the OnGetChartDataItem event (---> project 3)

I will go ahead and try solution number 3 and adapt it to my application.