Page 1 of 1

Line chart with error bars

Posted: Wed Dec 24, 2008 11:18 am
by 10547054
A line chart with error bars seems to be missing from the multitude of chart types available.

I want to show trends over time, and compare groups (three series), but I would like to inform the viewer of the underlying variation, which I can calculate.

Essentially, I want to Add (X, Y, Error, ... ) like a CustomErrorBar, but I want a line graph, not a bar graph.

Any way around this in the mean time?

Posted: Wed Dec 24, 2008 11:58 am
by narcis
Hi vas,

Ok, I've added your request to the wish-list to be considered for inclusion in future releases.

In the meantime you can hide the bars and custom draw the line like this:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
begin
  Series1.Clear;
  Series1.FillSampleValues();
  Series1.BarPen.Visible:=false;
  Series1.BarBrush.Style:=bsClear;
end;

procedure TForm1.Chart1AfterDraw(Sender: TObject);
var i ,x, y, tmp: Integer;
begin
  tmp:=Series1.BarWidth div 2;
  x:=Series1.CalcXPos(0) + tmp;
  y:=Series1.CalcYPos(0);

  Chart1.Canvas.Pen.Color:=clRed;
  Chart1.Canvas.Pen.Width:=2;
  Chart1.Canvas.MoveTo(x, y);

  for i:=1 to Series1.Count-1 do
  begin
    x:=Series1.CalcXPos(i) + tmp;
    y:=Series1.CalcYPos(i);
    Chart1.Canvas.LineTo(x, y);
    Chart1.Canvas.MoveTo(x, y);
  end;
end;

Posted: Wed Dec 24, 2008 12:50 pm
by narcis
Hi vas,

For completeness, for 3D series you can do this:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
begin
  Series1.Clear;
  Series1.FillSampleValues();
  Series1.BarPen.Visible:=false;
  Series1.BarBrush.Style:=bsClear;
end;

procedure TForm1.Chart1AfterDraw(Sender: TObject);
var i ,x, y, tmp: Integer;
begin
  tmp:=Series1.BarWidth div 2;
  x:=Series1.CalcXPos(0) + tmp;
  y:=Series1.CalcYPos(0);

  Chart1.Canvas.Pen.Color:=clRed;
  Chart1.Canvas.Pen.Width:=2;
  Chart1.Canvas.MoveTo3D(x, y, Series1.MiddleZ);

  for i:=1 to Series1.Count-1 do
  begin
    x:=Series1.CalcXPos(i) + tmp;
    y:=Series1.CalcYPos(i);
    Chart1.Canvas.LineTo3D(x, y, Series1.MiddleZ);
    Chart1.Canvas.MoveTo3D(x, y, Series1.MiddleZ);
  end;
end;

Thank you

Posted: Wed Dec 24, 2008 7:24 pm
by 10547054
Wow, instant service, works perfectly, and I learned new skills at the same time!

Clipping?

Posted: Thu Dec 25, 2008 7:03 am
by 10547054
Almost - this works, except when the screen is scrolled, the lines are displayed outside of the ChartRect.

Oddly, the error bars are not, nor are any other TeeChart series components.

But this is a minor issue for me, but it probably needs to addressed when the new ErrorLine Series is released.

Thanks again.