[Lazarus] tachart add point

Alexander Klenin klenin at gmail.com
Wed Dec 23 15:52:01 CET 2009


On Wed, Dec 23, 2009 at 23:25, Andrea Mauri <andrea.mauri.75 at gmail.com> 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.

> 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.

> 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




More information about the Lazarus mailing list