軸
加入官方社群740060302,歡迎相互交流
TeeChart Pro將自動為您定義所有Axis標簽,并提供足夠的靈活性來定制您可能擁有的任何特定要求。TeeChart Pro提供真正的多軸。它們在設計或運行時可用,并為Axis定義提供了無數(shù)的可能性和靈活性。有關更多信息,請參閱本教程中的內容。
軸控制-關鍵區(qū)域
刻度
將系列數(shù)據(jù)添加到圖表時,將自動設置軸刻度。您可以在設計時或運行時使用Axis屬性更改默認值。
非日期-時間數(shù)據(jù)
添加新系列時,圖表編輯器軸頁的刻度部分將顯示自動選中,其他選項顯示為灰色。所有顯示的值都是數(shù)字。
日期-時間數(shù)據(jù)
當Series -> General頁面上的Series日期時間設置為true(對于該軸)時,圖表編輯器的axis頁面的Scales部分將顯示自動選中,而其他選項將顯示為灰色。值與日期-時間值一起顯示。
自動選擇最佳的軸刻度范圍,以適應您的數(shù)據(jù)。如果您關閉自動縮放部分將取消灰色選項,您可以更改軸值。重要的是,請記住從頁面左側的坐標軸列表中選擇要配置的坐標軸。
在設計時使用TeeChart編輯器向圖表添加線系列,然后使用以下代碼添加命令按鈕:
[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 Integer For i = 0 To 40 Line1.Add(Convert.ToDouble(i), Rnd() * 100, Color.Red) Next i
運行按鈕中的代碼將繪制具有40個隨機值的Line Series。在設計時轉到圖圖編輯器。在軸頁面的底部軸縮放部分將自動“關閉”。您現(xiàn)在可以為Axis比例配置最大值和最小值。再次運行代碼將顯示值,這取決于您為Axis配置的值。使用鼠標右鍵,您可以滾動以查看剩余的值。
通過代碼設置軸的刻度
您可以使用以下代碼在運行時更改最大值和最小值:
[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 = 5 End 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 = 5 End With
增量
您可以為軸調整間隔。從Axis頁面的Scales部分選擇Desired Increment組合框,并添加所需的增量。你可以在運行時修改代碼:
[C#.Net] Steema.TeeChart.Axis bottomAxis = tChart1.Axes.Bottom; bottomAxis.Increment = 20; [VB.Net] With TChart1.Axes.Bottom .Increment = 20 End With
Datetime數(shù)據(jù)
如果您的數(shù)據(jù)是日期時間(您可以通過轉到系列,常規(guī)頁面為您的系列設置數(shù)據(jù)為日期時間),Series -> General頁面,刻度部分將顯示日期時間范圍。從期望增量組合框中顯示的范圍中選擇增量,并添加一些示例數(shù)據(jù):
[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 Integer Dim Today As DateTime = DateTime.Today Dim OneDay As TimeSpan = TimeSpan.FromDays(1) Line1.XValues.DateTime = True For 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屬性獲取有關日期軸標記的更多信息。
請注意
當更改軸標簽頻率時,請記住,根據(jù)AxisLabels的設置,TeeChart將避免AxisLabels.Separation屬性。這意味著如果標簽頻率太高,標簽無法匹配,那么TeeChart將分配“best fit”。改變標簽角度和標簽分隔是兩個選項,可以幫助你適應你需要的標簽。請參閱Labels和AxisLabels.Angle屬性部分。
Titles
在Axis頁面的標題部分設置標題。您可以更改軸的標題文本及其字體和陰影屬性。也可以指定標題文本的角度和大小。有關運行時,請參閱AxisTitle類。
Labels
有關標簽屬性的恢復,請參見AxisLabels類。
請注意
當更改軸標簽頻率時,請記住,根據(jù)AxisLabels的設置,TeeChart將避免AxisLabels.Separation屬性。這意味著如果標簽頻率太高,標簽無法匹配,那么TeeChart將分配“best fit”。改變標簽角度和標簽分隔是兩個選項,可以幫助你適應你需要的標簽。請參閱Labels和AxisLabels.Angle屬性部分。
Label formats
您可以將所有標準的數(shù)字和日期格式應用于Axis標簽。Axis頁面的標簽部分包含字段“Values format”。如果您的數(shù)據(jù)是datetime,則字段名稱更改為“Date time format”。運行時使用:
[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
或者日期時間數(shù)據(jù):
[C#.Net] tChart1.Axes.Bottom.Labels.DateTimeFormat = "dddd/MMMM/yyyy"; [VB.Net] With TChart1.Axes.Bottom .Labels.DateTimeFormat = "dddd/MMMM/yyyy" End With
多行標簽
軸標簽可以顯示為多行文本,而不是單行文本。使用lines分隔符()分隔行。
例如:
[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標簽的示例:
下面將以兩行文本顯示底部軸標簽,一行顯示月份和日期,另一行顯示年份:
Feb-28 Mar-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 = True TChart1.Axes.Bottom.Labels.DateTimeFormat = "MM/dd hh:mm" TChart1.Axes.Bottom.Labels.MultiLine = True TChart1.Panel.MarginBottom = 10
設置AxisLabels.MultiLine屬性為True將自動在有空格的行中拆分標簽,有效地將標簽分為兩部分:
第一行用'mm/dd'
第二行是'hh:mm'
在運行時,你總是可以使用OnGetAxisLabel event以編程方式將標簽分成幾行:
[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 = myLabelText End Sub
在上面的例子中,"TeeSplitInLines"的全部過程將"LabelText"中的所有空格轉換為行分隔符(返回)。
軸的AxisLabels.Angle屬性也可用于多線軸標簽。
定制Axis標簽
通過使用Axis event可以獲得進一步的Label控制。這些事件允許您激活/取消激活/更改任何單獨的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.Mark End Sub Private 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 If End Sub
有關使用Axis event自定義標簽的更多信息,請參閱Axis event一節(jié)。
Logarithmic Labels
通常,對數(shù)標簽可以通過以下方式設置:
[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 With End Sub
標簽將根據(jù)對數(shù)基數(shù)(默認為10)進行設置,因此,在本例中,將標簽設置為1,10,100,1000,10000。
Ticks 和 Minor
有3種tick類型和2種Grid類型。你可以改變每個刻度和網(wǎng)格類型的長度、寬度和顏色??梢酝ㄟ^“tick”選項卡更改tick,其關聯(lián)的網(wǎng)格和內部tick;可以通過“Minor”選項卡更改Minor Ticks及其相關網(wǎng)格。TeeChart Pro版本5的新功能是能夠更改寬度大于1(默認)的刻度和網(wǎng)格的樣式。
[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 = 10 End 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 = 50 End With
其他的軸
復制軸
TeeChart提供了與數(shù)據(jù)系列相關的5個軸:Left, Top, Bottom, Right和Depth。當您向圖表添加新系列時,您可以定義該系列應與哪個軸相關(轉到系列選項卡,常規(guī)頁面)。您可以使用Axis Customdraw方法在圖表的任何位置重復前面4個軸中的任何一個(或全部)。請注意,此方法會復制您的軸,但不會添加新的自定義軸。有關更多信息,請參閱下一節(jié)“多個自定義軸”。
例如:
[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 Sub Private 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 If End Sub
上面的示例代碼將生成下面的圖像:
自定義軸
在本例中,TeeChart將在圖表的中心繪制新軸,一個水平軸和一個垂直軸。當您滾動圖表(用鼠標右鍵拖動)時,新的垂直軸將始終保持在圖表的中央,新的水平軸將隨著垂直滾動上下移動。新軸是默認軸的精確副本。
多個自定義軸
加上PositionPercent和stretch屬性,可以在圖表的任何地方浮動無限的軸。滾動、縮放和軸命中檢測也適用于自定義創(chuàng)建的軸?,F(xiàn)在可以在設計時通過TeeChart編輯器和運行時通過幾行代碼創(chuàng)建額外的軸:
通過圖表編輯器
TeeChart為您提供了在設計時創(chuàng)建自定義軸的能力,使它們能夠以TeeChart的tee文件格式保存。要做到這一點,打開圖表編輯器并單擊軸選項卡,然后選擇“+”按鈕來添加自定義軸。然后選擇位置選項卡,確保新的自定義軸高亮顯示。此頁面上的水平復選框允許您將新的自定義軸定義為水平軸或將其保留為默認垂直軸。該頁的其余部分和軸頁面中的其他選項卡可用于更改刻度,增量,標題,標簽,刻度,小刻度和自定義軸的位置,如上所述。要將這個新的自定義軸與數(shù)據(jù)系列相關聯(lián),您需要選擇Series選項卡,然后轉到General頁面,其中的下拉組合框“水平軸”和“垂直軸”將使您能夠根據(jù)之前將其定義為垂直還是水平來選擇新的自定義軸。
Via Code [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 = 20 End Sub
上面的代碼示例將顯示以下圖表:
Multiple axes
選擇是無限的!建議在使用自定義軸時要謹慎,因為它很容易開始用新軸填充屏幕,并失去跟蹤那個你希望處理的軸。
Axis events
Axis events提供了運行時靈活性,可以修改Axis標簽,并在Axis單擊上呈現(xiàn)用戶交互性。
OnClickAxis
參見OnClickAxis event。
例如:
[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 If End Sub
OnGetAxisLabel
可用于修改軸標簽。參見OnGetAxisLabel event。
例如:
[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.Mark End Sub Private 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 If End Sub
OnGetNextAxisLabel
可用于決定應該顯示哪些軸標簽。參見OnGetNextAxisLabel事件。你應該使用e.Stop Boolean屬性來包含/排除軸標簽。
[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 Sub Private 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 If End Sub
這就是本教程的全部內容!下一個教程 Legend 設置。