Forum > TAChart

[solved] how to change axis number format

(1/1)

Muso:
(This thread is a spin-off from https://forum.lazarus.freepascal.org/index.php/topic,59909.msg447407.html#msg447407)

I need to change the number format of the x-axis to day:hour:minute on demand.

The XY data are in a TLineSeries and its x-values are minutes.

So I am looking for a method, that the user can choose to display the time instead of in minutes also in the format day.hour:minute
How can this be achieved?

wp:
You can provide a handler for the OnGetMarkText event of the x axis. It gets the x value (minutes, in your case) and returns the displayed text. So, if the user wants to see minutes, don't use this handler. But if he wants to see days.hours:minutes you can hook the handler to the event, do the conversion and return the result in the handler. Not 100% sure, but you may have to adjust the Chart.BottomAxis.Intervals.MaxLength to avoid overlapping labels when switching between these two cases.

Muso:

--- Quote from: wp on July 14, 2022, 10:05:01 pm ---You can provide a handler for the OnGetMarkText event of the x axis. It gets the x value (minutes, in your case) and returns the displayed text.

--- End quote ---

Many thanks!

For the case someone else needs this too, here is my solution:

Notes:
name of the chart is "SIXCH", I have 3 context menu items *MI with which the user can select if the time should be in days, minutes or hours. I also have an option to output the time in the format [day:hour:minute]

Here is the main procedure:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TSIXControl.SCSIXCHAxisList1GetMarkText(Sender: TObject;  var AText: String; AMark: Double);begin if MainForm.TimeDaysHoursMinMI.Checked then begin  AText:= SIXControl.CalcDaysHoursMins(AMark);  MainForm.SIXCH.BottomAxis.Intervals.MaxLength:= 100;  MainForm.SIXCH.BottomAxis.Title.Caption:= 'Time [dd:hh:mm]'; end else begin  // do nothing  AText:= Format(MainForm.SIXCH.BottomAxis.Marks{%H-}.Format, [AMark]);  // use the default width for labels  MainForm.SIXCH.BottomAxis.Intervals.MaxLength:= 50;  if MainForm.TimeMinuteMI.Checked then   MainForm.SIXCH.BottomAxis.Title.Caption:= 'Time [min]'  else if MainForm.TimeHourMI.Checked then   MainForm.SIXCH.BottomAxis.Title.Caption:= 'Time [hour]'  else if MainForm.TimeDayMI.Checked then   MainForm.SIXCH.BottomAxis.Title.Caption:= 'Time [day]'; end;end;
And here is the function that performs the formatting:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TSIXControl.CalcDaysHoursMins(x : double) : string;// reformats x to the format day:hour:minutevar days, hours, minutes : integer;begin // only if the data format is actually used, we do the calculation if not MainForm.TimeDaysHoursMinMI.checked then begin  if MainForm.TimeMinuteMI.checked then   // we explicitly want decimals here   result:= Format('%.2f', [x])  else   // take the chart's formatting   result:= Format(MainForm.SIXCH.BottomAxis.Marks{%H-}.Format, [x]);  exit; end;  days:= 0; hours:= 0; minutes:= 0; if MainForm.TimeMinuteMI.Checked then begin  days:= floor(x / 1440);  hours:= floor((x / 1440 - days) * 24);  minutes:= round(((x / 1440 - days) * 24 - hours) * 60); end else if MainForm.TimeHourMI.Checked then begin  days:= floor(x / 24);  hours:= floor((x / 24 - days) * 24);  minutes:= round(((x / 24 - days) * 24 - hours) * 60); end else if MainForm.TimeDayMI.Checked then begin  days:= floor(x);  hours:= floor((x - days) * 24);  minutes:= round(((x - days) * 24 - hours) * 60); end;  result:= Format('%.2d',[days]) + ':'          + Format('%.2d',[hours]) + ':' + Format('%.2d',[minutes]);end;

Navigation

[0] Message Index

Go to full version