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;
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.