Смекни!
smekni.com

ASP.NET пример построения круговой диаграммы

ASP.NET: пример построения круговой диаграммы

Христофоров Юрий

Ниже будет показано, как можно в ASP.NET приложении, используя графические методы GDI+, нарисовать круговую диаграмму. Это может быть полезно, например, для наглядного представления данных.

Файл pie.aspx (написан на VB.NET):

<%@ Page Language="VB" ContentType="image/jpeg" %> <%@ Import Namespace="System.Drawing" %> <%@ Import Namespace="System.Drawing.Imaging" %> <%@ Import Namespace="System.Drawing.Drawing2D" %> <% Response.Clear() Dim height As Integer = 200 Dim width As Integer = 320 Dim x As Integer Dim i As Integer Dim start_angle As Integer Dim pie_size As Integer Dim sub_total As Integer Dim offset As Integer Dim diameter As Integer Dim arrData() As Integer = {75, 45, 19, 10, 55} ' значенияDim arrProcent(4) As Integer Dim total As Integer Dim arrColor() As Object = {Color.Salmon, Color.SeaGreen, Color.Gold, Color.Maroon, Color.Orchid} Dim arrTitle() As String = {"Пункт1", "Пункт2", "Пункт3", "Пункт4", "Пункт5"} Dim rect As Object sub_total = 0 start_angle = 0 offset = 20 diameter = 170 total = 0 Dim bmp As New Bitmap(width, height, PixelFormat.Format32bppArgb) Dim g as Graphics = Graphics.FromImage(bmp) Dim fnt As New Font("Arial", 8) Dim sb As New SolidBrush(Color.Blue) g.Clear(Color.White) g.SmoothingMode = SmoothingMode.HighQuality ' суммазначенийвмассивеFor i = 0 To arrData.GetUpperBound(0) total = total + arrData(i) Next ' процентыFor i = 0 To arrData.GetUpperBound(0) arrProcent(i) = Math.Round((arrData(i)/total)*100) Next For i = 0 To arrProcent.GetUpperBound(0) sub_total = sub_total + arrProcent(i) pie_size = sub_total*360 / 100 - start_angle g.FillPie(New SolidBrush(arrColor(i)), offset, offset, diameter, diameter, start_angle, pie_size) start_angle = start_angle + pie_size rect = New Rectangle(offset + diameter + 10, offset + i*20, 15, 15) g.FillRectangle(New SolidBrush(arrColor(i)), rect) g.DrawString(arrTitle(i) & " - [" & arrProcent(i) & "%]", fnt, sb, offset + diameter + 10 + 20, offset + i*20) Next bmp.Save(Response.OutputStream, ImageFormat.Jpeg) g.Dispose() bmp.Dispose() Response.End() %>

Результат работы скрипта: