翻譯|使用教程|編輯:凌霄漢|2022-03-21 14:37:12.983|閱讀 235 次
概述:本次TeeChart Pro .NET使用教程將為大家帶來如何處理高級軸和圖例操作。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
本文接著上一篇未講完的《文章TeeChart Pro .NET使用教程(2022):如何處理高級軸和圖例操作》繼續為大家帶來下半部分。
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 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 和拉伸屬性一起,可以在圖表上的任何位置浮動無限的軸。 滾動、縮放和軸命中檢測也適用于自定義創建的軸。 現在可以在設計時通過 TeeChart 編輯器和在運行時通過幾行代碼創建額外的軸:
 
 
	TeeChart 使您能夠在設計時創建自定義軸,從而使它們能夠以 TeeChart 的 tee 文件格式保存。 為此,打開圖表編輯器并單擊軸選項卡,然后選擇“+”按鈕添加自定義軸。 然后選擇位置選項卡,確保突出顯示新的自定義軸。 此頁面上的水平復選框允許您將新的自定義軸定義為水平軸或將其保留為默認垂直軸。 此頁面的其余部分和軸頁面中的其他選項卡可用于更改自定義軸的刻度、增量、標題、標簽、刻度、次刻度和位置,如上所述。 要將這個新的自定義軸與您想要的數據系列相關聯,請選擇“系列”選項卡并轉到“常規”頁面,其中下拉組合框“水平軸”和“垂直軸”將使您能夠根據您之前是否定義選擇新的自定義軸 它是垂直的或水平的。
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 
	上面的編碼示例將顯示以下圖表:
 
 
	選擇是無限的! 我們建議在使用自定義軸時要小心,因為很容易開始用新軸填充屏幕并忘記要管理哪個軸!
軸事件提供運行時靈活性來修改軸標簽并在軸點擊上呈現用戶交互性。
接下來舉一個 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 If 
End Sub 
	可用于修改軸標簽。接下來來看一個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.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事件例子,您應該使用 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 
	如果您想了解TeeChart for .NET正版價格,歡迎咨詢
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn