zedgraph使用心得

来源:互联网 发布:linux查看网络配置命令 编辑:程序博客网 时间:2024/06/04 08:47

   最近在老板的要求下,要把本来的曲线图更改下。为了实现双轴的功能,我把本来的OWC更改用zedgraph。发现zedgraph是个比较不错的开源图表控件。

   

 

    我先间一个用户控件。AnalyticsGraph.ascx,页面代码如下:

  <%@ Control Language="C#" AutoEventWireup="true" CodeFile="AnalyticsGraph.ascx.cs" Inherits="ucccfei_zedgraph_AnalyticsGraph" %>
<%@ Register TagPrefix="zgw" Namespace="ZedGraph.Web" Assembly="ZedGraph.Web" %>
<zgw:ZedGraphWeb ID="zedGraphControl" runat="server" FontSpec-Size="100"    RenderMode="RawImage"/>

这里RenderMode我设置成了数据流格式,本来使用的文件格式,发现太占控件了,长期下去会生成太多的图片。

  代码如下:(这里我只粘帖一些主要代码,还有我只要生成曲线,所以柱形等图表的功能被我out了。)

protected void Page_Load(object sender, EventArgs e)
    {
               zedGraphControl.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(zedGraphControl_RenderGraph);
       
    }

在page_load里写上委托事件

 

 

   private void zedGraphControl_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, System.Drawing.Graphics g, ZedGraph.MasterPane pane)
    {
       
        InitProperty();

       
        GraphPane myPane = pane[0];

        myPane.Title.Text = Title;
        myPane.XAxis.Title.Text = XAxisTitle;
        myPane.YAxis.Title.Text = YAxisTitle;

        //if (true)
        //{
        //    DrawMessage(myPane, "yiafdhaskjhfasfksahfasdlhfaslf lasgfasglgsadi");
        //    pane.AxisChange(g);
        //    return;
        //}

        switch (Type)
        {
            case AnalyticsType.Line:

                DrawLine(myPane);
                break;
            case AnalyticsType.Bar:
                //  DrawBar(myPane);
                break;
            case AnalyticsType.Pie:
                //    DrawPie(myPane);
                break;
            default:
                break;
        }
        pane.AxisChange(g);

 

         Bitmap imgmap =myPane.GetImage();   
        // imgmap.Save(Context.Response.OutputStream, ImageFormat.Jpeg);
        byte[] imgbit = BmpToBytes_MemStream(imgmap);
        makeShuiYin(imgbit);
    }

 这里我给生成的数据流图片加上了水印。

 

  private void DrawLine(GraphPane graphPane)   //此方法是加水印的方法
    {

        graphPane.CurveList.Clear();

        if (Count != 0)
        {
            LineItem curve0 = graphPane.AddCurve(NameList[0], DataSource[0], Color.White, SymbolType.None);
            //graphPane.Title.
          //  curve0.Line.IsSmooth = true;
         //   curve0.Line.SmoothTension = 0.5F;

            curve0.Line.Width = 2.0F;
            curve0.Line.IsAntiAlias = true;
            curve0.IsVisible = false;
            for (int i = 1; i < Count; i++)
            {
                if (checkType(Count))//都是统一单位,则不需分左右轴
                {
                    LineItem curve = graphPane.AddCurve(NameList[i], DataSource[i], defaultColors[i - 1], SymbolType.None);
                    //graphPane.Title.
                    //curve.Line.IsSmooth = true;
                    //curve.Line.SmoothTension = 0.5F;

                    curve.Line.Width = 2.0F;
                    curve.Line.IsAntiAlias = true;

                }
                else  //有两种轴存在
                {
                    if (unitTypeList[i] == 0)
                    {
                        LineItem curve = graphPane.AddCurve(NameList[i], DataSource[i], defaultColors[i - 1], SymbolType.None);
                        //graphPane.Title.
                       // curve.Line.IsSmooth = true;
                       // curve.Line.SmoothTension = 0.5F;   //当要生成平滑的曲线时的参数设置

                        curve.Line.Width = 2.0F;
                        curve.Line.IsAntiAlias = true;
                    }
                    else
                    {
                        LineItem curve = graphPane.AddCurve(NameList[i], DataSource[i], defaultColors[i - 1], SymbolType.None);
                        //graphPane.Title.
                      //  curve.Line.IsSmooth = true;
                      //  curve.Line.SmoothTension = 0.5F;

                        curve.Line.Width = 2.0F;
                        curve.Line.IsAntiAlias = true;
                        curve.IsY2Axis = true;  //将此曲线对应到右边及另一个轴
                    }
                }
            }
        }

        graphPane.XAxis.Type = AxisType.Date;
        graphPane.XAxis.Title.FontSpec.Size = 16f;
        graphPane.Title.FontSpec.Size = 16;
        graphPane.Title.IsVisible = false;

        graphPane.LineType = LineType.Normal;

        // graphPane.Title.
        // graphPane.YAxis.Scale.MajorStepAuto = true;
        graphPane.YAxis.MajorGrid.IsVisible = true;
        graphPane.YAxis.Scale.IsUseTenPower = false;

        graphPane.YAxis.Title.IsOmitMag = false;
        graphPane.XAxis.Scale.Format = "yyyy-MM-dd";  //横坐标的日期格式化
        graphPane.YAxis.MajorTic.IsOpposite = false;
        graphPane.YAxis.MinorTic.IsOpposite = false;  

        graphPane.YAxis.Title.IsVisible = true;

        if (!checkType(Count))   //如果存在第二条轴,则将第二条轴放到右边,
        {
            graphPane.Y2Axis.IsVisible = true;
            graphPane.IsPenWidthScaled = false;
            graphPane.Y2Axis.MajorTic.IsOpposite = false;
            graphPane.Y2Axis.MinorTic.IsOpposite = false;
            graphPane.Y2Axis.Title.Text = "美元";
            graphPane.YAxis.Title.Text = "元";     
        }

        graphPane.IsIgnoreInitial = true;
        graphPane.IsIgnoreMissing = true;
        graphPane.YAxis.MajorGrid.IsZeroLine = false;
        graphPane.XAxis.Scale.Min = xminValue;
        graphPane.XAxis.Scale.Max = xmaxValue;

        graphPane.Legend.FontSpec.Size = 16;

        graphPane.YAxis.Scale.IsUseTenPower = true;
        graphPane.YAxis.Title.IsOmitMag = false;
        graphPane.YAxis.Title.FontSpec.IsBold = true;
              graphPane.XAxis.Scale.BaseTic = xminValue;
        graphPane.XAxis.Scale.IsSkipCrossLabel = false;
        graphPane.YAxis.Scale.Mag = 0;   //如果在纵坐标比较大的数据时,会默认显示成##×10^此放的格式,而我不要,所以我这里将这个参数设置为0;
      
    }

 

  public void makeShuiYin(byte[] b)
    {
        //System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(imgPath + fileName));
        System.IO.MemoryStream stream = new System.IO.MemoryStream(b, true);

        Bitmap bmPhoto = new Bitmap(stream);
        System.Drawing.Image image = (System.Drawing.Image)bmPhoto;
        Graphics g = null;
        if (IsPixelFormatIndexed(image.PixelFormat))
        {
            Bitmap bmp3 = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);
            g = Graphics.FromImage(bmp3);
           // g.Clear(Color.White);
            g.DrawImage(image, 0, 0, image.Width, image.Height);
            System.Drawing.Font f = new System.Drawing.Font("Arial", Convert.ToInt16(fontsize), (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            System.Drawing.Drawing2D.LinearGradientBrush b2 = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Gray, Color.GhostWhite, 9.0f, true);


            // Brush b2 = new SolidBrush(Color.FromName("LightSteelBlue"));//水印
            string addText = "www.ccfei.com";
            int intX = 10;
            int intY = 20;
            int i = 330;
            int j = 40;

            if ((image.Height - j) > 0) //源图比水印图高
                intY = (image.Height - j) / 2;
            else
                intY = image.Width;

            if ((image.Width - i) > 0) // 源图比水印图宽
                intX = (image.Width - i) / 2;
            else
                intX = image.Width;


            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.High;

            g.DrawString(addText, f, new SolidBrush(Color.FromArgb(30, 0, 0, 0)), intX, intY);


            bmp3.Save(Context.Response.OutputStream, ImageFormat.Jpeg);
        }
        else
        {
            g = Graphics.FromImage(bmPhoto);
            //g.Clear(Color.White);
            g.DrawImage(image, 0, 0, image.Width, image.Height);
            System.Drawing.Font f = new System.Drawing.Font("Arial Black", Convert.ToInt16(fontsize), System.Drawing.FontStyle.Italic, GraphicsUnit.Pixel);


            //    System.Drawing.Font f = new System.Drawing.Font("Arial", Convert.ToInt16("28"), (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            Brush b2 = new SolidBrush(Color.FromName("LightSlateGray"));//水印
            string addText = "文本";
            int intX = 10;
            int intY = 100;
            int i = 350;
            int j = 40;

            if ((image.Height - j) > 0) //源图比水印图高
                intY = (image.Height - j) / 2;
            else
                intY = image.Width;
            if ((image.Width - i) > 0) // 源图比水印图宽
                intX = (image.Width - i) / 2;
            else
                intX = image.Width;


            g.DrawString(addText, f, new SolidBrush(Color.FromArgb(30, 0, 0, 0)), intX, intY);
            bmPhoto.Save(Context.Response.OutputStream, ImageFormat.Jpeg);
        }
    }

贴出实例图。