分享
 
 
 

Simulating a Bar Graph Using Simple HTML and a DataSet...

王朝html/css/js·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

You may have seen web pages that include a horizontal bar graph showing responses to a questionaire or other numeric results without the use of a graphing tool. This article will demonstrate how to build such a graph using html and a dataset.

By: John Kilgo

Date: July 28, 2003
Download the code.
Printer Friendly Version

It really is not difficult to create a horizontal bar graph using an HTML table. All you need to do is compute a percentage of a total for each item and then set <td width= to the value of the percentage. Use a background color for the percentage and no color for the difference between 100 and value's percentage and you have your bar graph. In this example we will graph UnitsInStock from the Northwind Products table.

We will do all the computations in a code behind file (BarGraph.aspx.vb) which means we must also build our HTML in the code behind file. To accomodate this we include on a <div>...</div> in our .aspx file shown below. In our code behind file we will, at the end, set the InnerHtml property of the Div to produce all of the HTML for the .aspx page. The code for BarGraph.aspx is shown below without any further explanation.

<%@ Page Language="vb" AutoEventWireup="false" Src="BarGraph.aspx.vb" Inherits="DotNetJohn.BarGraph"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>

<head>

<title>BarGraph</title>

<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">

<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">

<meta name=vs_defaultClientScript content="JavaScript">

<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">

</head>

<body MS_POSITIONING="GridLayout">

<form id="Form1" method="post" runat="server">

</form>

<div id="ShowTable" runat="server">Our table appears here</div>

</body>

</html>

To make our graph a little more interesting we will color code the bars. We will (arbitrarily) set 125 as the maximum UnitsInStock for any product and base our percentage on it. In other words, if there 25 units in stock of a certain product, the colored <td> will have a width of 20% (25 / 125) * 100. Any product with a percentage of less than 20% will be colored red, products with percentages between 20% and 80% will be blue, and products with a percentage over 80% will be in green.

The code behind file is not very long but we will display it in three sections for ease of discussion. This first section is just the declarations and the database access to obtain the data and create the DataSet.

Imports System.Data

Imports System.Data.SqlClient

Imports System.Configuration

Namespace DotNetJohn

Public Class BarGraph

Inherits System.Web.UI.Page

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim objConn As New SqlConnection(ConfigurationSettings.AppSettings("NorthwindConnection"))

Dim strSql As String = "Select ProductName, UnitsInStock From Products Order By ProductName"

Dim da As New SqlDataAdapter(strSql, objConn)

Dim ds As New DataSet()

da.Fill(ds, "Products")

This next section just establishes our html table and defines the table header.

Dim strHTML As String

strHTML = "<table width='80%' border='1'>"

strHTML &= "<tr>"

strHTML &= "<td>ProductName</td><td>Percentage of 125 | red <20% | blue 20-80% | green >80% |"

strHTML &= "</td><td>UnitsInStock</td>"

strHTML &= "</tr>"

This last section is where most of the work gets done to produce our bar graph. intValue is the percentage of the total (125) that gets plotted in color. intBlank is the right-most portion of the graph and is the difference between 100 and intValue. We then start the body of the table. The first <td> contains the product name. Within the the next <td> we have an embedded table containing intValue, color coded, and intBlank colorless. In that section of code we check the value of intValue. If it is less than 20 we color it red. If it is greater than 80 we color it green, otherwise we color it blue. We might want to do something like this to show that the red products need to be re-stocked and that the green items may be overstocked and we might want to hold a sale on these items. We then finish out the table with the actual UnitsInStock count (the actual count, not a percentage). Finally we set ShowTable's InnerHtml to the strHTML variable that we have built withing the code behind file. Remember that "ShowTable" was the ID we gave the <Div> tag on the .aspx page. We can do this because we included the runat="server" attribute in the <Div> tag on the .aspx page.

Dim dr As DataRow

Dim intValue, intBlank As Integer

For Each dr In ds.Tables("Products").Rows

intValue = 100 * (dr("UnitsInStock") / 125)

intBlank = 100 - intValue

strHTML &= "<tr><td width='30%'>" & dr("ProductName") & "</td>" & _

"<td width='60%'><table width='100%'><tr>"

If intValue < 20 Then

strHTML &= "<td width=" & intValue.ToString & "% bgcolor=red>"

ElseIF intValue > 80 Then

strHTML &= "<td width=" & intValue.ToString & "% bgcolor=green>"

Else

strHTML &= "<td width=" & intValue.ToString & "% bgcolor=blue>"

End If

strHTML &= " </td>" & _

"<td width=" & intBlank.ToString & "% </td>" & _

"</tr></table></td>" & _

"<td width=10%>" & dr("UnitsInStock").ToString & "</td></tr>"

Next

strHTML &= "</table>"

ShowTable.InnerHtml = strHTML

End Sub

End Class

End Namespace

I hope you have learned a new techinque you can put in you bag in case you need it in the future. You may also have learned something about cycling through a DataSet using the Rows collection. Best of luck.

You may run the program here.

You may download the code here.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有