Page 1 of 1

Legend in TCandleSeries

Posted: Tue Sep 02, 2014 5:42 pm
by 16467044
ref: TCandleSeries

Hi,

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

Thanks,
Cheryll

Re: Legend in TCandleSeries

Posted: Wed Sep 03, 2014 12:05 pm
by yeray
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;

Re: Legend in TCandleSeries

Posted: Wed Sep 03, 2014 3:50 pm
by 16467044
Hi Yeray,

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

Cheryll