logo TeeChart .NET教程2018 我也要发布文档

(十二)导出和导入图表


【下载TeeChart.Net最新版本】

(一)导出图表

1.1 可用格式

可用导出格式,可以将所有格式复制到文件或剪贴板或Stream。

1.1.1 图像格式
  • BMP: BitmapFormat Class
  • JPEG: JPEGFormat Class
  • WMF & EMF: MetafileFormat Class
  • GIF: GIFFormat Class
  • PNG: PNGFormat Class
  • TIFF: TIFFFormat Class
1.1.2 数据格式
  • Text: TextFormat Class
  • XML: XMLFormat Class
  • HTML: HTMLFormat Class
  • Excel: ExcelFormat Class
1.1.3 其他格式

TEN格式是一种灵活的格式,可存储图表属性信息和(可选)图表数据,文件很小(取决于数据),非常适合网络用于更新基于实时客户端的图表。

  • TEN (TeeChart): TemplateExport Classs

1.2 示例导出

导出到文件是合理的,在大多数情况下,只需要定义目标文件名。

[C#.Net]

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

(二) 导出图像

2.1 BMP

BMP文件导出具有颜色和大小的附加参数。例:

[C#.Net]

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)

2.2 JPEG

JPEG文件导出现在具有SaveToFile和SaveToStream的速度和质量参数,例

[C#.Net]

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)

Quality value质量值(从1到100)将使文件更大,网络传输就慢,但质量更好!您需要根据自己的应用决定最佳平衡。通过将“Enhanced”设置为True简单地创建

2.3 WMF & EMF

通过将“Enhanced”设置为True,可以简单地创建增强型图元文件,例:

[C#.Net]

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)

2.4 GIF

由于Microsoft的.Net Framework类库,特别是System.Drawing.Imaging命名空间中包含的类,TeeChart for .Net能够支持以GIF格式导出位图。 到目前为止,尚不清楚在其应用程序中实现这些类的开发人员是否需要从Unisys获得GIF专利许可。

[C#.Net]

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)

2.5 PNG

PNG格式保留了GIF格式的许多优点,但也提供了超出GIF格式的功能,PNG改进了GIF逐步显示图像的能力。 也就是说,当图像通过网络连接到达时,显示更好和更好的图像近似值,例

[C#.Net]

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)

2.6 TIFF

TIFF是一种灵活且可扩展的格式,受到各种平台和图像处理应用程序的支持,例

[C#.Net]

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)

2.7 Adobe Flex/Flash/SWF

TeeChart现在可以导出为Adobe Flex脚本文件(* .mxml),然后可以通过Flex的免费编译器(mxmlc.exe)进行编译,以创建* .swf(Flash)文件。

(三)导出数据

系列数据有四种导出格式:

  • Text: TextFormat Class
  • XML: XMLFormat Class
  • HTML: HTMLFormat Class
  • Excel: ExcelFormat Class

上述组件可以与图表系列相关联,从中可以将数据导出为文件,Stream或剪贴板。以下示例将图表系列中的数据导出到HTML表:

[C#.Net]

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

带有随机数据集的上述输出如下:

<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的'Ten'模板和数据导出/导入格式

Ten files

Ten files是TeeChart自己的模板格式,用于保存图表及其数据。 修改的图表属性随模板一起保存,并在模板导入新图表时重现,优点:

  • 大小非常小,在大多数情况下,它提供了优于纯图形格式(qicker)的优势。
  • 模板的目标图表是“live实时”,可以进行缩放和滚动,并修改其属性。
  • 根据用户的喜好,数据可以选择包含在十个模板中。

例:

[C#.Net]

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

(五)导入

5.1 将XML导入TeeChart

5.1.1 从TextBox导入XML

[C#.Net]

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(); 
5.1.2 从URL导入XML

[C#.Net]

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();

5.2 导入Ten格式文件

从本地文件源或http数据源导入已保存的Ten文件。

5.3 导入示例

5.3.1 从文件导入

[C#.Net]

tChart1.Import.Template.LoadFileDialog();

[VB.Net]

TChart1.Import.Template.LoadFileDialog()
5.3.2 URl

[C#.Net]

tChart1.Import.Template.FromURL("http://www.berneda.com/demo.ten"); 

[VB.Net]

TChart1.Import.Template.FromURL("http://www.berneda.com/demo.ten")
5.3.3 Stream

[C#.Net]

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)

5.4 导入图表所做更改的效果

As.ten文件时所做更改的效果仅保存对标准图表定义的更改,如果目标图表包含未导入要导入的图表的属性更改,则原件将继续执行。一个例子是图表页脚。如果.ten文件未设置页脚并且目标图表具有,则页脚将在导入后通过图表显示。如果希望在导入图表后对导入图表进行更改可能会对所需结果产生负面影响,那么可以创建图表的新实例,如下例所示:

[C#.Net]

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

5.5 其他数据源

有关从其他数据库信息源导入数据的信息,请参阅数据库访问教程