Recent

Author Topic: how to get a X Y line chart with the X-axis dates  (Read 2840 times)

daveinhull

  • Sr. Member
  • ****
  • Posts: 297
  • 1 divided by nothing must still be 1!
how to get a X Y line chart with the X-axis dates
« on: February 20, 2019, 02:26:21 pm »
Hi,

I've look everywhere but just can't seem to get much help on creating a chart where the x-axis are dates.
I'm using a TChart with a TDBChartSource and I can get the chart to show correctly with correct Y values, but the X vlues are just numbers rather than dates (or maybe as I eventually want just years).

Could someone give me a ittle starter or pointer to where I can learn about creating charts.

Many thanks in advance
Dave
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: how to get a X Y line chart with the X-axis dates
« Reply #1 on: February 20, 2019, 02:53:45 pm »
I've look everywhere but just can't seem to get much help on creating a chart where the x-axis are dates.
I'm using a TChart with a TDBChartSource and I can get the chart to show correctly with correct Y values, but the X vlues are just numbers rather than dates (or maybe as I eventually want just years).
To give a specific answer I'd need to know how the dates are stored in the DB: as TDateTime/TDate values? Or as strings? It would also be helpful if you could post a little standard DB file (TBufDataset, TDbf, or so - but no server requirements please) so that I can see your exact requirements.

When they are stored ar TDateTime/TDate you simple set the option dcsoDateTimeX of the DBChartSource. Or you can also add a TDateTimeIntervalChartSource an attach it to the property Marks.Source of the BottomAxis of the chart (and set Marks.Style to smsLabel).

Could someone give me a ittle starter or pointer to where I can learn about creating charts.
There's a series of tutorials, beginning with http://wiki.lazarus.freepascal.org/TAChart_Tutorial:_Getting_started. At the bottom of the page there is a link to other tutorials, find the TAChart tutorials under letter "T".

You can also/additonally study the samples in folder components/tachart/demo/db and components/tachart/demo/db-barseries of your lazarus installation

daveinhull

  • Sr. Member
  • ****
  • Posts: 297
  • 1 divided by nothing must still be 1!
Re: how to get a X Y line chart with the X-axis dates
« Reply #2 on: February 21, 2019, 02:22:16 am »
Hi wp,

Firstly, thanks for the reply and also would like to say that is is an awsome package with many options, I'm only just getting to understand it fully.

I was just about to reply to my self to say that I figure it out using the secon option you provided.

I did try your first option, but it didn't work (my problem for not understand I suspect). The data is stored in an Access DB as a Date/Time. I'll take another look and make sure I got things right and if I still can't get it to work, I'll get back with some more details.

Many thanks
Dave
« Last Edit: February 21, 2019, 04:56:46 am by daveinhull »
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: how to get a X Y line chart with the X-axis dates
« Reply #3 on: February 21, 2019, 04:57:16 pm »
See the attached demo which is for a MemDataset, but should also work for MS-Access tables/queries accordingly.

Please note that my description was not very complete, look at the comments in the source code to see all the steps needed to create a labeled date axis from a TDataset.

Note also that the program does not react nicely when the window size is changed, or when the splitter next to the chart is dragged. This is because the DBChartSource iterates through the entire dataset many times to collect the data for painting. You get normal performance when the DBChartSource is copied to a ListChartSoruce as is done when the corresponding radiobutton is selected in the demo.

daveinhull

  • Sr. Member
  • ****
  • Posts: 297
  • 1 divided by nothing must still be 1!
Re: how to get a X Y line chart with the X-axis dates
« Reply #4 on: March 05, 2019, 08:33:17 am »
Thanks wp, got it all working
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

 

TinyPortal © 2005-2018