TeeChart for .NET图表控件教程:轴控制

TeeChart Pro会自动为你定义所有的轴标记,并提供足够的灵活性来定制你可能有的任何具体要求。更多信息请参见本教程中的章节。

TeeChart for .NET是优秀的工业4.0 WinForm图表控件,官方独家授权汉化,集功能全面、性能稳定、价格实惠等优势于一体。TeeChart for .NET 中文版还可让您在使用和学习上没有任何语言障碍,至少可以节省30%的开发时间。

点击立即下载最新版TeeChart for .NET

TeeChart Pro会自动为你定义所有的轴标记,并提供足够的灵活性来定制你可能有的任何具体要求。TeeChart Pro提供真正的多轴。这些都可以在设计或运行时使用,为轴的定义提供了无数的可能性和灵活性。更多信息请参见本教程中的章节。

轴控制 – 关键领域 

尺度 
当你将系列数据添加到图表中时,轴的刻度会自动设置。你可以在设计时或在运行时通过使用轴的属性来改变默认值。

TeeChart for .NET图表控件教程:轴控制非日期时间数据 
当添加一个新的系列时,TeeChart Editor的Axis Page的Scales部分将显示自动选择,其他选项为灰色。所有显示的值都是数字。

TeeChart for .NET图表控件教程:轴控制

自动选择最佳的轴刻度范围以适应你的数据。如果你把自动关闭,刻度部分将取消灰色选项,你可以改变轴的值。重要的是,记得从页面左边的坐标轴列表中选择你想配置的坐标轴。

