我用下面的代码在运行时创建一个简单的条形图,然后将其作为图像导出到用户的计算机。
[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;
}
}
}
由于某种原因,当我运行这段代码时,返回的图形是空白的。我能够查看图像,它具有我想要的所有属性(即高度,宽度等),但由于某种原因,没有轴或条或其他数据。如果有人能指出我做错了什么,我将不胜感激。
谢谢
我在下面的链接上找到了我的问题的解决方案。问题是,我应该使用只是常规的图表控件,而不是网络图表控件。
https://www.devexpress.com/Support/Center/Question/Details/T136901