Recent

Author Topic: Horizontal bar chart (missing labels)  (Read 3086 times)

JD

  • Hero Member
  • *****
  • Posts: 1810
Horizontal bar chart (missing labels)
« on: May 21, 2018, 08:28:45 pm »
Hi there everyone,

I had created a normal vertical bar chart with TAChart that I now want to show as a horizontal bar chart because it has long bar labels. I have modified the chart bar series and set AxisIndexX to the index of the LeftAxis and AxisIndexY to the index of the BottomAxis. The chart bar series Marks->Style property is set to smsValue.

However for some reason the bottom axis labels that used to be visible are no longer visible when the chart is rotated. It normally should now be on the left axis (see screenshot). From the screenshot, the bars have been shortened to make room for the labels but the labels are not visible!  :(  :(

Can anyone point out what I'm doing wrong?

Thanks,

JD





« Last Edit: May 21, 2018, 09:15:58 pm by JD »
Windows (10, 7) - Lazarus 2.1/FPC 3.2, Delphi

Indy 10.6 series; mORMot; Zeos 7.3; SQLite, Firebird, PostgreSQL & MariaDB; VirtualTreeView 5.5.3 R1

wp

  • Hero Member
  • *****
  • Posts: 7920
Re: Horizontal bar chart (missing labels)
« Reply #1 on: May 21, 2018, 10:07:27 pm »
It's hard to tell from the distance what you did wrong. If possible upload a small demo which shows the issue.

Did you look at the tutorial http://wiki.lazarus.freepascal.org/TAChart_Tutorial:_BarSeries#Horizontal_bar_series)? And the tutorial project is also avaiable in your Lazarus installation in folder (lazarus)/tachart/tutorials/bar_series, both in a vertical and a horizontal bar series version.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

JD

  • Hero Member
  • *****
  • Posts: 1810
Re: Horizontal bar chart (missing labels)
« Reply #2 on: May 21, 2018, 10:49:12 pm »
It's hard to tell from the distance what you did wrong. If possible upload a small demo which shows the issue.

Did you look at the tutorial http://wiki.lazarus.freepascal.org/TAChart_Tutorial:_BarSeries#Horizontal_bar_series)? And the tutorial project is also avaiable in your Lazarus installation in folder (lazarus)/tachart/tutorials/bar_series, both in a vertical and a horizontal bar series version.

Hi there wp. Yes I've looked at the tutorials, tried to follow them but I still have a problem with the labels. Here is a sample project.

Thanks,

JD
Windows (10, 7) - Lazarus 2.1/FPC 3.2, Delphi

Indy 10.6 series; mORMot; Zeos 7.3; SQLite, Firebird, PostgreSQL & MariaDB; VirtualTreeView 5.5.3 R1

wp

  • Hero Member
  • *****
  • Posts: 7920
Re: Horizontal bar chart (missing labels)
« Reply #3 on: May 22, 2018, 12:10:55 am »
Thanks for demo.

This issue, unfortunately, is not covered by the tutorial which has a separate chartsource for the labels. In your case, however, the axis labels are taken from the same chartsource which also provides the x/y values.

When you use the Marks.Style for the bottom axis of an unrotated chart (vertical bars) the labels are drawn at the x values of the ChartSource items.

When the bars are rotated the labels should still be drawn at the x positions, but since the vertical axis still is the y axis internally it picks the coordinates from the y value of the data point.

An axis does not "know" whether it is drawn for a normal or a rotated series, and therefore it cannot detect automatically that it should pick the other coordinate for label positioning of a rotated series. (This is different from the series which has the AxisIndexX and AxisIndexY properties).

Of course you can help the axis and exchange the x/y values in another chartsource assigned to the Marks.Source of the left axis. You can use a UserDefinedChartSource which picks the data from the main chartsource and swaps x and y.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.UserDefinedChartSource1GetChartDataItem(
  2.   ASource: TUserDefinedChartSource; AIndex: Integer; var AItem: TChartDataItem);
  3. begin
  4.   AItem.X := PublicPrioritaire.Item[AIndex]^.Y;
  5.   AItem.Y := PublicPrioritaire.Item[AIndex]^.X;
  6.   AItem.Text := PublicPrioritaire.Item[AIndex]^.Text;
  7. end;  

Don't forget to set the PointsNumber of the UserDefinedChartSource to the data point count of the ListChartSource which provides the data:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormShow(Sender: TObject);
  2. begin
  3.   PublicPrioritaire.Clear;
  4.   PublicPrioritaire.Add(0, 29, 'Nord');
  5.   PublicPrioritaire.Add(1, 15, 'Sud');
  6.   PublicPrioritaire.Add(2, 11, 'Est');
  7.   PublicPrioritaire.Add(3, 5, 'Ouest');
  8.  
  9.   UserDefinedChartSource1.PointsNumber := PublicPrioritaire.Count;
  10. end;  

Assign this UserDefinedChartSource to the Marks.Source of the LeftAxis, and set its Marks.Style to smsLabel.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

JD

  • Hero Member
  • *****
  • Posts: 1810
Re: Horizontal bar chart (missing labels)
« Reply #4 on: May 22, 2018, 11:02:23 pm »
Excellent, just excellent! Thanks a lot wp. You and Ask have done a great deal of work to make TAChart as good as it is now. Thanks for all the time and effort you've put into it.

Cheers,

JD
Windows (10, 7) - Lazarus 2.1/FPC 3.2, Delphi

Indy 10.6 series; mORMot; Zeos 7.3; SQLite, Firebird, PostgreSQL & MariaDB; VirtualTreeView 5.5.3 R1

 

TinyPortal © 2005-2018