0

I am using TCanvas to draw a blue line whenever the left mouse button is clicked, and a red line whenever the right mouse button is clicked. Currently whenever I click another line is drawn on the chart. What I want to do is to clear the old line, and then draw a new line.

Below is some sample code.

The code for the onClick event

procedure TForm2.Chart1ChartClick(Sender: TJvChart; Button: TMouseButton;
   Shift: TShiftState; X, Y, ChartValueIndex, ChartPenIndex: Integer;
   var ShowHint, HintFirstLineBold: Boolean; HintStrs: TStrings);

begin


   if Button = mbLeft then
   begin

     canvas.pen.color := clblue;
     PlotHorizontal(X, Y);


   end
     else if Button = mbRight then
     begin

        Canvas.Pen.color := clred;
        PlotHorizontal(X, Y);

     end

end;

The PlotHorizontal procedure

procedure TForm2.PlotHorizontal(X, Y : integer);

begin
   with canvas do
   begin
   // draw the horizontal line
      MoveTo(X, Y);
   // without the 4 the line doesn't seem to reach the end of the graph
      LineTo(X, Chart1.Options.XStartOffset -4);
      MoveTo(X, Y);
      LineTo(X, Chart1.Options.XStartOffset +
       + Chart1.Options.Yend);
   end;
end;
Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
TheEndIsNear
  • 395
  • 3
  • 17
  • By "clear the old line", do you mean "remove the last line drawn of any color", or "remove the last line drawn in this color"? – Ken White Jun 21 '13 at 16:36
  • Remove the last line drawn in this color. – TheEndIsNear Jun 24 '13 at 06:40
  • I would think drawing a line in background color over the line that should be cleared will do the trick. You'll have to store the necessary coordinates to do that though. But that's not really hard. – Sherlock70 Jun 25 '13 at 13:51

1 Answers1

0

I was able to get it working, by saving the old X, and Y values for where the lines were draw. Then when the mouse was clicked again, I refreshed the chart, and redrew the line again.

TheEndIsNear
  • 395
  • 3
  • 17