【TeeChart Pro ActiveX教程】(十三):圖表面板上的自定義繪圖(上)
阿新 • • 發佈:2019-01-03
TeeChart通過Canvas物件提供廣泛的自定義繪圖工具。使用Canvas,您可以在“Chart Panel”的任何位置新增形狀,線條和文字,並定義其顏色,筆和畫筆樣式。
TeeChart畫布
繪圖順序
使用TeeChart的Canvas方法時,請記住繪圖順序很重要。在圖表上繪製一條線然後新增系列資料點將導致線透支。 有四個主要的圖表繪製事件,按順序排列:
BeforeDraw事件
BeforeDrawAxes事件
BeforeDrawSeries事件
AfterDraw事件
[C#]
private bool afterDraw; private bool beforeDraw; private bool beforeDrawAxis; private bool beforeDrawSeries; private void Form1_Load(object sender, System.EventArgs e) { SetFlags(ref beforeDraw); Bar bar1 = new Bar(tChart1.Chart); bar1.FillSampleValues(20); radioButton1.Checked = true; } private void SetFlags(ref bool Flag) { beforeDraw = false; afterDraw = false; beforeDrawAxis = false; beforeDrawSeries = false; Flag = true; } private void DrawShape(Steema.TeeChart.Drawing.Graphics3D gg) { gg.Brush.Color = Color.Yellow; gg.Pen.Visible = true; gg.Pen.Style = System.Drawing.Drawing2D.DashStyle.Dash; gg.Brush.Visible = true; gg.Ellipse(1,1,gg.Chart.Width - 1,gg.Chart.Height - 1); } private void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g) { if(afterDraw) { DrawShape(g); } } private void tChart1_BeforeDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g) { if(beforeDraw) { DrawShape(g); } } private void tChart1_BeforeDrawAxes(object sender, Steema.TeeChart.Drawing.Graphics3D g) { if(beforeDrawAxis) { DrawShape(g); } } private void tChart1_BeforeDrawSeries(object sender, Steema.TeeChart.Drawing.Graphics3D g) { if(beforeDrawSeries) { DrawShape(g); } } private void radioButton4_Click(object sender, System.EventArgs e) { SetFlags(ref afterDraw); tChart1.Refresh(); } private void radioButton3_Click(object sender, System.EventArgs e) { SetFlags(ref beforeDrawSeries); tChart1.Refresh(); } private void radioButton2_Click(object sender, System.EventArgs e) { SetFlags(ref beforeDrawAxis); tChart1.Refresh(); } private void radioButton1_Click(object sender, System.EventArgs e) { SetFlags(ref beforeDraw); tChart1.Refresh(); }
[VB.Net]
Private BeforeDraw As Boolean Private BeforeDrawAxis As Boolean Private BeforeDrawSeries As Boolean Private AfterDraw As Boolean Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load SetFlags(BeforeDraw) Dim Bar1 As New Steema.TeeChart.Styles.Bar(TChart1.Chart) Bar1.FillSampleValues(20) RadioButton1.Checked = True End Sub Private Sub SetFlags(ByRef Flag As Boolean) BeforeDraw = False BeforeDrawAxis = False BeforeDrawSeries = False AfterDraw = False Flag = True End Sub Private Sub DrawShape(ByVal gg As Steema.TeeChart.Drawing.Graphics3D) gg.Brush.Color = Color.Yellow gg.Pen.Visible = True gg.Pen.Style = Drawing.Drawing2D.DashStyle.Dash gg.Brush.Visible = True gg.Ellipse(1, 1, gg.Chart.Width - 1, gg.Chart.Height - 1) End Sub Private Sub TChart1_AfterDraw(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.AfterDraw If AfterDraw = True Then DrawShape(g) End If End Sub Private Sub TChart1_BeforeDraw(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.BeforeDraw If BeforeDraw = True Then DrawShape(g) End If End Sub Private Sub TChart1_BeforeDrawAxes(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.BeforeDrawAxes If BeforeDrawAxis = True Then DrawShape(g) End If End Sub Private Sub TChart1_BeforeDrawSeries(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.BeforeDrawSeries If BeforeDrawSeries = True Then DrawShape(g) End If End Sub Private Sub RadioButton4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton4.Click SetFlags(AfterDraw) TChart1.Refresh() End Sub Private Sub RadioButton3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton3.Click SetFlags(BeforeDrawSeries) TChart1.Refresh() End Sub Private Sub RadioButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton2.Click SetFlags(BeforeDrawAxis) TChart1.Refresh() End Sub Private Sub RadioButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.Click SetFlags(BeforeDraw) TChart1.Refresh() End Sub
確保將自定義繪製的專案儲存到Canvas
如果不在其中一個Chart事件中呼叫Canvas繪製程式碼,則自定義繪圖將不會永久儲存到Canvas,從而導致在應用程式最小化時丟失任何新增內容或另一個視窗放在它上面。您的程式碼不需要直接駐留在Chart事件中; 如果將程式碼放在BeforeDrawSeries / AfterDraw中並且檢查執行時設定的標記,則可以在圖表視窗的生命週期中儲存使用者繪製的專案。當活動標記為true時,繪製方法因此執行繪製程式碼,如上例所示。
繪製線
讓我們新增一個畫布線: 示例(從左上角到右下角對角畫一條線)
[C#]
private void Form1_Load(object sender,System.EventArgs e){ line1.FillSampleValues(20); line1.VertAxis = VerticalAxis.Both; line1.HorizAxis = HorizontalAxis.Both; tChart1.Aspect.View3D = false; } private void tChart1_AfterDraw(object sender,Steema.TeeChart.Drawing.Graphics3D g){ Point s = new Point(tChart1.Axes.Left.Position,tChart1.Axes.Top.Position); 點e =新點(tChart1.Axes.Right.Position,tChart1.Axes.Bottom.Position); g.MoveTo(一個或多個); g.LineTo(E,0); }
[VB.Net]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Line1.FillSampleValues(20) Line1.VertAxis = Steema.TeeChart.VerticalAxis.Both Line1.HorizAxis = Steema.TeeChart.HorizontalAxis.Both TChart1.Aspect.View3D = False End Sub Private Sub TChart1_AfterDraw(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.AfterDraw Dim S As New Point(TChart1.Axes.Left.Position, TChart1.Axes.Top.Position) Dim E As New Point(TChart1.Axes.Right.Position, TChart1.Axes.Bottom.Position) g.MoveTo(S) g.LineTo(E, 0) End Sub
在3D圖表上,由於3D正交位移,軸位置偏離圖表區域。我們可以相應地移動線: 示例(在3D圖表的圖表區域中從左上角到右下角對角繪製一條線)
[C#]
private void Form1_Load(object sender, System.EventArgs e) { line1.FillSampleValues(20); line1.VertAxis = VerticalAxis.Both; line1.HorizAxis = HorizontalAxis.Both; tChart1.Aspect.Chart3DPercent = 50; } private void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g) { Steema.TeeChart.Drawing.Point3D s = new Steema.TeeChart.Drawing.Point3D(); s.X = tChart1.Axes.Left.Position; s.Y = tChart1.Axes.Top.Position; s.Z = 0; Steema.TeeChart.Drawing.Point3D e = new Steema.TeeChart.Drawing.Point3D(); e.X = tChart1.Axes.Right.Position; e.Y = tChart1.Axes.Bottom.Position; e.Z = tChart1.Aspect.Width3D; g.MoveTo(s); g.LineTo(e); }
[VB.Net]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Line1.FillSampleValues(20) Line1.VertAxis = Steema.TeeChart.VerticalAxis.Both Line1.HorizAxis = Steema.TeeChart.HorizontalAxis.Both 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 S As New Steema.TeeChart.Drawing.Point3D() S.X = TChart1.Axes.Left.Position S.Y = TChart1.Axes.Top.Position S.Z = 0 Dim E As New Steema.TeeChart.Drawing.Point3D() E.X = TChart1.Axes.Right.Position E.Y = TChart1.Axes.Bottom.Position E.Z = TChart1.Aspect.Width3D g.MoveTo(S) g.LineTo(E) End Sub
畫布筆和畫筆
上面的線是使用為繪製線之前繪製的最後一個物件定義的筆和畫筆繪製的。那可能是也可能不是你想要的筆。相應地更改筆: 示例(在繪製線之前定義筆)
[C#]
private void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g) { Point p5 = new Point(line1.CalcXPos(5), line1.CalcYPos(5)); Point p15 = new Point(line1.CalcXPos(15), line1.CalcYPos(15)); g.Pen.DashCap = System.Drawing.Drawing2D.DashCap.Triangle; g.Pen.EndCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor; g.Pen.Style = System.Drawing.Drawing2D.DashStyle.DashDotDot; g.Pen.Transparency = 70; g.Pen.Width = 3; g.Pen.Color = Color.BlueViolet; g.MoveTo(p5); g.LineTo(p15, 0); }
[VB.Net]
Private Sub TChart1_AfterDraw(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.AfterDraw Dim P5 As New Point(Line1.CalcXPos(5), Line1.CalcYPos(5)) Dim P15 As New Point(Line1.CalcXPos(15), Line1.CalcYPos(15)) g.Pen.DashCap = System.Drawing.Drawing2D.DashCap.Triangle g.Pen.EndCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor g.Pen.Style = System.Drawing.Drawing2D.DashStyle.DashDotDot g.Pen.Transparency = 70 g.Pen.Width = 3 g.Pen.Color = Color.BlueViolet g.MoveTo(P5) g.LineTo(P15, 0) End Sub