DevExpress WebChartControl在运行时创建

时间:2014-08-05 03:44:02

标签: c# devexpress webchartcontrol

我有以下代码在运行时创建一个简单的条形图,然后将其作为图像导出到用户计算机。

    [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;
    }

}

}

由于某种原因,当我运行此代码时,返回的图形为空白。我能够查看图像并且它具有我想要的所有属性(即高度,宽度等),但由于某种原因,没有轴或条或其他数据。如果有人能指出我做错了什么,我们将不胜感激。

由于

1 个答案:

答案 0 :(得分:0)

我设法在下面的链接中找到我的问题的解决方案。问题是我应该只使用常规图表控件而不是web图表控件。

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