ASP.NET drawing full Raiders (on)

xiaoxiao2021-03-06  49

http://www.aspcool.com/lanmu/browse1.asp?ID=817&bbsuser=aspnet Author: bben_h www.ASPCool.com Time: 2002-1-13 16:04:29 Views: 12213

This document is based on Beta2 development, more and more web applications requires a chart to perform data display and analysis. For example: voting results show that the company's production statistical display analysis and so on. With the chart to display data, it is intuitive, clear and other advantages. Traditional ASP technology is not supported, so you have to use Active X or Java Applets to implement this feature. Newly appeared ASP.NET solves this problem, as long as the class displayed in ASP.NET can draw a rich, dynamic chart (Figure 1). This article will tell how to use ASP.NET technology to combine ADO.NET technology to draw bar charts and pie charts. Figure 1 first creates a C # class library. Open vs.net, create a new class library project called Insight_CS.WebCharts, change the name of the solution to Insight, change the Class.cs file name to Insight_CS.WebCharts.cs, and finally open the Insight_CS.WebCharts.cs file.

The code is as follows: / * Custom class, by entering different parameters, these classes can draw different graphics * / using system; using system.io; // Used for file access using system.data; // Used for data Access using system.drawing; // Provide basic functions for painting GDI graphs Using system.drawing.text; // Provide advanced features for painting GDI graphs Using system.drawing.drawing2d; // Provide high-grade two-dimensional, vector graphics function Using System.Drawing.Imaging; // providing advanced graphics drawing function of GDI namespace Insight_cs.WebCharts {public class PieChart {public PieChart () {} public void Render (string title, string subTitle, int width, int height, DataSet chartData, Stream Target) {const INT SIDE_LENGTH = 400; const INT PIE_DIAMETER = 200; DATATABLE DT = chartdata.tables [0]; // Take the total base amount float sumdata = 0 in the pie chart by entering the parameters; Foreach (DataRow Dr In DT. Rows) {Sumdata = Convert.TOSINGLE (DR [1]);} // Generate an Image object, and thus generate a Graphics object Bitmap BM = new bitmap (width, height); graphics g = graphics.FromImage (BM ); // Set the attribute G.Scaletransform (Convert. Tosingle (Height)) / side_length) / sIDE_LENGTH); g.smoothingMode = SMO OtHingMode.default; g.TextRenderingHint = textrenderingHint.antialias; // Canvas and edge setting g.clear (color.White); g.drawRectangle (Pens.black, 0, 0, sIDE_LENGTH-1, SIDE_LENGTH-1); // Painted pie chart title G. DrawString (Title, New Font ("Tahoma", 24), Brushes.Black, New Pointf (5, 5)); // Painting Chart of the legend g.drawstring (Subtitle, New Font ("Tahoma", 14), Brushes.black, New Pointf (7,35)); // Painting Chart Float Curangle = 0; Float Totalangle = 0; for (INT I = 0; i

g.FillPie (new SolidBrush (ChartUtil.GetChartItemColor (i)), 100,65, PIE_DIAMETER, PIE_DIAMETER, totalAngle, curAngle); g.DrawPie (Pens.Black, 100,65, PIE_DIAMETER, PIE_DIAMETER, totalAngle, curAngle); totalAngle = Curangle;} // Painting Maxel box and its text G.drawRectangle (Pens.Black, 200, 300, 199, 99); G.drawString ("Legend", New Font ("Taoma", 12, FontStyle.Bold), brushes. Black, New Pointf (200, 300)); // Painting Graphics Pointf Boxorigin = New Pointf (210, 330); Pointf TextORIGIN = New PointF (235, 326); float percent = 0; for (int i = 0; i

Foreach (DATAROINT

, 12, fontstyle.bold, brushes.black, new pointf (200, 300)); // Draw legend Pointf Boxorigin = New Pointf (210, 330); Pointf TextRigin = New PointF (235, 326); for (INT i = 0; i < Dt.Rows.count; i ) {g.fillRectangle (New SolidBrush (New SolidBrush (CHARTUTIL.GETCHARTEMCOLOR (I)), Boxorigin.x, Boxorigin.y, 20, 10); g.drawRectangle (Pens.Black, Boxorigin.x, Boxorigin .Y, 20, 10); g.drawstring (Dt.Rows [i] [0] .tostring () "-" dt.rows [i] [1] .tostring (), New Font ("tahoma" , 10), brushes.black, textorigin; Boxorigin.y = 15; TextORIGIN.Y = 15;} // Output graphic bm.save (target, imageformat.gif); // Resource recycling bm.dispose () ; g.Dispose ();}} public class ChartUtil {public ChartUtil () {} public static Color GetChartItemColor (int itemIndex) {Color selectedColor; switch (itemIndex) {case 0: selectedColor = Color.Blue; break; case 1: SELECTEDCOLOR = Color.red; Break; Case 2: SelectedColor = Color.Yellow; Break; Case 3: SelectedColor = Color .Purple; break;}}} code analysis: 1. Introducing some namespace using system; usingspace using system; usingspace using system; using system.io; // is introduced. // Used for data access Using system.drawing; // provides basic functions for drawing GDI graphs Using system.drawing.text; // Provides high-grade feature for painting GDI graphs Using system.drawing.drawing2d; // Provide high-grade two-dimensional , Vector graphics function Using system.drawing.imaging; // Advanced feature for drawing GDI graphics These Namespace will be applied later. 2. Customize a namespace for INSIGHT_CS.WEBCHARTS, including two classes of PieChart and Barchart, very clear, Class Piechart is built for painting charts, Class Barchart is built for drawing bar. Because Class Piechart and Class Barchar are almost, so we take the pie chart as an example for code analysis.

转载请注明原文地址:https://www.9cbs.com/read-110175.html

New Post(0)