Tchart更改标签

时间:2017-04-03 10:41:46

标签: delphi

我需要一些帮助才能更改图表上显示的标签 我使用此代码填充tchart:

   With Series1 do
    begin
     clear;
     Add (v[1], 'abcdef', clRed);
     Add (v[2], 'aaaaaaaaaaaaaaaaaaaaaaaa', clBlue);
     Add (v[3], 'bbbbbbbbbbbbbb', clGreen);
    end;

我得到这张图表: image of chart

请帮助我将矩形上方的标签从图像中显示的文本更改为其他变量通过代码;另外请告诉我怎么做才能在x轴上显示长文本以及如何在多行上分解

1 个答案:

答案 0 :(得分:0)

首先,我假设你的“v”变量是一个整数数组;我的例子是它的名字“BarValue”。表格中添加了一个带有2D条形系列的TChart。

构造标签时,你需要用“TeeLineSeparator”替换空格和分词,然后重新扫描它们并在“TeeLineSeparator”中插入更长的单词以强制它们换行。 (在我的例子中,我把长长的“aaaaaaaaa ......”标签随机分成了几个字。)

这个例子在Delphi 10.2 Tokyo和Delphi 2007中有效。

type    
  TForm1 = class(TForm)
    Chart1: TChart;
    Series1: TBarSeries;

    procedure FormCreate(Sender: TObject);
    procedure Series1GetMarkText(Sender: TChartSeries; ValueIndex: Integer; var MarkText: string);

  private
   BarMark : array[1..3] of string;
   BarValue : array[1..3] of integer;
   BarLabel : array[1..3] of string;
   BarColor : array[1..3] of TColor;
  end;


procedure TForm1.FormCreate(Sender: TObject);

var
   i : integer;

begin
   Chart1.Legend.Visible := false;

   // This maybe needed on earlier versions of TChart (e.g. Delphi 2007-)
   //   to make room for multi-line labels
   //Chart1.Axes.Bottom.LabelSize := 32;

   Series1.OnGetMarkText := Series1GetMarkText;

   BarValue[1] := 100;
   BarValue[2] := 200;
   BarValue[3] := 300;

   BarLabel[1] := 'abcdefg';
   BarLabel[2] := 'aaaa'+TeeLineSeparator+'aaaaaaaaa'+TeeLineSeparator+'aaaaaaaaaaa';
   BarLabel[3] := 'bbbbbbbbbbbbbb';

   BarColor[1] := clRed;
   BarColor[2] := clBlue;
   BarColor[3] := clGreen;

   BarMark[1] := 'ok1';
   BarMark[2] := 'ok2';
   BarMark[3] := 'ok3';

   Series1.Clear;
   for i := 1 to 3 do
      Series1.Add (BarValue[i], BarLabel[i], BarColor[i]);
end;

procedure TForm1.Series1GetMarkText(Sender: TChartSeries; ValueIndex: Integer; var MarkText: string);

begin
   MarkText := BarMark[ValueIndex+1];
end;

DFM:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 374
  ClientWidth = 702
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Chart1: TChart
    Left = 204
    Top = 40
    Width = 400
    Height = 250
    Title.Text.Strings = (
      'TChart')
    View3D = False
    TabOrder = 0
    DefaultCanvas = 'TGDIPlusCanvas'
    ColorPaletteIndex = 13
    object Series1: TBarSeries
      OnGetMarkText = Series1GetMarkText
      XValues.Name = 'X'
      XValues.Order = loAscending
      YValues.Name = 'Bar'
      YValues.Order = loNone
    end
  end
end