您的位置:首页 > 运维架构 > Apache

TeeChart Pro VCL/FMX教程(四):轴控制(下)

2019-05-15 13:48 1396 查看

下载TeeChart Pro VCL/FMX最新版本

附加轴

复制轴

    TeeChart提供5个轴与数据系列相关联:Left、Top、Bottom、Right和Depth。向图表添加新系列时,您可以定义系列应与哪些轴相关(转到“选项卡”“General”页面)。您可以使用Axis Customdraw方法在图表上的任何位置重复4个前轴中的任何一个(或全部)。请注意,此方法会复制Axis,但不会添加新的自定义轴。

    您将找到这个名为“CustAxisProject1”的示例,其中包含TeeChart示例代码:

//fill the Series for this example with random data
procedure TForm1.BitBtn1Click(Sender: TObject);
Var t:integer;
begin
  For t := 0 To 20 do
    Series1.AddXY(t, Random(100) - Random(70), '', clRed);
end;

//Put this code in the OnBeforeDrawValues() event:
procedure TForm1.Series1BeforeDrawValues(Sender: TObject);
var posaxis :Integer;
begin
  With Chart1 do
  begin
    If Axes.Left.Maximum > 0 Then
    begin
      
      //When scrolling or on zoom always keep the gridlines enclosed in the Chart rectangle
      Canvas.ClipRectangle(ChartRect);

      //Always draw the 2nd vertical Axis at the middle point of the Chart
      posaxis := (ChartRect.Left) + (ChartRect.Right - ChartRect.Left) div 2;
      Axes.Left.CustomDraw(posaxis - 10, posaxis - 20, posaxis, True);

      //Draw the 2nd Horizontal axis at the level of "10" on the vertical axis
      posaxis := (Axes.Left.CalcYPosValue(10));
      Axes.Bottom.CustomDraw(posaxis + 10, posaxis + 40, posaxis, True);

      Canvas.UnClipRectangle;
    end;
  end;
end;

自定义轴

    在此示例中,TeeChart将绘制新轴,一个水平,一个垂直位于图表的中心。当您滚动图表(用鼠标右键拖动)时,新的垂直轴将始终保持在图表的中心,新的水平轴将垂直滚动上下移动。新轴是默认轴的精确副本。

多个自定义轴

    与PositionPercent和拉伸属性一起,可以在图表上的任何位置浮动无限轴。滚动,缩放和轴命中检测也适用于自定义创建的轴。现在可以通过图表编辑器在设计时创建额外的轴,也可以在运行时通过几行代码创建附加轴:

通过图表编辑器

    TeeChart为您提供在设计时创建自定义轴的功能,使其能够以TeeChart的Teechart格式保存。要实现此目的,请打开图表编辑器并单击“Axis”选项卡,然后选择“+”按钮以添加自定义轴。然后选择位置选项卡确保您突出显示新的自定义轴。此页面上的“Horizontal”复选框允许您将新的自定义轴定义为水平轴或将其保留为默认垂直轴。如上所述,此页面的其余部分和Axis页面中的其他选项卡可用于更改自定义轴的比例,增量,标题,标签,刻度,次刻度和位置。要将此新的自定义轴与所需的数据系列相关联,请选择“Series”选项卡,然后转到“General”页面,其中下拉组合框“Horizontal Axis”和“Vertical Axis”将允许您根据先前是否定义选择新的自定义轴它是垂直的或水平的。

通过代码

procedure TForm1.BitBtn2Click(Sender: TObject);
Var MyAxis : TChartAxis ;
begin
  MyAxis := TChartAxis.Create(  Chart1 );
  Series2.CustomVertAxis := MyAxis;

  //You can modify any property of the new created axes, such as the axis color or axis title
  With MyAxis do
  begin
    Axis.Color:=clGreen ;
    Title.Caption := 'Extra axis' ;
    Title.Font.Style:=[fsBold];
    Title.Angle := 90;
    PositionPercent := 20; //percentage of Chart rectangle
  end;
end;

    然后,您可以使用StartPosition和EndPosition属性将新轴与图表的整体关系定位。

StartPosition:=50;
EndPosition:=100;

    这些数字表示为图表矩形的百分比,其中0(零)(在垂直轴的情况下)为Top。这些属性可以应用于标准轴,以在图表中创建完全分区的“SubCharts”;。

 With Chart1.Axes.Left do
  begin
    StartPosition:=0;
    EndPosition:=50;
    Title.Caption:='1st Left Axis';
    Title.Font.Style:=[fsBold];
  end;

    以上2个编码示例与以下数据结合使用:

var t: integer;
begin
  for t := 0 to 10 do
  begin
    Series1.AddXY(t,10+t,'',clTeeColor);
    if t > 1 then
      Series2.AddXY(t,t/2,'',clTeeColor);
  end;

    将显示以下图表:

多轴

    另一种添加自定义轴的技术如下,使用“Axis List”作为焦点,使用“List Add”,然后通过“Axis by Index”访问:

//Add data to Series:
procedure TForm1.FormCreate(Sender: TObject);
begin
  Series1.FillSampleValues(10);
  Series2.FillSampleValues(10);
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
 tmpVertAxis: TChartAxis;
 tmpHorizAxis: TChartAxis;
begin
  {Add vertical Axis}
  Chart1.CustomAxes.Add;
  tmpVertAxis:=Chart1.CustomAxes[0];
  tmpVertAxis.PositionPercent:=50;
  Series1.CustomVertAxis:=tmpVertAxis;

  {Add horizontal Axis}
  Chart1.CustomAxes.Add;
  tmpHorizAxis:=Chart1.CustomAxes[1];

  tmpHorizAxis.Horizontal:=True;
  tmpHorizAxis.Axis.Color:=clGreen;
  tmpHorizAxis.PositionPercent:=50;
  Series1.CustomHorizAxis:=tmpHorizAxis;
end;

    建议在使用自定义轴时要小心,因为很容易开始用新轴填充屏幕并且无法跟踪您想要管理的轴!

轴事件

    Axis事件提供运行时灵活性,可以修改Axis标签并在Axis Clicks上为用户提供交互性。

OnClickAxis

procedure TForm1.Chart1ClickAxis(Sender: TCustomChart; Axis: TChartAxis;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  //Shows Axis point clicked when click on Bottom Axis.

  if Axis = Chart1.Axes.Bottom Then
     ShowMessage('Clicked Bottom Axis at '  + FloatToStr(Chart1.Axes.Bottom.CalcPosPoint(X)));
end;

OnGetAxisLabel

procedure TForm1.Chart1GetAxisLabel(Sender: TChartAxis;
  Series: TChartSeries; ValueIndex: Integer; var LabelText: String);
begin
  if Sender = Chart1.Axes.Bottom Then
     Case StrToInt(FormatDateTime('mm', StrToDate(LabelText))) of
       1,2,3 : LabelText := '1st Qtr';
       4,5,6 : LabelText := '2nd Qtr';
     end;
end;

OnGetNextAxisLabel

procedure TForm1.Chart1GetNextAxisLabel(Sender: TChartAxis;
  LabelIndex: Integer; var LabelValue: Double; var Stop: Boolean);
begin
  Stop:=False;

  if Sender = Chart1.Axes.Bottom Then
  begin
    if LabelValue>=5 then LabelValue:=LabelValue+5
                     else LabelValue:=5;
  end
  else
    Stop:=True;

end;

    以上示例将开始在每5个点的底轴标记处标记为'5'。其他轴标签不受影响。

查看Teechart.Net系列教程>>

(adsbygoogle = window.adsbygoogle || []).push({});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  TeeChart VCL Apache Axis