Draggable Vertical Lines...

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Maxc72
Newbie
Newbie
Posts: 7
Joined: Tue May 11, 2004 4:00 am

Draggable Vertical Lines...

Post by Maxc72 » Fri Nov 25, 2005 12:59 pm

I must create horizontal-draggable vertical lines over a graph, moreover i must know what value them have over a serie..

Somebody could help me?

Reagards

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Fri Nov 25, 2005 1:08 pm

Hi Maxc72,

I'd suggest you to use a TColorLineTool and implement something like the code below on its DragLine event.

Code: Select all

procedure TForm1.ChartTool1DragLine(Sender: TColorLineTool);
begin
  Chart1.Title.Text[0]:=FloatToStr(Sender.Value);
end;
For more information on TChart tools features please have a look at TeeChart features demo available at its program group.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Maxc72
Newbie
Newbie
Posts: 7
Joined: Tue May 11, 2004 4:00 am

Post by Maxc72 » Fri Nov 25, 2005 5:37 pm

Hi thank's for your help, i've tried it and it works fine, but i need that the line height is adjustable and not fixed to the graph panel. i need that the line heigth is about half of the graph panel height..

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Mon Nov 28, 2005 10:11 am

Hi Maxc72,

Then you can use a TColorBandTool and do something like:

Code: Select all

var
  Form1: TForm1;
  StartVal, MidVal, EndVal: Double;
  ToolHeight: Integer;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Series1.FillSampleValues();

  ToolHeight:=Chart1.Height div 2;
  MidVal:=(Series1.MaxYValue-Series1.MinYValue)/2;

  With ChartTool1 do
  begin
    StartValue:=MidVal - (ToolHeight div 2);
    EndValue:=MidVal + (ToolHeight div 2);

    StartLine.AllowDrag:=True;
    EndLine.AllowDrag:=True;

    StartVal:=StartValue;
    EndVal:=EndValue;
  end;

  Chart1.Title.Text[0]:=FloatToStr(MidVal);
end;

procedure TForm1.Chart1AfterDraw(Sender: TObject);
begin
  if ChartTool1.StartValue <> StartVal then
  begin
    ChartTool1.EndValue:=ChartTool1.StartValue+ToolHeight;
    StartVal:=ChartTool1.StartValue
  end
  else
    if ChartTool1.EndValue <> EndVal then
    begin
      ChartTool1.StartValue:=ChartTool1.EndValue-ToolHeight;
      EndVal:=ChartTool1.EndValue;
    end;

  MidVal:=StartVal+(ToolHeight/2);
  Chart1.Title.Text[0]:=FloatToStr(MidVal);
end;
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Maxc72
Newbie
Newbie
Posts: 7
Joined: Tue May 11, 2004 4:00 am

Post by Maxc72 » Mon Nov 28, 2005 12:00 pm

Hi I've tried to use the code but i obtain a very wide band, I've reduced the wide of the band but the heigth is still equal to graph panel and if i vertical scroll the graph the band is still here.

There is another problem, when i try to set dragging at runtime it have no effect, this code don't work at runtime:

Code: Select all

ChartTool1.StartLine.AllowDrag:=True;
ChartTool1.EndLine.AllowDrag:=True;  


if i create the tool in the ide and i set the dragging properties to true it work's fine and i can turn it on and off at runtime, but if i create the bar at runtime i can't set this properties (i.e. i can set them but there is no effect)


narcis wrote:Hi Maxc72,

Then you can use a TColorBandTool and do something like:

Code: Select all

var
  Form1: TForm1;
  StartVal, MidVal, EndVal: Double;
  ToolHeight: Integer;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Series1.FillSampleValues();

  ToolHeight:=Chart1.Height div 2;
  MidVal:=(Series1.MaxYValue-Series1.MinYValue)/2;

  With ChartTool1 do
  begin
    StartValue:=MidVal - (ToolHeight div 2);
    EndValue:=MidVal + (ToolHeight div 2);

    StartLine.AllowDrag:=True;
    EndLine.AllowDrag:=True;

    StartVal:=StartValue;
    EndVal:=EndValue;
  end;

  Chart1.Title.Text[0]:=FloatToStr(MidVal);
end;

procedure TForm1.Chart1AfterDraw(Sender: TObject);
begin
  if ChartTool1.StartValue <> StartVal then
  begin
    ChartTool1.EndValue:=ChartTool1.StartValue+ToolHeight;
    StartVal:=ChartTool1.StartValue
  end
  else
    if ChartTool1.EndValue <> EndVal then
    begin
      ChartTool1.StartValue:=ChartTool1.EndValue-ToolHeight;
      EndVal:=ChartTool1.EndValue;
    end;

  MidVal:=StartVal+(ToolHeight/2);
  Chart1.Title.Text[0]:=FloatToStr(MidVal);
