【TeeChart .NET教程】(十二)導出和導入圖表
【下載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 數(shù)據(jù)格式
- Text: TextFormat Class
- XML: XMLFormat Class
- HTML: HTMLFormat Class
- Excel: ExcelFormat Class
1.1.3 其他格式
TEN格式是一種靈活的格式,可存儲圖表屬性信息和(可選)圖表數(shù)據(jù),文件很小(取決于數(shù)據(jù)),非常適合網(wǎng)絡用于更新基于實時客戶端的圖表。
- TEN (TeeChart): TemplateExport Classs
1.2 示例導出
導出到文件是合理的,在大多數(shù)情況下,只需要定義目標文件名。
[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文件導出具有顏色和大小的附加參數(shù)。例:
[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文件導出現(xiàn)在具有SaveToFile和SaveToStream的速度和質(zhì)量參數(shù),例
[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質(zhì)量值(從1到100)將使文件更大,網(wǎng)絡傳輸就慢,但質(zhì)量更好!您需要根據(jù)自己的應用決定最佳平衡。通過將“Enhanced”設置為True簡單地創(chuàng)建
2.3 WMF & EMF
通過將“Enhanced”設置為True,可以簡單地創(chuàng)建增強型圖元文件,例:
[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格式導出位圖。 到目前為止,尚不清楚在其應用程序中實現(xiàn)這些類的開發(fā)人員是否需要從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格式的許多優(yōu)點,但也提供了超出GIF格式的功能,PNG改進了GIF逐步顯示圖像的能力。 也就是說,當圖像通過網(wǎng)絡連接到達時,顯示更好和更好的圖像近似值,例
[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現(xiàn)在可以導出為Adobe Flex腳本文件(* .mxml),然后可以通過Flex的免費編譯器(mxmlc.exe)進行編譯,以創(chuàng)建* .swf(Flash)文件。
(三)導出數(shù)據(jù)
系列數(shù)據(jù)有四種導出格式:
- Text: TextFormat Class
- XML: XMLFormat Class
- HTML: HTMLFormat Class
- Excel: ExcelFormat Class
上述組件可以與圖表系列相關聯(lián),從中可以將數(shù)據(jù)導出為文件,Stream或剪貼板。以下示例將圖表系列中的數(shù)據(jù)導出到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
帶有隨機數(shù)據(jù)集的上述輸出如下:
<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'模板和數(shù)據(jù)導出/導入格式
Ten files
Ten files是TeeChart自己的模板格式,用于保存圖表及其數(shù)據(jù)。 修改的圖表屬性隨模板一起保存,并在模板導入新圖表時重現(xiàn),優(yōu)點:
- 大小非常小,在大多數(shù)情況下,它提供了優(yōu)于純圖形格式(qicker)的優(yōu)勢。
- 模板的目標圖表是“live實時”,可以進行縮放和滾動,并修改其屬性。
- 根據(jù)用戶的喜好,數(shù)據(jù)可以選擇包含在十個模板中。
例:
[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數(shù)據(jù)源導入已保存的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文件時所做更改的效果僅保存對標準圖表定義的更改,如果目標圖表包含未導入要導入的圖表的屬性更改,則原件將繼續(xù)執(zhí)行。一個例子是圖表頁腳。如果.ten文件未設置頁腳并且目標圖表具有,則頁腳將在導入后通過圖表顯示。如果希望在導入圖表后對導入圖表進行更改可能會對所需結果產(chǎn)生負面影響,那么可以創(chuàng)建圖表的新實例,如下例所示:
[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 其他數(shù)據(jù)源
有關從其他數(shù)據(jù)庫信息源導入數(shù)據(jù)的信息,請參閱數(shù)據(jù)庫訪問教程。