Legend in TCandleSeries

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Chartist
Newbie
Newbie
Posts: 60
Joined: Wed Sep 18, 2013 12:00 am

Legend in TCandleSeries

Post by Chartist » Tue Sep 02, 2014 5:42 pm

ref: TCandleSeries

Hi,

how can I make the legend "display for each candle":
Date, open, high, low, close?

Thanks,
Cheryll
Cheryll

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

Re: Legend in TCandleSeries

Post by Yeray » Wed Sep 03, 2014 12:05 pm

Hi Cheryll,

I'm afraid there isn't a LegendStyle for this, but you can always draw you own legend manually.
As a starting example:

Code: Select all

uses CandleCh, Math, TeCanvas, Types;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.View3D:=false;
  Chart1.Legend.Visible:=false;

  Chart1.AddSeries(TCandleSeries).FillSampleValues(10);

  Chart1.OnAfterDraw:=Chart1AfterDraw;

  Chart1.MarginRight:=25;
end;

procedure TForm1.Chart1AfterDraw(Sender: TObject);
var tmpCloseWidth, tmpHighWidth, tmpLowWidth, tmpOpenWidth: Integer;

  function Width(line: Integer): Integer;
  begin
    with Chart1[0] as TCandleSeries do
    begin
      tmpCloseWidth:=Chart1.Canvas.TextWidth(FormatFloat('#0.##', CloseValues[line]));
      tmpHighWidth:=Chart1.Canvas.TextWidth(FormatFloat('#0.##', HighValues[line]));
      tmpLowWidth:=Chart1.Canvas.TextWidth(FormatFloat('#0.##', LowValues[line]));
      tmpOpenWidth:=Chart1.Canvas.TextWidth(FormatFloat('#0.##', OpenValues[line]));
      result:=tmpCloseWidth+tmpHighWidth+tmpLowWidth+tmpOpenWidth;
    end;
  end;

var maxCloseWidth, maxHighWidth, maxLowWidth, maxOpenWidth: Integer;

  function MaxRowWidth: Integer;
  var i: Integer;
  begin
    result:=maxCloseWidth+maxHighWidth+maxLowWidth+maxOpenWidth;
    for i:=0 to Chart1[0].Count-1 do
    begin
      result:=Max(result, Width(i));
      maxCloseWidth:=Max(maxCloseWidth, tmpCloseWidth);
      maxHighWidth:=Max(maxHighWidth, tmpHighWidth);
      maxLowWidth:=Max(maxLowWidth, tmpLowWidth);
      maxOpenWidth:=Max(maxOpenWidth, tmpOpenWidth);
    end;
  end;

var i, tmpLeft, tmpTop, tmpWidth, tmpHeight, rowHeight: Integer;
begin
  maxCloseWidth:=Chart1.Canvas.TextWidth('Close');
  maxHighWidth:=Chart1.Canvas.TextWidth('High');
  maxLowWidth:=Chart1.Canvas.TextWidth('Low');
  maxOpenWidth:=Chart1.Canvas.TextWidth('Open');

  tmpTop:=Chart1.ChartRect.Top+10;
  tmpWidth:=MaxRowWidth+10;
  tmpLeft:=Chart1.Width-tmpWidth-20;
  rowHeight:=Chart1.Canvas.TextHeight('Hj')+2;
  tmpHeight:=(Chart1[0].Count+1)*rowHeight+2;

  with Chart1.Canvas do
  begin
    Rectangle(tmpLeft, tmpTop, tmpLeft+tmpWidth, tmpTop+tmpHeight);
    TextOut(tmpLeft+2, tmpTop+2, 'Close');
    TextOut(tmpLeft+2+maxCloseWidth+2, tmpTop+2, 'High');
    TextOut(tmpLeft+2+maxCloseWidth+2+maxHighWidth+2, tmpTop+2, 'Low');
    TextOut(tmpLeft+2+maxCloseWidth+2+maxHighWidth+2+maxLowWidth+2, tmpTop+2, 'Open');

    with Chart1[0] as TCandleSeries do
      for i:=0 to Count-1 do
      begin
        TextOut(tmpLeft+2, tmpTop+2+(i+1)*rowHeight, FormatFloat('#0.##', CloseValues[i]));
        TextOut(tmpLeft+2+maxCloseWidth+2, tmpTop+2+(i+1)*rowHeight, FormatFloat('#0.##', HighValues[i]));
        TextOut(tmpLeft+2+maxCloseWidth+2+maxHighWidth+2, tmpTop+2+(i+1)*rowHeight, FormatFloat('#0.##', LowValues[i]));
        TextOut(tmpLeft+2+maxCloseWidth+2+maxHighWidth+2+maxLowWidth+2, tmpTop+2+(i+1)*rowHeight, FormatFloat('#0.##', OpenValues[i]));
      end;
  end;
end;
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

Chartist
Newbie
Newbie
Posts: 60
Joined: Wed Sep 18, 2013 12:00 am

Re: Legend in TCandleSeries

Post by Chartist » Wed Sep 03, 2014 3:50 pm

Hi Yeray,

this is a very useful code for many purposes.
Thank you!

Cheryll
Cheryll

Post Reply