Page 1 of 1

How to keep TRectangleTool staying at the old position

Posted: Thu Nov 07, 2013 1:48 am
by 16566869
We have a question about TRectangleTool.
When we scroll chart, we want to keep the TRectangleTool stay at the old position.
It works well when scroll at one direction,
but when we change the scroll direction, it seems working strangely.

Code: Select all

__fastcall TForm7::TForm7(TComponent* Owner)
	: TForm(Owner)
{

	Series1->FillSampleValues(100);

	ChartToolRect->Left = 100;
	ChartToolRect->Width = 100;
	ChartToolRect->Top = Chart1->MarginTop;
	ChartToolRect->Height = Chart1->Height - Chart1->MarginTop - Chart1->MarginBottom;
	ChartToolRect->AllowDrag = false;
	ChartToolRect->AllowResize = false;
}

void __fastcall TForm7::Chart1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
		  float X, float Y)
{
	if (Y > Chart1->Height * 1 / 2)
		return;

	// Move to the cusor position
	ChartToolRect->Left = X;

	// Remember the axes position of ChartToolRect
	ChartToolRect->Tag = Chart1->BottomAxis->InternalCalcPosPoint(X);
}

void __fastcall TForm7::Chart1Scroll(TObject *Sender)
{
	ChartToolRect->Left = Series1->CalcXPos(ChartToolRect->Tag);
}


Please find the attachment for example project.

Re: How to keep TRectangleTool staying at the old position

Posted: Thu Nov 07, 2013 1:49 am
by 16566869
The example project

Re: How to keep TRectangleTool staying at the old position

Posted: Fri Nov 08, 2013 10:46 am
by narcis
Hi elmec,

There are some mistakes converting screen coordinates to axis values and viceversa. Also, some TChart1->Draw() calls are necessary in order to have the chart repainted and therefore properties updated before assigning them to the values which will be later used for saving the Rectangle tool position. You should do something like this:

Code: Select all

//---------------------------------------------------------------------------

#include <fmx.h>
#pragma hdrstop

#include "Unit7.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FMXTee.Tools"
#pragma resource "*.fmx"
TForm7 *Form7;
double XVal;

__fastcall TForm7::TForm7(TComponent* Owner)
	: TForm(Owner)
{

	Series1->FillSampleValues(100);

	ChartToolRect->Left = 100;
	ChartToolRect->Width = 100;
	ChartToolRect->Top = cht1->MarginTop;
	ChartToolRect->Height = cht1->Height - cht1->MarginTop - cht1->MarginBottom;
	ChartToolRect->AllowDrag = false;
	ChartToolRect->AllowResize = false;

	cht1->Draw();
	XVal=cht1->Axes->Bottom->InternalCalcPosPoint(ChartToolRect->Left);
}

void __fastcall TForm7::cht1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
          float X, float Y)
{
	if (Y > cht1->Height * 1 / 2)
		return;

	// Move to the cusor position
	ChartToolRect->Left = X;
	cht1->Draw();
	XVal=cht1->Axes->Bottom->CalcPosPoint(ChartToolRect->Left);

	// Remember the axes position of ChartToolRect
	XVal = cht1->BottomAxis->InternalCalcPosPoint(X);

}

void __fastcall TForm7::cht1Scroll(TObject *Sender)
{
	cht1->Draw();
	ChartToolRect->Left = Series1->CalcXPosValue(XVal);
}

Re: How to keep TRectangleTool staying at the old position

Posted: Sat Nov 09, 2013 12:48 am
by 16566869
Firstly, the "Draw()" in the construct of form makes the App. crashed.
Secondly, the "Draw()" in the Chart1Scroll makes the the scroll disabled...

Could you please take a look at it again?

Re: How to keep TRectangleTool staying at the old position

Posted: Sat Nov 09, 2013 1:03 am
by 16566869
The attachment for the example project.

Re: How to keep TRectangleTool staying at the old position

Posted: Mon Nov 11, 2013 9:43 am
by narcis
Hi elmec,

