翻譯|使用教程|編輯:凌霄漢|2022-04-13 16:01:31.837|閱讀 158 次
概述:此次報表開發工具TeeChart Pro .NET使用教程將為大家帶來如何自定義序列化。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
接下來咱們接著上一篇未講完的部分繼續進行講解!
一旦您在自定義類中填充了例程以序列化和反序列化您的自定義元素,圖表保存和加載可能會以與未修改圖表相同的方式調用,如前所示。
private void button1_Click(object sender, EventArgs e) { MyLine mLine=new MyLine(); mLine.MyStrProp = "now set"; mLine.MyIntProp = 43; mLine.setMyObj(43, "set val 43"); tChart1.Series.Add(mLine); mLine.FillSampleValues(); tChart1.Export.Template.Save(@"c:\files\customLine.ten"); }
導入帶有修改系列的圖表
private void button2_Click(object sender, EventArgs e) { tChart1.Clear(); tChart1.Import.Template.Load(@"c:\files\customLine.ten"); label1.Text = ((MyLine)(tChart1[0])).MyStrProp; label2.Text = ((MyLine)(tChart1[0])).MyIntProp.ToString(); label3.Text = ((MyLine)(tChart1[0])).MyObj.MyInt.ToString(); label4.Text = ((MyLine)(tChart1[0])).MyObj.MyString; }
Label1、Label2、Label3 和 Label4 將顯示保存圖表之前設置的值。
序列化自定義圖表所需的步驟與應用于自定義系列的步驟略有不同。
從 Steema.TeeChart.Chart 導出您的自定義圖表。 將其標記為 Serializable 并將其設置為 ICustomSerialization 類(參見下面的示例)。 已知類型將序列化,無需您執行任何必要步驟,如上面的系列示例所示。 對于未知類型的示例,我們將使用與 Series 示例中相同的對象 MyObj。
[System.Serializable()] public class MyChart : Steema.TeeChart.Chart, TemplateExport.ICustomSerialization { private MyObj myMyObj; public MyChart() : base() { } //required constructor protected MyChart(SerializationInfo info, StreamingContext context) : base(info, context) { } private string myStrVar = ""; private int myIntVar = 0; public string MyStrProp { get { return myStrVar; } set { myStrVar = value; } } public int MyIntProp { get { return myIntVar; } set { myIntVar = value; } } public void setMyObj(int i, string s) { myMyObj = new MyObj(i, s); } public MyObj MyObj { get { return myMyObj; } set { myMyObj = value; } } //required method public void Serialize(SerializationInfo info) { object o = myMyObj.MyString; info.AddValue("myObjStr", o, o.GetType()); info.AddValue("myObjInt", myMyObj.MyInt); } //required method public void DeSerialize(SerializationInfo info) { myMyObj = new MyObj(info.GetInt32("myObjInt"), info.GetString("myObjStr")); } }
保存圖表時會自動處理序列化。
{ MyChart myChart = new MyChart(); myChart.Series.Add(new Steema.TeeChart.Styles.Bar()); myChart[0].FillSampleValues(); myChart.setMyObj(22, "set 22"); myChart.MyStrProp = "set String Prop"; myChart.Export.Template.IncludeData = true; myChart.Export.Template.Save(@"c:\files\customChart.ten"); }
反序列化需要一個額外的步驟,告訴 TeeChart 的反序列化器將 MyChart 類型綁定到序列化的圖表,而不是默認類型的圖表。
請注意設置活頁夾的行:
myChart.Import.Template.CustomType = myChart.ToString();
私人無效按鈕2_Click(對象發送者,EventArgs e)
{ MyChart myChart = new MyChart(); myChart.Import.Template.CustomType = myChart.ToString(); myChart = (MyChart)(myChart.Import.Template.Load(@"c:\files\customChart.ten")); myChart.Header.Text = myChart.MyObj.MyString; myChart.Footer.Text = myChart.MyStrProp; myChart.Footer.Visible = true; myChart.Export.Template.Save(@"c:\files\testCustomChartmodded.ten"); myChart.Export.Image.PNG.Save(@"c:\files\testCustomChart.png"); }
如果您想了解TeeChart for .NET正版價格,歡迎咨詢
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn