[CLOSED] Saving chart as image

  1. #1

    [CLOSED] Saving chart as image

    Hello

    In this example : https://examples4.ext.net/#/Chart/Misc/Download/ the chart legend is not saved. Is it possible to save the legend with the chart image? Because having a chart without is legend is meanigfull.

    It works in very old version of Ext, but since 2.x or 3.x version, I don't remember, saving legend doesn't work any more. I reported it years ago and it was said that it was a bug that will be solved in next Ext version, but apparently, it was not.
    Last edited by fabricio.murta; Jun 20, 2017 at 2:26 AM.
  2. #2
    Hello!

    Make the legend be drawn as a sprite to the chart and it will be going to your downloaded file.

    Add a line like this in your chart and it will switch to sprite-based legend:

    <LegendSpriteConfig runat="server" />
    I hope this helps!
    Fabrício Murta
    Developer & Support Expert
  3. #3
    Hi

    Could you fulfill an example base on the example thread please?

    Anyway, it will be tested soon as I will have charts to produce.
  4. #4

    Should be LegendSpriteConfig, not LegendConfig!

    Hello @feanor91!

    Hardly a problem!

    Here you go:

    <%@ Page Language="C#" %>
    
    <%@ Import Namespace="System.Drawing" %>
    <%@ Import Namespace="System.IO" %>
    
    <script runat="server">
        protected void ReloadData(object sender, DirectEventArgs e)
        {
            this.Chart1.GetStore().DataBind();
        }
    
        [DirectMethod]
        public void Download(string data)
        {
            data = data.Substring(data.IndexOf("base64,") + 7);
            byte[] bitmapData = Convert.FromBase64String(data);
    
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=chart.png");
            Response.ContentType = "image/png";
    
            using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(bitmapData)))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    ms.WriteTo(Response.OutputStream);
                }
            }
    
            Response.End();
        }
    </script>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <title>Export Chart - Ext.NET Examples</title>
        <link href="/resources/css/examples.css" rel="stylesheet" />
    
        <script>
            var saveChart = function (btn) {
                Ext.MessageBox.confirm('Confirm Save', 'Save Chart to an image?', function (choice) {
                    if (choice === 'yes') {
                        btn.up('panel').down('chart').download({
                            format: 'image/png'
                        });
                    }
                });
            };
    
            var exportChart = function (btn) {
                Ext.MessageBox.confirm('Confirm Export', 'Export Chart to an image?', function (choice) {
                    if (choice == 'yes') {
                        var data = btn.up('panel').down('chart').getImage().data;
    
                        App.direct.Download(data, { isUpload: true });
                    }
                });
            };
        </script>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
    
            <h1>Download Chart Sample</h1>
    
            <ext:Panel
                runat="server"
                Title="Area Chart"
                Width="800"
                Height="600"
                Layout="FitLayout">
                <TopBar>
                    <ext:Toolbar runat="server">
                        <Items>
                            <ext:ToolbarFill runat="server" />
    
                            <ext:Button
                                runat="server"
                                Text="Download via chart.download()"
                                Icon="Disk"
                                Handler="saveChart" />
    
                            <ext:Button
                                runat="server"
                                Text="Download via DirectMethod"
                                Icon="DiskDownload"
                                Handler="exportChart" />
    
                            <ext:Button
                                runat="server"
                                Text="Reload Data"
                                Icon="ArrowRefresh"
                                OnDirectClick="ReloadData" />
                        </Items>
                    </ext:Toolbar>
                </TopBar>
                <Items>
                    <ext:CartesianChart
                        ID="Chart1"
                        runat="server"
                        Legend="true"
                        InsetPadding="20">
                        <LegendSpriteConfig runat="server" />
                        <Background>
                            <Gradient>
                                <ext:LinearGradient GradientID="Gradient1" Degrees="90">
                                    <Stops>
                                        <ext:GradientStop Color="#feffff" Offset="0" />
                                        <ext:GradientStop Color="#d2ebf9" Offset="60" />
                                    </Stops>
                                </ext:LinearGradient>
                            </Gradient>
                        </Background>
                        <Store>
                            <ext:Store
                                runat="server"
                                Data="<%# Ext.Net.Examples.ChartData.GenerateData() %>"
                                AutoDataBind="true">
                                <Model>
                                    <ext:Model runat="server">
                                        <Fields>
                                            <ext:ModelField Name="Name" />
                                            <ext:ModelField Name="Data1" />
                                            <ext:ModelField Name="Data2" />
                                            <ext:ModelField Name="Data3" />
                                            <ext:ModelField Name="Data4" />
                                            <ext:ModelField Name="Data5" />
                                            <ext:ModelField Name="Data6" />
                                            <ext:ModelField Name="Data7" />
                                        </Fields>
                                    </ext:Model>
                                </Model>
                            </ext:Store>
                        </Store>
                        <Axes>
                            <ext:NumericAxis
                                Fields="Data1,Data2,Data3,Data4,Data5,Data6,Data7"
                                Title="Number of Hits"
                                Minimum="0" />
    
                            <ext:CategoryAxis
                                Position="Bottom"
                                Fields="Name"
                                Title="Month of the Year"
                                Grid="true">
                                <Label RotationDegrees="315" />
                            </ext:CategoryAxis>
                        </Axes>
                        <Series>
                            <ext:AreaSeries
                                XField="Name"
                                YField="Data1,Data2,Data3,Data4,Data5,Data6,Data7">
                            </ext:AreaSeries>
                        </Series>
                    </ext:CartesianChart>
                </Items>
            </ext:Panel>
        </form>
    </body>
    </html>
    Hope this helps!

    EDIT: ops! I mistyped the block as LegendConfig instead of LegendSpriteConfig. Fixed.
  5. #5
    Hi

    Yes, it's works fine. Thanks. Meanwhile, didn't refresh on chart data update (for example in a polar chart on country, then country names didn't changes) and how to docked left or rught? I tried Docked="right" but the legend simply disapear.
  6. #6
    Hello @feanor91!

    Thanks for the feedback, glad it worked! I believe for the remaining part of your response you already opened an inquiry at Refreshin chart legend, so we'll post you a reply about refreshing the chart there.
    Fabrício Murta
    Developer & Support Expert
  7. #7
    Yes you are right, I must have put the link to the new thread, my bad.

Similar Threads

  1. [CLOSED] Export Chart as Image
    By jchau in forum 2.x Legacy Premium Help
    Replies: 7
    Last Post: Feb 28, 2014, 5:14 AM
  2. [CLOSED] while saving image on server it throwing No Response error
    By PriceRightHTML5team in forum 2.x Legacy Premium Help
    Replies: 5
    Last Post: Jan 28, 2014, 4:37 AM
  3. [CLOSED] Using SVG library for saving chart images
    By cwolcott in forum 2.x Legacy Premium Help
    Replies: 12
    Last Post: Dec 01, 2013, 7:17 PM
  4. [CLOSED] Saving chart to svg
    By JCarlosF in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Jun 27, 2013, 1:29 AM
  5. [CLOSED] Saving image in byte format
    By AnulekhaK in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Mar 01, 2012, 12:37 PM

Posting Permissions