Page 1 of 1

Adding a TColorBandTool, resizing immediately

Posted: Mon Sep 19, 2016 11:13 pm
by 16577368
I am adding a band to a chart as soon as the user clicks on the chart--and I want to start resizing the band as soon as it is added. Can that be done?

Thanks
Ed Dressel

Re: Adding a TColorBandTool, resizing immediately

Posted: Tue Sep 20, 2016 8:01 am
by yeray
Hello Ed,

Yes, you can use the mouse events to do so.
I'm not sure how do you want to determine what TColorLineTool (StartLine/EndLine) should be dragged (note dragging one of the two TColorLineTools in the TColorBandTool is a TColorBandTool resizing) when you just created the tool.
Here you have an example that creates the TColorBandTool and starts dragging it at OnMouseDown event:

Code: Select all

uses Series, TeeTools;

type TMyColorBandTool=class(TColorBandTool)
  private
    Dragging : Boolean;
    Old      : Double;
end;

var myColorBand: TMyColorBandTool;

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

  Chart1.AddSeries(TBarSeries).FillSampleValues();
end;

procedure TForm1.Chart1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if not Assigned(myColorBand) then
  begin
    myColorBand:=Chart1.Tools.Add(TMyColorBandTool) as TMyColorBandTool;

    with myColorBand do
    begin
      Axis:=Chart1.Axes.Bottom;
      StartValue:=2;
      EndValue:=4;

      if not ParentChart.CancelMouse then
      begin
        Dragging:=True;

        if Axis.Horizontal then
           Old:=Axis.CalcPosPoint(X)-StartValue
        else
           Old:=Axis.CalcPosPoint(Y)-StartValue;

        ParentChart.CancelMouse:=True;
      end;
    end;
  end;
end;

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var tmpValue,
    tmpStartValue,
    tmpDiff  : Double;
begin
  if Assigned(myColorBand) and myColorBand.Dragging then
  begin
    with myColorBand do
    begin
      tmpDiff:=EndValue-StartValue;

      if Axis.Horizontal then
         tmpValue:=Axis.CalcPosPoint(X)-Old
      else
         tmpValue:=Axis.CalcPosPoint(Y)-Old;

      if NoLimitDrag then
         tmpStartValue:=tmpValue
      else
         tmpStartValue:=StartLine.LimitValue(tmpValue);

      tmpValue:=EndLine.LimitValue(tmpStartValue+tmpDiff);

      if (not NoLimitDrag) and (tmpValue<(tmpStartValue+tmpDiff)) then
         StartValue:=tmpValue-tmpDiff
      else
         StartValue:=tmpStartValue;

      EndValue:=StartValue+tmpDiff;
    end;
  end;
end;

procedure TForm1.Chart1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Assigned(myColorBand) then
     myColorBand.Dragging:=False;
end;