Unit GifChart; Interface Uses GIFImage, Graphics; Type // Index 0 is the oldest. TPrices = Array Of Double; Procedure FillInChart(Gif : TGIFImage; Prices : TPrices; Width : Integer = 0); Procedure CreateAndSaveChart(Filename : String; Prices : TPrices; Width : Integer = 0); Implementation Uses Math, Classes; Procedure CreateAndSaveChart(Filename : String; Prices : TPrices; Width : Integer); Var Gif : TGIFImage; Begin Gif := TGIFImage.Create; Try Gif.Compression := gcLZW; Gif.ColorReduction := rmNone; FillInChart(Gif, Prices, Width); Gif.SaveToFile(FileName); Finally Gif.Free End end; Procedure FillInChart(Gif : TGIFImage; Prices : TPrices; Width : Integer); Const ImageHeight = 20; Var B : TBitmap; TopColor, BodyColor : TColor; Highest, Lowest : Double; I : Integer; LineTop, LineX : Integer; Begin If Width = 0 Then Width := Length(Prices); If Width = 0 Then Begin Gif.Height := ImageHeight; Gif.Width := 0 End Else Begin B := TBitmap.Create; Try B.PixelFormat := pf24bit; B.Height := ImageHeight; B.Width := Width; B.Canvas.Brush.Color := clBlack; B.Canvas.FillRect(Rect(0, 0, Width, ImageHeight)); If Prices[0] > Prices[Pred(Length(Prices))] Then Begin TopColor := $0000ff; BodyColor := $141480 End Else If Prices[0] < Prices[Pred(Length(Prices))] Then Begin TopColor := $00ff00; BodyColor := $008000 End Else Begin TopColor := $C0C0C0; BodyColor := $606060 End; If Length(Prices) > Width Then Prices := Copy(Prices, Length(Prices) - Width, Width); Lowest := MaxDouble; Highest := -MaxDouble; For I := Low(Prices) To High(Prices) Do Begin Lowest := Min(Lowest, Prices[I]); Highest := Max(Highest, Prices[I]) End; If Highest = Lowest Then Begin Highest := Highest + 1; Lowest := Lowest - 2 // Technically this could still fail for large numbers! End; LineX := Pred(Width); For I := High(Prices) DownTo Low(Prices) Do Begin LineTop := Round((Highest - Prices[I]) / (Highest - Lowest) * ImageHeight - 0.5); // Ideally we don't need this, but their might be a round off error. If LineTop < 0 Then LineTop := 0 Else If LineTop >= ImageHeight Then LineTop := Pred(ImageHeight); B.Canvas.Pen.Color := TopColor; B.Canvas.MoveTo(LineX, LineTop); B.Canvas.LineTo(LineX, Succ(LineTop)); B.Canvas.Pen.Color := BodyColor; B.Canvas.LineTo(LineX, ImageHeight); Dec(LineX) End; Gif.Assign(B) Finally B.Free End End End; End.