.net学习总结(8)使用ZedGraph画图,不使用临时文件

来源:互联网 发布:网络推广部门组织架构 编辑:程序博客网 时间:2024/04/29 13:39

 刚接触到ZedGraph,到网上搜素到的方法基本上都是使用临时文件来存储图片,然后再显示,但是临时图片太多的话会占用大量的空间。很不划算。最后看到有人说把RenderMode="RawImage"就可以了 ,但是会出现乱码。如何解决呢?下面是我的方法。

新建一个目录,命名为bin,把文件ZedGraph.Web.dllZedGraph.dll拷到bin目录下面。

建立文件tuppian.aspx。其内容为:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="tuppian.aspx.cs" Inherits="tuppian" %>

<%@ Register assembly="ZedGraph.Web" namespace="ZedGraph.Web" tagprefix="cc1" %>

<%--

特别注意了:本页面不要有HTML代码,和asp.net代码。不然会出现乱码,RenderMode="RawImage"一定要设置RawImage,不然会报错。

--%>

<cc1:ZedGraphWeb ID="ZedGraphWeb1" runat="server" RenderMode="RawImage">

</cc1:ZedGraphWeb>

tuppian.aspx.cs为:

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Drawing;
  12. using ZedGraph;
  13. using ZedGraph.Web;
  14. public partial class tuppian : System.Web.UI.Page
  15. {
  16.     DarwGrapClass dg = new DarwGrapClass();
  17.     protected void Page_Load(object sender, EventArgs e)
  18.     {
  19.     }
  20.     protected override void OnInit(EventArgs e)
  21.     {
  22.         InitializeComponent();
  23.         base.OnInit(e);
  24.     }
  25.     private void InitializeComponent()
  26.     {
  27.         string id = Request.QueryString["id"];
  28.         switch (id)
  29.         {
  30.             case "1":
  31.                 DrawLine();
  32.                 break;
  33.             case "2":
  34.                 DrawPie();
  35.                 break;
  36.             default:
  37.                 DrawBar();
  38.                 break;
  39.         }
  40.     }
  41.     private void DrawBar()
  42.     {
  43.         dg.Type = AnalyticsType.Bar;
  44.         dg.Title = "用户访问柱状图";
  45.         dg.XAxisTitle = "月份";
  46.         dg.YAxisTitle = "用户访问数量";
  47.         Random rand = new Random();
  48.         string[] aa = { "企业1""企业2""企业3" };
  49.         for (int i = 0; i < 2; i++)
  50.         {
  51.             ZedGraph.PointPairList ppl = new ZedGraph.PointPairList();
  52.             for (int j = 0; j < 12; j++)
  53.             {
  54.                 double x = rand.Next(10);
  55.                 double y = rand.NextDouble() * 100;
  56.                 ppl.Add(x, y);
  57.                 //dg.NameList.Add((j + 1).ToString() + "月");
  58.                 //ppl.Add(j+1,j+1);以此递增
  59.                 //dg.NameList.Add("第" + j.ToString() + "月份");
  60.             }
  61.             dg.DataSource.Add(ppl);
  62.             dg.LabelList.Add("企业" + i.ToString());
  63.             //dg.NameList.Add((i + 1).ToString() + "月");
  64.         }
  65.         for (int k = 0; k < 12; k++)
  66.         {
  67.             dg.NameList.Add((k + 1).ToString() + "月");
  68.         }
  69.         dg.y_step = 5;
  70.         dg.DarwGrap(ZedGraphWeb1);
  71.     }
  72.     private void DrawPie()
  73.     {
  74.         dg.Type = AnalyticsType.Pie;
  75.         dg.Title = "用户访问饼图";
  76.         Random rand = new Random();
  77.         for (int i = 0; i < 3; i++)
  78.         {
  79.             dg.ScaleData.Add((i + 2) * rand.Next(100));
  80.             dg.NameList.Add("企业:" + i.ToString());//各个部分所代表的含义
  81.         }
  82.         dg.DarwGrap(ZedGraphWeb1);
  83.     }
  84.     private void DrawLine()
  85.     {
  86.         dg.Type = AnalyticsType.Line;
  87.         dg.Title = "用户访问曲线图";
  88.         dg.XAxisTitle = "月份";
  89.         dg.YAxisTitle = "用户访问数量";
  90.         Random rand = new Random();
  91.         for (int i = 0; i < 2; i++)
  92.         {
  93.             ZedGraph.PointPairList ppl = new ZedGraph.PointPairList();
  94.             //数据源添加
  95.             for (double x = 0; x < 12; x += 1.0)
  96.             {
  97.                 double y = rand.NextDouble() * 100;
  98.                 ppl.Add(x, y);
  99.             }
  100.             //从数据库中取得
  101.             //for (int i = 0; i < this.dt.Rows.Count; i++)  //这个循环主要是取到里面的说明文字,用了一个数组的方法
  102.             //{
  103.             //    ppl.Add(i,this.dt.Rows[i].Cells[1].Text.Trim());
  104.             //}
  105.             //dg.NameList.Add("第" + i.ToString() + "月份");
  106.             dg.DataSource.Add(ppl);
  107.             dg.NameList.Add("企业:" + i.ToString());
  108.         }
  109.         //改变x组的显示字符,当然也可以绑定数据库,从数据库中取得。
  110.         for (int k = 0; k < 12; k++)
  111.         {
  112.             dg.LabelList.Add((k + 1).ToString() + "月");
  113.         }
  114.         //for (int i = 0; i < this.dt.Rows.Count; i++)  //这个循环主要是取到里面的说明文字,用了一个数组的方法
  115.         //{
  116.         //    dg.LabelList.Add(this.dt.Rows[i].Cells[0].Text.Trim());
  117.         //}
  118.         dg.DarwGrap(ZedGraphWeb1);
  119. }
  120. }

