FMXTutorial12

From Steema Software Reference Wiki
Jump to: navigation, search

Tchfmx.jpg

Contents page
Previous | Next

Tutorial 12 - Exporting and Importing Charts


This tutorial is overviews exporting TeeCharts in various formats and importing TeeChart's own .tee format Chart templates. See the TeeChart example code in the Examples folder below your TeeChart installation folder.


Exporting Charts

Available formats

Available Export formats. All formats may be copied to either a file or Clipboard. Some formats may be copied to Stream.

Image formats

(BMP)

CopyToClipboardBitmap

(WMF)

CopyToClipboardMetafile

BMP

TBMPExportFormat

JPEG

SaveChartToJPEGFile

WMF

TEMFExportFormat.Enhanced (false)

EMF

TEMFExportFormat

GIF

TGIFExportFormat

PNG

TPNGExportFormat

PCX

TPCXExportFormat

VML (HTML)

TVMLExportFormat

PDF

TPDFExportFormat

SVG

TSVGExportFormat

EPS

TEPSExportFormat

Flex(Flash)

TFlexExportFormat

VRML 3D

TVRMLExportFormat

XAML (WPF)

TXAMLExportFormat

Data formats

Text

TSeriesDataText

XML

TSeriesDataXML

HTML

TSeriesDataHTML

Excel

TSeriesDataXLS

Other formats

Tee format is a flexible format that stores Chart property information and, optionally, Chart data. Files are small (data dependent) and ideal for network use to update live client based Charts.

TEE (TeeChart)

SaveChartToFile


At runtime you can display the export dialog by using the :

Chart TeeExport

method.

Example:


TeeExport( Self, Chart1 );

Example export

Exporting to a file is reasonable straightforward, in most cases you just need to define the destination filename.

Example:

if SaveDialog1.Execute then
Chart1.SaveToBitmapFile(SaveDialog1.FileName);

Exporting Images

JPEG

JPEG file export has additional parameters for speed and quality. See the JPEG demo included in the Examples folder. The Export method has not been included natively for TeeChart to avoid the memory overhead for non-JPEG applications.

Example

// You need to include the Delphi JPEG unit in the Uses section of your project
// Pass the name of the Chart to this function. We've fixed the parameters here.
// In the demo they are presented as options for the user.
// Place a Chart (Chart1) on a Form and populate it with data.

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
 With GetChartJPEG(Chart1) do
 try
  SaveToFile('c:\temp\myJPEGChart.jpg');    { <-- save the JPEG to disk }
 finally
  Free;  { <-- free the temporary JPEG object }
 end;
end;

Function GetChartJPEG(AChart:TCustomChart):TJPEGImage;
var tmpBitmap:TBitmap;
begin
  result:=TJPEGImage.Create;   { <-- create a TJPEGImage }

  tmpBitmap:=TBitmap.Create;   { <-- create a temporary TBitmap }
  try

    tmpBitmap.Width :=AChart.Width;   { <-- set the bitmap dimensions }
    tmpBitmap.Height:=AChart.Height;

    { draw the Chart on the temporary Bitmap... }
    AChart.Draw(tmpBitmap.Canvas,Rect(0,0,tmpBitmap.Width,tmpBitmap.Height));

    { set the desired JPEG options... }
    With result do
    begin
      GrayScale            :=False;
      ProgressiveEncoding  :=True;
      CompressionQuality   :=50;  // % 0 - 100
      PixelFormat          :=jf24bit;  // or jf8bit
      ProgressiveDisplay   :=True;
      Performance          :=jpBestQuality;  // or jpBestSpeed
      Scale                :=jsFullSize;  // or jsHalf, jsQuarter, jsEighth
      Smoothing            :=True;

      { Copy the temporary Bitmap onto the JPEG image... }
      Assign(tmpBitmap);
    end;
  finally
    tmpBitmap.Free;  { <-- free the temporary Bitmap }
  end;
end;

Performance, jpegBestQuality, and the Compression Quality percentage (high value less compression), will make the file larger and thus slower to transmit across a network - quality is better though! You will need to decide on the best balance to suit your application.

GIF

GIF support is included with TeeChart courtesy of Anders Melander's Delphi GIFImage classes ((c) 1997-99 Anders Melander). Full documentation for the GIF classes are outside the scope of TeeChart's own documentation. Anders has donated GIFImage to the Jedi project who now co-ordinate documentation and support for it.

