【TeeChart for Java教程】(四)上——轴控制:附加轴

【下载TeeChart for Java最新版本】

上一篇文章讲到轴控制——关键领域部分,本文将续讲轴控制的附加轴部分。

(一)附加轴

1.1 复制轴

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

//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 LeftAxis.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;
      LeftAxis.CustomDraw(posaxis - 10, posaxis - 20, posaxis, True);
      //Draw the 2nd Horizontal axis at the level of "10" on the vertical axis
      posaxis := (LeftAxis.CalcYPosValue(10));
      BottomAxis.CustomDraw(posaxis + 10, posaxis + 40, posaxis, True);
      Canvas.UnClipRectangle;
    end;
  end;
end;

teechart_for_java

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

1.2 多个自定义轴(仅限专业版)

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

1.2.1 通过图表编辑器

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

1.2.2 通过代码

Line line1 = new Line();
Line line2 = new Line();
  tChart2.getAspect().setView3D(false);
  tChart2.getPanel().getGradient().setVisible(true);
  tChart2.getHeader().setText("TeeChart Multiple Axes");
  tChart2.getSeries(0).add(line1);
  tChart2.getSeries(1).add(line2);
    for(int t = 0; t <= 10; ++t) {
      line1.add(t, (10 + t), Color.Red);
      if(t > 1) { 
        line2.add(t, t, Color.Green);
    }
} 
  tChart2.getAxes().getLeft().setStartPosition(0);
  tChart2.getAxes().getLeft().setEndPosition(50);
  tChart2.getAxes().getLeft().getAxisPen().color = Color.Red;
  tChart2.getAxes().getLeft().getTitle().getFont().setColor(Color.Red);
  tChart2.getAxes().getLeft().getTitle().getFont().setBold(true);
  tChart2.getAxes().getLeft().getTitle().setText("1st Left Axis");

然后,您可以使用StartPosition和EndPosition方法将新轴与图表的整体关系定位,这些数字表示为图表矩形的百分比,其中0(零)(在 垂直轴的情况下)为Top。这些属性可以应用于标准轴,以在图表中创建完全分区的“SubCharts”。

Axis axis1 = new Axis(false, false, tChart2.getChart());
  tChart2.getAxes().getCustom().add(axis1);
  line2.setCustomVertAxis(axis1);
    axis1.setStartPosition(50);
    axis1.setEndPosition(100);
    axis1.getAxisPen().setColor(Color.Green);
    axis1.getTitle().getFont().setColor(Color.Green);
    axis1.getTitle().getFont().setBold(true);
    axis1.getTitle().setText("Extra Axis");
    axis1.setRelativePosition(20);
    }
}   

上面的编码示例将显示以下图表:

teechart_for_java

猜你喜欢

转载自blog.csdn.net/xiaochuachua/article/details/82423464