Page 1 of 1

Deleting more than one point in a series

Posted: Tue Jun 17, 2008 12:28 pm
by 10546084
I am attempting to deleting multiple points is a series. The code belows shows what I am trying to do.
The problem is that it does not delete all the points.

Code: Select all


{*************************************************}
procedure TFormPlot.chrtPlotMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if (Button = mbLeft) and bDelete then
  begin
    nX0 := X;
    nY0 := Y;
    bMouseDown := TRUE;
  end
  else
end;


{**************************************************}
procedure TFormPlot.chrtPlotMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  i, j : Integer;
  nValue : Integer;
begin
  if (Button = mbLeft) and bDelete then
  begin
    nX1 := X;
    nY1 := Y;
    bMouseDown := FALSE;

    if (nX0 <> -1) and (nY0 <> -1) then
    begin
        //
        //how many series are there in this plotsheet
        //
        for i:=0 to chrtPlot.SeriesCount-1 do
          //how many points are there in this selected series
          for j:=0 to chrtPlot.Series[i].Count-1 do
            //is the point a valid selection
            if chrtPlot.Series[i].CalcXPos(j) >= nX0) and
                  chrtPlot.Series[i].CalcYPos(j) >= nY0) and
                  chrtPlot.Series[i].CalcXPos(j) <= nX1) and
                  chrtPlot.Series[i].CalcYPos(j) <= nY1) then
            begin

                chrtPlot.Series[i].Delete(j);
            end;//if
    end;// if (nX0 <> -1) and (nY0 <> -1) then}
  end
end;


{*************************************************}
procedure TFormPlot.chrtPlotMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  wX, wYl, wYr : double;
  strX, strY : String;
begin
  if bMouseDown then
  begin
    nX1 := X;
    nY1 := Y;
    //
    chrtPlot.Draw;
  end;
  //
  if bDelete then
  begin
    if (nX0 <> -1) and (nY0 <> -1) then
    begin
        //draw a rectangular square around the selected area
        chrtPlot.Canvas.Brush.Style := bsClear;
        chrtPlot.Canvas.Pen.Color := clBlack;
        chrtPlot.Canvas.Rectangle(nX0, nY0, nX1, nY1);
      end;
    end
  end;
Please advise.
Thank you.[/code]

Posted: Tue Jun 17, 2008 1:17 pm
by narcis
Hi bamboo,

This is because when deleting a point in the series points are reindexed. In that case you should use a for downto loop. This code works fine for me here:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
begin
  Series1.FillSampleValues();
  bDelete:=true;
  bMouseDown:=false;
  nX0 := -1;
  nY0 := -1;
  nX1 := -1;
  nY1 := -1;

  Chart1.Zoom.Allow:=false;
end;

procedure TForm1.Chart1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if (Button = mbLeft) and bDelete then
  begin
    nX0 := X;
    nY0 := Y;
    bMouseDown := TRUE;
  end
  else
end;

procedure TForm1.Chart1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  i, j : Integer;
  nValue : Integer;
begin
  if (Button = mbLeft) and bDelete then
  begin
    nX1 := X;
    nY1 := Y;
    bMouseDown := FALSE;

    if (nX0 <> -1) and (nY0 <> -1) then
    begin
        //
        //how many series are there in this plotsheet
        //
        for i:=0 to Chart1.SeriesCount-1 do
          //how many points are there in this selected series
          for j:=Chart1[i].Count-1 downto 0 do
            //is the point a valid selection
            if (Chart1[i].CalcXPos(j) >= nX0) and
                  (Chart1[i].CalcYPos(j) >= nY0) and
                  (Chart1[i].CalcXPos(j) <= nX1) and
                  (Chart1[i].CalcYPos(j) <= nY1) then
            begin

                Chart1[i].Delete(j);
            end;//if
    end;// if (nX0 <> -1) and (nY0 <> -1) then}
  end
end;

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  wX, wYl, wYr : double;
  strX, strY : String;
begin
  if bMouseDown then
  begin
    nX1 := X;
    nY1 := Y;
    //
    Chart1.Draw;
  end;
  //
  if bDelete then
  begin
    if (nX0 <> -1) and (nY0 <> -1) then
    begin
        //draw a rectangular square around the selected area
        Chart1.Canvas.Brush.Style := bsClear;
        Chart1.Canvas.Pen.Color := clBlack;
        Chart1.Canvas.Rectangle(nX0, nY0, nX1, nY1);
      end;
    end

end;

Posted: Wed Jun 18, 2008 7:47 am
by 10546084
Thank you very much.