Stacked Bar Chart and Legend

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Yeray
Site Admin
Site Admin
Posts: 9612
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Stacked Bar Chart and Legend

Post by Yeray » Wed Mar 19, 2014 10:56 am

Hi again Errol,
Yeray wrote:Regarding the legend text alignment. I'll create a new ticket for it.
Just found setting TextStyle to ltsPlain seem to fix it.

Code: Select all

Chart1.Legend.TextStyle:=ltsPlain;
Note the ltsXValue, ltsValue, ltsPercent, ltsXandValue, ltsXandPercent, ltsLeftPercent and ltsLeftValue TextStyles force the alignment to the Right.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Errol
Newbie
Newbie
Posts: 75
Joined: Thu Jul 11, 2013 12:00 am

Re: Stacked Bar Chart and Legend

Post by Errol » Tue Apr 01, 2014 8:25 pm

Good morning Yeray

I appear to have solved how to left-justify legend text in a self-stacked bar chart which uses a single series, and where the legend text is defined by assigning XLabelsSource to a field in the data set other than the primary bar axis. This results in the legend having two components, separated by chr(6), which need to be parsed and edited using an OnGetLegendText event. I found that if the chr(6) separator is removed in the editing procedure, the remaining legend text is right justified, but if it is left in the text string, the legend text is left-justified (see attached images). I have attached the OnGetLegendText code for the two cases.

Thanks and regards

Errol
Left_Justified_Legend_Text.png
Left_Justified_Legend_Text.png (2.82 KiB) Viewed 5882 times
Right_Justified_Legend_Text.png
Right_Justified_Legend_Text.png (2.77 KiB) Viewed 5883 times

Code: Select all

procedure GetLegendText(Sender: TCustomAxisPanel;
         LegendStyle: TLegendStyle; Index: Integer; var LegendText: string);
var
  iPos: integer;
begin
  iPos := Pos(Chr(6),LegendText);
  if (iPos = 0) then
    LegendText := ' '
  else
  begin
//  Left-justified legend text code
    LegendText := Copy(LegendText,iPos,Length(LegendText));
  end;
end;

procedure GetLegendText(Sender: TCustomAxisPanel;
        LegendStyle: TLegendStyle; Index: Integer; var LegendText: string);
var
  iPos: integer;
begin
  iPos := Pos(Chr(6),LegendText);
  if (iPos = 0) then
    LegendText := ' '
  else
  begin
//  Right-justified legend text code
    LegendText := Copy(LegendText,iPos+1,Length(LegendText));
  end;
end;


Errol
Newbie
Newbie
Posts: 75
Joined: Thu Jul 11, 2013 12:00 am

Re: Stacked Bar Chart and Legend

Post by Errol » Fri Apr 04, 2014 2:27 am

Good afternoon Yeray

I have made good progress with bar charts representing geological columns but I have a persistent error that I cannot correct.

I use a self-stacked bar chart with a single series for the chart and another series for the legend. The field used for the legend text (Rock Type) is defined by the XLabelsSource setting, with an OnGetLegendText event to parse and edit the legend text to remove the default legend text (the bar length).

When I switch to another table in my database and draw a standard line series, I have a problem. If the line series is drawn first, the legend correctly shows the series name, but if drawn after drawing a bar chart, it displays the series name followed by the Rock Type from the bar chart. When I go from bar chart to line series, I clear all the series, etc, but clearly not clearing legend settings. Can you please suggest how I might do this.

Best regards

Errol

Yeray
Site Admin
Site Admin
Posts: 9612
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Stacked Bar Chart and Legend

Post by Yeray » Fri Apr 04, 2014 9:58 am

Hi Errol,
Errol wrote:I use a self-stacked bar chart with a single series for the chart and another series for the legend. The field used for the legend text (Rock Type) is defined by the XLabelsSource setting, with an OnGetLegendText event to parse and edit the legend text to remove the default legend text (the bar length).

When I switch to another table in my database and draw a standard line series, I have a problem. If the line series is drawn first, the legend correctly shows the series name, but if drawn after drawing a bar chart, it displays the series name followed by the Rock Type from the bar chart. When I go from bar chart to line series, I clear all the series, etc, but clearly not clearing legend settings. Can you please suggest how I might do this.
Please, note that without a simple example we can run as is to reproduce the problem here, it's difficult for us to be accurate in our replies.
I've taken the example from here, and modified a bit to match your description.
I think the problem will be that you may be assigning the OnGetLegendText event when creating the bars, but you may be missing to reset it when clearing the chart:

Code: Select all

Chart1.OnGetLegendText:=nil;
See the project attached:
testBarsLine.zip
(2.28 KiB) Downloaded 440 times
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Errol
Newbie
Newbie
Posts: 75
Joined: Thu Jul 11, 2013 12:00 am

Re: Stacked Bar Chart and Legend

Post by Errol » Fri Apr 04, 2014 9:38 pm

Good morning Yeray

Thanks for your reply concerning bar chart legends being displayed in a line chart. I had already set the OnGetLegendText event to nil so this did not solve the problem. However my problem went away once I set Chart.Legend.TextStyle := ltsPlain to cure the bar chart legend placement. This is because the ltsPlain style uses only the value in XLabelsSource, so I do not require an OnGelLegendText event to parse and edit the LegendText. Clearly the OnGetLegendText event was causing the line chart legend error, so I guess I was not turning it off at the right place. Anyway, problem is now solved.

Thanks and regards

Errol

Post Reply