此次 表开发工具TeeChart Pro .NET使用教程将为大家带来如何自定义序列化。
接下来咱们接着上一篇未讲完的部分继续进行讲解!
保存和导入
一旦您在自定义类中填充了例程以序列化和反序列化您的自定义元素,图表保存和加载可能会以与未修改图表相同的方式调用,如前所示。
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:filescustomLine.ten");}
导入带有修改系列的图表
private void button2_Click(object sender, EventArgs e){ tChart1.Clear(); tChart1.Import.Template.Load(@"c:filescustomLine.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:filescustomChart.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:filescustomChart.ten")); myChart.Header.Text = myChart.MyObj.MyString; myChart.Footer.Text = myChart.MyStrProp; myChart.Footer.Visible = true; myChart.Export.Template.Save(@"c:filestestCustomChartmodded.ten"); myChart.Export.Image.PNG.Save(@"c:filestestCustomChart.png");}
如果您想了解TeeChart for .NET正版价格,欢迎咨询在线客服

标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!