Page 1 of 1

Serialize Custom Series Into .TEN File?

Posted: Fri Nov 04, 2005 7:30 pm
by 8129718
I'm trying to derive my own series from a TeeChart.NET series type (I want to be able to do this for various series types, actually) and then in my derived class, I add my own custom properties. I need the custom series to be serialized to and deserialized from a .TEN file.

This seemed to work ok in a simple test case (as long as I provided a default constructor and a constructor taking a Chart), but in my main project, I can't seem to get it to work. TeeChart.NET would crash when attempting to load my ".TEN" file with the custom series.

Can you give me an overview of what I must do to allow my derived series to be serialized and deserialized correctly?

Thanks,

Walt

Posted: Wed Nov 09, 2005 12:01 am
by 9637193
Hello Walt,

A custom class, deriving from a TeeChart Series, could be constructed in the following way:

Code: Select all

namespace MyTest
{
  public class MyLine : Line, Steema.TeeChart.Export.TemplateExport.ICustomSerialization
  {
    public double SomeFloat=12345.678;
    public string SomeString="Hello";

    public MyLine(Steema.TeeChart.Chart c) : base(c) {} 

    public void Serialize(SerializationInfo info)
    {
      info.AddValue("TestFloat",SomeFloat);

      object o=SomeString;
      info.AddValue("TestString",o,o.GetType());
    }

    public void DeSerialize(SerializationInfo info)
    {
      SomeFloat=info.GetDouble("TestFloat");
      SomeString=info.GetString("TestString");
    }
  }
}
You could then save the Chart to .ten in the following way:

Code: Select all

    private void button1_Click(object sender, System.EventArgs e)
    {
      MyTest.MyLine myLine1=new MyTest.MyLine(tChart1.Chart);

      myLine1.SomeFloat=456.123;
      myLine1.SomeString="Some Test";

      //here using a textfile datasource
      Steema.TeeChart.Data.TextSource mSrc=new Steema.TeeChart.Data.TextSource();

      mSrc.FileName=@"c:\datastore\testdata.txt";
      mSrc.HeaderLines=1;
      mSrc.Fields.Add(1,"YY");

      myLine1.DataSource=mSrc;

      // SAVE
      System.IO.MemoryStream memory=new System.IO.MemoryStream();
      tChart1.Export.Template.IncludeData=true;
      tChart1.Export.Template.Save(memory);
      tChart1.Export.Template.Save(@"c:\output\singleChart.ten");
}
and load the Chart in the following way:

Code: Select all

    private void button3_Click(object sender, System.EventArgs e)
    {
      tChart2.Import.Template.Load(@"c:\output\singleChart.ten");
    }
Regards,
Marc Meumann

Posted: Wed Nov 09, 2005 2:11 am
by 8129718
9637193 wrote:Hello Walt,

A custom class, deriving from a TeeChart Series, could be constructed in the following way...

Regards,
Marc Meumann
Thanks! That looks like it is exactly the info I was looking for. I will update my project tomorrow (to implement ICustomSerialization) and let you know.

FYI, the following is the exception I am getting with my current code (which doesn't implement ICustomSerialization):

Code: Select all

Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Steema.TeeChart.Styles.SeriesCollection.Clear(Boolean dispose)
   at Steema.TeeChart.Styles.SeriesCollection.Clear()
   at Steema.TeeChart.Chart.RemoveAllComponents()
   at Steema.TeeChart.Import.TemplateImport.Load(Stream stream)
   at Steema.TeeChart.Editors.ImportEditor.button1_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.RunDialog(Form form)
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at System.Windows.Forms.Form.ShowDialog()
   at Steema.TeeChart.Editors.ImportEditor.ShowModal(Chart c)
   at Steema.TeeChart.Import.Imports.ShowImportDialog()
   at CommonCodeLibrary.Test.Form1.button1_Click(Object sender, EventArgs e)
I guess that exception makes sense given that I didn't implement ICustomSerialization, but I'm posting it in case you want to catch the exception in your code and throw a better exception.

Walt

Posted: Wed Nov 09, 2005 9:09 pm
by 8129718
It appears that TeeChart already serializes public properties on my custom series without me needing to implement ICustomSerialization. When I tried implementing ICustomSerialization, I got an error for a duplicate name (although the name of my property is unique), and so it seemed like the property had already been serialized. I confirmed the property was already getting serialized by putting a breakpoint on my "get" accessor and it is called during TeeChart's own serialization process.

Thus, I am still stuck getting an exception, but the exception seems to have changed (not sure why).

Please tell me what could cause the following exception:

Code: Select all

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentNullException: Value cannot be null.
Parameter name: type
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Activator.CreateInstance(Type type, Object[] args)
   at Steema.TeeChart.Import.Imports.DeserializeFrom(SerializationInfo info, StreamingContext context)
   at Steema.TeeChart.Chart..ctor(SerializationInfo info, StreamingContext context)
   --- End of inner exception stack trace ---
   at System.Reflection.RuntimeConstructorInfo.SerializationInvoke(Object target, SerializationInfo info, StreamingContext context)
   at System.Runtime.Serialization.ObjectManager.CompleteISerializableObject(Object obj, SerializationInfo info, StreamingContext context)
   at System.Runtime.Serialization.ObjectManager.FixupSpecialObject(ObjectHolder holder)
   at System.Runtime.Serialization.ObjectManager.DoFixups()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
   at Steema.TeeChart.Import.TemplateImport.Load(Stream stream)
   at Steema.TeeChart.Editors.ImportEditor.button1_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.RunDialog(Form form)
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at System.Windows.Forms.Form.ShowDialog()
   at Steema.TeeChart.Editors.ImportEditor.ShowModal(Chart c)
   at Steema.TeeChart.Import.Imports.ShowImportDialog()
   at CommonCodeLibrary.Test.Form1.button1_Click(Object sender, EventArgs e)
Please note: The exception occurs during the import process (when importing a chart with a custom series). I don't get any error when exporting the chart.

Thanks for your help!

Posted: Thu Nov 10, 2005 11:20 am
by narcis
Hi Walt,

Could you please send us an example we can run "as-is" to reproduce the problem here? You can post your files at [url]news://www.steema.net/steema.public.attachments[/url] newsgroup.