Let's say that you have an Intraweb (VCL for the Web) application.
And you want to display in it some charts.
One of the possible solutions is to use the standard TChart component
distributed with Delphi:
- create a TChart in the background; add series and results
- save the TChart to a graphics file
- display that file with the standard HTML - IMG
The code is pretty simple:
var
LImage: TGraphic;
. . .
// GetChartFileName creates a file name for your chart - more about this below:
LImageFileName := GetChartFileName;
ForceDirectories(ExtractFilePath(LImageFileName));
// Create a bitmap that will
LBitmap := TBitmap.Create;
try
// We don't want any distortions when saving the chart's image:
LBitmap.SetSize(Chart.Width, Chart.Height);
LRect := Rect(0, 0, Chart.Width, Chart.Height);
Chart.Draw(LBitmap.Canvas, LRect);
LImage := TGIFImage.Create;
try
LImage.Assign(LBitmap);
LImage.SaveToFile(LImageFileName);
finally
FreeAndNIL(LImage);
end;
finally
FreeAndNIL(LBitmap);
end;
Now, you can refer to this file using a template page (and an IW layout manager) with an IMG tag.
Or you can use IW's TIWImageFile component:
ChartImageFile.ImageFile.FileName := GetChartFileName;
ChartImageFile.OutputType := ioGIF;
The above code handled GIF images; however, if you're using complex shading, background images, etc.
you may want to change to JPEG images:
// In the first code snippet:
LImage := TJPEGImage.Create;
. . .
// In the second code snippet:
ChartImageFile.OutputType := ioJPEG;
Note that TJPEGImage handles compression levels (CompressionQuality) - you may want to experiment with that.
Earlier, I wrote that GetChartFileName needs some more discussion:
- First, chart files must be created in the ./Files subfolder (or any subfolder there).
-
Second, a chart's file name may need to be unique for different sessions.
If this is the case in your program,
you can easily generate such names by appending the session's ID to the chart's file name.
Anyway, a pretty quick and easy way to display charts in Intraweb applications.
Top
|