Page 1 of 1

Sevral useful function you would like to want

Posted: Mon Dec 26, 2005 6:37 am
by 6919081
hi everybody
when we mouse the mouse ,we want to get the nearest point Value, however, we don't move the mouse on the seriesline.How can we do it.now ,you can use this function ,it's writted in vbsript,i think you can rewrite it in other language,like vb,vb and so on.
if there are some problems,please send e_mail to sx_qfh@hotmail.com

Check this code as next:
First ,Get the ValueIndex of the Xval ,used in the trend Chart

Code: Select all

Function GetSeriesIndex_XVal(seriesIndex,xvals)
dim i,ci
dim xv
  xv=xvals
  ci=0
  for i=0 to TChart1.Series(seriesIndex).count-1 
 	if xv<=TChart1.Series(seriesIndex).xvalues.value(i) then
  	Exit for
  else
  	ci=i
	end if
 Next
 
  if ci=-1 then
  	ci=0
  ElseIF ci<TChart1.Series(seriesIndex).count-1 then
    if abs(xv-TChart1.Series(seriesIndex).xvalues.value(ci))>abs(xv-TChart1.Series(seriesIndex).xvalues.value(ci+1)) then
			ci=ci+1
    end if
 end if
  GetSeriesIndex_XVal=ci
end Function
Second.Get YVal of the Current Xval between x1 and x2,used in the wave chart.

Code: Select all

Function GetSeriesYValByXVal(tmpindex,xval)
dim  i,xi1,xi2,result
dim  xv1,yv1,xv2,yv2
  result=0
  if TChart1.Series(tmpindex).count=0 then exit Function 
  xi1=0
  xi2=0
  for i=0 to TChart1.Series(tmpindex).count-1
 	if xval<=TChart1.Series(tmpindex).xvalues.value(i) then
  	 Exit for
  else
    xi1=xi2
  	xi2=i
    end  if
 Next
  xv1=TChart1.Series(tmpindex).xvalues.value(xi1)
  yv1=TChart1.Series(tmpindex).yvalues.value(xi1)
  xv2=TChart1.Series(tmpindex).xvalues.value(xi2)
  yv2=TChart1.Series(tmpindex).yvalues.value(xi2)
  if xv2<>xv1 then
    result=yv1+(xval-xv1)*(yv2-yv1)/(xv2-xv1)
  else
    result=yv1
  end if
  GetSeriesYValByXVal=result
end Function 

Posted: Sat Dec 31, 2005 1:05 pm
by Pep
Hi Richard,

to get this values you could also use the Nearest Point Tool, using similar code to the following :

Code: Select all

Dim ValueInd

Private Sub Form_Load()
With TChart1
    .Aspect.View3D = False
    .AddSeries scPoint
    .Series(0).FillSampleValues 20
    .Tools.Add tcNearest
    .Tools.Items(0).asNearest.Series = .Series(0)
    .Tools.Items(0).asNearest.Pen.Visible = False
    .Tools.Items(0).asNearest.DrawLine = False
End With
End Sub

Private Sub TChart1_OnMouseDown(ByVal Button As TeeChart.EMouseButton, ByVal
Shift As TeeChart.EShiftState, ByVal X As Long, ByVal Y As Long)
With TChart1
    If Button = mbLeft Then
        .StopMouse
        MsgBox "Nearest Point X=" & .Series(0).XValues.Value(ValueInd) & "
Y=" & .Series(0).YValues.Value(ValueInd)
    End If
End With
End Sub

Private Sub TChart1_OnNearestToolChange()
With TChart1
    ValueInd = .Tools.Items(0).asNearest.Point
End With
End Sub
Many thanks for you contribution.