Lazarus

Programming => Graphics and Multimedia => TAChart => Topic started by: old_geek on March 25, 2023, 05:31:16 pm

Title: [SOLVED] TAChart Replace Y-Axis Numbers with Text
Post by: old_geek on March 25, 2023, 05:31:16 pm
Complexities of project have been removed such that this is simple representation of status and needs.

I want to generate a Chart that compares fuel consumption of two types of vehicles where tests were performed in a controlled environment. Data will represent 31 days and 2 vehicle brands. Results to be broken into 4 categories: (No data, Below Advertisd, As Advertised, Better than Advertised)

The app will ensure that the Y-Axis will only have the values (0,1,2,3) BUT I would like to replace the numbers with the 4 categories of Text. The Y-Axis will show Day Number (0 - 31)

I have attached a project that demonstrates my basic requirements.

Platform Specifics:
Lazarus V 2.2.2
Date: 2022-05-15
FPC: V 3.2.2
Revision: lazarus_2_2_2
i386-win32-win32/win64
OS: Win 10

TAChart/README.txt shows latest change seems to indicate V1.0 (relevant?)

Can what I want to do be accomplished? If so, how can I accomplish it? Maybe I am over-complicating my requirements? Maybe I can ony document what the Y-Axis values represent (as I have done in the example)? Sure could use some help and direction. Thanks in advance.
Title: Re: TAChart Replace Y-Axis Numbers with Text
Post by: wp on March 25, 2023, 06:35:01 pm
The easiest way to replace the auto-generated y labels by specific labels is by using a dedicated TListChartSource for the y axis:
Code: Pascal  [Select][+][-]
  1. uses
  2.   TAChartUtils;
  3.  
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. begin
  6.   with AxisLabels do
  7.   begin
  8.     Add(0, 0, 'No data reported');
  9.     Add(0, 1, 'Below Advertised');
  10.     Add(0, 2, 'As Advertised');
  11.     Add(0, 3, 'Better than advertised');
  12.   end;
  13.   Chart1.LeftAxis.Marks.Source := AxisLabels;
  14.   Chart1.LeftAxis.Marks.Style := smsLabel;
  15.  
  16.   with DataSource do
  17.   begin
  18.     AddXYList(1,[1,3]);
  19.     AddXYList(2,[2,1]);
  20.     AddXYList(3,[3,2]);
  21.     AddXYList(4,[2,3]);
  22.     AddXYList(5,[1,2]);
  23.     AddXYList(6,[3,3]);
  24.     AddXYList(7,[1,1]);
  25.     AddXYList(8,[2,2]);
  26.     AddXYList(9,[0,0]);
  27.     AddXYList(10,[1,1]);
  28.     AddXYList(11,[2,2]);
  29.     AddXYList(12,[3,3]);
  30.   end;
  31. end;

TAChart/README.txt shows latest change seems to indicate V1.0 (relevant?)
No, not relevant, just out-dated. And it's hard to delete files written by other people who started the project but retired from it in the mean-time. A quite up-to-date documentation is https://wiki.lazarus.freepascal.org/TAChart_documentation.
Title: Re: TAChart Replace Y-Axis Numbers with Text
Post by: old_geek on March 25, 2023, 06:44:55 pm
@wp, Thank you so much! Your explanation is very easy to follow. I worked your solution into this demo project and the results speak for themselves! This will be very easy to implement in my main project.

Kudos to your clear explanation. I only wish had not wallowed in my conundrum so long before asking for help!
TinyPortal © 2005-2018