Ok, looks like this approach gives problems in Firemonkey. It worked fine in a Delphi VCL project. Draw() call is necessary to have some properties updated and the values for the rectangle tool are correct. I've found the code below works fine in a C++ Builder Firemonkey project. Find attached the modified project as well.

Code: Select all

//---------------------------------------------------------------------------

#include <fmx.h>
#pragma hdrstop

#include "Unit10.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FMXTee.Tools"
#pragma resource "*.fmx"
TForm10 *Form10;

double XVal;
Boolean DirtyX;
Boolean DirtyRect;
//---------------------------------------------------------------------------
__fastcall TForm10::TForm10(TComponent* Owner)
	: TForm(Owner)
{
   Series1->FillSampleValues(100);

   ChartToolRect->Left = 100;
   ChartToolRect->Width = 100;
   ChartToolRect->Top = Chart1->MarginTop;
   ChartToolRect->Height = Chart1->Height - Chart1->MarginTop - Chart1->MarginBottom;
   ChartToolRect->AllowDrag = false;
   ChartToolRect->AllowResize = false;

   DirtyX=True;
   DirtyRect=False;

   //Chart1->Draw();
   //XVal=Chart1->Axes->Bottom->InternalCalcPosPoint(ChartToolRect->Left);
}
//---------------------------------------------------------------------------
void __fastcall TForm10::Chart1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
		  float X, float Y)
{
   if (Y > Chart1->Height * 1 / 2)
	  return;

   // Move to the cusor position
   ChartToolRect->Left = X;
   DirtyX=True;
}
//---------------------------------------------------------------------------
void __fastcall TForm10::Chart1Scroll(TObject *Sender)
{
	DirtyRect=True;
}
//---------------------------------------------------------------------------
void __fastcall TForm10::Chart1AfterDraw(TObject *Sender)
{
	if (DirtyX)
	{
		XVal=Chart1->Axes->Bottom->InternalCalcPosPoint(ChartToolRect->Left);
		DirtyX=False;
	}
}
//---------------------------------------------------------------------------

void __fastcall TForm10::ChartToolRectAfterDraw(TObject *Sender)
{
	if (DirtyRect)
	{
		DirtyRect=False;
		ChartToolRect->Left = Series1->CalcXPosValue(XVal);
		Chart1->Draw();
	}
}
//---------------------------------------------------------------------------

Re: How to keep TRectangleTool staying at the old position

Posted: Mon Nov 11, 2013 10:12 am
by 16566869
Hello Narcís,
Thank you so much!
So it is a bug and will be modified in next release?

Re: How to keep TRectangleTool staying at the old position

Posted: Mon Nov 11, 2013 10:23 am
by narcis
Hi elmec,

Can not confirm it for now. We are investigating the issue. Did my last solution suggestion work fine at your end?

Re: How to keep TRectangleTool staying at the old position

Posted: Mon Nov 11, 2013 11:17 am
by 16566869
Hello Narcís,
Yes , we have just tried that and it worked!
Thanks so much!

Re: How to keep TRectangleTool staying at the old position

Posted: Mon Nov 11, 2013 11:41 am
by narcis
Hi elmec,

Thinking about your requirements, a ColorBand tool might be more suitable instead of a Rectangle tool. ColorBand is defined with Axis values (Start and End) so that you don't need to do anything with chart mouse events. When Zooming and scrolling it also preserves its original position.

Re: How to keep TRectangleTool staying at the old position

Posted: Mon Nov 11, 2013 12:07 pm
by 16566869
Hello Narcís,
Thanks for your advice.
We'll have a try of ColorBand tool!.

Re: How to keep TRectangleTool staying at the old position

Posted: Mon Nov 11, 2013 12:16 pm
by 16566869
Hello Narcís,
BTW, how to show some text on a ColorBand tool like TRectangleTool ?

Re: How to keep TRectangleTool staying at the old position

Posted: Mon Nov 11, 2013 12:23 pm
by narcis
Hi elmec,

It lacks this functionality. You could either use an Annotation tool on top of it, custom draw the text on TeeChart's canvas or continue using a rectangle tool.