Page 1 of 1

Initial position of TCursorTool

Posted: Wed Jul 15, 2009 7:34 am
by 9245460
Hi,

I have a 2D chart with 2 vertical cursors which are by default positioned in center of my graph. How can I move them to initil position that differe from center.

eg. I would like to position cursor 1 to the left on chart and cursor 2 to the right. I saw you could set XValue or YValue bur what if my series had more than one same YValue or XValue?

Thank you in advance.

Best regards.

Re: Initial position of TCursorTool

Posted: Wed Jul 15, 2009 8:28 am
by yeray
Hi PoLabs,

Please, take a look at the following example. Everything seems to work as expected:

Code: Select all

uses series, TeeTools;

procedure TForm1.FormCreate(Sender: TObject);
var series1: TPointSeries;
    i: Integer;
    cursor1, cursor2: TCursorTool;
begin
  Chart1.View3D := false;

  series1 := TPointSeries.Create(self);
  Chart1.AddSeries(series1);

  for i:=0 to 25 do
    series1.AddXY(i, round(random*10));

  for i:=0 to 25 do
    series1.AddXY(i, round(random*10));

  cursor1 := TCursorTool.Create(self);
  cursor2 := TCursorTool.Create(self);

  Chart1.Tools.Add(cursor1);
  Chart1.Tools.Add(cursor2);

  cursor1.Style := cssVertical;
  cursor2.Style := cssVertical;

  cursor1.Pen.Color := clRed;
  cursor2.Pen.Color := clGreen;

  Chart1.Draw;
  cursor1.XValue := series1.XValues.MinValue;
  cursor2.XValue := series1.XValues.MaxValue;
end;
If there is any important step I missed, please post here a code (or attach a simple example project) we can run as-is to reproduce the problem here.

Re: Initial position of TCursorTool

Posted: Wed Jul 15, 2009 8:41 am
by 9245460
Dummy me!! I am so deep into it so I just need to step on step back to see the solution. :oops:

Thank you very much.

Best regards.

Re: Initial position of TCursorTool

Posted: Wed Jul 15, 2009 9:50 am
by 9245460
One more question. :oops:

What if TCursorTool doesn't have assigned series property? Is still possible to set position of cursor?

I am using one cursor (general on chart) for several series, so I never really need one series to be assigned to cursor and I still would like to set it's position. Is that possible? I assume tha position could be only set if series is assigned to TCursorTool.

Thanks.
BR

Re: Initial position of TCursorTool

Posted: Wed Jul 15, 2009 12:05 pm
by yeray
Hi BR,

In the example I posted above I used two cursor tools without linking them to any series. I only used a series to obtain the left and the right values. You can also use the axis if you want:

Code: Select all

  cursor1.XValue := Chart1.Axes.Bottom.Minimum;
  cursor2.XValue := Chart1.Axes.Bottom.Maximum;

Re: Initial position of TCursorTool

Posted: Wed Jul 15, 2009 12:12 pm
by 9245460
Thank you very much. It works! :)

Best regards,
Aljosa

Re: Initial position of TCursorTool

Posted: Wed Jul 15, 2009 1:06 pm
by 9245460
Again me... :oops:

I have some TLabel objects which I would like to move along with cursors. It is not problem to do that while moving cursor (i use onchange event), but how can I do that programaticly to set labels at cursor position?

an eg.
Label1.Left := GetCoordsFromvalue(Cursor.XValue)

What should be that GetCoordsFromvalue function?

Thanks.

Re: Initial position of TCursorTool

Posted: Wed Jul 15, 2009 2:41 pm
by yeray
Hi BR,

This is the one:

Code: Select all

tChart1.Axes.Bottom.CalcXPosValue();
PoLabs wrote:Again me... :oops:
Don't worry. We are here for that!

Re: Initial position of TCursorTool

Posted: Wed Jul 15, 2009 7:15 pm
by 9245460
Hi,

thanks for your answer. Your support is realy good.

