如何完全从代码隐藏中制作动态的内存 ASP.NET 图表?

赫里曼编码器

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#代码完成。我也不能使用任何将图表图像写入磁盘的方法。

它是如何完成的?

VDWWD

我做了一个完整的例子,包括为其他用户创建图表。但基本上,您将图表图像保存到流而不是文件中。

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在ASP.NET图表控件中水平对齐图表区域

来自分类Dev

ASP.NET图表控件

来自分类Dev

如何手动定位ASP.Net图表图例?

来自分类Dev

如何更改Asp.net图表控件系列颜色?

来自分类Dev

ASP.NET 4.0图表控件中的缩放功能

来自分类Dev

ASP.NET MVC:显示模型中的多个图表

来自分类Dev

ASP.NET MVC中的Google API图表

来自分类Dev

ASP.NET MVC中的Google API图表

来自分类Dev

ASP.NET 4.0图表控件中的缩放功能

来自分类Dev

在ASP.NET MVC中填充Morris图表

来自分类Dev

ASP.NET MVC Razor 中的多个图表

来自分类Dev

ASP.net图表控件-XValueMembers计数

来自分类Dev

asp.net图表未知错误

来自分类Dev

HighCharts图表ASP.NET MVC渲染

来自分类Dev

asp.net图表未知错误

来自分类Dev

asp.Net MVC如何为图表中的每个条形设置颜色?

来自分类Dev

如何在asp.net图表中获得每月比较显示

来自分类Dev

asp.Net MVC如何为图表中的每个条形设置颜色?

来自分类Dev

使用 Chart.js 和动态数据加载图表 ASP.net MVC-Core

来自分类Dev

要从asp.net的html javascript图表中的数据库中获取数据

来自分类Dev

数据库列中的总计值并在图表 ASP.net 中显示

来自分类Dev

ASP.NET MVC图表:如何在每列上显示值

来自分类Dev

如何使用主题文件自定义ASP.NET图表

来自分类Dev

如何自定义ASP.NET图表数据绑定到SqlDataSource

来自分类Dev

数据绑定ASP.NET图表控件时如何设置标签值

来自分类Dev

ASP.NET MVC图表:如何在每列上显示值

来自分类Dev

如何在asp.net图表中将序列点显示为X轴标签

来自分类Dev

如何使用主题文件自定义ASP.NET图表

来自分类Dev

将图表绘制到ASP.NET MVC 4(Razor,C#)网站中

Related 相关文章

热门标签

归档