Recent

Author Topic: TChart start position  (Read 1759 times)

wp

  • Hero Member
  • *****
  • Posts: 8568
Re: TChart start position
« Reply #15 on: February 28, 2021, 12:15:12 pm »
See the attached modified demo with the dbf file.

The problem is that you essentially need two x values - one for the labels (years), and one for plotting, and the latter ones must be equidistant. If you'd use TAChart of Lazarus trunk you'd have the possibility to use multiple x values. But in the release version - which I am assuming in the demo project - this is not possible. However, you can extend the Text field of the data point record by the year values:
Code: Pascal  [Select][+][-]
  1.   Chart1ListSource1.Item[datapoint_index]^.Text := Format('%s|%g', [
  2.     Chart1ListSource1.Item[datapoint_index]^.Text,
  3.     Chart1ListSource1.Item[datapoint_index]^.X ]);
As you can see this adds the x value as a string the the original farm name, separated by a pipe character ('|').

Then for drawing the axis labels you must extract the year string from the combined Text field by finding the separator character and copying the text afterwards. This happens in the OnGetMarkText event of the x axis (or the OnMarkToText event)

The same is needed for the series labels - if you want to display them - by picking the text before the pipe character. This is the responsibility of the OnGerMark event of the series.

Since your data are not entered in an ordered fashion you must sort them. You can either sort the data set (which is preferred because it will also sort the DBGrid), or you can sort the ListChartSource (ListChartSource.Sorted := true).

When the data is sorted you iterate through the dataset and populate the ListSource. I avoid the DBChartSource at all because it turned out that it does not report the RecordCount correctly - which would show many empty bars in the chart...

After (or during) population of the ListChartSource the Text field is extended as explained above, and the X field is set to the running index to make it equidistant.

I think I should note that it is important to enclose the population steps of the ListChartSource by a BeginUpdate/EndUpdate block because otherwise the extent of the axis wil not be up-to-date.

I hope this description is clear enough so that you understand the principle. Study the code provided. Otherwise ask again.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

bpranoto

  • Full Member
  • ***
  • Posts: 103
Re: TChart start position
« Reply #16 on: February 28, 2021, 12:20:33 pm »
Thank you. It works with just 2 rows. While I add to 3 rows doesn't work.  (ID(2012)...+ID (i))

You have to manually set the content of the field XLABEL for every record. Modify the grid to also shows the XLABEL field....

bpranoto

  • Full Member
  • ***
  • Posts: 103
Re: TChart start position
« Reply #17 on: February 28, 2021, 12:21:50 pm »
and make sure the records are sorted by XLABEL....

bpranoto

  • Full Member
  • ***
  • Posts: 103
Re: TChart start position
« Reply #18 on: February 28, 2021, 12:29:38 pm »
Here is the modified version:

flori

  • Full Member
  • ***
  • Posts: 185
Re: TChart start position
« Reply #19 on: February 28, 2021, 01:09:30 pm »
WP and bpranoto thank you for your help

I am trying to implement the codes in FastReport (tfrxReport).

MANY THANK YOU!

bpranoto

  • Full Member
  • ***
  • Posts: 103
Re: TChart start position
« Reply #20 on: February 28, 2021, 01:30:55 pm »
You're welcome!

 

TinyPortal © 2005-2018