manokethan
New Member
In my asp pages, I have some tables that create bar graphs based on scores from the database, so I would have something like this:<BR>...<BR><td><img src=http://aspmessageboard.com/archive/index.php/"bluedot.gif" width="<%=rs("Score")%>"></td><BR>...<BR><BR>Is this possible using the datagrid? I've been looking for examples but have yet to find any. Thanks for any help!Why not create a JPG of GIF of the entire graph on the fly from the datagrid? It is cool!!!!How do I do that?LOL... here is some sample code... you can't run it: uses our db and DataAccessLayer Object, but you can get the gist of it...<BR><BR><BR><%@ Page ContentType="image/jpeg" %><BR><%@ Import nameSpace="System" %><BR><%@ Import nameSpace="System.Data" %><BR><%@ Import nameSpace="System.Data.OleDB" %><BR><%@ Import nameSpace="System.IO" %><BR><%@ Import namespace="System.Drawing" %><BR><%@ Import nameSpace="System.Drawing.Imaging" %><BR><Script Runat="Server"><BR>Sub Page_Load<BR> Dim objBitmap As Bitmap<BR> Dim objGraphics As Graphics<BR> if session("chartUsers") = "" then exit sub<BR> Dim strUsers() as String = split(session("chartUsers"), ", ")<BR> Dim intusers as integer = ubound(strUsers)<BR> Dim bmpWidth as integer = (intUsers + 1) * 80 + 10<BR> if bmpWidth < 350 then bmpWidth = 350<BR> Dim bmpHeight as integer = 200<BR> Dim strConn as string = configurationSettings.appSettings("roleConn")<BR> Dim strSQL as string = "SELECT OWNER, COUNT(ID) * 4 AS RuleCount FROM fid_rad_rules WHERE OWNER IN (" & session("chartUsers") & ") GROUP BY OWNER" ' ORDER BY RuleCount<BR> Dim strXMl as string<BR> Dim intXincrement as integer = 10<BR> Dim intBrushAlt as integer = 0<BR> Dim drawFont As New Font("Tahoma", 8, FontStyle.Bold) 'FontStyle.Bold<BR> 'drawFont.Bold = true<BR> ' Create Bitmap<BR> objBitmap = New Bitmap(bmpWidth, bmpHeight)<BR> ' Initialize Graphics Class<BR> objGraphics = Graphics.FromImage( objBitmap )<BR> <BR> <BR> objGraphics.FillRectangle(Brushes.ghostwhite, 0, 0, bmpWidth, bmpHeight)<BR> Dim titleFont As New Font("Tahoma", 10, FontStyle.Bold) 'FontStyle.Bold<BR> objGraphics.DrawString("#Components Owned in RAD, by Selected User(s):", titleFont, Brushes.Black, 4, 4)<BR><BR><BR> dim objConn as new oleDbConnection(strConn)<BR> dim objCommand as new OleDbCommand(strSQL, objConn)<BR> dim objReader as oleDbDataReader<BR> objConn.open()<BR> objReader = objCommand.ExecuteReader()<BR> <BR> while objReader.Read()<BR> if intBrushAlt Mod 2 = 0 then<BR> objGraphics.FillRectangle(Brushes.LightBlue, intXIncrement, bmpHeight-objReader(1), 70, objReader(1))<BR> else<BR> objGraphics.FillRectangle(Brushes.SteelBlue, intXIncrement, bmpHeight-objReader(1), 70, objReader(1))<BR> end if<BR> objGraphics.DrawString(objReader(0) & " [" & ctype(objReader(1) / 4, string) & "]" , drawFont, Brushes.Black, intXIncrement, (bmpHeight-objReader(1)) - 20)<BR> intXincrement += 80<BR> intBrushAlt += 1<BR> end while<BR> <BR> objReader.Close()<BR> objConn.close()<BR> <BR> ' Display Bitmap<BR> objBitmap.Save(Response.OutputStream, ImageFormat.jpeg)<BR>End Sub<BR></Script><BR>you need to write a Web Form that has an asp:image control, who's source is set to the file that creates the image:<BR><BR><asp:Image ID="imgGraph" Runat="server" ImageUrl="barGraph.aspx" BorderColor="#336699" BorderWidth="2" /><BR>There's a lot of good stuff on creating charts on the fly in Sam's ASP.NET: Tips, Tutorials & Code and a lot of drawing object information in Visual Basic.Net Bible. Between the two, you can put together some nice looking charts that are created on the fly from your dataset information. Below is a sample of creating a bar chart on the fly. You can copy it, save it as BarChart.aspx and run it.<BR><BR>Good Luck,<BR>Tom T<BR><BR><%@ Page Debug=True %><BR><%@ Import Namespace="System.Drawing" %><BR><%@ Import Namespace="System.Drawing.Imaging" %><BR><BR><script language="VB" runat="server"><BR>' ================================================== ===<BR>' FOR CREATING A BAR CHART ON THE FLY.<BR>' FROM ASP.NET TIPS, TUTORIALS & CODE (SAMS) <BR>' ================================================== ===<BR><BR>Dim arrDataPoints as ArrayList = new ArrayList()<BR><BR>Dim strTitle As String = "Scoring History"<BR><BR>' ==== PAGE_LOAD FILLS ARRAYLIST. NORMALLY THIS WOULD COME <BR>' FROM YOUR DATA SOURCE.<BR><BR>Sub Page_Load(sender as Object, e as EventArgs)<BR><BR> ' == LOAD DATA INTO ARRAYLIST.<BR><BR> arrDataPoints.Add(240500)<BR> arrDataPoints.Add(220950)<BR> arrDataPoints.Add(283500)<BR> arrDataPoints.Add(340000)<BR> arrDataPoints.Add(325750)<BR> arrDataPoints.Add(222345)<BR> arrDataPoints.Add(322345)<BR> arrDataPoints.Add(422345)<BR> arrDataPoints.Add(322345)<BR><BR>End Sub<BR><BR><BR>' ==== USE ARRAYLIST DATA TO DRAW BAR CHART.<BR><BR>Sub DrawChart(obj as Object, e as EventArgs)<BR><BR> Dim objBrush as SolidBrush = new SolidBrush(Color.FromARGB(200,100,100))<BR><BR> Dim fontTitle as Font = new Font("Times", 32)<BR><BR> Const iColWidth as Integer = 60<BR> Const iColSpace as Integer = 10<BR> Const iMaxHeight as Integer = 700<BR> Const iHeightSpace as Integer = 150<BR> Const iTitleSpace as Integer = 50<BR><BR> Dim iMaxWidth as Integer = (iColWidth + iColSpace) * arrDataPoints.Count _<BR> + iColSpace + 800<BR> Dim iMaxColHeight as Integer = 0<BR> Dim iTotalHeight as Integer = iMaxHeight + iTitleSpace<BR> Dim objBitmap as Bitmap = new Bitmap(iMaxWidth, iTotalHeight)<BR> Dim objGraphics as Graphics = Graphics.FromImage(objBitmap)<BR><BR> objGraphics.FillRectangle(new SolidBrush(Color.Tan), 0, 0, iMaxWidth, iTotalHeight)<BR><BR> Dim iBarX as Integer = iColSpace<BR> Dim iCurrentHeight as Integer<BR> Dim i as Integer<BR> Dim DataPoint as Integer <BR><BR> ' == FIND MAXIMUM VALUE IN arrDataPoints.<BR><BR> For Each DataPoint in arrDataPoints<BR><BR> If DataPoint > iMaxColHeight then iMaxColHeight = DataPoint<BR><BR> Next<BR><BR> ' == LOOP THROUGH ARRAY AND DRAW EACH BAR.<BR><BR> For i = 0 to arrDataPoints.Count - 1<BR><BR> iCurrentHeight = (Convert.ToDouble(arrDataPoints(i)) / Convert.ToDouble(iMaxColHeight)) * Convert.ToDouble(iMaxHeight - iHeightSpace)<BR><BR> objGraphics.FillRectangle(objBrush, iBarX, iMaxHeight - iCurrentHeight, iColWidth, iCurrentHeight)<BR><BR> iBarX += (iColSpace + iColWidth)<BR><BR> Next<BR><BR> objGraphics.DrawString(strTitle, fontTitle, objBrush, _<BR> (iMaxWidth / 2) - strTitle.Length * 6, iMaxHeight)<BR><BR> Response.ContentType = "image/jpeg"<BR><BR> objBitmap.Save(Response.OutputStream, ImageFormat.JPEG)<BR> <BR> objGraphics.Dispose()<BR> objBitmap.Dispose()<BR><BR>End Sub<BR><BR></script><BR><BR><HTML><BODY><BR><BR> <FORM RunAt="Server"><BR><BR> <ASP:Button ID="btnSubmit" <BR> Text="Make Chart" <BR> Runat="Server" <BR> OnClick="DrawChart" /><BR> </FORM><BR><BR></BODY></HTML>