When I try to use Chart.Axes.Bottom.CalcPos... but on runtime I get for every point 0. My axis min is -5 and max is +5. I set cursor C1 to left (-5) and C2 to right (5) and that works fine. When I try to move coresponding label (labels marks cursor like an eg. 'C1', 'C2') I would need coords to set properties Left and Top. I tried it with Label1.Left := Chart.Axes.Bottom.CalcPosFromValue(Chart.Axes.Bottom.Minimum), but if I inspect that property I alwys get 0 on runtime.
Well if there exist any answer to that I would very happy in other case I will just ignore it and remove labels and forget about it. Don't want to loose time on pretty small thing.

Thank you again.
Aljosa

Re: Initial position of TCursorTool

Posted: Thu Jul 16, 2009 10:48 am
by yeray
Hi

If I understand well you simply need to assign the new label X position at OnChartToolChange event. Here is a complete example using annotation tool instead of labels:

Code: Select all

uses series;

var cursor1, cursor2: TCursorTool;
    annot1, annot2: TAnnotationTool;

procedure TForm1.FormCreate(Sender: TObject);
var series1: TPointSeries;
    i: Integer;
begin
  Chart1.View3D := false;

  series1 := TPointSeries.Create(self);
  Chart1.AddSeries(series1);

  for i:=0 to 25 do
    series1.AddXY(i, round(random*10));

  for i:=0 to 25 do
    series1.AddXY(i, round(random*10));

  cursor1 := TCursorTool.Create(self);
  cursor2 := TCursorTool.Create(self);
  Chart1.Tools.Add(cursor1);
  Chart1.Tools.Add(cursor2);
  cursor1.Style := cssVertical;
  cursor2.Style := cssVertical;
  cursor1.Pen.Color := clRed;
  cursor2.Pen.Color := clGreen;

  annot1 := TAnnotationTool.Create(self);
  annot2 := TAnnotationTool.Create(self);
  Chart1.Tools.Add(annot1);
  Chart1.Tools.Add(annot2);
  annot1.Text := 'Q1';
  annot2.Text := 'Q2';
  annot1.Shape.Color := cursor1.Pen.Color;
  annot2.Shape.Color := cursor2.Pen.Color;
  annot1.Shape.Transparency := 50;
  annot2.Shape.Transparency := 50;
  annot1.Shape.Shadow.Visible := false;
  annot2.Shape.Shadow.Visible := false;
  cursor1.OnChange := ChartTool1Change;
  cursor2.OnChange := ChartTool2Change;

  Chart1.Draw;
  annot1.Top := Chart1.ChartRect.Top;
  annot2.Top := Chart1.ChartRect.Top;
  cursor1.XValue := Chart1.Axes.Bottom.Minimum;
  cursor2.XValue := Chart1.Axes.Bottom.Maximum;
end;


procedure TForm1.ChartTool1Change(Sender: TCursorTool; x, y: Integer;
  const XValue, YValue: Double; Series: TChartSeries; ValueIndex: Integer);
begin
  annot1.Left := x;
end;

procedure TForm1.ChartTool2Change(Sender: TCursorTool; x, y: Integer;
  const XValue, YValue: Double; Series: TChartSeries; ValueIndex: Integer);
begin
  annot2.Left := x;
end;

Re: Initial position of TCursorTool

Posted: Fri Jul 17, 2009 10:34 am
by 9245460
Hi,

thank you very much. It realy helped me a lot. I am very grateful for help.

I have another question. When I move cursor I have XValue of it. How can I get YValue of some series according to XValue of cursor. Now I am trying with

Ycoord := Series.CalcYPosValue(Cursor.XValue); // get y coord of XValue of cursor
Result := Series.YScreenToValue(Ycoord);


but it doesn't seem to be ok, values are different than expected. What am I doing wrong?

Thanks.

Aljosa

Re: Initial position of TCursorTool

Posted: Fri Jul 17, 2009 11:18 am
by yeray
Hi PoLabs,

Please, take a look at the interpolation example that NarcĂ­s posted here

Re: Initial position of TCursorTool

Posted: Tue Jul 21, 2009 11:27 am
by 9245460
Hi,

I would like to thank you for your help. I solved problems with your and Narcis help.

Thanks again.

Best regards.