Search code examples
c#.netwinformschartsclone

How can I clone microsoft chart control?


What's the recommended way of cloning an object of Microsoft Chart Control? Because it is a third-party library, I can't use the solution mentioned here as I can't mark the object as serializable.

Preferably, I would not like to introduce any third party controls to clone the chart unless if it is absolutely impossible to do so without one.


Solution

  • You should not need to mark it as serializable as the charts already have the ability to be serialized. Check out this MS Charts Documentation for more information

    From here, you can serialize the chart into a string, then immediately deserialize the string into a new instance of the chart object. This would act similarly to cloning, and appears to be what the answer mentioned in your quesiton is doing. It is probably not the most efficient method for doing this, but it will work

    EDIT

    This code is unteseted but should work (Be fairly accurate for how to accomplish this)

    Chart chart1 = new Chart();
    //Enter your chart building code here
    System.IO.MemoryStream myStream = new System.IO.MemoryStream();
    Chart chart2 = new Chart();
    chart1.Serializer.Save(myStream);
    chart2.Serializer.Load(myStream);