Recent

Author Topic: Round Number for the Marks  (Read 483 times)

AmirNA

  • New member
  • *
  • Posts: 9
Round Number for the Marks
« on: September 07, 2022, 05:21:44 am »
Hi all,

I have problem with marks interval setting. Is there any option to limit the minimum interval between the marks. I do not want to have interval less than 1 in the axis. For example for the attached image,  I want to have only 0,1 and 2 on the left axis.

wp

  • Hero Member
  • *****
  • Posts: 10286
Re: Round Number for the Marks
« Reply #1 on: September 07, 2022, 12:45:41 pm »
This is not possible with the built-in labeller. As a replacement you can add a TListChartSource to the form and populate it with the y values at which you want to see a label. When you assign this chartsource to the Source property of the chart's LeftAxis.Marks.Source it will replace the built-in labels - see attached demo.

But be aware that when you zoom into such a chart you very easily will be lost with no labels being displayed any more.

AmirNA

  • New member
  • *
  • Posts: 9
Re: Round Number for the Marks
« Reply #2 on: September 08, 2022, 12:07:35 am »
Thanks a lot. :D

wp

  • Hero Member
  • *****
  • Posts: 10286
Re: Round Number for the Marks
« Reply #3 on: November 06, 2022, 07:13:24 pm »
Just committed a new version of TAChart to Laz/main which has a new option "aipInteger" for Axis.Intervals.Options. It forces the labeling algorithm to select only integer labels. Thus, the original issue of this post can be solved easier now: Simply add aipInteger to the Chart.BottomAxis.Intervals.Options. See attached project.

The new code is in the main branch of the Lazarus repository. But I think it is not difficult to add this also to older versions:
  • Make a backup copy of the files TACustomSource.pas and TAIntervalSources.pas in folder components/tachart of your Lazarus installation.
  • Open the file TACustomSource.pas, find the declaration of the type TAxisIntervalParamOption (near the top) and add "aipInteger" to the list. The declaration should look like this finally:
Code: Pascal  [Select][+][-]
  1. type
  2.   TAxisIntervalParamOption = (
  3.     aipGraphCoords,
  4.     aipUseCount, aipUseMaxLength, aipUseMinLength, aipUseNiceSteps,
  5.     aipInteger);  
  • Open the file TAIntervalSources and find the procedure TIntervalChartSource.CalculateIntervals. Replace the body of the procedure by the following code (or add the highlighted lines):
Code: Pascal  [Select][+][-]
  1. procedure TIntervalChartSource.CalculateIntervals(
  2.   AParams: TValuesInRangeParams; out ABestStart, ABestStep: Double);
  3. [...]
  4. begin
  5.   CalcMinMaxCount(minCount, maxCount);
  6.   bestCount := 0;
  7.   if aipInteger in Params.Options then begin
  8.     ABestStart := Int(AParams.FMin);
  9.     ABestStep := 1.0;
  10.     bestCount := Round(AParams.FMax) - round(ABestStart);
  11.     if bestCount <= maxCount then
  12.       exit;
  13.   end;
  14.   if aipUseNiceSteps in Params.Options then begin
  15.     s := AParams.CountToStep(minCount)  * 10;
  16.     while s >= Max(AParams.CountToStep(maxCount), AParams.FMinStep) do begin
  17.       for sv in Params.StepValues do
  18.         TryStep(s * sv, bestCount);
  19.       // We are not required to pick the best count, so any one will do.
  20.       if not (aipUseCount in Params.Options) and (bestCount > 0) then break;
  21.       s *= 0.1;
  22.     end;
  23.   end;
  24.   if (aipInteger in Params.Options) and (bestCount > maxCount) then
  25.     bestCount := 0;
  26.   if bestCount > 0 then
  27.     exit;
  28.   // Either nice steps were not required, or we failed to find one.
  29.   if aipUseCount in Params.Options then
  30.     bestCount := EnsureRange(Params.Count, minCount, maxCount)
  31.   else
  32.     bestCount := minCount;
  33.   ABestStep := (AParams.FMax - AParams.FMin) / bestCount;
  34.   ABestStart := AParams.FMin - ABestStep;
  35. end;
  • Rebuild the IDE: "Tools" > "Build Lazarus with profile...". After a while, when Lazarus restarts, you can use the new feature.
« Last Edit: November 06, 2022, 07:26:19 pm by wp »

 

TinyPortal © 2005-2018