如果已有的曲线是通过公式计算suode,则依赖公式可以获得任一X点的y值.其他情况下, 可以通过x点左右两个已知坐标点, 计算当前x点的y点 如3条线 A x:0, y:0; x:1, y:2; B x:0, y:0; x:1, y:4; C x:0, y:0; x:1, y:8;求X:0.5时, y值, 解方程即可
procedure DBChart1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); { This procedure draws the crosshair lines } Procedure DrawCross(AX,AY:Integer); begin With DBChart1,Canvas do begin Pen.Color:=CrossHairColor; Pen.Style:=CrossHairStyle; Pen.Mode:=pmXor; Pen.Width:=1; MoveTo(ax,ChartRect.Top-Height3D); LineTo(ax,ChartRect.Bottom-Height3D); // MoveTo(ChartRect.Left+Width3D,ay); // LineTo(ChartRect.Right+Width3D,ay); end; end; var I : Integer; Vx : Double; AValueIndex : Integer; TmpSeies : TChartSeries; tmpX,tmpY,xpox:double; begin DBChart1.Repaint; if (OldX<>-1) then begin DrawCross(OldX,OldY); { draw old crosshair } OldX:=-1; end; { check if mouse is inside Chart rectangle } if PtInRect( DBChart1.ChartRect, Point(X-DBChart1.Width3D,Y+DBChart1.Height3D) ) then begin DrawCross(x,y); { draw crosshair at current position } { store old position } OldX:=x; OldY:=y; Series1.GetCursorValues(tmpX,tmpY); { <-- get values under mouse cursor } if tmpX > 0 then begin DBChart1.Canvas.Font.Color:=clRed;//字体颜色 DBChart1.Canvas.Font.Size := 14; DBChart1.Canvas.Brush.Style := bsClear;//透明 Vx := Series1.YValues.Value[Trunc(tmpx)]; DBChart1.Canvas.TextOut(x+5,y-25,FloatToStr(Vx)); end; end; end;
如3条线
A x:0, y:0; x:1, y:2;
B x:0, y:0; x:1, y:4;
C x:0, y:0; x:1, y:8;求X:0.5时, y值, 解方程即可
Shift: TShiftState; X, Y: Integer);
{ This procedure draws the crosshair lines }
Procedure DrawCross(AX,AY:Integer);
begin
With DBChart1,Canvas do
begin
Pen.Color:=CrossHairColor;
Pen.Style:=CrossHairStyle;
Pen.Mode:=pmXor;
Pen.Width:=1;
MoveTo(ax,ChartRect.Top-Height3D);
LineTo(ax,ChartRect.Bottom-Height3D);
// MoveTo(ChartRect.Left+Width3D,ay);
// LineTo(ChartRect.Right+Width3D,ay);
end;
end;
var
I : Integer;
Vx : Double;
AValueIndex : Integer;
TmpSeies : TChartSeries;
tmpX,tmpY,xpox:double;
begin
DBChart1.Repaint;
if (OldX<>-1) then
begin
DrawCross(OldX,OldY); { draw old crosshair }
OldX:=-1;
end; { check if mouse is inside Chart rectangle }
if PtInRect( DBChart1.ChartRect, Point(X-DBChart1.Width3D,Y+DBChart1.Height3D) ) then
begin
DrawCross(x,y); { draw crosshair at current position }
{ store old position }
OldX:=x;
OldY:=y; Series1.GetCursorValues(tmpX,tmpY); { <-- get values under mouse cursor }
if tmpX > 0 then
begin
DBChart1.Canvas.Font.Color:=clRed;//字体颜色
DBChart1.Canvas.Font.Size := 14;
DBChart1.Canvas.Brush.Style := bsClear;//透明
Vx := Series1.YValues.Value[Trunc(tmpx)];
DBChart1.Canvas.TextOut(x+5,y-25,FloatToStr(Vx));
end;
end;
end;