Page 1 of 1

Possible to keep line on Chart drawn by Cursor Tool?

Posted: Thu Dec 16, 2010 4:03 am
by 9244525
Hello,

Regarding Cursor Tool set up like this:
with Chart1.Tools.Add(TCursorTool) as TCursorTool do
begin
FollowMouse := True;
ScopeStyle := scsEmpty;
Pen.Visible := False;
Series := Chart1[0]; //A series must be assigned to show cross hair with cursor tool.
end;

As you can see, the cross hair is normally not drawn, as Pen.Visible is false.
I only show the cross hair when the mouse is pressed:
procedure TfrDataLogger.Chart1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Chart1.Tools.Items[0].Pen.Visible := True;
end;

When the user is moving the mouse, of course the cross hair is then following the mouse as planned.

My question is:
Is there a way to keep the cross hair that is drawn the moment the mouse is pressed, so it is not "ereased" when the mouse is moving to another xy position?
I need this function for a measuring tool, that shows the difference between the point where the mouse was pressed, and the actual mouse position.

I tried it with drawing the first cross hair manually, but it seems like I'm running into repainting issues again like I have had previously with others stuff.

What could be the right way to do this?
Thanks for any suggestions in advance :)

Re: Possible to keep line on Chart drawn by Cursor Tool?

Posted: Fri Dec 17, 2010 3:58 pm
by yeray
Hi ChartIt,

I'm not sure to understand what is the exact behaviour you are trying to achieve.
It sounds similar to TNearestTool. Have you seen it?
Do you want the Cursor to snap to the nearest Series point or you want only a vertical and an horizontal line to be drawn where you clicked into the chart?

Here it is an example of how you could draw an horizontal line and a vertical line where the mouse has been clicked and activate the cursor at the same time:

Code: Select all

uses Series, TeeTools;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.View3D:=false;
  Chart1.Legend.Visible:=false;

  with Chart1.AddSeries(TPointSeries) do FillSampleValues(20);

  with Chart1.Tools.Add(TColorLineTool) as TColorLineTool do
  begin
    Axis := Chart1.Axes.Bottom;
    AllowDrag:=false;
    Active := false;
  end;

  with Chart1.Tools.Add(TColorLineTool) as TColorLineTool do
  begin
    Axis := Chart1.Axes.Left;
    AllowDrag:=false;
    Active := false;
  end;

  with Chart1.Tools.Add(TCursorTool) as TCursorTool do
  begin
    FollowMouse := True;
    ScopeStyle := scsEmpty;
    Series := Chart1[0];
    Active := false;
  end;
end;

procedure TForm1.Chart1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  with Chart1.Tools[0] as TColorLineTool do
  begin
    Active := true;
    Value := Axis.CalcPosPoint(X);
  end;

  with Chart1.Tools[1] as TColorLineTool do
  begin
    Active := true;
    Value := Axis.CalcPosPoint(Y);
  end;

  Chart1.Tools[2].Active := true;
end;
If you want the ColorLines to snap to the series points, you can add a TNearestTool and use it to calculate the nearest index at OnMouseDown. Then, you only have to use this index to retrieve the Series X and Y values for that ValueIndex and give them to the Color Lines.

Code: Select all

ValueIndex:=(Chart1.Tools[3] as TNearestTool).GetNearestPoint(Chart1[0], X, Y);

Re: Possible to keep line on Chart drawn by Cursor Tool?

Posted: Tue Jan 04, 2011 8:07 pm
by 9244525
>>you want only a vertical and an horizontal line to be drawn where you clicked into the chart?
That's what I wanted, and your example was exactly what I needed.

Thank you very much!

By the way: If I deactivate the Cursor Tool in the MouseUp event, and then activate it in the MouseDown event, the lines are not drawn on the new cursor position, but on the last position before it got deactivated.
How is it possible to force a recalculation of the XY position in the MouseDown event?

This is why I didn't deactivate the Cursor Tool in my example, but just made the pin invisible. Hereby the position was recalculated while the cursor was moved, and ready when the pen was made visible again.
Not a very nice solution...

Re: Possible to keep line on Chart drawn by Cursor Tool?

Posted: Wed Jan 05, 2011 2:33 pm
by yeray
Hi ChartIt,

If I understood well, you could solve it moving the cursor manually to the position where the mouse is at OnMouseDown:

Code: Select all

uses Series, TeeTools;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.View3D:=false;
  Chart1.Legend.Visible:=false;
  Chart1.AllowZoom:=false;

  with Chart1.AddSeries(TPointSeries) do FillSampleValues(20);

  with Chart1.Tools.Add(TColorLineTool) as TColorLineTool do
  begin
    Axis := Chart1.Axes.Bottom;
    AllowDrag:=false;
    Active := false;
  end;

  with Chart1.Tools.Add(TColorLineTool) as TColorLineTool do
  begin
    Axis := Chart1.Axes.Left;
    AllowDrag:=false;
    Active := false;
  end;

  with Chart1.Tools.Add(TCursorTool) as TCursorTool do
  begin
    FollowMouse := True;
    ScopeStyle := scsEmpty;
    Series := Chart1[0];
    Active := false;
  end;
end;

procedure TForm1.Chart1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  with Chart1.Tools[0] as TColorLineTool do
  begin
    Active := true;
    Value := Axis.CalcPosPoint(X);
  end;

  with Chart1.Tools[1] as TColorLineTool do
  begin
    Active := true;
    Value := Axis.CalcPosPoint(Y);
  end;

  with Chart1.Tools[2] as TCursorTool do
  begin
    Active := true;
    XValue := Chart1.Axes.Bottom.CalcPosPoint(X);
    YValue := Chart1.Axes.Left.CalcPosPoint(Y);
  end;
end;

procedure TForm1.Chart1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Chart1.Tools[2].Active := false;
end;
But hiding the pen as you've done is also a working solution, of course. :)