我System.Web.UI.DataVisualization.Charting 4.0
在我的 ASP.NET 应用程序中使用。因此,它在某些环境中运行良好:
var chart = new Chart();
// Define the chart area
Grid grid = new Grid();
ChartArea chartArea = new ChartArea();
[... setting lots of chartArea properties here...]
ChartArea3DStyle areaStyle = new ChartArea3DStyle(chartArea);
chart.ChartAreas.Add(chartArea);
[... more ...]
[... build Series, Legends, etc here ...]
chart.SaveImage("c:\fakepath\myreport.png", ChartImageFormat.Png);
我省略了 95% 的用于构建图表的代码(非常复杂的逻辑,通过复杂的数据结构进行大量循环),然后将其保存到磁盘,并从 aspx 页面呈现如下:
<img src="http://fakeserver/fakepath/myreport.png">
在某些客户环境中,这种方法不起作用,因为 IIS 进程没有写入本地磁盘的权限,而且他们不想打开它。此外,当多个用户查看(和生成)图表时,它的可扩展性不是很好,每个用户的情况都不同。
我怎样才能纯粹在内存中生成这个图表?
我一直在看ChartHttpHandler类,但找不到相关的代码示例。理想的情况是,如果我可以完全如上所示构建图表,但不是保存到磁盘,而是可以将图表存储在内存中(缓存?会话?),这样我就可以将我的<img>
标签指向aspx page 到内存中的数据,图像将在页面上呈现。我不想求助于声明式aspx图表构建,因为逻辑太复杂,需要全部用c#代码完成。我也不能使用任何将图表图像写入磁盘的方法。
它是如何完成的?
我做了一个完整的例子,包括为其他用户创建图表。但基本上,您将图表图像保存到流而不是文件中。
using (MemoryStream stream = new MemoryStream())
{
chart.SaveImage(stream, ChartImageFormat.Png);
}
我创建了一个通用处理程序来在浏览器中显示图像。但是,一旦图像在流中,您就可以做更多的事情,例如将其放入 PDF 或电子邮件消息中。
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;
namespace YourNameSpace
{
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//needed to generate random numbers
Random rnd = new Random();
//select 12 random numbers between 1 and 50 for column chart
int[] yRange1 = Enumerable.Range(1, 50).OrderBy(i => rnd.Next()).Take(12).ToArray();
//select 12 random numbers between 1 and 25 for line chart
int[] yRange2 = Enumerable.Range(0, 25).OrderBy(i => rnd.Next()).Take(12).ToArray();
//select all the month names for the labels
string[] xLabels = Enumerable.Range(1, 12).Select(i => DateTimeFormatInfo.CurrentInfo.GetMonthName(i)).ToArray();
//create the chart
Chart chart = new Chart();
//add the bar chart series
Series series = new Series("ChartBar");
series.ChartType = SeriesChartType.Column;
chart.Series.Add(series);
//add the line chart series
Series series2 = new Series("ChartLine");
series2.ChartType = SeriesChartType.Line;
series2.Color = System.Drawing.Color.Purple;
series2.BorderWidth = 2;
chart.Series.Add(series2);
//define the chart area
ChartArea chartArea = new ChartArea();
Axis yAxis = new Axis(chartArea, AxisName.Y);
Axis xAxis = new Axis(chartArea, AxisName.X);
//add the data and define color
chart.Series["ChartBar"].Points.DataBindXY(xLabels, yRange1);
chart.Series["ChartLine"].Points.DataBindXY(xLabels, yRange2);
chart.Series["ChartBar"].Color = System.Drawing.Color.Green;
chart.ChartAreas.Add(chartArea);
//set the dimensions of the chart
chart.Width = new Unit(600, UnitType.Pixel);
chart.Height = new Unit(400, UnitType.Pixel);
//create an empty byte array
byte[] bin = new byte[0];
//save the chart to the stream instead of a file
using (MemoryStream stream = new MemoryStream())
{
chart.SaveImage(stream, ChartImageFormat.Png);
//write the stream to a byte array
bin = stream.ToArray();
}
//send the result to the browser
context.Response.ContentType = "image/png";
context.Response.AddHeader("content-length", bin.Length.ToString());
context.Response.AddHeader("content-disposition", "attachment; filename=\"chart.png\"");
context.Response.OutputStream.Write(bin, 0, bin.Length);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
要在浏览器中显示图像,只需src
将 img 元素的 指向处理程序。
<img src="Handler1.ashx" width="600" height="400" />
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句