[C#.Net]Random rnd = new Random();for(int i = 0; i <= 40; ++i)line1.Add(Convert.ToDouble(i),rnd.Next(100),Color.Red);[VB.Net]Dim i As IntegerFor i = 0 To 40   Line1.Add(Convert.ToDouble(i), Rnd() * 100, Color.Red)Next i 
Setting axis scales by code 

你可以用这段代码在运行时改变最大和最小值。

[C#.Net]Steema.TeeChart.Axis bottomAxis = tChart1.Axes.Bottom;bottomAxis.Automatic = false;bottomAxis.Maximum = 36;bottomAxis.Minimum = 5;[VB.Net]With TChart1.Axes.Bottom   .Automatic = False   .Maximum = 36   .Minimum = 5End With 

你可以单独设置轴刻度的最大值和最小值为自动,例如 

[C#.Net]Steema.TeeChart.Axis bottomAxis = tChart1.Axes.Bottom;bottomAxis.AutomaticMaximum = true;bottomAxis.AutomaticMinimum = false;bottomAxis.Minimum = 5;[VB.Net]With TChart1.Axes.Bottom   .AutomaticMaximum = True   .AutomaticMinimum = False   .Minimum = 5End With 

递增 

你可以定制轴的间隔。从轴页面的Scales部分选择Desired Increment组合框,并添加你需要的增量。你可以在运行时通过代码改变这一点。

[C#.Net]Steema.TeeChart.Axis bottomAxis = tChart1.Axes.Bottom;bottomAxis.Increment = 20;[VB.Net]With TChart1.Axes.Bottom   .Increment = 20End With 

日期时间数据 

如果你的数据是数据时间(你可以通过进入系列,常规页面为你的系列设置数据为数据时间),Chart->Axis页面,scales部分将显示数据时间范围。从 “期望的增量 “组合框中选择增量,并添加一些样本数据。

[C#.Net]Random rnd = new Random();DateTime today = DateTime.Today;TimeSpan oneDay = TimeSpan.FromDays(1);line1.XValues.DateTime = true;for(int i = 1; i <= 25; ++i)      line1.Add(today,rnd.Next(100),Color.Red);     today += oneDay;[VB.Net]Dim i As IntegerDim Today As DateTime = DateTime.TodayDim OneDay As TimeSpan = TimeSpan.FromDays(1)Line1.XValues.DateTime = TrueFor i = 1 To 25     Line1.Add(Today, Rnd() * 100, Color.Red)     Today = Today.Add(OneDay)Next 

在运行时改变增量。

[C#.Net]Steema.TeeChart.Axis bottomAxis = tChart1.Axes.Bottom;bottomAxis.Increment = Steema.TeeChart.Utils.GetDateTimeStep(Steema.TeeChart.DateTimeSteps.TwoDays);[VB.Net]With TChart1.Axes.Bottom   .Increment = Steema.TeeChart.Utils.GetDateTimeStep(Steema.TeeChart.DateTimeSteps.TwoDays)End With 

参见AxisLabels.ExactDateTime属性以了解更多关于日期轴标记的信息。
注意 
当改变轴标签频率时,请记住,TeeChart将根据AxisLabels.Separation属性的设置来避免标签重叠。这意味着,如果标签频率太高,标签无法适应,那么TeeChart将分配 “最适合”。改变标签角度和标签分离是2个选项,可以帮助你适合你所需要的标签。参见标签部分和AxisLabels.Angle属性。

标题 
标题是在轴页的标题部分设置的。你可以改变轴的标题文本及其字体和阴影属性。标题文本的角度和大小也可以被指定。关于运行时间,请看 AxisTitle 类。
标签 
请参阅AxisLabels类,了解标签属性的简历。
注意 
当改变轴标签频率时,请记住,TeeChart将根据AxisLabels.Separation属性的设置来避免标签重叠。这意味着,如果标签频率太高,标签无法适应,那么TeeChart将分配 “最适合”。改变标签角度和标签分离是2个选项,可能有助于你适合你所需要的标签。参见AxisLabels.Angle属性。
标签格式 
你可以将所有标准的数字和日期格式应用于轴的标签。轴页,标签部分包含 “数值格式 “字段。如果你的数据是日期时间,字段名就会变成 “日期时间格式”。在运行时使用。

[C#.Net]tChart1.Axes.Bottom.Labels.ValueFormat = "#,##0.00;(#,##0.00)";[VB.Net]With TChart1.Axes.Bottom   .Labels.ValueFormat = "#,##0.00;(#,##0.00)"End With 

或者对于日期时间数据 

[C#.Net]tChart1.Axes.Bottom.Labels.DateTimeFormat = "dddd/MMMM/yyyy";[VB.Net]With TChart1.Axes.Bottom   .Labels.DateTimeFormat = "dddd/MMMM/yyyy"End With 

多线标签 
轴标签可以被显示为多行文本,而不是单行文本。行与行之间用LineSeparator字符()分开。
例子  

[C#.Net]bar1.Add(1234, "New" + Steema.TeeChart.Texts.LineSeparator + "Cars", Color.Red);bar1.Add(2000, "Old" + Steema.TeeChart.Texts.LineSeparator + "Bicycles", Color.Red);tChart1.Panel.MarginBottom = 10;[VB.Net]Bar1.Add(1234, "New" + Steema.TeeChart.Texts.LineSeparator + "Cars", Color.Red)Bar1.Add(2000, "Old" + Steema.TeeChart.Texts.LineSeparator + "Bicycles", Color.Red)TChart1.Panel.MarginBottom = 10 

DateTime标签的例子
下面将用两行文字显示底轴标签,一行显示月和日,第二行显示年。
2月28日 3月1日 … 
2003 2003 .. 

[C#.Net]bar1.Add(DateTime.Parse("28/2/2003"), 100, Color.Red)。bar1.Add(DateTime.Parse("1/3/2003"), 200, Color.Red)。bar1.Add(DateTime.Parse("2/3/2003"), 150, Color.Red);bar1.XValues.DateTime = true;tChart1.Axes.Bottom.Labels.DateTimeFormat = "MM/dd hh:mm" 。tChart1.Axes.Bottom.Labels.MultiLine = true;tChart1.Panel.MarginBottom = 10;[VB.Net]Bar1.Add(DateValue("28/2/2003"), 100, Color.Red)Bar1.Add(DateValue("1/3/2003"), 200, Color.Red)Bar1.Add(DateValue("2/3/2003"), 150, Color.Red)Bar1.XValues.DateTime = TrueTChart1.Axes.Bottom.Labels.DateTimeFormat = "MM/dd hh:mm"TChart1.Axes.Bottom.Labels.MultiLine = TrueTChart1.Panel.MarginBottom = 10 

将AxisLabels.MultiLine属性设置为True将自动在有空格的地方分割标签,有效地将标签分成两部分。
第一行为’mm/dd 
第二行为’hh:mm’。 

在运行时,你总是可以使用OnGetAxisLabel事件,以编程方式将标签分成几行。

[C#.Net]private void tChart1_GetAxisLabel(object sender, Steema.TeeChart.TChart.GetAxisLabelEventArgs e)             string myLabelText = e.LabelText;            tChart1.Axes.Bottom.Labels.SplitInLines(ref myLabelText, " ");            e.LabelText = myLabelText; [VB.Net]Private Sub TChart1_GetAxisLabel(ByVal sender As Object, ByVal e As Steema.TeeChart.TChart.GetAxisLabelEventArgs) Handles TChart1.GetAxisLabel        Dim myLabelText As String        myLabelText = e.LabelText        TChart1.Axes.Bottom.Labels.SplitInLines(myLabelText, " ")        e.LabelText = myLabelTextEnd Sub 

在上面的例子中,全局的 “TeeSplitInLines “过程将 “LabelText “中的所有空格转换为行分隔符(回车)。

轴AxisLabels.Angle属性也可以用于多行轴标签。

定制轴标签 
进一步的标签控制可以通过使用Axis事件获得。这些事件允许你激活/停用/改变任何单独的轴标签。下面的例子修改了每个Label,在点的索引值前面加上一个文本短语。

[C#.Net]private void button1_Click(object sender, System.EventArgs e)             bar1.FillSampleValues(20);            tChart1.Axes.Bottom.Labels.Style = AxisLabelStyle.Mark; private void tChart1_GetAxisLabel(object sender, Steema.TeeChart.TChart.GetAxisLabelEventArgs e)             if(((Steema.TeeChart.Axis)sender).Equals(tChart1.Axes.Bottom))            e.LabelText = "Period " + Convert.ToString(e.ValueIndex); [VB.Net]Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click        Bar1.FillSampleValues(20)        TChart1.Axes.Bottom.Labels.Style = Steema.TeeChart.AxisLabelStyle.MarkEnd SubPrivate Sub TChart1_GetAxisLabel(ByVal sender As Object, ByVal e As Steema.TeeChart.TChart.GetAxisLabelEventArgs) Handles TChart1.GetAxisLabel        If CType(sender, Steema.TeeChart.Axis) Is TChart1.Axes.Bottom Then            e.LabelText = "Period " & e.ValueIndex        End IfEnd Sub 

关于用轴事件定制标签的更多信息,请参见题为轴事件的章节。

对数标签 
正常的对数标签可以通过以下方式设置。

[C#.Net]private void button1_Click(object sender, System.EventArgs e)             Random rnd = new Random();            Steema.TeeChart.Axis leftAxis = tChart1.Axes.Left;            tChart1.Aspect.View3D = false;            bar1.Marks.Visible = false;            for(int i = 0; i <= 100; ++i)            bar1.Add(rnd.Next(100) * i);            leftAxis.LogarithmicBase = 10;            leftAxis.Logarithmic = true;            leftAxis.SetMinMax(0, 10000);            leftAxis.Labels.ValueFormat = "#e+0"; //exponential format  [VB.Net]Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click        Dim i As Integer        TChart1.Aspect.View3D = False        Bar1.Marks.Visible = False        For i = 0 To 10000 Step 100            Bar1.Add(Rnd() * i)        Next        With TChart1.Axes.Left            .LogarithmicBase = 10            .Logarithmic = True            .SetMinMax(0, 10000)            .Labels.ValueFormat = "#e+0" ' exponential format         End WithEnd Sub 

标签将根据对数基数(默认为10)进行设置,因此,在这种情况下,会在1、10、100、1000、10000处设置标签。

Ticks and Minor 

 

TeeChart for .NET图表控件教程:轴控制有3种刻度线类型和2种 格类型。你可以改变每种刻度和 格类型的长度、宽度和颜色。可以通过 “Ticks “选项卡对Ticks、其相关的Grid和Inner Ticks进行修改;可以通过 “Minor “选项卡对Minor Ticks和其相关的Grid进行修改。TeeChart Pro第5版的新功能是可以改变宽度大于1(默认)的Ticks和Grid的样式。
[C#.Net]Steema.TeeChart.Axis bottomAxis = tChart1.Axes.Bottom;bottomAxis.Ticks.Length = 7;bottomAxis.Ticks.Color = Color.Green;bottomAxis.MinorTickCount = 10;[VB.Net]With TChart1.Axes.Bottom   .Ticks.Length = 7   .Ticks.Color = Color.Green   .MinorTickCount = 10End With 

轴的位置 

轴有一个属性可以修改每个轴的位置。在这个例子中,轴被移动到图表总宽度的50%,所以它被显示在图表中心。

[C#.Net]Steema.TeeChart.Axis bottomAxis = tChart1.Axes.Bottom;bottomAxis.PositionUnits = PositionUnits.Percent;bottomAxis. RelativePosition = 50[VB.Net]With TChart1.Axes.Bottom   .PositionUnits = PositionUnits.Percent   .RelativePosition = 50End With 

附加轴 

复制坐标轴 
TeeChart提供了5个与数据系列相关的轴。左、顶、底、右和深度。当你在图表中添加一个新的系列时,你可以定义该系列应该与哪个轴相关(进入系列标签,常规页面)。您可以通过使用Axis Customdraw方法在图表的任何地方重复前4个轴的任何一个(或全部)。请注意,该方法是对你的轴进行复制,而不是添加一个新的自定义轴。更多信息请参见下一节 “多个自定义轴”。
例子。

[C#.Net]private void Form1_Load(object sender, System.EventArgs e)             Random Rnd = new Random();            tChart1.Aspect.View3D = false;            tChart1.Panel.Gradient.Visible = true;            for(int t = 0; t <= 20; ++t)            line1.Add(t, ((Rnd.Next(100)) + 1) - ((Rnd.Next(70)) + 1), Color.Red);private void line1_BeforeDrawValues(object sender, Steema.TeeChart.Drawing.Graphics3D g)             int posAxis = 0;            if(tChart1.Axes.Left.Maximum > 0)                     tChart1.Axes.Left.Draw(g.ChartXCenter - 10,g.ChartXCenter - 20,g.ChartXCenter,true);                posAxis = tChart1.Axes.Left.CalcYPosValue(10);                tChart1.Axes.Bottom.Draw(posAxis + 10, posAxis + 40, posAxis, true);      [VB.Net]Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        Dim t As Integer        TChart1.Aspect.View3D = False        TChart1.Panel.Gradient.Visible = True        For t = 0 To 20            Line1.Add(t, ((Rnd() * 100) + 1) - ((Rnd() * 70) + 1), Color.Red)        Next    End SubPrivate Sub Line1_BeforeDrawValues(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles Line1.BeforeDrawValues        Dim posAxis As Integer        If TChart1.Axes.Left.Maximum > 0 Then            TChart1.Axes.Left.Draw(g.ChartXCenter - 10, g.ChartXCenter - 20, g.ChartXCenter, True)            posAxis = TChart1.Axes.Left.CalcYPosValue(10)            TChart1.Axes.Bottom.Draw(posAxis + 10, posAxis + 40, posAxis, True)        End IfEnd Sub 

上面的示例代码将产生下面的图像。

TeeChart for .NET图表控件教程:轴控制
自定义坐标轴 
在这个例子中,TeeChart将绘制新轴,一个水平轴和一个垂直轴在你的图表中心。当你滚动图表时(用鼠标右键拖动),新的垂直轴将始终保持在图表的中央,新的水平轴将随着垂直滚动上下移动。新的坐标轴是默认坐标轴的精确拷贝。

通过代码 

[C#.Net]private void Form1_Load(object sender, System.EventArgs e)             Line line1 = new Line();            Line line2 = new Line();             tChart1.Aspect.View3D = false;            tChart1.Panel.Gradient.Visible = true;            tChart1.Header.Text = "TeeChart Multiple Axes";            tChart1.Series.Add(line1);            tChart1.Series.Add(line2);            for(int t = 0; t <= 10; ++t)                     line1.Add(Convert.ToDouble(t), Convert.ToDouble(10 + t), Color.Red);                if(t > 1)                line2.Add(Convert.ToDouble(t), Convert.ToDouble(t), Color.Green);             Axis leftAxis = tChart1.Axes.Left;            leftAxis.StartPosition = 0;            leftAxis.EndPosition = 50;            leftAxis.AxisPen.Color = Color.Red;            leftAxis.Title.Font.Color = Color.Red;            leftAxis.Title.Font.Bold = true;            leftAxis.Title.Text = "1st Left Axis";//            You are able to then position the new Axis in overall relation to the Chart //            by using the StartPosition and EndPosition  properties.////            StartPosition=50//            EndPosition=100////            These figures are expressed as percentages of the Chart Rectangle with 0 (zero)//            (in the case of a vertical Axis) being Top. These properties can be applied to //            the Standard Axes to create completely partitioned 'SubCharts' within the Chart.            Axis axis1 = new Axis(false, false, tChart1.Chart);            tChart1.Axes.Custom.Add(axis1);            line2.CustomVertAxis = axis1;            axis1.StartPosition = 50;            axis1.EndPosition = 100;            axis1.AxisPen.Color = Color.Green;            axis1.Title.Font.Color = Color.Green;            axis1.Title.Font.Bold = true;            axis1.Title.Text = "Extra Axis";            axis1.PositionUnits= PositionUnits.Percent;                                                axis1.RelativePosition = 20; [VB.Net]Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        Dim Line1 As New Steema.TeeChart.Styles.Line()        Dim Line2 As New Steema.TeeChart.Styles.Line()        Dim t As Integer        TChart1.Aspect.View3D = False        TChart1.Panel.Gradient.Visible = True        TChart1.Header.Text = "TeeChart Multiple Axes"        TChart1.Series.Add(Line1)        TChart1.Series.Add(Line2)        For t = 0 To 10            Line1.Add(t, 10 + t, Color.Red)            If (t > 1) Then                Line2.Add(t, t, Color.Green)            End If        Next        With TChart1.Axes.Left            .StartPosition = 0            .EndPosition = 50            .AxisPen.Color = Color.Red            .Title.Font.Color = Color.Red            .Title.Font.Bold = True            .Title.Text = "1st Left Axis"        End With        'You are able to then position the new Axis in overall relation to the Chart         'by using the StartPosition and EndPosition  properties.        '      StartPosition = 50        '      EndPosition = 100        'These figures are expressed as percentages of the Chart Rectangle with 0 (zero)        '(in the case of a vertical Axis) being Top. These properties can be applied to         'the Standard Axes to create completely partitioned 'SubCharts' within the Chart.        Dim Axis1 As New Steema.TeeChart.Axis(False, False, TChart1.Chart)        TChart1.Axes.Custom.Add(Axis1)        Line2.CustomVertAxis = Axis1        Axis1.StartPosition = 50        Axis1.EndPosition = 100        Axis1.AxisPen.Color = Color.Green        Axis1.Title.Font.Color = Color.Green        Axis1.Title.Font.Bold = True        Axis1.Title.Text = "Extra Axis"        Axis1.PositionUnits.=  PositionUnits.Percent;        Axis1.RelativePosition = 20End Sub 

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

TeeChart for .NET图表控件教程:轴控制多轴 
选项是无限的! 我们建议在使用自定义坐标轴时要谨慎,因为很容易开始用新的坐标轴填满屏幕,并失去了你希望管理的坐标轴的踪迹! 

轴事件 

轴事件提供了运行时的灵活性,以修改轴标签,并在轴点击时呈现用户的互动性。

OnClickAxis 

参见OnClickAxis事件。
例子 

[C#.Net]private void tChart1_ClickAxis(object sender, System.Windows.Forms.MouseEventArgs e)             if(((Steema.TeeChart.Axis)sender).Equals(tChart1.Axes.Bottom))                     MessageBox.Show("Clicked Bottom Axis at: " + line1.XScreenToValue(e.X)); [VB.Net]Private Sub TChart1_ClickAxis(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TChart1.ClickAxis        If CType(sender, Steema.TeeChart.Axis) Is TChart1.Axes.Bottom Then            MsgBox("Clicked Bottom Axis at: " & Line1.XScreenToValue(e.X))        End IfEnd Sub 

OnGetAxisLabel 

可以用来修改轴的标签。参见OnGetAxisLabel事件。
例子 

[C#.Net]private void button1_Click(object sender, System.EventArgs e)             bar1.FillSampleValues(20);            tChart1.Axes.Bottom.Labels.Style = AxisLabelStyle.Mark; private void tChart1_GetAxisLabel(object sender, Steema.TeeChart.TChart.GetAxisLabelEventArgs e)             if(((Steema.TeeChart.Axis)sender).Equals(tChart1.Axes.Bottom))            e.LabelText = "Period " + Convert.ToString(e.ValueIndex); [VB.Net]Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click        Bar1.FillSampleValues(20)        TChart1.Axes.Bottom.Labels.Style = Steema.TeeChart.AxisLabelStyle.MarkEnd SubPrivate Sub TChart1_GetAxisLabel(ByVal sender As Object, ByVal e As Steema.TeeChart.TChart.GetAxisLabelEventArgs) Handles TChart1.GetAxisLabel        If CType(sender, Steema.TeeChart.Axis) Is TChart1.Axes.Bottom Then            e.LabelText = "Period " & e.ValueIndex        End IfEnd Sub 

OnGetNextAxisLabel 

可以用来决定哪些轴标签应该被显示。参见OnGetNextAxisLabel事件。你应该使用e.Stop布尔属性来包括/排除轴标签。
例子 

[C#.Net]private void Form1_Load(object sender, System.EventArgs e)             line1.FillSampleValues(20); private void tChart1_GetNextAxisLabel(object sender, Steema.TeeChart.TChart.GetNextAxisLabelEventArgs e)             if(((Steema.TeeChart.Axis)sender).Equals(tChart1.Axes.Bottom))                     e.Stop = false;                switch(e.LabelIndex)                             case 0: e.LabelValue = 5; break;                    case 1: e.LabelValue = 13; break;                    case 2: e.LabelValue = 19; break;                    default: e.Stop = true; break;           [VB.Net]Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        Line1.FillSampleValues(20)End SubPrivate Sub TChart1_GetNextAxisLabel(ByVal sender As Object, ByVal e As Steema.TeeChart.TChart.GetNextAxisLabelEventArgs) Handles TChart1.GetNextAxisLabel        If CType(sender, Steema.TeeChart.Axis) Is TChart1.Axes.Bottom Then            e.Stop = False            Select Case e.LabelIndex                Case 0 : e.LabelValue = 5                Case 1 : e.LabelValue = 13                Case 2 : e.LabelValue = 19                Case Else : e.Stop = True            End Select        End IfEnd Sub 

现TeeChart for .NET已加入在线订购,现在抢购可立享优惠!

如果您对该图表控件感兴趣,欢迎加入图表控件QQ交流群:


标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年5月5日
下一篇 2021年5月5日

相关推荐

发表回复

登录后才能评论