Tutorial 12 - Exporting and Importing Charts

This tutorial is overviews exporting TeeCharts in various formats and importing TeeChart's own .ten format Chart templates.  

Contents

Exporting Charts
     Available formats
     Example export

Exporting Images
     BMP
     JPEG
     WMF & EMF
     GIF
     PNG
     TIFF
     Flex/Flash/SWF

Exporting data
     Text, XML, HTML, XLS

TeeChart's 'Ten' template and data export import format
     Ten files

Import
     Importing XML into TeeChart
     Importing Ten format files
     Example import Ten file
     Effect of changes made in importing Chart
     Other datasources


Exporting Charts

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

Image formats 
  • BMP: BitmapFormat Class

  • JPEG: JPEGFormat Class

  • WMF & EMF: MetafileFormat Class

  • GIF: GIFFormat Class

  • PNG: PNGFormat Class

  • TIFF: TIFFFormat Class


  • Data formats 
  • Text: TextFormat Class

  • XML: XMLFormat Class

  • HTML: HTMLFormat Class

  • Excel: ExcelFormat Class


  • Other formats 
    Ten 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.
  • TEN (TeeChart): TemplateExport Classs


  • Example export
    Exporting to a file is reasonable straightforward, in most cases you just need to define the destination filename.
    [C#] 
    private void button1_Click(object sender, System.EventArgs e)
                saveFileDialog1.Filter = "Images (*.bmp)|*.bmp|Images(*.jpg)|*.jpg|Images(*.wmf)|*.wmf|Images(*.emf)|*.emf|Images (*.gif)|*.gif|Images (*.png)|*.png|Images (*.tiff)|*.tiff";
                saveFileDialog1.ShowDialog();
                if(saveFileDialog1.FileName != "")
                    switch(saveFileDialog1.FilterIndex)
                        case 1:
                            tChart1.Export.Image.Bitmap.Save(saveFileDialog1.FileName);
                            break;
                        case 2:
                            tChart1.Export.Image.JPEG.Save(saveFileDialog1.FileName);
                            break;
                        case 3:
                            tChart1.Export.Image.Metafile.Enhanced = false;
                            tChart1.Export.Image.Metafile.Save(saveFileDialog1.FileName);
                            break;
                        case 4:
                            tChart1.Export.Image.Metafile.Enhanced = true;
                            tChart1.Export.Image.Metafile.Save(saveFileDialog1.FileName);
                            break;
                        case 5:
                            tChart1.Export.Image.GIF.Save(saveFileDialog1.FileName);
                            break;
                        case 6:
                            tChart1.Export.Image.PNG.Save(saveFileDialog1.FileName);
                            break;
                        case 7:
                            tChart1.Export.Image.TIFF.Save(saveFileDialog1.FileName);
                            break;
                    
                
            

    [VB.Net]
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SaveFileDialog1.Filter = "Images (*.bmp)|*.bmp|Images(*.jpg)|*.jpg|Images(*.wmf)|*.wmf|Images(*.emf)|*.emf|Images (*.gif)|*.gif|Images (*.png)|*.png|Images (*.tiff)|*.tiff"
            SaveFileDialog1.ShowDialog()
            If SaveFileDialog1.FileName <> "" Then
                Select Case SaveFileDialog1.FilterIndex
                    Case 1
                        TChart1.Export.Image.Bitmap.Save(SaveFileDialog1.FileName)
                    Case 2
                        TChart1.Export.Image.JPEG.Save(SaveFileDialog1.FileName)
                    Case 3
                        TChart1.Export.Image.Metafile.Enhanced = False
                        TChart1.Export.Image.Metafile.Save(SaveFileDialog1.FileName)
                    Case 4
                        TChart1.Export.Image.Metafile.Enhanced = True
                        TChart1.Export.Image.Metafile.Save(SaveFileDialog1.FileName)
                    Case 5
                        TChart1.Export.Image.GIF.Save(SaveFileDialog1.FileName)
                    Case 6
                        TChart1.Export.Image.PNG.Save(SaveFileDialog1.FileName)
                    Case 7
                        TChart1.Export.Image.TIFF.Save(SaveFileDialog1.FileName)
                End Select
            End If
    End Sub

    Exporting Images

    BMP
    BMP file export has additional parameters for colours and size.
    Example
    [C#] 
    case 1:
         Steema.TeeChart.Export.BitmapFormat bitMap = tChart1.Export.Image.Bitmap;
         bitMap.Height = 500;
         bitMap.Width = 700;
         bitMap.Monochrome = true;
         bitMap.Save(saveFileDialog1.FileName);
         break;


    [VB.Net]
    Case 1
         Dim BitMap As Steema.TeeChart.Export.BitmapFormat = TChart1.Export.Image.Bitmap
         BitMap.Height = 500
         BitMap.Width = 700
         BitMap.Monochrome = True
         BitMap.Save(SaveFileDialog1.FileName)

    JPEG
    JPEG file export now has parameters for speed and quality for both SaveToFile and SaveToStream.
    Example
    [C#] 
    case 2:
    Steema.TeeChart.Export.JPEGFormat jPEG = tChart1.Export.Image.JPEG;
         jPEG.GrayScale = true;
         jPEG.Height = 500;
         jPEG.Width = 700;
         jPEG.Quality = 100;
         jPEG.Save(saveFileDialog1.FileName);
         break;

    [VB.Net]
    Case 2
    Dim JPEGf As Steema.TeeChart.Export.JPEGFormat = TChart1.Export.Image.JPEG
         JPEGf.Height = 500
         JPEGf.Width = 700
         JPEGf.GrayScale = True
         JPEGf.Quality = 100
         JPEGf.Save(SaveFileDialog1.FileName)

    The Quality value (from 1 - 100) 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.

    WMF & EMF
    Enhanced Metafiles are simply created by setting "Enhanced" to True.
    Example
    [C#] 
    case 3:
         Steema.TeeChart.Export.MetafileFormat metaFile = tChart1.Export.Image.Metafile;
         metaFile.Height = 500;
         metaFile.Width = 700;
         metaFile.Enhanced = false;
         metaFile.Save(saveFileDialog1.FileName);
         break;

    [VB.Net]
    Case 3
         Dim MetaFile As Steema.TeeChart.Export.MetafileFormat = TChart1.Export.Image.Metafile
         MetaFile.Height = 500
         MetaFile.Width = 700
         MetaFile.Enhanced = False
         MetaFile.Save(SaveFileDialog1.FileName)

    GIF
    TeeChart for .Net is able to support the exportation of bitmaps in GIF format thanks to Microsoft's .Net Framework Class Library, specifically those classes contained in the System.Drawing.Imaging Namespace. To this day it remains unclear whether developers who implement these classes in their applications need to license the GIF patent from Unisys or not. Our best suggestion, if in doubt, is to visit the Unisys Website.

    Example
    [C#] 
    case 5:
         Steema.TeeChart.Export.GIFFormat gIF = tChart1.Export.Image.GIF;
         gIF.Height = 500;
         gIF.Width = 700;
         gIF.ColorReduction = Steema.TeeChart.Export.GIFFormat.GIFColorReduction.GrayScale;
         gIF.Save(saveFileDialog1.FileName);
         break;

    [VB.Net]
    Case 5
         Dim GIF As Steema.TeeChart.Export.GIFFormat = TChart1.Export.Image.GIF
         GIF.Height = 500
         GIF.Width = 700
         GIF.ColorReduction = Steema.TeeChart.Export.GIFFormat.GIFColorReduction.GrayScale
         GIF.Save(SaveFileDialog1.FileName)

    PNG
    The PNG format retains many of the advantages of the GIF format but also provides capabilities beyond those of GIF. PNG improves on GIF in its ability to progressively display an image; that is, to display better and better approximations of the image as it arrives over a network connection.
    Example
    [C#] 
    case 6:
         Steema.TeeChart.Export.PNGFormat pNG = tChart1.Export.Image.PNG;
         pNG.Compression = 100;
         pNG.GrayScale = true;
         pNG.Height = 500;
         pNG.Width = 700;
         pNG.Save(saveFileDialog1.FileName);
         break;

    [VB.Net]
    Case 6
         Dim PNGf As Steema.TeeChart.Export.PNGFormat = TChart1.Export.Image.PNG
         PNGf.Compression = 100
         PNGf.GrayScale = True
         PNGf.Height = 500
         PNGf.Width = 700
         PNGf.Save(SaveFileDialog1.FileName)

    TIFF
    TIFF is a flexible and extendable format that is supported by a wide variety of platforms and image-processing applications.
    Example
    [C#] 
    case 7:
         Steema.TeeChart.Export.TIFFFormat tIFF = tChart1.Export.Image.TIFF;
         tIFF.Compression = Steema.TeeChart.Export.TIFFFormat.TIFFCompression.LZW;
         tIFF.Height = 500;
         tIFF.Width = 700;
         tIFF.Save(saveFileDialog1.FileName);
         break;

    [VB.Net]
    Case 7
         Dim TIFF As Steema.TeeChart.Export.TIFFFormat = TChart1.Export.Image.TIFF
         TIFF.Compression = Steema.TeeChart.Export.TIFFFormat.TIFFCompression.LZW
         TIFF.Height = 500
         TIFF.Width = 700
         TIFF.Save(SaveFileDialog1.FileName)

    Adobe Flex/Flash/SWF
    TeeChart can now be exported to an Adobe Flex script file (*.mxml) which can then be compiled by Flex's free compiler (mxmlc.exe) to create a *.swf (Flash) file. A comprehensive example of how to achieve this at runtime, including details of how to add animations to the exported chart, can be found in the Feature demo under 'Welcome !\New in Canvas\Flash "CompactCharts"'.

    Exporting data

    There are four Export formats for Series data:
  • Text: TextFormat Class

  • XML: XMLFormat Class

  • HTML: HTMLFormat Class

  • Excel: ExcelFormat Class


  • The above components may be 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:

    [C#] 
    private void Form1_Load(object sender, System.EventArgs e)
                Random rnd = new Random();
                char c = new char();

                for(int i = 0; i < 10; ++i)
                    c = Convert.ToChar(65 + i);
                    bar1.Add(Convert.ToDouble(i),rnd.Next(100),c.ToString(),Color.Red);
                
                Steema.TeeChart.Export.DataExport dataExport = new Steema.TeeChart.Export.DataExport(tChart1.Chart);
                dataExport.HTML.IncludeHeader = true;
                dataExport.HTML.IncludeIndex = true;
                dataExport.HTML.IncludeLabels = true;
                dataExport.HTML.Series = bar1;
                dataExport.HTML.Save(@"C:.htm");
            

    [VB.Net]
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                Dim C As New Char()
                Dim i As New Integer()

                For i = 0 To 10
                    C = Convert.ToChar(65 + i)
                    Bar1.Add(Convert.ToDouble(i), Rnd() * 100, C.ToString(), Color.Red)
                Next
                Dim DataExport As New Steema.TeeChart.Export.DataExport(TChart1.Chart)
                DataExport.HTML.IncludeHeader = True
                DataExport.HTML.IncludeIndex = True
                DataExport.HTML.IncludeLabels = True
                DataExport.HTML.Series = Bar1
                DataExport.HTML.Save("C:.htm")
    End Sub

    The output of the above with a random dataset is the following:
    <table border="1">
    <tr><td>Index</td><td>Text</td><td>bar1</td></tr>
    <tr><td>0</td><td>A</td><td>69</td></tr>
    <tr><td>1</td><td>B</td><td>93</td></tr>
    <tr><td>2</td><td>C</td><td>18</td></tr>
    <tr><td>3</td><td>D</td><td>88</td></tr>
    <tr><td>4</td><td>E</td><td>42</td></tr>
    <tr><td>5</td><td>F</td><td>33</td></tr>
    <tr><td>6</td><td>G</td><td>58</td></tr>
    <tr><td>7</td><td>H</td><td>43</td></tr>
    <tr><td>8</td><td>I</td><td>63</td></tr>
    <tr><td>9</td><td>J</td><td>49</td></tr>
    </table>

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

    Ten files
    Ten 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:
  • Ten files are very small in size, in most cases that offers an advantage
        over pure graphic formats (qicker).
  •   
  • 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 ten template according to your preference.


  • Example
    [C#] 
    private void button1_Click(object sender, System.EventArgs e)
                saveFileDialog1.Filter = "TenFile (.ten)|*.ten";
                saveFileDialog1.ShowDialog();
                if(saveFileDialog1.FileName != "")
                    tChart1.Export.Template.IncludeData = true;
                    tChart1.Export.Template.Save(saveFileDialog1.FileName);
                
            
    [VB.Net]
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SaveFileDialog1.Filter = "TenFile (.ten)|*.ten"
            SaveFileDialog1.ShowDialog()
            If SaveFileDialog1.FileName <> "" Then
                TChart1.Export.Template.IncludeData = True
                TChart1.Export.Template.Save(SaveFileDialog1.FileName)
            End If
    End Sub

    Import

    Importing XML into TeeChart
    Importing XML from a TextBox.
    Example 
    [C#]
    protected override void button1_Click(object sender, System.EventArgs e)
                tChart1.Series.RemoveAllSeries();
                Steema.TeeChart.Styles.Points points1 = new Steema.TeeChart.Styles.Points(tChart1.Chart);
                points1.Title = "Points Series";
                xmlSource1.Series = points1;
                xmlSource1.SeriesNode = "Points Series";
                xmlSource1.Load(textBox2.Text);


                tChart1.Refresh();
            

    Importing XML from a URL.
    Example 
    [C#]
    protected override void button2_Click(object sender, System.EventArgs e)
                tChart1.Series.RemoveAllSeries();
                xmlSource1.Chart = tChart1.Chart;
                xmlSource1.Load("http://www.steema.com/SampleNETData.xml");

                tChart1.Refresh();
            

    Importing Ten format files
    Import a saved Tee file from a local file source or http data source.

    Example Imports
    Example 
    import from file
    [C#] 
    tChart1.Import.Template.LoadFileDialog();
    [VB.Net]
    TChart1.Import.Template.LoadFileDialog()

    or
    URL..
    [C#] 
    tChart1.Import.Template.FromURL("http://www.berneda.com/demo.ten");
    [VB.Net]
    TChart1.Import.Template.FromURL("http://www.berneda.com/demo.ten")

    or
    Stream..
    [C#] 
    System.IO.MemoryStream tenStream = new System.IO.MemoryStream();
    tChart1.Export.Template.Save(tenStream);
    tChart2.Import.Template.Load(tenStream);
    [VB.Net]
    Dim TenStream As New System.IO.MemoryStream()
    TChart1.Export.Template.Save(TenStream)
    TChart2.Import.Template.Load(TenStream)

    Effect of changes made in importing Chart
    As .ten files only save changes to the standard Chart definition, if the destination Chart includes property changes not  touched upon with the Chart to be imported, the originals will carry through. An example would be a Chart Footer. If the .ten file has not had the Footer set and the destination Chart does have, then the Footer will show through the Chart after  importation. That may be a desirable result. If you expect changes to be made to the importing Chart that could adversely affect the desired result after a Chart has been imported into it, then you can create a new instance of a Chart as in the following example:

    [C#] 
    private void Form1_Load(object sender, System.EventArgs e)
            Bar bar1 = new Bar(tChart1.Chart);
            bar1.FillSampleValues(20);

    private void button1_Click(object sender, System.EventArgs e)
            saveFileDialog1.Filter = "TenFile (.ten)|*.ten";
            saveFileDialog1.ShowDialog();
            if(saveFileDialog1.FileName != "")
                tChart1.Export.Template.IncludeData = true;
                tChart1.Export.Template.Save(saveFileDialog1.FileName);
            

    private void button2_Click(object sender, System.EventArgs e)
            tChart1.Dispose();
            tChart1 = new Steema.TeeChart.TChart();
            ini_Chart();

    private void ini_Chart()
            this.tChart1.Header.Lines = new string[] "TeeChart";
            this.tChart1.Location = new System.Drawing.Point(24, 24);
            this.tChart1.Name = "tChart1";
            this.tChart1.Size = new System.Drawing.Size(400, 240);
            this.tChart1.TabIndex = 5;
            this.Controls.AddRange(new System.Windows.Forms.Control[] this.tChart1);

    private void button3_Click(object sender, System.EventArgs e)
            tChart1.Import.Template.LoadFileDialog();


    [VB.Net]
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Bar1 As New Steema.TeeChart.Styles.Bar(TChart1.Chart)
            Bar1.FillSampleValues(20)
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SaveFileDialog1.Filter = "TenFile (.ten)|*.ten"
            SaveFileDialog1.ShowDialog()
            If Not SaveFileDialog1.FileName = "" Then
                TChart1.Export.Template.IncludeData = True
                TChart1.Export.Template.Save(SaveFileDialog1.FileName)
            End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            TChart1.Dispose()
            TChart1 = New Steema.TeeChart.TChart()
            Ini_Chart()
    End Sub
    Private Sub Ini_Chart()
            Me.TChart1.Header.Lines = New String() "TeeChart"
            Me.TChart1.Location = New System.Drawing.Point(16, 16)
            Me.TChart1.Name = "TChart1"
            Me.TChart1.Size = New System.Drawing.Size(400, 296)
            Me.TChart1.TabIndex = 3
            Me.Controls.AddRange(New System.Windows.Forms.Control() Me.TChart1)
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            TChart1.Import.Template.LoadFileDialog()
    End Sub

    Other datasources
    For information about data import from other database information sources please see the Database Access tutorial.