Page 1 of 1

Changing order of items in the legend

Posted: Fri Oct 23, 2009 11:56 pm
by 10546565
I need to change the order of a few items in the legend.

(I see that this is in the demo, but I do not have the current demo installed (only an older version, though I am running TChart 8.05)).

How can I move items around?

Thank you,

Ed Dressel

Re: Changing order of items in the legend

Posted: Mon Oct 26, 2009 9:03 am
by yeray
Hi Ed Dressel,

In TeeChart v8 you can directly access to the legend items as shown in the demo at What's New\Welcome !\New in Legend\Items property. The only problem is that can't modify the number of items to show, only the legend style and text style. So you could personalize the text to show as follows:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
  Chart1.Legend.TextStyle:=ltsValue;
  Chart1.Legend.ColumnWidthAuto:=False;
  Chart1.Legend.ColumnWidths[0]:=100;

  Chart1.Draw;
  for i := 0 to Chart1.Legend.Items.Count - 1 do
    Chart1.Legend.Item[i].Text:='Item nÂș ' + inttostr(i+1);
end;
On the other hand you can also change the legend symbols like in the demo at All features\Miscellaneous\Legend\Symbol OnDraw:

Code: Select all

procedure TLegendSymbolDraw.LegendDraw(Sender: TObject; Series:TChartSeries;
                            ValueIndex:Integer; R:TRect);
var Flag : TBitmap;
begin
  if CheckBox1.Checked then
  begin

    Flag:=TBitmap.Create;  // create flag bitmap
    try
      Dec(R.Top,3);
      Inc(R.Bottom,3);

      // copy image from imagelist
      ImageList1.GetBitmap(ValueIndex,Flag);

      with Chart1.Canvas do
      begin
        StretchDraw(R,Flag);  // draw image to legend

        // draw border
        if Chart1.Legend.Symbol.Pen.Visible then
        begin
          Brush.Style:=bsClear;
          AssignVisiblePen(Chart1.Legend.Symbol.Pen);
          Rectangle(R,0);
        end;
      end;

    finally
      Flag.Free;  // destroy bitmap
    end;
  end;
end;
In fact, you can use custom drawing to draw a custom rectangle, custom text and custom symbols directly over the chart. But with v9 that will be available soon, it is ready a new CustomLegendTool, that will allow much more customization:

Code: Select all

procedure TTool_CustomLegendForm.DrawCustomLegend();
begin
  with CustomLegendTool do
  begin
    ParentChart := Chart1;
    Grid.OnDrawCell := StringGrid1DrawCell;
    If ComboBox1.ItemIndex =0 then
    begin
      with Grid do
      begin
        ColCount := 6;
        RowCount := 3;
        RowHeights[0] := 30;
        RowHeights[1] := 30;
        RowHeights[2] := 30;
        ColWidths[1] := 70;        
        Height := 95;
        Width := 390;
      end;
    end
    else begin
      with Grid do
      begin
        ColCount := 2;
        RowCount := 3;
        RowHeights[0] := 30;
        RowHeights[1] := 30;
        RowHeights[2] := 30;
        ColWidths[1] := 190;
        Height := 95;
        Width := 250;
        Shape.Width := 270;
      end;
    end;
  end;
end;

procedure TTool_CustomLegendForm.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  I, J: Integer;
  Data: array[1..5,0..3] of String;
  ABitmap: TBitmap;
  FixRect, TopRect, BotRect: TRect;
  BmpWidth: Integer;
begin
  if ComboBox1.ItemIndex=0 then
  begin
  //add some data
  for I := 1 to 5 do
    for J := 0 to 2 do
      Data[I,J] := Format('%0:3d', [round(Chart1[J].YValue[I-1])]);

  if (ACol = 0) then
  begin
    //draw the bitmap
    FixRect := Rect;
    ABitmap := TBitmap.Create;
    try
      //  ABitmap.LoadFromFile('BitmapGreen.bmp'); //use for a single image
      ImageList1.GetBitmap(ARow, ABitmap);       //different images
      BmpWidth := (Rect.Bottom - Rect.Top);
      FixRect.Right := Rect.Left + BmpWidth;
      CustomLegendTool.Grid.Canvas.StretchDraw(FixRect, ABitmap);
    finally
      ABitmap.Free;
    end;
  end
  else
  begin
    //output the text
    TopRect := Rect;
    TopRect.Bottom := TopRect.Top+(TopRect.Bottom-TopRect.Top) div 2;
    BotRect := Rect;
    BotRect.Top := TopRect.Bottom;

    with CustomLegendTool.Grid.Canvas.Font do
    begin
      Name := 'Arial';
      Size := 8;
      Style := [fsItalic];
      Color := clBlue;
    end;
    CustomLegendTool.Grid.Canvas.TextRect(TopRect, TopRect.Left+5, TopRect.Top+2, 'value is:');

    with CustomLegendTool.Grid.Canvas.Font do
    begin
      Name := 'Arial';
      Size := 12;
      Style := [fsBold];
      Color := clBlue;
    end;
    CustomLegendTool.Grid.Canvas.TextRect(BotRect, BotRect.Left+10, BotRect.Top, Data[ACol,ARow]);
  end;
  end
  else begin

  //add some data text
  Data[1,0] := 'My custom Text 1';
  Data[1,1] := 'My custom Text 2';
  Data[1,2] := 'My custom Text 3';

  if (ACol = 0) then
  begin
    //draw the bitmap
    FixRect := Rect;
    ABitmap := TBitmap.Create;
    try
      //  ABitmap.LoadFromFile('BitmapGreen.bmp'); //use for a single image
      ImageList1.GetBitmap(ARow, ABitmap);       //different images
      BmpWidth := (Rect.Bottom - Rect.Top);
      FixRect.Right := Rect.Left + BmpWidth;
      CustomLegendTool.Grid.Canvas.StretchDraw(FixRect, ABitmap);
    finally
      ABitmap.Free;
    end;
  end
  else
  begin
    //output the text
    TopRect := Rect;
    TopRect.Bottom := TopRect.Top+(TopRect.Bottom-TopRect.Top) div 2;
    BotRect := Rect;
    BotRect.Top := TopRect.Top ;

    with CustomLegendTool.Grid.Canvas.Font do
    begin
      Name := 'Arial';
      Size := 8;
      Style := [fsItalic];
      Color := clBlue;
    end;
//    CustomLegendTool.Grid.Canvas.TextRect(TopRect, TopRect.Left+5, TopRect.Top+2, 'value is:');

    with CustomLegendTool.Grid.Canvas.Font do
    begin
      Name := 'Arial';
      Size := 12;
      Style := [fsBold];
      Color := clBlue;
    end;
    CustomLegendTool.Grid.Canvas.TextRect(BotRect, BotRect.Left+10, BotRect.Top + 10, Data[ACol,ARow]);
  end;
  end;
end;

Re: Changing order of items in the legend

Posted: Mon Oct 26, 2009 2:21 pm
by 10546565
Thank you for the response. I don't see anything in your post that shows off reordering, e.g. switching item #1 with #2. Is that not possible?

Ed Dressel

Re: Changing order of items in the legend

Posted: Mon Oct 26, 2009 3:04 pm
by yeray
Hi Ed Dressel,

In the first snipped of code I've changed all the legend items text. If you only want to change two of them you have to do something similar...

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
var tmp: string;
begin
  tmp:=Chart1.Legend.Item[0].Text;
  Chart1.Legend.Item[0].Text:=Chart1.Legend.Item[1].Text;
  Chart1.Legend.Item[1].Text:=tmp;
end;