end;

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Tue Nov 29, 2005 4:58 pm

Hi Maxc72,
Hi I've tried to use the code but i obtain a very wide band, I've reduced the wide of the band but the heigth is still equal to graph panel and if i vertical scroll the graph the band is still here.
Ok, then you can calculate it from TChart's rectangle (ChartRect property). Using the code below may work better for what you request.

Code: Select all

var
  Form1: TForm1;
  StartVal, MidVal, EndVal: Double;
  ToolHeight: Integer;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Series1.FillSampleValues();

  MidVal:=Series1.MinYValue+(Series1.MaxYValue-Series1.MinYValue)/2;

  With ChartTool1 do
  begin
    Axis:=Chart1.Axes.Left;

    StartLine.AllowDrag:=True;
    EndLine.AllowDrag:=True;

    StartValue:=Series1.MinYValue;
    EndValue:=MidVal;

    StartVal:=StartValue;
    EndVal:=EndValue;
  end;

  Chart1.Title.Text[0]:=FloatToStr(StartVal+((EndVal-StartVal)/2));
end;

procedure TForm1.Chart1AfterDraw(Sender: TObject);
begin
  ToolHeight:=(Chart1.ChartRect.Bottom-Chart1.ChartRect.Top) div 2;

  if ChartTool1.StartValue <> StartVal then
  begin
    ChartTool1.EndValue:=ChartTool1.StartValue+ToolHeight;
    StartVal:=ChartTool1.StartValue
  end
  else
    if ChartTool1.EndValue <> EndVal then
    begin
      ChartTool1.StartValue:=ChartTool1.EndValue-ToolHeight;
      EndVal:=ChartTool1.EndValue;
    end;

  MidVal:=StartVal+(ToolHeight/2);
  Chart1.Title.Text[0]:=FloatToStr(MidVal);
end;
There is another problem, when i try to set dragging at runtime it have no effect, this code don't work at runtime:
Yes, this is a problem I've been able to reproduce. I've added it to our defect list (TV52011105) to be fixed for future releases.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Maxc72
Newbie
Newbie
Posts: 7
Joined: Tue May 11, 2004 4:00 am

Post by Maxc72 » Wed Nov 30, 2005 12:50 pm

Hi, I'm sorry for bother you but i i've tried your code, and i think there was some misunderstanding, i must repruduce a graph like this:

Image

and i must drag (left and rigth) the yellow vertical lines.

Regards,
Massimiliano

Pep
Site Admin
Site Admin
Posts: 3299
Joined: Fri Nov 14, 2003 5:00 am
Contact:

Post by Pep » Thu Dec 01, 2005 4:27 pm

Hi Massimiliano,

one way to do this could be using the ClipRectangle method, in conjunction with TCursor Tools, this method will restric the Rect to display the Cursor. An example could be :

Code: Select all

procedure TForm1.Series1BeforeDrawValues(Sender: TObject);

  Function SeriesRect(Series:TChartSeries):TRect;
  begin
    With result do
    begin
      Left:=Series.GetHorizAxis.IStartPos;
      Right:=Series.GetHorizAxis.IEndPos;
      Top:=Series.GetVertAxis.IStartPos+100;
      Bottom:=Series.GetVertAxis.IEndPos-100;
    end;
  end;

begin
  With Chart1 do
       if CanClip then
          Canvas.ClipRectangle(SeriesRect( Sender as TChartSeries ));
end;

procedure TForm1.ChartTool2Change(Sender: TCursorTool; x, y: Integer;
  const XValue, YValue: Double; Series: TChartSeries; ValueIndex: Integer);
begin
Charttool2.Repaint;
end;
Another way to do this (which solves the fickering) could be a trick. It's to use a fake Series, adding the same data as the first Series, and creating a custom vertical axis for this Serie, setting the Start position to desired position you want that end the Cursor line. Then you only will have to asign the Cursor Tool to the fake series and hide the Axis and Series, you could use similar code like the following :

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Series
  Series1.FillSampleValues(10);
  Series2.FillSampleValues(10);
  Series2.Color := clnone;
  Series2.ShowInLegend:=false;

// Custom vertical axis
with Chart1.CustomAxes.Items[0] do
begin
  StartPosition:=50;
  Visible :=false;
  Series2.CustomVertAxis:=Chart1.CustomAxes.Items[0];
end;
  // Cursor Tool
  ChartTool1.Series := Series2;
  Charttool1.Style := cssVertical;
end;

Post Reply