test

progging - To wander about and beg; to seek food or other supplies by low arts; to seek for advantage by mean shift or tricks.
progging - Programmer slang for writing computer code.
Viser innlegg med etiketten QuickReport. Vis alle innlegg
Viser innlegg med etiketten QuickReport. Vis alle innlegg

tirsdag 30. august 2011

Dynamically adding TQRChart at run-time on a QuickReport

Handle the BeforePrint method in the TQuickRep class to clone the TeeChart graphs...

procedure TQReport.QReportBeforePrint(Sender: TCustomQuickRep;
  var PrintReport: Boolean);
var
  i: Integer;
  LV_Height: Integer;
  LV_Top: Integer;
  LV_Chart : TChart;
  tmp: TQRChart;
begin
  // Divide height between the N charts
  LV_Height := Round(DetailBand1.Height / ChartList.Count);
  LV_Top := 0; // Starting point

  for LV_Chart in ChartList do begin
    { Create the QRChart }
    tmp:=TQRChart.Create(Self);
    { Create the QRDBChart }
    With TQRDBChart.Create(tmp) do
    begin
      Parent:=TWinControl(tmp);
      Name:=TeeGetUniqueName(Owner,'QRChart');
      Title.Text.Clear;
      Title.Text.Add(tmp.ClassName);
    end;
    { add the QRChart to the QuickReport... }
    With tmp do
    begin
      ParentReport := Self;
      Parent := DetailBand1;
      Width := DetailBand1.Width;
      Height := LV_Height;
      Left := 0;
      Top := LV_Top;
      // Move next chart down
      LV_Top := LV_Top + LV_Height;
      { Copy Series and do special formatting}
      Chart.FreeAllSeries;
      Chart.Assign(LV_Chart);
      for i:=0 to LV_Chart.SeriesCount-1 do
        CloneChartSeries(LV_Chart[i]).ParentChart := Chart;

      Chart.Color := clWhite;
      // To include all data - and not only the last 10 years
      Chart.BottomAxis.Automatic := True;
      // Remove gradient background for printing
      Chart.BackWall.Gradient.Visible := False;
    end;
  end;
end;
The QReport tips are taken from here:
http://www.steema.com/support/faq/NewVCL/FAQ_VCL_QUICKREPORT.htm

Print/Preview of Quick Report problem

While testing some code that creates a Quick Report with a TeeChart on it, I changed the Print() call to a PreviewModal(). All though this seems to work at first, two problems occurred.

  1. The second time I clicked to see the preview dialog, the report did not show (only some black blocks)
  2. When I closed the application, I got several error messages.
Calling Preview() instead seem to fix the problem. Not sure what the problem was but this is from the QReport documentation:
Prepare
procedure Prepare.
Use ‘Prepare’ if you want to generate a report without automatically bring up a preview window or print it.
Preview
procedure Preview
Use ‘Preview’ to generate the report and bring up an on screen preview of it. From the preview window the user can choose to print the report.
PreviewModal
procedure PreviewModal
As Preview with the following differences -
When calling PreviewModal the report is generated in a background thread. Some database drivers are not thread safe and this might cause unexpected behavior or program crashes. Only use PreviewModal in situations where it is sure that a thread safe database driver will be used.

fredag 5. august 2011

Getting an Image into QuickReport


I had a big problem getting a .bmp file to show up in a QReport. Everything seemed straight forward. I had to do this dynamically during run-time, so I would read in the image to a paradox database and then have the QReport point to the correct database field.

The problem was that when I first configured the QReport, the database field was MEMO and not GRAPHIC. This information is stored in the .QR2 report file. And even though I corrected this field in the database using Database Manager, it would not be updated in the QuickReport.

The solution was to remove the dataset and add it again. This time the field correctly showed up as GRAPHIC, and the image showed up beautifully :-)

One possible solution could be to only remove the field in the dataset and then add it again.

The initial code for getting in image into the DB was:


(FieldByName('Icon') as TGraphicField).LoadFromFile('\path\to\image.bmp');

Although this works fine, the image file has to be available on the disk when the program runs. A better solution is to get the images included in the .exe as an embedded resource. The simplest way to achieve this was to include a TImageList on a form, and populate this with the images I needed. Then use the following code to put them in the database:


procedure InsertImageToDB(Field: TField; ImageIndex: Integer);
var
  stream: TStream;
  bmp: TBitmap;
begin
  bmp := TBitmap.Create();
  stream := Field.DataSet.CreateBlobStream(Field, bmWrite);
  try
    Frm.ImageList.GetBitmap(ImageIndex, bmp);
    bmp.SaveToStream(stream);
  finally
    stream.Free;
    bmp.Free;
  end;
end;
This has to be included in a Edit()...Post() block to get it posted to the DB.