Recent

Author Topic: [SOLVED] Format time on left axis (Y) on a Line graph  (Read 855 times)

Odacir

  • New Member
  • *
  • Posts: 38
[SOLVED] Format time on left axis (Y) on a Line graph
« on: January 14, 2020, 01:08:14 pm »
Hello,

I'm trying to setup a graph line with "times" on the left axis of a line graph.
Tryed on mask -> format but no success. :-(

This is how i need to show data:
a scale of time between 00:00 and max data (about 10:50)
(minutes and seconds)

And Too format a label on any point, showing:
03:16
04:10
06:58
02:35

(minutes and seconds)

Attached how its showing (decimal).

How to format of show the data for the Y field?

Thanks any help. 
« Last Edit: January 15, 2020, 02:05:29 pm by Odacir »

wp

  • Hero Member
  • *****
  • Posts: 7353
Re: Format time on left axis (Y) on a Line graph
« Reply #1 on: January 14, 2020, 01:24:29 pm »
Are the y values of type TDateTime/TTime? In other words, what is the unit of the y axis? For TDateTime/TTime it is 1 day. I need this to guide you to the correct solution.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Odacir

  • New Member
  • *
  • Posts: 38
Re: Format time on left axis (Y) on a Line graph
« Reply #2 on: January 14, 2020, 01:29:41 pm »
Hello WP!!!

The master of TaChart! :-D

The format of data, from mysql query is this:
Code: Pascal  [Select][+][-]
  1. TIME_FORMAT(inicio,'%h:%i') as hora_formatada,

Odacir

  • New Member
  • *
  • Posts: 38
Re: Format time on left axis (Y) on a Line graph
« Reply #3 on: January 14, 2020, 01:32:13 pm »
Ops WP, and in other case i have the field saved on the mysql database like "time" format directly, but i think is the same way.

wp

  • Hero Member
  • *****
  • Posts: 7353
Re: Format time on left axis (Y) on a Line graph
« Reply #4 on: January 14, 2020, 06:31:13 pm »
OK, it seems that the y values are TTime. In this case add a TDateTimeIntervalChartSource to the form (the one with the calendar in its icon), link it to the Marks.Source of the Chart's LeftAxis, and set Marks.Style to smsLabel. This way the chartsource will automatically convert the floating-point presentation that you had so far to a time string. The format depends on the date range. In many cases I find the string to be "nicer" when the "SuppressPrevUnit" property is turned off -- otherwise for a series of hour:min label such as 00:01, 00:05, 00:09, 00:20 the hour part ('00') will be displayed only for the first label and suppressed for the following labels as long as the hours do not change (i.e. 00:001, 05, 09, 20).

Look at attached demo which plots some dummy data having a TTime as y.

Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Odacir

  • New Member
  • *
  • Posts: 38
Re: Format time on left axis (Y) on a Line graph
« Reply #5 on: January 14, 2020, 08:56:13 pm »
WP, your solution worked like a charm, and "easy" to-do, only needed know, how!!!!  :-D

Thanks a lot! For the help and clarification of concepts.

Only a detail: is possible to show the value (formated like time) on a label on the point?

I send a print of your app (setup like % only to not show a big label).
The options to show not possible set to show like time. (or i don't know how).

wp

  • Hero Member
  • *****
  • Posts: 7353
Re: Format time on left axis (Y) on a Line graph
« Reply #6 on: January 14, 2020, 10:02:16 pm »
I am not sure if I understand correctly. You want to display data point labels of the y value formatted as time?

In this case, set the Marks.Style of the series to smsValue. This does display the y values near each data point, but only as normal floating point values. There is a property Marks.Format which you could use the format the number, but this format string goes into the SysUtils Format() function and thus cannot create a time string. However, there is an event OnGetMark which you can employ to create the data point label according to any rule -- here, simply call FormatDateTime for each data point y value with an appropriate time format string, e.g. 'hh:nn:ss':

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Chart1LineSeries1GetMark(out AFormattedMark: String;
  2.   AIndex: Integer);
  3. var
  4.   y: Double;
  5. begin
  6.   y := Chart1LineSeries1.YValue[AIndex];
  7.   AFormattedMark := FormatDateTime('hh:nn:ss', y);
  8. end;
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Odacir

  • New Member
  • *
  • Posts: 38
Re: Format time on left axis (Y) on a Line graph
« Reply #7 on: January 15, 2020, 02:05:08 pm »
One more perfect answer WP!

Exactly what i need.

Thanks a lot!  :)

All doubt solveds!

 

TinyPortal © 2005-2018