我正在尝试在运行时创建图表控件并将其保存到文件中。我没有必要在页面上显示图表,我纯粹是想创建图表并将其保存到文件系统。
到目前为止我的代码是:
DataTable dt = new DataTable();
dt.Columns.Add("Col", typeof(String));
dt.Columns.Add("Score", typeof(int));
DataRow dr = dt.NewRow();
dr["Col"] = "Col1";
dr["Score"] = 10;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Col"] = "Col2";
dr["Score"] = 5;
dt.Rows.Add(dr);
Chart chrt = new Chart();
ChartArea chrtArea = new ChartArea("ChartArea1");
chrtArea.Area3DStyle.Enable3D = true;
chrtArea.Area3DStyle.WallWidth = 0;
chrt.ChartAreas.Add(chrtArea);
Series series = new Series("Series1");
series.ChartArea = "ChartArea1";
series.ChartType = SeriesChartType.Bar;
series.XValueMember = "Col";
series.YValueMembers = "Score";
chrt.DataSource = dt;
chrt.DataBind();
//Using MemoryStream to bypass permissions issue that I can investigate at another stage
using (MemoryStream imgStream = new MemoryStream())
{
chrt.SaveImage(imgStream, ChartImageFormat.Gif);
using (FileStream file = new FileStream(@"PATH_TO_FOLDER\img.gif", FileMode.Create, FileAccess.Write))
{
imgStream.WriteTo(file);
}
}
这是因为图表实际上没有渲染,还是我做了一些非常愚蠢的事情?
答案 0 :(得分:0)
我无法相信,只要我提出这个问题,我就发现我做错了什么!
我添加了以下内容:)
chrt.Series.Add(series);
我没有将series
添加到图表中!
答案 1 :(得分:0)
以下是在您的文件系统中保存图表图像的代码段。
Chart1.SaveImage(Server.MapPath("Folder Name to save image") + "\\" + "Sample143.jpeg");