Recent

Author Topic: [SOLVED] TAChart Replace Y-Axis Numbers with Text  (Read 1577 times)

old_geek

  • Newbie
  • Posts: 5
[SOLVED] TAChart Replace Y-Axis Numbers with Text
« 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.
« Last Edit: March 25, 2023, 07:55:11 pm by old_geek »

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: TAChart Replace Y-Axis Numbers with Text
« Reply #1 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:
  • Add a TListChartSource to the form, name it "AxisLabels" (or similar).
  • Assign it to the Marks.Source property of the Chart.LeftAxis, and set its Marks.Style to smsLabel (if you do this in code you must add the unit TAChartUtils to "uses"
  • In the form's OnCreate handler add the label values and the label texts to the AxisLabels source. In the Add method, you can enter the axis texts (as 3rd parameter) and their y values as 2nd parameter. What about x (1st parameter)? Well since this source contains the labels of the y axis, x is not needed, it can hold any values. But I'd recommend to duplicate the y values here because maybe once you want to rotate the axes (plot y horizontally), and then you already have these labels correctly in place. So, your event handler should be extended by the highlightes lines:
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.

old_geek

  • Newbie
  • Posts: 5
Re: TAChart Replace Y-Axis Numbers with Text
« Reply #2 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!
« Last Edit: March 25, 2023, 07:51:31 pm by old_geek »

 

TinyPortal © 2005-2018