Page 1 of 1
Drawing lines in AfterDraw not always working
Posted: Thu Apr 12, 2007 2:36 pm
by 9637396
Hi
i have a series for which I draw lines on the chart in the AfterDraw Event. (It's a long story why)
Anyway somtimes these lines that I draw are not shown. Then if I zoom in they appear again.
Here is a sample of my code (VB 2005):
For i = 0 To .Series(0).Count - 1
.Graphics3D.Pen.Color = System.Drawing.Color.Black
.Graphics3D.Pen.Width = 2
.Graphics3D.MoveTo(Convert.ToInt32(.Series(0).CalcXPos(i) )), Convert.ToInt32(.Axes.Left.CalcYPosValue(k + 1))))
.Graphics3D.LineTo(Convert.ToInt32(.Series(0).CalcXPos(i))), Convert.ToInt32(.Axes.Left.CalcYPosValue(k + 1))))
Next i
Can It be explained why sometimes the drawed lines are shown and sometimes not ?
Thanks in advance
Lars Iversen
Posted: Thu Apr 12, 2007 3:08 pm
by narcis
Hi Lars,
I can't tell you for sure why this code is not executed without being able to reproduce the exact problem here. However, to force the chart being repainted and thus, AfterDraw event fired, you could try making a tChart1.Bitmap call when you want to force this. For exapmle, you could add this code in your chart's initialization code:
Posted: Fri Apr 13, 2007 8:37 am
by 9637396
Hi Narcís
But the problem is not that the AfterDraw event isn't being fired. It is. I know because I have a few lines of code above the ones that I showed you end they work ok.
Adding:
Dim bmp As Bitmap = TChart1.Bitmap
to the AfterDraw event code of course results in an endless loop where after draw is calling itself.
Now the problem is still that these lines that I'm drawing not always are shown. It happens sometimes when the form initially loads and sometimes when I zoom out but it seems to be ok always when I zoom in.
Do you have any other suggestions? Maybe I could place this Dim bmp As Bitmap = TChart1.Bitmap in another event which is fired after zoom/undo-zoom and after the form initially loads?
Furthermore I would like to tell you that I'm using the Teechart version from May 8 2006. Do you know if it an issue that has been fixed in later versions?
Thanks in advance
Lars iversen
Posted: Fri Apr 13, 2007 10:34 am
by narcis
Hi Lars,
Adding:
Dim bmp As Bitmap = TChart1.Bitmap
to the AfterDraw event code of course results in an endless loop where after draw is calling itself.
Of course, you should never call Bitmap in the AfterDraw event.
Now the problem is still that these lines that I'm drawing not always are shown. It happens sometimes when the form initially loads and sometimes when I zoom out but it seems to be ok always when I zoom in.
Do you have any other suggestions? Maybe I could place this Dim bmp As Bitmap = TChart1.Bitmap in another event which is fired after zoom/undo-zoom and after the form initially loads?
Yes, you should call Bitmap, for example, after the chart has been initialized: in the form constructor, InitializeComponent method, form Load event, etc. You could also make this call after a Zoom/Unzoom has ocurred.
Furthermore I would like to tell you that I'm using the Teechart version from May 8 2006. Do you know if it an issue that has been fixed in later versions?
I don't think so. Please notice that almost a year has past since then and a number of newer versions have been posted at the client area. At our
version info. page you'll find which is the current version and its release notes, which contain all that has been implemented and fixed in each release.
Posted: Fri Apr 13, 2007 12:50 pm
by 9637396
Hi Narcís
Now I tried downloading and using the latest version. 2.0.2652.22325
Now none of the thinsg that I write in afterdraw is shown on the chart. The event is fired though.
Here's my afterdraw code (In which I have tried a few things in order to make it work):
Dim i As Integer
With TChart1
'Do limit lines
If .Series(0).Count > 0 Then
DrawLine(50)
WriteText(50, "Upper")
DrawLine(0)
DrawLine(-50)
WriteText(-50, "Lower")
End If
Windows.Forms.Application.DoEvents()
Try
For i = 0 To .Series(0).Count - 1
.Graphics3D.Pen.Color = System.Drawing.Color.Black
.Graphics3D.Pen.Width = 2
.Graphics3D.MoveTo(Convert.ToInt32(.Series(0).CalcXPos(i) - (Candle1.CandleWidth / 2)), Convert.ToInt32(.Axes.Left.CalcYPosValue(Median(i + 1))))
.Graphics3D.LineTo(Convert.ToInt32(.Series(0).CalcXPos(i) + (Candle1.CandleWidth / 2)), Convert.ToInt32(.Axes.Left.CalcYPosValue(Median(i + 1))))
Next i
Catch ex As Exception
' Do nothing. Sometimes during print the LineTo function fails. It should be ignored. NOE\liversen 07-02-2007
' MsgBox(ex.Message)
End Try
System.Windows.Forms.Application.DoEvents()
If Not Repainted Then
Repainted = True
.Refresh()
End If
TChart1.Aspect.View3D = False
End With
Can you help me get those lines that I draw to be shown?
Thanks in advance
Lars
Posted: Fri Apr 13, 2007 1:41 pm
by narcis
Hi Lars,
I had to some changes to your code because I couldn't build it here. Notice that I used AfterDraw's g parameter insteat of TChart1.Graphics3D canvas.
Anyway, the code below worked fine for me, does it work fine at your end?
Code: Select all
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Candle1.FillSampleValues()
Bar1.FillSampleValues()
Dim bmp As Bitmap = TChart1.Bitmap
End Sub
Private Sub TChart1_AfterDraw(ByVal sender As System.Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.AfterDraw
Dim i As Integer
g.Line(0, 0, 100, 100)
With g
'Do limit lines
'If TChart1.Series(0).Count > 0 Then
' DrawLine(50)
' WriteText(50, "Upper")
' DrawLine(0)
' DrawLine(-50)
' WriteText(-50, "Lower")
'End If
'Windows.Forms.Application.DoEvents()
Try
For i = 0 To TChart1.Series(0).Count - 1
.Pen.Color = System.Drawing.Color.Black
.Pen.Width = 2
'.MoveTo(Convert.ToInt32(TChart1.Series(0).CalcXPos(i) - (Candle1.CandleWidth / 2)), Convert.ToInt32(TChart1.Axes.Left.CalcYPosValue(Median(i + 1))))
'.LineTo(Convert.ToInt32(TChart1.Series(0).CalcXPos(i) + (Candle1.CandleWidth / 2)), Convert.ToInt32(TChart1.Axes.Left.CalcYPosValue(Median(i + 1))))
.MoveTo(Convert.ToInt32(TChart1.Series(0).CalcXPos(i) - (Candle1.CandleWidth / 2)), Convert.ToInt32(TChart1.Axes.Left.CalcYPosValue(i + 1)))
.LineTo(Convert.ToInt32(TChart1.Series(0).CalcXPos(i) + (Candle1.CandleWidth / 2)), Convert.ToInt32(TChart1.Axes.Left.CalcYPosValue(i + 1)))
Next i
Catch ex As Exception
' Do nothing. Sometimes during print the LineTo function fails. It should be ignored. NOE\liversen 07-02-2007
' MsgBox(ex.Message)
End Try
'System.Windows.Forms.Application.DoEvents()
'If Not Repainted Then
' Repainted = True
' .Refresh()
'End If
TChart1.Aspect.View3D = False
End With
End Sub
Posted: Fri Apr 13, 2007 2:04 pm
by 9637396
Hi Narcís
Thats seem to work fine. Thanks.
)
But before I wrote like this:
DrawLine(-50) ' To create a horisontal line at the value 50.
and that is not possible to do with this g parameter.
What would I do in order to use g.line instead. I can figure this much:
g.Line(?,50,?,50)
Now what should I write at the questionmarks in order to have a horisontal line that goes from one end of the chart area to the other?
Thanks
Lars
Posted: Fri Apr 13, 2007 2:58 pm
by narcis
Hi Lars,
But before I wrote like this:
DrawLine(-50) ' To create a horisontal line at the value 50.
That's one of the reasons why I couldn't compile your code. Is that a method you implemented?
and that is not possible to do with this g parameter.
What would I do in order to use g.line instead. I can figure this much:
g.Line(?,50,?,50)
Now what should I write at the questionmarks in order to have a horisontal line that goes from one end of the chart area to the other?
Line method draws a line from point defined by x0 and y0 to x1 and y1 where x0, y0, x1 and y1 are the methods parameters in that order. Please notice that thoser parameters are screen pixel coordinates.
Posted: Mon Apr 16, 2007 9:57 am
by 9637396
Hi Narcis.
You are right the drawline method was one create here in my code. But it does not work either.
I still do not know what code to write instead of the questionmakrs in the line below:
g.Line(?,50,?,50)
What I need is a horisontal line at the value 50 that goes through the complete graph area.
I have tried this
.Line(TChart1.Series(0).CalcXPos(0) , TChart1.Series(0).CalcYPosValue(-50), TChart1.Series(0).CalcXPos(TChart1.Series(0).Count - 1) + 1, TChart1.Series(0).CalcYPosValue(-50))
But that gives me a problem if my candle graph series has only one candle, then no line is drawn. So what I need instead of the questionmarks is the horisontal point/value where the bottom axis start and the horisontal value where the bottom axis ends. Is there a way to find these pixel values?
Thanks in advance
Lars
Posted: Mon Apr 16, 2007 11:09 am
by narcis
Hi Lars,
I have tried this
.Line(TChart1.Series(0).CalcXPos(0) , TChart1.Series(0).CalcYPosValue(-50), TChart1.Series(0).CalcXPos(TChart1.Series(0).Count - 1) + 1, TChart1.Series(0).CalcYPosValue(-50))
It works fine for me here using this:
Code: Select all
Line(TChart1.Series(0).CalcXPos(0), TChart1.Series(0).CalcYPosValue(50), TChart1.Series(0).CalcXPos(TChart1.Series(0).Count - 1) + 1, TChart1.Series(0).CalcYPosValue(50))
But that gives me a problem if my candle graph series has only one candle, then no line is drawn. So what I need instead of the questionmarks is the horisontal point/value where the bottom axis start and the horisontal value where the bottom axis ends. Is there a way to find these pixel values?
Yes, in that case try something like this:
Code: Select all
Line(TChart1.Axes.Bottom.IStartPos, TChart1.Series(0).CalcYPosValue(50), TChart1.Axes.Bottom.IEndPos, TChart1.Series(0).CalcYPosValue(50))
Posted: Mon Apr 16, 2007 11:17 am
by 9637396
Hi Narcís
Thanks that was exactly what I needed
Thanks once again for perfect support
)
Br Lars iversen
Posted: Mon Apr 16, 2007 11:39 am
by narcis
Hi Lars,
You're welcome! I'm glad to hear that helped.