FlexChart | ComponentOne
In This Topic
    Export
    In This Topic

    Exporting a chart refers to saving it in some other file format to enable the end-user to store it for later use. For instance, a chart saved as an image can be used in a presentation or any other application.

    Save as Image

    FlexChart provides the SaveImage method of FlexChartBase class that lets you save a chart as an image to the specified stream. This method takes four parameters, the stream, the image format, width and height of the image. The formats currently supported by the FlexChart control are .png, .jpeg, and .svg.

    Also, the FlexChartBase class also provides the SaveImage (int w, int h) method to save the chart as image to the clipboard, with the method taking two parameters, the image width and height.

    exporting charts

    private void btnSave_Click(object sender, EventArgs e)
    {
        var filter = "JPEG Image(*.jpg)|*.jpeg|PNG Image(*.png)|*.png|SVG Image(*.svg)|*.svg";
        SaveFileDialog sfd = new SaveFileDialog() { OverwritePrompt = true, Filter = filter };
        var format = ImageFormat.Jpg;
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            using (var fileStream = sfd.OpenFile())
            {
                var fmt = Path.GetExtension(sfd.FileName);
                switch (fmt)
                {
                    case ".png":
                        format = ImageFormat.Png;
                        break;
                    case ".svg":
                        format = ImageFormat.Svg;
                        break;
                }
                //Saves chart as image to the specified stream
                flexChart1.SaveImage(fileStream, format, flexChart1.Width, flexChart1.Height);                   
                //Saves chart as image to the clipboard
                flexChart1.SaveImage(flexChart1.Width, flexChart1.Height);                   
            }
        }
    }
    
    Private Sub btnSave_Click(sender As Object, e As EventArgs)
        Dim filter As String = "JPEG Image(*.jpg)|*.jpeg|PNG Image(*.png)|*.png|SVG Image(*.svg)|*.svg"
        Dim sfd As New SaveFileDialog() With {
             .OverwritePrompt = True,
             .Filter = filter
        }
        Dim format As ImageFormat = ImageFormat.Jpg
        If sfd.ShowDialog() = DialogResult.OK Then
            Using fileStream As Stream = sfd.OpenFile()
                Dim fmt As String = Path.GetExtension(sfd.FileName)
                Select Case fmt
                    Case ".png"
                        format = ImageFormat.Png
                        Exit Select
                    Case ".svg"
                        format = ImageFormat.Svg
                        Exit Select
                End Select
                ' Saves chart as image to the specified stream
                flexChart1.SaveImage(fileStream, format, flexChart1.Width, flexChart1.Height)
                ' Saves chart as image to the clipboard
                flexChart1.SaveImage(flexChart1.Width, flexChart1.Height)
            End Using
        End If
    End Sub
    

    Serialization

    Serialization refers to the conversion of chart object in to a sequence of bytes or a file, that can be stored and transmitted. This concept is generally used when data related to objects have to be transferred from one application to another to replicate the same in another application for further use.

    In FlexChart, you can serialize chart into various file formats using the C1.Win.Chart.Serialization assembly. You can obtain this assembly by building the product sample named C1.Win.Chart.Serialization and accessing obj\Debug folder inside the project. This assembly provides the Serializer class which provides methods to serialize a chart to the xml, json, binary and base64 formats. In this example, we are using SerializeChartToFile method that lets you serialize a chart to any of these formats. This method accepts three parameters, file name to which FlexChart object properties are to be stored, the FlexChart instance to be serialized and the file format to which FlexChart instance is to be saved. Similarly, you can use other methods such as SerializeChartToXml for serializing chart to a specific format such as XML in this case.

    Note: C1.Win.Chart.Serialization sample is located at \Documents\ComponentOne Samples\WinForms\v4.5.2\C1FlexChart\CS\FlexChartSerializer on your system, if you have installed the samples while installing WinForms Edition using ComponentOneC1ControlPanel.exe.

    private void btnExport_Click(object sender, EventArgs e)
    {
        var filter = "XML File (*.xml)|*.xml";
        var format = "xml";
        SaveFileDialog sfd = new SaveFileDialog() { OverwritePrompt = true, Filter = filter };
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            var fmt = Path.GetExtension(sfd.FileName);
            Serializer.SerializeChartToFile(sfd.FileName, flexChart1, format);
        }
    }
    
    Private Sub btnExport_Click(sender As Object, e As EventArgs)
        Dim filter As String = "XML File (*.xml)|*.xml"
        Dim format As String = "xml"
        Dim sfd As New SaveFileDialog() With {
             .OverwritePrompt = True,
             .Filter = filter
        }
        If sfd.ShowDialog() = DialogResult.OK Then
            Dim fmt As String = Path.GetExtension(sfd.FileName)
            Serializer.SerializeChartToFile(sfd.FileName, flexChart1, format)
        End If
    End Sub
    

    De-serialization

    De-serialization refers to the process of reading the state of object stored in a byte stream to import the original object. In FlexChart, just like serialization, you can de-serialize these settings saved in a particular file format to re-construct the chart by using various de-serialization methods exposed by the Serializer class of C1.Win.Chart.Serialization assembly. In this example, we are using DeserializeChartFromFile method which accepts three parameters and can re-create a chart from any file format. The three parameters are name of the file that contains the FlexChart object properties, FlexChart instance to be recovered and format of the file from which FlexChart instance has to be recovered.

    private void btnImport_Click(object sender, EventArgs e)
    {
        var filter = "XML File (*.xml)|*.xml";
        var format = "xml";
        OpenFileDialog ofd = new OpenFileDialog() { Filter = filter };
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            var fmt = Path.GetExtension(ofd.FileName);
            Serializer.DeserializeChartFromFile(ofd.FileName, flexChart1, format);
        }
    }
    
    Private Sub btnImport_Click(sender As Object, e As EventArgs)
        Dim filter As String = "XML File (*.xml)|*.xml"
        Dim format As String = "xml"
        Dim ofd As New OpenFileDialog() With {
             .Filter = filter
        }
        If ofd.ShowDialog() = DialogResult.OK Then
            Dim fmt As String = Path.GetExtension(ofd.FileName)
            Serializer.DeserializeChartFromFile(ofd.FileName, flexChart1, format)
        End If
    End Sub