Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2009-12

tachart add point

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Andrea Mauri

Dear all, dear Alexander,
I saw in tachart source code that adding points to a serie they are kept ordered by X coordinate.
There is any reason for that?

Taken from source:
  // We keep data points ordered by X coordinate.
  // Note that this leads to O(N^2) time except
  // for the case of adding already ordered points.
  // So, is the user wants to add many (>10000) points to a graph,
  // he should pre-sort them to avoid performance penalty.

Here my problem related to that.
I am working with a TLineSerie where I do not plot lines, in order to obtain a simple scatter plot.
I add many points to my chart, the points are added in a defined order that is not related to x value.
After adding all the points if I use GetNearestPoint I obtain as AIndex the integer value related to the new indexing order. For example if I add points with the following coordinates in this order: X Y
7 0
1 2
3 1

When I will use GetNearestPoint I will get AIndex 0 for the second added point, AIndex 1 for the third and AIndex =3 for the first added point. Why? The points are added in a specific order that is useful to me. When I move the mouse over the chart I need to obtain the correct index in order to perform some actions related to that specific point. Is it possible to modify this feature? Otherwise is it possible to make the x ordering optional? For example introducing a property like Sorted = True, False
Regards,
Andrea

Reply by email

Alexander Klenin
to Andrea Mauri
Andrea Mauri wrote
Dear all, dear Alexander, I saw in tachart source code that adding points to a serie they are kept ordered by X coordinate. There is any reason for that?

This decision predates my involvement with TAChart. Still, here are my thoughts:
X-ordered points make sense as the default use case for the chart. In principle, some functions, like GetNearestPoint, might take advantage of the ordering by implementing binary instead of linear search. Right now, to the best of my knowledge, nothing in TAChart requires correct X-order. Moreover, it is not fully enforced either: e.g. TODO item in TListChartSource.SetXValue.

Show quoted text 6 lines
Taken from source: ?// We keep data points ordered by X coordinate. ?// Note that this leads to O(N^2) time except ?// for the case of adding already ordered points. ?// So, is the user wants to add many (>10000) points to a graph, ?// he should pre-sort them to avoid performance penalty.

This is the reaction to real bug reports from users who apparently relied on sorting behavior.

Show quoted text 7 lines
Here my problem related to that. I am working with a TLineSerie where I do not plot lines, in order to obtain a simple scatter plot. I add many points to my chart, the points are added in a defined order that is not related to x value. After adding all the points if I use GetNearestPoint I obtain as AIndex the integer value related to the new indexing order.

...

The points are added in a specific order that is useful to me.

I see. Before we discuss TAChart modifications, let me note that you have several workarounds available. I do not say they are all good, but want to consider
them for completeness:

1) Store original index in Label field.
2) First add all points using Index instead of X,
and then use SetXValue to assign real X.
3) Use TUserDefinedChartSource.
4) Create your own TCustomChartSource descendant
(on a related note, probably most of the TListSource methods should be virtual...)

Is it possible to modify this feature? Otherwise is it possible to make the x ordering optional? For example introducing a property like Sorted = True, False

Yes, that is possible.
Sorted property is maybe a good idea in the long term, but it requires some subtle changes to do right,
so for now I just added AInOrder parameter to Add procedure. The default is still "true" to preserve backwards compatibility, so you should manually call Series.ListSource.Add instead of Series.AddXY

See r23240.

Alexander S. Klenin

Reply by email

Alexander Klenin
to Andrea Mauri
Andrea Mauri wrote 5 lines
> 1) Store original index in Label field. > Dear Alexander I used this workaround to solve my problem. Actually I am so late in my job that I cannot explore the other options.

Ok, thanks for the feedback.

Alexander Klenin wrote
so you should manually call Series.ListSource.Add instead of Series.AddXY

If / when you have time, you can try this option too. I would like to know whether this is a good direction for a change, or should be rolled back as unnecessary complication.

Alexander S. Klenin

Reply by email