Recent

Author Topic: How to set bottom axis to 15 minute intervals  (Read 3822 times)

ronhud

  • Jr. Member
  • **
  • Posts: 84
How to set bottom axis to 15 minute intervals
« on: September 27, 2018, 11:41:26 am »
I have a TAChart where the bottom axis is DateTime.  The xvalue is approximately every 15 mins and I am using a lineseries to show this.    I dont understand which of the various properties to do with the axis will result in the bottom axis labels appear as 15 minute intervals evenly across the chart .  I have looked at various tutorials but these mostly use fixed  non-date values rather than external database values. 

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: How to set bottom axis to 15 minute intervals
« Reply #1 on: September 27, 2018, 12:25:59 pm »
The most flexible way is to assign a TDateTimeIntervalChartSource to the Marks.Source of the BottomAxis. To see the time strings you must also set the Marks.Style to smsLabel.

In order to see labels only at 15 minute intervals you must play with the Params of the DateTimeIntervalChartSource. The exact setting depends on the total axis range and on the size of the chart. Although you only want labels in 15min intervals you must take care of the cases, when the labels get too crowded and overlap, or too sparse. Prepare the favored labels in the Params.NiceSteps '5|10|15|30|60'. The labeller begins with 60 min intervals and calculates the distance between label positions. Normally this is longer than the Params.MaxLength. Therefore the next smaller interval is selected until the label position distance is between Params.Maxlength and Params.MinLength. Changing these values gives a tool to tune the labels used.

I am aware that playing with these settings may soon become frustrating. Therefore, I could also recommend an alternative way:

Add a TChartListSource to the form (instead of the DateTimeChartSource), link it with the Marks.Source and set Marks.Style to smsLabel again. Then write some code to add the labels to the chart source:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.CalcLabelPositions;
  2. const
  3.   INTERVAL_15_MIN = 1.0 / (24*60) * 15;
  4. var
  5.   ext: TDoubleRect;
  6.   t: TDateTime;
  7. begin
  8.   ListChartSource1.Clear;
  9.   ext := Chart1.LogicalExtent;
  10.   t := trunc(ext.a.x / INTERVAL_15_MIN) * INTERVAL_15_MIN;
  11.   while t <= ext.b.x do begin
  12.     ListChartSource1.Add(t, t, FormatDateTime('hh:nn', t));
  13.     t := t + INTERVAL_15_MIN;
  14.   end;
  15. end;  
This code determines the current extent of x axis (ext.a.x = minimum, ext.b.x = maximum) and calculates the first multiple of a 15min interval which is below the minimum. Then, starting at this value, all times in 15min intervals are added to the chart source until the axis maximum is exceeded. Axis labels will only drawn at the positions defined in the chart source.

This code can be called in the chart's OnExtentChanging event.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Chart1ExtentChanging(ASender: TChart);
  2. begin
  3.   CalcLabelPositions;
  4. end;
The problem with this code is that the axis labels will be very crowded if the axis range is too large, or the chart is too small (you may switch Marks.OverlapPolicy to opHideNeighbour to avoid this). On the other hand, it is guaranteed that the system will never switch to a different interval. You can't get both...

The attached project shows both solutions for an average-sized chart and an axis range of 3 hours.

« Last Edit: September 27, 2018, 02:12:57 pm by wp »

ronhud

  • Jr. Member
  • **
  • Posts: 84
Re: How to set bottom axis to 15 minute intervals
« Reply #2 on: September 28, 2018, 07:01:05 pm »
Thanks again - very helpful in getting to grips with TAChart

 

TinyPortal © 2005-2018