文檔首頁>>【TeeChart Pro ActiveX教程】2018>>【TeeChart Pro ActiveX教程】(十三):圖表面板上的自定義繪圖(下)
【TeeChart Pro ActiveX教程】(十三):圖表面板上的自定義繪圖(下)
添加2D形狀
以與Canvas Lines類似的方式添加Canvas Shapes。以下示例在圖表區(qū)域的中心添加一個Rectangle:
2D圖表
2D圖表僅支持2D形狀。
[C#]
private void tChart1_AfterDraw(object sender,Steema.TeeChart.Drawing.Graphics3D g){ Size s = new Size(100,100); Point l = new Point(g.ChartXCenter - (s.Width / 2),g.ChartYCenter - (s.Height / 2)); Rectangle r = new Rectangle(l,s); g.Pen.Color = Color.Aquamarine; g.Brush.Color = Color.Blue; g.Rectangle(R); }
[VB.Net]
Private Sub TChart1_AfterDraw(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.AfterDraw Dim S As New Size(100, 100) Dim L As New Point(g.ChartXCenter - (S.Width / 2), g.ChartYCenter - (S.Height / 2)) Dim R As New Rectangle(L, S) g.Pen.Color = Color.Aquamarine g.Brush.Color = Color.Blue g.Rectangle(R) End Sub
3D圖表
在3D圖表上,您也可以在Z平面中移動矩形。請參閱RectangleWithZ方法。此示例將矩形放置在左側(cè)墻壁上,但將其移向圖表后部的中間位置(朝向后墻)。
[C#]
private void Form1_Load(object sender, System.EventArgs e) { point3DSeries1.LinePen.Visible = false; point3DSeries1.FillSampleValues(20); point3DSeries1.VertAxis = VerticalAxis.Both; point3DSeries1.HorizAxis = HorizontalAxis.Both; tChart1.Aspect.Chart3DPercent = 50; tChart1.Axes.Depth.Visible = true; } private void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g) { Size s = new Size(100,100); Point l = new Point(tChart1.Axes.Left.Position, g.ChartYCenter - (s.Height / 2)); Rectangle r = new Rectangle(l,s); g.Pen.Color = Color.Aquamarine; g.Brush.Color = Color.Blue; g.Rectangle(r, tChart1.Aspect.Width3D/2); }
[VB.Net]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Point3DSeries1.LinePen.Visible = False Point3DSeries1.FillSampleValues(20) Point3DSeries1.VertAxis = Steema.TeeChart.VerticalAxis.Both Point3DSeries1.HorizAxis = Steema.TeeChart.HorizontalAxis.Both TChart1.Aspect.Chart3DPercent = 50 TChart1.Axes.Depth.Visible = True End Sub Private Sub TChart1_AfterDraw(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.AfterDraw Dim S As New Size(100, 100) Dim L As New Point(TChart1.Axes.Left.Position, g.ChartYCenter - (S.Height / 2)) Dim R As New Rectangle(L, S) g.Pen.Color = Color.Aquamarine g.Brush.Color = Color.Blue g.Rectangle(R, TChart1.Aspect.Width3D / 2) End Sub
添加3D形狀
您可以將3D形狀添加到3D圖表中。此示例在Chart矩形的中間繪制一個Cube:
[C#]
private void Form1_Load(object sender, System.EventArgs e) { point3DSeries1.LinePen.Visible = false; point3DSeries1.FillSampleValues(20); tChart1.Aspect.Chart3DPercent = 50; tChart1.Legend.Visible = false; tChart1.Axes.Depth.Visible = true; } private void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g) { Size s = new Size(50,50); Point p = new Point(g.ChartXCenter - (s.Width/2), g.ChartYCenter - (s.Height/2)); Rectangle r = new Rectangle(p,s); g.Cube(r, 0, 20, true); }
[VB.Net]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Point3DSeries1.LinePen.Visible = False Point3DSeries1.FillSampleValues(20) TChart1.Aspect.Chart3DPercent = 50 TChart1.Legend.Visible = False TChart1.Axes.Depth.Visible = True End Sub Private Sub TChart1_AfterDraw(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.AfterDraw Dim S As New Size(50, 50) Dim P As New Point(g.ChartXCenter - (S.Width / 2), g.ChartYCenter - (S.Height / 2)) Dim R As New Rectangle(P, S) g.Cube(R, 0, 20, True) End Sub
添加文字
2D文本位置
將文本添加到最后一個矩形:
[C#]
private void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g) { string text = "My Text"; Size s = new Size(150, 50); Point p = new Point(g.ChartXCenter - (s.Width/2), g.ChartYCenter - (s.Height/2)); Rectangle r = new Rectangle(p,s); g.Pen.Color = Color.Blue; g.Rectangle(r); g.TextOut(Convert.ToInt32(g.ChartXCenter - (g.TextWidth(text)/2)), Convert.ToInt32(g.ChartYCenter - (g.TextHeight(text)/2)), text); }
[VB.Net]
Private Sub TChart1_AfterDraw(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.AfterDraw Dim Text As String = "My Text" Dim S As New Size(50, 50) Dim P As New Point(g.ChartXCenter - (S.Width / 2), g.ChartYCenter - (S.Height / 2)) Dim R As New Rectangle(P, S) g.Pen.Color = Color.Blue g.Rectangle(R) g.TextOut(Convert.ToInt32(g.ChartXCenter - (g.TextWidth(Text) / 2)), Convert.ToInt32(g.ChartYCenter - (g.TextHeight(Text) / 2)), Text) End Sub
3D文字位置
通過使用帶有az坐標(biāo)的TextOut重載,可以將文本放置在不同的3D平面中。
[c#]
private void Form1_Load(object sender, System.EventArgs e) { point3DSeries1.FillSampleValues(20); point3DSeries1.LinePen.Visible = false; tChart1.Aspect.Chart3DPercent = 50; } private void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g) { string text = "My Text"; g.TextOut(g.ChartXCenter, g.ChartYCenter, tChart1.Aspect.Width3D / 2, text); }
[VB.Net]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Point3DSeries1.FillSampleValues(20) Point3DSeries1.LinePen.Visible = False TChart1.Aspect.Chart3DPercent = 50 End Sub Private Sub TChart1_AfterDraw(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.AfterDraw Dim Text As String = "My Text" g.TextOut(g.ChartXCenter, g.ChartYCenter, TChart1.Aspect.Width3D / 2, Text) End Sub
應(yīng)用示例
此示例獲取系列的第3和第10個值,在它們之間繪制一條直線,并告訴我們新線的第一個和最后一個點的值以及它們之間的差異:
[c#]
private void Form1_Load(object sender, System.EventArgs e) { tChart1.Aspect.View3D = false; line1.FillSampleValues(20); } private void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g) { if(tChart1.Series.Count > 0){ if(tChart1.Series[0].Count > 10) { Series s = tChart1.Series[0]; int h = Convert.ToInt32(g.TextHeight("H")); Point p1 = new Point(s.CalcXPos(3), s.CalcYPos(3)); Point p2 = new Point(s.CalcXPos(10), s.CalcYPos(10)); g.Pen.Color = Color.Blue; g.Pen.Width = 2; g.Pen.Style = System.Drawing.Drawing2D.DashStyle.Dash; g.MoveTo(p1); g.LineTo(p2, 0); g.TextOut(p1.X, p1.Y - h, "Point value: " + s.YValues[3].ToString()); g.TextOut(p2.X, p2.Y, "Point value: " + s.YValues[10].ToString()); g.TextOut(p2.X, p2.Y + h, "Change is: " + Convert.ToString(s.YValues[3] - s.YValues[10])); } } }
[VB.Net]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TChart1.Aspect.View3D = False Line1.FillSampleValues(20) End Sub Private Sub TChart1_AfterDraw(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.AfterDraw If TChart1.Series.Count > 0 Then If TChart1.Series(0).Count > 10 Then Dim S As Steema.TeeChart.Series = TChart1.Series(0) Dim H As Integer = Convert.ToInt32(g.TextHeight("H")) Dim P1 As New Point(S.CalcXPos(3), S.CalcYPos(3)) Dim P2 As New Point(S.CalcXPos(10), S.CalcYPos(10)) g.Pen.Color = Color.Blue g.Pen.Width = 2 g.Pen.Style = System.Drawing.Drawing2D.DashStyle.Dash g.MoveTo(P1) g.LineTo(P2, 0) g.TextOut(P1.X, P1.Y - H, "Point value: " & S.YValues(3)) g.TextOut(P2.X, P2.Y, "Point value: " & S.YValues(10)) g.TextOut(P2.X, P2.Y + H, "Change is: " & (S.YValues(3) - S.YValues(10))) End If End If End Sub
購買TeeChart Pro AciveX正版授權(quán),請點擊“咨詢在線客服”喲!