TeeChart provides the means to create GIF Chart images but you should check your licensing position with Unisys for use of GIF LZW encoded images. The alternative RLE encoding is not subject to Unisys licenses.

Example using TeeChart dialogue


Uses TeeGIF, TeExport;

procedure TGIFExportForm.Button1Click(Sender: TObject);
begin
  TeeSavePanel(TGIFExportFormat,Chart1);
end;

Example by code


Uses TeeGIF, GIFImage;

procedure TForm1.Button1Click(Sender: TObject);
var tmpGIF : TGIFImage;
begin
  tmpGIF:=TGIFImage.Create;

  With tmpGIF do
  begin
    Compression:=gcLZW;
    DitherMode:=dmStucki;
    ColorReduction:=rmQuantizeWindows;

    Assign(Chart1.TeeCreateBitmap(Chart1.Color,Rect(0,0,Chart1.Width,Chart1.Height)));

    SaveToFile('c:\tempCharts\Chart1.gif');
  end;
end;

PNG

To use PNG image format the LPng.DLL is required in \Windows\System folder or path. PNG provides a compressed image format that is supported by the principle browsers.

Example


Uses TeePNG, TeExport;

procedure TPNGExportForm.Button1Click(Sender: TObject);
begin
  TeeSavePanel(TPNGExportFormat,Chart1);
end;


Exporting data

TeeStore unit includes the definition of the TSeriesData component and its descendants:

Text

TSeriesDataText

XML

TSeriesDataXML

HTML

TSeriesDataHTML

Excel

TSeriesDataXLS

The above components may be created and associated with a Chart Series from which they can export data as either File, Stream or to the Clipboard. The following example exports the data from a Chart Series to an HTML table:


  With TSeriesDataHTML.Create(Chart1,Series1) do
  Begin
    IncludeHeader:=True;
    SaveToFile('c:\tempdata\Series1HTMLData.txt');
  end;

The output of the above with a random dataset is the following:

Series1
308
267
170
192
284
253
265
296
335
454

TeeChart's 'Tee' template and data export/import format

Tee files

Tee files are TeeChart's own template format for saving Charts and their data. Modified Chart properties are saved with the template and reproduced when the template is imported to a new Chart.

Advantages:

  • Tee files are very small in size, in most cases that offers an advantage over pure graphics formats (Quicker).
  • The destination Chart for the template is 'live', it can be zoomed and scrolled and have its properties modified.
  • Data may optionally be included with the tee template according to your preference.

The declarations for SaveChartToFile/SaveChartToStream are:


 procedure SaveChartToFile(AChart: TCustomChart; Const AName: String; IncludeData: Boolean);

 procedure SaveChartToStream(AChart: TCustomChart; AStream: TStream; IncludeData: Boolean);

 {See the TeeStore unit for more information}

Example

// Add the unit teestore to the 'Uses' section of your project

With SaveDialog1 do
begin
  Filter:='Teefiles|*.tee';

  if Execute then
     SaveChartToFile(Chart1,SaveDialog1.FileName,True);
end;


Import

Importing Tee format files

Import a saved Tee file from a local file source or http data source.


Example Imports

Example


// Import from file

procedure TForm1.Button1Click(Sender: TObject);
var tmpChart : TCustomChart;
begin

  Chart1.Free;   // Assuming Chart1 is already on the Form
  tmpChart:=TChart.Create(Self);

  With OpenDialog1 do
  begin
    Filter:= 'Teefiles|*.tee';
    if Execute then
       LoadChartfromFile(tmpChart,OpenDialog1.FileName);
  end;

  Chart1 := tmpChart as TChart;

  With Chart1 do
  begin
    Parent:=Self;
  end;
end;

You may also import a TeeChart file from URL, LoadChartFromURL.


ChartWebSource and SeriesTextSource

Tee files may be imported from remote web sources via the LoadChartFromURL method or the ChartWebSource component. CSV data may be imported via the TSeriesTextSource component. Please see the [Tutorial9.htm Internet Applications tutorial] for more information.


Other datasources

For information about data import from other database information sources please see the [Tutorial8.htm Database Access tutorial].



PREVIOUS

NEXT


© 1998-2019 Steema Software SL. All rights reserved.