asp.net chart Statistics class

Source: Internet
Author: User
Tags throw exception
Asp.net| Statistics | charts

Previous time project needs, wrote a simple icon statistics class. Decide to write something to blog later.


Using System;
Using System.Drawing;
Using System.Drawing.Imaging;
Using System.Drawing.Drawing2D;

Namespace Crystalreportsdemo
... {
/**////<summary>
CreateReport draw a graphical report-column chart.
</summary>
public class CreateReport
... {

Public CreateReport ()
... {
//
TODO: Add constructor logic here
//
}

        public Bitmap createpicturereport (string[] x,int[] y,string ReportName )
        ... {
            if (x.length!= y.length)
             ... {
                throw new Exception ("Inconsistent number of x and y numbers");
           }
            int colnum=y.length;
            int colscale=880/(colnum+1);


Bitmap bmp = new Bitmap (900,700,PIXELFORMAT.FORMAT24BPPRGB);
Graphics DC = graphics.fromimage (BMP);
dc. FillRectangle (New SolidBrush (Color.White), 0,0,900,700);

Pen Scalepen = new Pen (color.black,2);
Pen Gridpen = new Pen (color.black,1);

dc. DrawLine (scalepen,20,680,880,680);
dc. DrawLine (scalepen,20,680,20,20);

dc. DrawLine (gridpen,20,20,880,20);
dc. DrawLine (gridpen,880,20,880,680);

Point Drawpoint;
int Xpoint;
int yheight;
if (colnum==0)
... {
throw new Exception ("No data value, Colnum value is 0 as divisor, throw exception");

}
int drawwidth=880/(4*colnum);
int drawwidth=580/(4*colnum+1);//4*colnum+1 ensure Colnum is not 0. Otherwise 580/(4*0); 0 cannot be a divisor
int Maxnum=getmaxnum (y);
Rectangle RCT;
Random rnd = new Random ();
int red,green,blue;

for (int i=20;i<680;i=i+96)
... {
dc. DrawLine (Gridpen,20,i,880,i);
}

for (int i=0;i<colnum;i++)
... {
Red=rnd. Next (0,255);
Green=rnd. Next (0,255);
Blue=rnd. Next (0,255);
SolidBrush brush=new SolidBrush (Color.FromArgb (Red,green,blue));
xpoint=20+ ((i+1) *colscale)-drawwidth;
Yheight=y[i]*650/maxnum;
Drawpoint=new Point (Xpoint,680-yheight);
Rct=new Rectangle (drawpoint,new Size (drawwidth*2,yheight));
dc. FillRectangle (BRUSH,RCT);
dc. DrawRectangle (GRIDPEN,RCT);

Draw x values
dc. DrawString (x[i],new Font ("Arial", 9), New SolidBrush (color.orangered), New PointF ((float) (XPOINT+DRAWWIDTH/2), 680- YHEIGHT/2));
Draw Y values
dc. DrawString (Y[i]. ToString (), New Font ("Arial", 9), New SolidBrush (Color.Black), New PointF ((float) (Xpoint + drawwidth/2), 680-yheight-15) ;
}


dc. DrawString (reportname,new Font ("XXFarEastFont-Arial"), brushes.black,3,3);

return BMP;

           }
        Private int getmaxnum (int[] arr)
         ... {
            int max=arr[0];
            for (int i=1;i<arr. length;i++)
            ... {
                if (arr[i]> max)
                ... {
                     Max=arr[i];
               }

}
return Max;

}

}
}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.