新建一个类DarwGrapClass.cs,放在App_Code目录下面。其内容为:

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.HtmlControls;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Drawing;
  11. using ZedGraph;
  12. using ZedGraph.Web;
  13. using System.Collections.Generic;
  14. public enum AnalyticsType
  15. {
  16.     Line, //折线图
  17.     Line2,//带阴影区域的折线图
  18.     Curve,//带星的折线图
  19.     Curve2,//带阴影区域的星行折线图
  20.     Bar, //柱状图
  21.     Graph,
  22.     Pie //饼图
  23. };
  24. public class DarwGrapClass
  25. {
  26.     public DarwGrapClass()
  27.     {
  28.         //
  29.         //TODO: 在此处添加构造函数逻辑
  30.         //
  31.     }
  32.     #region Private Attribute
  33.     /**/
  34.     /// 
  35.     /// 默认颜色种类
  36.     /// 
  37.     private List<Color> defaultColors = new List<Color>();
  38.     /**/
  39.     /// 
  40.     /// 统计的个数
  41.     /// 
  42.     private int Count;
  43.     #endregion
  44.     //Public Property;
  45.     #region Public Property
  46.     /**/
  47.     /// 
  48.     /// 统计图的名称
  49.     /// 
  50.     public string Title;
  51.     /**/
  52.     /// 
  53.     /// 横轴的名称(饼图不需要)
  54.     /// 
  55.     public string XAxisTitle;
  56.     /**/
  57.     /// 
  58.     /// 纵轴的名称(饼图不需要)
  59.     /// 
  60.     public string YAxisTitle;
  61.     /**/
  62.     /// 
  63.     /// 显示的曲线类型:Line,Bar,Pie
  64.     /// 
  65.     public AnalyticsType Type;
  66.     /**/
  67.     /// 
  68.     /// 折线图和柱状图的数据源
  69.     /// 
  70.     public List<PointPairList> DataSource = new List<PointPairList>();
  71.     /**/
  72.     /// 
  73.     /// 饼图的数据源
  74.     /// 
  75.     public List<double> ScaleData = new List<double>();
  76.     /**/
  77.     /// 
  78.     /// 各段数据的颜色
  79.     /// 
  80.     public List<Color> Colors = new List<Color>();
  81.     /**/
  82.     /// 
  83.     /// 各段数据的名称
  84.     /// 
  85.     public List<string> NameList = new List<string>();
  86.     /**/
  87.     /// 
  88.     /// 用于柱状图,每个圆柱体表示的含义
  89.     /// 
  90.     public List<string> LabelList = new List<string>();
  91.     public double y_step;
  92.     public double x_step;
  93.     #endregion
  94.     public void DarwGrap(ZedGraphWeb ZedGraph)
  95.     {
  96.         ZedGraph.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(zedGraphControl_RenderGraph);
  97.     }
  98.     private void InitDefaultColors()
  99.     {
  100.         defaultColors.Add(Color.Red);
  101.         defaultColors.Add(Color.Green);
  102.         defaultColors.Add(Color.Blue);
  103.         defaultColors.Add(Color.Yellow);
  104.         defaultColors.Add(Color.YellowGreen);
  105.         defaultColors.Add(Color.Brown);
  106.         defaultColors.Add(Color.Aqua);
  107.         defaultColors.Add(Color.Cyan);
  108.         defaultColors.Add(Color.DarkSeaGreen);
  109.         defaultColors.Add(Color.Indigo);
  110.     }
  111.     /**/
  112.     /// 
  113.     /// 如果属性为空则初始化属性数据
  114.     /// 
  115.     private void InitProperty()
  116.     {
  117.         InitDefaultColors();
  118.         if (string.IsNullOrEmpty(Title))
  119.         {
  120.             Title = "未命名统计图";
  121.         }
  122.         if (string.IsNullOrEmpty(XAxisTitle))
  123.         {
  124.             XAxisTitle = "横轴";
  125.         }
  126.         if (string.IsNullOrEmpty(YAxisTitle))
  127.         {
  128.             YAxisTitle = "纵轴";
  129.         }
  130.         if (Type == AnalyticsType.Pie)
  131.         {
  132.             Count = ScaleData.Count;
  133.         }
  134.         else
  135.         {
  136.             Count = DataSource.Count;
  137.         }
  138.         if (Colors.Count == 0 || Colors.Count != Count)
  139.         {
  140.             Random r = new Random();
  141.             int tempIndex = 0;
  142.             List<int> tempIndexList = new List<int>();
  143.             for (int i = 0; i < Count; i++)
  144.             {
  145.                 tempIndex = r.Next(defaultColors.Count);
  146.                 if (tempIndexList.Contains(tempIndex))
  147.                 {
  148.                     i--;
  149.                 }
  150.                 else
  151.                 {
  152.                     tempIndexList.Add(tempIndex);
  153.                     Colors.Add(defaultColors[tempIndex]);
  154.                 }
  155.             }
  156.         }
  157.         if (NameList.Count == 0)
  158.         {
  159.             if (Type == AnalyticsType.Bar)
  160.             {
  161.                 for (int i = 1; i < DataSource[0].Count + 1; i++)
  162.                 {
  163.                     NameList.Add("第" + i.ToString() + "组");
  164.                 }
  165.             }
  166.             else
  167.             {
  168.                 for (int i = 1; i < Count + 1; i++)
  169.                 {
  170.                     NameList.Add("第" + i.ToString() + "组");
  171.                 }
  172.             }
  173.         }
  174.         if (LabelList.Count == 0)
  175.         {
  176.             for (int i = 0; i < Count; i++)
  177.             {
  178.                 LabelList.Add("含义" + i.ToString());
  179.             }
  180.         }
  181.         if (x_step == 0.0)
  182.             x_step = 5;
  183.         if (y_step == 0.0)
  184.             y_step = 5;
  185.     }
  186.     /**/
  187.     /// 
  188.     /// 画图
  189.     /// 
  190.     /// 
  191.     /// 
  192.     /// 
  193.     private void zedGraphControl_RenderGraph(ZedGraphWeb zgw, System.Drawing.Graphics g, ZedGraph.MasterPane masterPane)
  194.     {
  195.         InitProperty();
  196.         GraphPane myPane = masterPane[0];
  197.         myPane.Title.Text = Title;
  198.         myPane.XAxis.Title.Text = XAxisTitle;
  199.         myPane.YAxis.Title.Text = YAxisTitle;
  200.         //if (true)
  201.         //{
  202.         // DrawMessage(myPane, "yiafdhaskjhfasfksahfasdlhfaslf lasgfasglgsadi");
  203.         // pane.AxisChange(g);
  204.         // return;
  205.         //}
  206.         switch (Type)
  207.         {
  208.             case AnalyticsType.Line:
  209.                 DrawLine(myPane);
  210.                 break;
  211.             case AnalyticsType.Bar:
  212.                 DrawBar(myPane);
  213.                 break;
  214.             case AnalyticsType.Pie:
  215.                 DrawPie(myPane);
  216.                 break;
  217.             case AnalyticsType.Line2:
  218.                 DrawLine2(myPane);
  219.                 break;
  220.             case AnalyticsType.Curve:
  221.                 DrawCurve(myPane);
  222.                 break;
  223.             case AnalyticsType.Curve2:
  224.                 DrawCurve2(myPane);
  225.                 break;
  226.             default:
  227.                 break;
  228.         }
  229.         masterPane.AxisChange(g);
  230.     }
  231.     #region Draw
  232.     /**/
  233.     /// 
  234.     /// 画折线图
  235.     /// 
  236.     /// 
  237.     private void DrawLine(GraphPane graphPane)
  238.     {
  239.         for (int i = 0; i < Count; i++)
  240.         {
  241.             graphPane.AddCurve(NameList[i], DataSource[i], Colors[i], SymbolType.None);
  242.             string[] labels = LabelList.ToArray();
  243.             graphPane.XAxis.Scale.TextLabels = labels;
  244.             graphPane.XAxis.Type = AxisType.Text;
  245.             graphPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 166), 90F);
  246.             graphPane.Fill = new Fill(Color.FromArgb(250, 250, 255));
  247.             graphPane.YAxis.Scale.MajorStep = y_step;
  248.         }
  249.     }
  250.     /**/
  251.     /// 
  252.     /// 画折线图,带阴影区域
  253.     /// 
  254.     /// 
  255.     private void DrawLine2(GraphPane graphPane)
  256.     {
  257.         for (int i = 0; i < Count; i++)
  258.         {
  259.             graphPane.AddCurve(NameList[i], DataSource[i], Colors[i], SymbolType.None).Line.Fill = new Fill(Color.White, Colors[i], 90F);
  260.             string[] labels = LabelList.ToArray();
  261.             graphPane.XAxis.Scale.TextLabels = labels;
  262.             graphPane.XAxis.Type = AxisType.Text;
  263.             graphPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 166), 90F);
  264.             graphPane.Fill = new Fill(Color.FromArgb(250, 250, 255));
  265.             graphPane.YAxis.Scale.MajorStep = y_step;
  266.         }
  267.     }
  268.     /**/
  269.     /// 
  270.     /// 画星行折线图
  271.     /// 
  272.     /// 
  273.     private void DrawCurve(GraphPane graphPane)
  274.     {
  275.         for (int i = 0; i < Count; i++)
  276.         {
  277.             graphPane.AddCurve(NameList[i], DataSource[i], Colors[i], SymbolType.Star);
  278.             string[] labels = LabelList.ToArray();
  279.             graphPane.XAxis.Scale.TextLabels = labels;
  280.             graphPane.XAxis.Type = AxisType.Text;
  281.             graphPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 166), 90F);
  282.             graphPane.Fill = new Fill(Color.FromArgb(250, 250, 255));
  283.             graphPane.YAxis.Scale.MajorStep = y_step;
  284.         }
  285.     }
  286.     /**/
  287.     /// 
  288.     /// 画星行折线图,带阴影区域
  289.     /// 
  290.     /// 
  291.     private void DrawCurve2(GraphPane graphPane)
  292.     {
  293.         for (int i = 0; i < Count; i++)
  294.         {
  295.             graphPane.AddCurve(NameList[i], DataSource[i], Colors[i], SymbolType.Star).Line.Fill = new Fill(Color.White, Colors[i],90F);
  296.             string[] labels = LabelList.ToArray();
  297.             graphPane.XAxis.Scale.TextLabels = labels;
  298.             graphPane.XAxis.Type = AxisType.Text;
  299.             graphPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 166), 90F);
  300.             graphPane.Fill = new Fill(Color.FromArgb(250, 250, 255));
  301.             graphPane.YAxis.Scale.MajorStep = y_step;
  302.         }
  303.     }
  304.     /**/
  305.     /// 
  306.     /// 画柱状图
  307.     /// 
  308.     /// 
  309.     private void DrawBar(GraphPane graphPane)
  310.     {
  311.         for (int i = 0; i < Count; i++)
  312.         {
  313.             graphPane.AddBar(LabelList[i], DataSource[i], Colors[i]).Bar.Fill = new Fill(Colors[i], Color.White, Colors[i]);
  314.             //.Line.Fill = new Fill(Color.White, Color.Red, 45F);
  315.             //.Line.Fill = new Fill(Color.White, Color.Blue, 45F);
  316.         }
  317.         graphPane.XAxis.MajorTic.IsBetweenLabels = true;
  318.         string[] labels = NameList.ToArray();
  319.         graphPane.XAxis.Scale.TextLabels = labels;
  320.         graphPane.XAxis.Type = AxisType.Text;
  321.         graphPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 166), 90F);
  322.         //graphPane.Fill = new Fill(Color.FromArgb(250, 250, 255));
  323.         graphPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);
  324.         //graphPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);
  325.         graphPane.YAxis.Scale.MajorStep = y_step;
  326.         //graphPane.BaseDimension =8;
  327.     }
  328.     /**/
  329.     /// 
  330.     /// 画饼图
  331.     /// 
  332.     /// 
  333.     private void DrawPie(GraphPane graphPane)
  334.     {
  335.         graphPane.Fill = new Fill(Color.White, Color.Silver, 45.0f);
  336.         graphPane.Legend.Position = LegendPos.Float;
  337.         graphPane.Legend.Location = new Location(0.95f, 0.15f, CoordType.PaneFraction, AlignH.Right, AlignV.Top);
  338.         graphPane.Legend.FontSpec.Size = 20f;
  339.         graphPane.Legend.IsHStack = false;
  340.         for (int i = 0; i < Count; i++)
  341.         {
  342.             graphPane.AddPieSlice(ScaleData[i], Colors[i], Color.White, 45f, 0, NameList[i]);
  343.         }
  344.     }
  345.     /**/
  346.     /// 
  347.     /// 如果系统出错,显示错误信息
  348.     /// 
  349.     /// 
  350.     /// 
  351.     private void DrawMessage(GraphPane graphPane, string message)
  352.     {
  353.         TextObj text = new TextObj(message, 200, 200);
  354.         text.Text = message;
  355.         graphPane.GraphObjList.Add(text);
  356.     }
  357.     #endregion
  358. }

 

最后,注意当画饼图时,有时注释会把图片遮住,这时只要设置图片长和高的比例就可以了。曲线图和直方图的x組的说明文字如果太多的话,就会屏蔽掉一些,这是也只要设置长和高的比例就可以解决问题了。