Page 1 of 1

Creating a custom legend

Posted: Thu Jun 02, 2016 7:39 pm
by 16577368
Delphi Seattle, TChart Pro 2015.16

I need to create a custom legend. The chart only has one bar series, but negative values are displayed in red (additional lump savings needed for today) and represent a different value than positive value (cash balance at life expectancy).

How can I in code create a custom legend that shows 2 items, one with the series color and one with clRed color box, each with custom description/text. (I checked out the extended demo, but it gives me a "Control has no parent window" exception when I look at the New Chart Tools/Custom Legend Tool demo).

Thank you,

Ed Dressel

PS: I really don't like changing my login every year--I lose easy access to my previous posts. Sigh.

Re: Creating a custom legend

Posted: Fri Jun 03, 2016 11:09 am
by yeray
Hello,

It could be it's easier than that. You can hide your TBarSeries from the legend and create two extra series without values but with color and title, just to be shown at the legend. Ie:

Code: Select all

uses Series;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
  with Chart1.AddSeries(TBarSeries) as TBarSeries do
  begin
    for i:=0 to 5 do
      if i mod 2 = 0 then
        AddBar(50+random*50, '', clTeeColor)
      else
        AddBar(-50-random*50, '', clRed);

    ShowInLegend:=false;
    MultiBar:=mbNone;
  end;

  with Chart1.AddSeries(TBarSeries) as TBarSeries do
  begin
    Color:=Chart1[0].Color;
    Title:='Positive';
  end;

  with Chart1.AddSeries(TBarSeries) as TBarSeries do
  begin
    Color:=clRed;
    Title:='Negative';
  end;
end;

Re: Creating a custom legend

Posted: Thu Jun 30, 2016 5:00 pm
by 16575285
Hi,

I want to do something similar, except that:

- I'm using a TLineSeries, so I need to keep all the points in one series so that they are drawn properly connected;
- Values are coloured according to some quality flag which affects only a few points in the series.

So I'd like to keep the default legend but add at run-time some custom lines, describing the quality flags, that include a coloured symbol and some text. Is this possible?

Thanks

Toreba

Re: Creating a custom legend

Posted: Mon Jul 04, 2016 7:29 am
by yeray
Hi Toreba,
Toreba wrote:So I'd like to keep the default legend but add at run-time some custom lines, describing the quality flags, that include a coloured symbol and some text. Is this possible?
The solution would be almost the same. You can add as many dummy series (series without values) as new items you want in the legend.
The only difference is that you don't want to hide the original series from the legend.

If you still find any doubt about it, please don't hesitate to let us know.

Re: Creating a custom legend

Posted: Mon Jul 04, 2016 8:37 am
by 16575285
Hi Yeray,

Thanks for the suggestion. This would work well if we could prevent the dummy series from appearing in the list of series shown by TChartEditor. Is this possible?

Regards

Toreba

Re: Creating a custom legend

Posted: Mon Jul 04, 2016 9:40 am
by yeray
Hi Toreba,
Toreba wrote:This would work well if we could prevent the dummy series from appearing in the list of series shown by TChartEditor. Is this possible?
Yes, you can set a series ShowInEditor property to false.

Re: Creating a custom legend

Posted: Mon Jul 04, 2016 11:43 am
by 16575285
Yeray,

Ah yes, so there is. That's excellent. Thank you very much for your help.

Regards

Toreba