1

I have the following code to create a simple bar graph at run-time and then exports it as an image to the users computer.

    [HttpGet]
    public HttpResponseMessage Get([FromBody]ScoreByLocation sblx)
    {
        sbl.ImageWidth = 400;
        sbl.ImageHeight = 300;

        HttpResponseMessage msg = null;

        try
        {
            WebChartControl sideBySideBarChart = new WebChartControl();
            sideBySideBarChart.Width = sbl.ImageWidth;
            sideBySideBarChart.Height = sbl.ImageHeight;

            DevExpress.XtraCharts.Series series1 = new DevExpress.XtraCharts.Series("Side-by-Side Bar Series 1", ViewType.Bar);

            sideBySideBarChart.Series.Add(series1);

            DataTable dt = new DataTable();
            DataColumn dc;

            dc = new DataColumn();
            dc.ColumnName = "Name";
            dt.Columns.Add(dc);
            dc = new DataColumn();
            dc.ColumnName = "Age";
            dt.Columns.Add(dc);

            (other data creation here..)

            sideBySideBarChart.DataSource = dt;

            series1.ArgumentScaleType = ScaleType.Numerical;
            series1.ArgumentDataMember = "Argument";
            series1.ValueScaleType = ScaleType.Numerical;
            series1.ValueDataMembers.AddRange(new string[] { "Value" });

            sideBySideBarChart.DataBind();

            //save to memory stream and then to byte array
            byte[] b = null;

            using (MemoryStream ms = new MemoryStream())
            {
                ((IChartContainer)sideBySideBarChart).Chart.ExportToImage(ms, System.Drawing.Imaging.ImageFormat.Png);
                b = ms.ToArray();
            }
            msg = Request.CreateResponse(HttpStatusCode.OK);
            msg.Content = new ByteArrayContent(b);
            msg.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/png");
            msg.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
            msg.Content.Headers.ContentDisposition.FileName = "report.png";
        }
        catch (Exception ex)
        {
            msg = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message.ToString());
        }
        return msg;
    }

}

}

For some reason when I run this code, the graph that is returned is blank. I am able to view the image and it has all the properties I want (i.e height, width etc.) but for some reason there are no axis or bars or other data for that matter. If someone could point out what I have done wrong it would be appreciated.

Thanks

Nathan
  • 95
  • 6
  • 17

1 Answers1

0

I managed to locate the solution to my problem on the link below. The problem was that I should be using just regular chart control not the web chart control.

https://www.devexpress.com/Support/Center/Question/Details/T136901

Nathan
  • 95
  • 6
  • 17