PROWAREtech

articles » archived » asp-net » bar-graph-control

ASP.NET: Bar Graph Control

A custom server control example (.NET Framework).

Example custom ASP.NET server control -- this can be used as a learning tool for creating custom server controls. Copy this code into a source file named C:\Bargraph.vb (or Bargraph.cs). Make sure it is located in the ASP.NET server's C:\ directory or if not wanting to compile this source, dowload the compiled library here: BARGRAPH.zip

See the example at the bottom of this page.

//Bargraph.cs
using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;

[assembly: TagPrefix("prowaretech.Bargraph.Controls", "abc")]
namespace prowaretech.Bargraph.Controls
{

	[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), DefaultProperty("Text"), ToolboxData("<{0}:BargraphVertical ID=\"BargraphID\" ForeColor=\"Black\" MaxBarHeight=\"100\" " + "BarWidth=\"50\" runat=\"server\"></{0}:BargraphVertical>")]
	public class BargraphVertical : Control
	{

		private class Bar
		{
			public double Value;
			public string Text;
			public string Color;
			public string Format;
			public Bar NextBar;
			public Bar(double Value, string Text, string Color, string Format)
			{
				this.Value = Value;
				this.Text = Text;
				this.Color = Color;
				this.Format = Format;
			}
		}

		private Bar _bar;
		private Bar _last;
		private double _max;
		private HtmlTable _table;
		private string _forecolor;
		private string _backcolor;
		private string _headerforecolor;
		private string _headerbackcolor;
		private string _border;
		private int _barheight = 100;
		private int _barwidth = 50;

		private string _text;
		[Bindable(true), Category("Appearance"), Description("The bargraph header text."), Localizable(true)]
		public string Text
		{
			get { return _text; }
			set { _text = value; }
		}

		[Browsable(true), Category("Appearance"), Description("The bargraph border.")]
		public string Border
		{
			get { return _border; }
			set { _border = value; }
		}

		[Browsable(true), Category("Appearance"), Description("The bargraph header back color.")]
		public string HeaderBackColor
		{
			get { return _headerbackcolor; }
			set { _headerbackcolor = value; }
		}

		[Browsable(true), Category("Appearance"), Description("The bargraph header fore color.")]
		public string HeaderForeColor
		{
			get { return _headerforecolor; }
			set { _headerforecolor = value; }
		}

		[Browsable(true), Category("Appearance"), Description("The bargraph text color.")]
		public string ForeColor
		{
			get { return _forecolor; }
			set { _forecolor = value; }
		}

		[Browsable(true), Category("Appearance"), Description("The bargraph back color.")]
		public string BackColor
		{
			get { return _backcolor; }
			set { _backcolor = value; }
		}

		[Browsable(true), Category("Appearance"), DefaultValue(100), Description("The maximum possible bar height.")]
		public int MaxBarHeight
		{
			get { return _barheight; }
			set { _barheight = value; }
		}

		[Browsable(true), Category("Appearance"), DefaultValue(50), Description("The width of each bar.")]
		public int BarWidth
		{
			get { return _barwidth; }
			set { _barwidth = value; }
		}

		public void Add(double Value, string Text, string Color, string Format)
		{
			if (_bar == null)
			{
				_bar = new Bar(Value, Text, Color, Format);
				_last = _bar;
				_max = Value;
			}
			else
			{
				_last.NextBar = new Bar(Value, Text, Color, Format);
				_last = _last.NextBar;
				if (Value > _max)
				{
					_max = Value;
				}
			}
		}

		protected override void CreateChildControls()
		{
			base.CreateChildControls();
			double FACTOR = (_max == 0 ? 0 : (_barheight / _max));
			HtmlTableRow row = default(HtmlTableRow);
			HtmlTableCell cell = default(HtmlTableCell);
			_table = new HtmlTable();
			base.Controls.Add(_table);
			_table.CellPadding = 0;
			_table.CellSpacing = 1;
			if ((_backcolor != null))
				_table.Style.Add("background-color", _backcolor);
			if ((_border != null))
				_table.Style.Add("border", _border);
			row = new HtmlTableRow();
			_table.Rows.Add(row);
			cell = new HtmlTableCell();
			row.Cells.Add(cell);
			if ((_text != null))
			{
				cell.Style.Add("text-align", "center");
				cell.Style.Add("color", _headerforecolor);
				cell.Style.Add("background-color", _headerbackcolor);
				cell.InnerHtml = _text;
			}
			row = new HtmlTableRow();
			_table.Rows.Add(row);
			cell = new HtmlTableCell();
			row.Cells.Add(cell);
			_table = new HtmlTable();
			cell.Controls.Add(_table);
			_table.CellPadding = 0;
			_table.CellSpacing = 1;
			row = new HtmlTableRow();
			_table.Rows.Add(row);
			Bar nextb = _bar;
			while ((nextb != null))
			{
				cell = new HtmlTableCell();
				cell.Style.Add("vertical-align", "bottom");
				HtmlTable tbl = new HtmlTable();
				HtmlTableRow tr = default(HtmlTableRow);
				HtmlTableCell td = default(HtmlTableCell);
				tbl.CellPadding = 0;
				tbl.CellSpacing = 0;

				tr = new HtmlTableRow();
				tbl.Rows.Add(tr);
				td = new HtmlTableCell();
				td.Style.Add("text-align", "center");
				td.Style.Add("color", _forecolor);
				td.InnerHtml = string.Format(nextb.Format, nextb.Value);
				tr.Cells.Add(td);

				tr = new HtmlTableRow();
				tbl.Rows.Add(tr);
				td = new HtmlTableCell();
				tr.Cells.Add(td);
				HtmlTable tbl2 = new HtmlTable();
				tbl2.CellPadding = 0;
				tbl2.CellSpacing = 0;
				tbl2.Style.Add("height", Math.Round(nextb.Value * FACTOR) + "px");
				tbl2.Style.Add("width", _barwidth + "px");
				tbl2.Style.Add("border", "solid 1px " + nextb.Color);
				tbl2.Style.Add("background-color", nextb.Color);
				tr = new HtmlTableRow();
				tr.Cells.Add(new HtmlTableCell());
				tbl2.Rows.Add(tr);
				td.Controls.Add(tbl2);

				tr = new HtmlTableRow();
				tbl.Rows.Add(tr);
				td = new HtmlTableCell();
				tr.Cells.Add(td);
				td.Style.Add("text-align", "center");
				td.Style.Add("color", _forecolor);
				td.InnerHtml = nextb.Text;

				cell.Controls.Add(tbl);
				row.Cells.Add(cell);
				nextb = nextb.NextBar;
			}
		}
	}

	[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), DefaultProperty("Text"), ToolboxData("<{0}:BargraphHorizontal ID=\"BargraphID\" ForeColor=\"Black\" BarHeight=\"25\" " + "MaxBarWidth=\"100\" runat=\"server\"></{0}:BargraphHorizontal>")]
	public class BargraphHorizontal : Control
	{

		private class Bar
		{
			public double Value;
			public string Text;
			public string Color;
			public string Format;
			public Bar NextBar;
			public Bar(double Value, string Text, string Color, string Format)
			{
				this.Value = Value;
				this.Text = Text;
				this.Color = Color;
				this.Format = Format;
			}
		}

		private Bar _bar;
		private Bar _last;
		private double _max;
		private HtmlTable _table;
		private string _forecolor;
		private string _backcolor;
		private string _headerforecolor;
		private string _headerbackcolor;
		private string _border;
		private int _barheight = 25;
		private int _barwidth = 100;

		private string _text;
		[Bindable(true), Category("Appearance"), Description("The bargraph header."), Localizable(true)]
		public string Text
		{
			get { return _text; }
			set { _text = value; }
		}

		[Browsable(true), Category("Appearance"), Description("The bargraph border.")]
		public string Border
		{
			get { return _border; }
			set { _border = value; }
		}

		[Browsable(true), Category("Appearance"), Description("The bargraph header back color.")]
		public string HeaderBackColor
		{
			get { return _headerbackcolor; }
			set { _headerbackcolor = value; }
		}

		[Browsable(true), Category("Appearance"), Description("The bargraph header fore color.")]
		public string HeaderForeColor
		{
			get { return _headerforecolor; }
			set { _headerforecolor = value; }
		}

		[Browsable(true), Category("Appearance"), Description("The bargraph text color.")]
		public string ForeColor
		{
			get { return _forecolor; }
			set { _forecolor = value; }
		}

		[Browsable(true), Category("Appearance"), Description("The bargraph back color.")]
		public string BackColor
		{
			get { return _backcolor; }
			set { _backcolor = value; }
		}

		[Browsable(true), Category("Appearance"), DefaultValue(25), Description("The height of each bar.")]
		public int BarHeight
		{
			get { return _barheight; }
			set { _barheight = value; }
		}

		[Browsable(true), Category("Appearance"), DefaultValue(100), Description("The maximum possible bar width.")]
		public int MaxBarWidth
		{
			get { return _barwidth; }
			set { _barwidth = value; }
		}

		public void Add(double Value, string Text, string Color, string Format)
		{
			if (_bar == null)
			{
				_bar = new Bar(Value, Text, Color, Format);
				_last = _bar;
				_max = Value;
			}
			else
			{
				_last.NextBar = new Bar(Value, Text, Color, Format);
				_last = _last.NextBar;
				if (Value > _max)
				{
					_max = Value;
				}
			}
		}

		protected override void CreateChildControls()
		{
			base.CreateChildControls();
			double FACTOR = (_max == 0 ? 0 : (_barwidth / _max));
			HtmlTableRow row = default(HtmlTableRow);
			HtmlTableCell cell = default(HtmlTableCell);
			_table = new HtmlTable();
			base.Controls.Add(_table);
			_table.CellPadding = 0;
			_table.CellSpacing = 1;
			if ((_backcolor != null))
				_table.Style.Add("background-color", _backcolor);
			if ((_border != null))
				_table.Style.Add("border", _border);
			row = new HtmlTableRow();
			_table.Rows.Add(row);
			cell = new HtmlTableCell();
			row.Cells.Add(cell);
			if ((_text != null))
			{
				cell.Style.Add("text-align", "center");
				cell.Style.Add("color", _headerforecolor);
				cell.Style.Add("background-color", _headerbackcolor);
				cell.InnerHtml = _text;
			}
			row = new HtmlTableRow();
			_table.Rows.Add(row);
			cell = new HtmlTableCell();
			row.Cells.Add(cell);
			_table = new HtmlTable();
			cell.Controls.Add(_table);
			_table.CellPadding = 0;
			_table.CellSpacing = 1;
			Bar nextb = _bar;
			while ((nextb != null))
			{
				HtmlTable tbl = default(HtmlTable);
				HtmlTableRow tr = default(HtmlTableRow);
				HtmlTableCell td = default(HtmlTableCell);

				row = new HtmlTableRow();
				_table.Rows.Add(row);
				cell = new HtmlTableCell();
				row.Cells.Add(cell);
				cell.Style.Add("text-align", "right");
				cell.Style.Add("vertical-align", "middle");
				cell.Style.Add("white-space", "nowrap");
				cell.Style.Add("color", _forecolor);
				cell.InnerHtml = nextb.Text;

				cell = new HtmlTableCell();
				row.Cells.Add(cell);
				cell.Style.Add("text-align", "left");
				cell.Style.Add("vertical-align", "middle");
				tbl = new HtmlTable();
				cell.Controls.Add(tbl);
				tbl.CellPadding = 0;
				tbl.CellSpacing = 0;
				row = new HtmlTableRow();
				tbl.Rows.Add(row);
				td = new HtmlTableCell();
				row.Cells.Add(td);
				td.Style.Add("text-align", "left");
				td.Style.Add("vertical-align", "middle");
				HtmlTable tbl2 = new HtmlTable();
				td.Controls.Add(tbl2);
				tbl2.CellPadding = 0;
				tbl2.CellSpacing = 0;
				tbl2.Style.Add("width", Math.Round(nextb.Value * FACTOR) + "px");
				tbl2.Style.Add("height", _barheight + "px");
				tbl2.Style.Add("border", "solid 1px " + nextb.Color);
				tbl2.Style.Add("background-color", nextb.Color);
				tr = new HtmlTableRow();
				tbl2.Rows.Add(tr);
				tr.Cells.Add(new HtmlTableCell());

				td = new HtmlTableCell();
				row.Cells.Add(td);
				td.Style.Add("text-align", "left");
				td.Style.Add("vertical-align", "middle");
				td.Style.Add("white-space", "nowrap");
				td.Style.Add("color", _forecolor);
				td.InnerHtml = "&nbsp;" + string.Format(nextb.Format, nextb.Value);

				nextb = nextb.NextBar;
			}
		}
	}
}
'Bargraph.vb
Imports System
Imports System.ComponentModel
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.HtmlControls

<Assembly: TagPrefix("prowaretech.Bargraph.Controls", "abc")> 
Namespace prowaretech.Bargraph.Controls

	< _
	AspNetHostingPermission(SecurityAction.Demand, _
		Level:=AspNetHostingPermissionLevel.Minimal), _
	AspNetHostingPermission(SecurityAction.InheritanceDemand, _
		Level:=AspNetHostingPermissionLevel.Minimal), _
	DefaultProperty("Text"), _
	ToolboxData("<{0}:BargraphVertical ID=""BargraphID"" ForeColor=""Black"" MaxBarHeight=""100"" " & _
		"BarWidth=""50"" runat=""server""></{0}:BargraphVertical>") _
	> _
	Public Class BargraphVertical
		Inherits Control

		Private Class Bar
			Public Value As Double
			Public Text As String
			Public Color As String
			Public Format As String
			Public NextBar As Bar
			Public Sub New(ByVal Value As Double, ByVal Text As String, ByVal Color As String, _
				ByVal Format As String)
				Me.Value = Value
				Me.Text = Text
				Me.Color = Color
				Me.Format = Format
			End Sub
		End Class

		Private _bar As Bar
		Private _last As Bar
		Private _max As Double
		Private _table As HtmlTable
		Private _forecolor As String
		Private _backcolor As String
		Private _headerforecolor As String
		Private _headerbackcolor As String
		Private _border As String
		Private _barheight As Integer = 100
		Private _barwidth As Integer = 50
		Private _text As String

		<Bindable(True), Category("Appearance"), _
		Description("The bargraph header text."), _
		Localizable(True) _
		> _
		Public Property Text() As String
			Get
				Return _text
			End Get
			Set(ByVal Value As String)
				_text = Value
			End Set
		End Property

		<Browsable(True), Category("Appearance"), _
		Description("The bargraph border.")> _
		Public Property Border() As String
			Get
				Return _border
			End Get
			Set(ByVal Value As String)
				_border = Value
			End Set
		End Property

		<Browsable(True), Category("Appearance"), _
		Description("The bargraph header back color.")> _
		Public Property HeaderBackColor() As String
			Get
				Return _headerbackcolor
			End Get
			Set(ByVal Value As String)
				_headerbackcolor = Value
			End Set
		End Property

		<Browsable(True), Category("Appearance"), _
		Description("The bargraph header fore color.")> _
		Public Property HeaderForeColor() As String
			Get
				Return _headerforecolor
			End Get
			Set(ByVal Value As String)
				_headerforecolor = Value
			End Set
		End Property

		<Browsable(True), Category("Appearance"), _
		Description("The bargraph text color.")> _
		Public Property ForeColor() As String
			Get
				Return _forecolor
			End Get
			Set(ByVal Value As String)
				_forecolor = Value
			End Set
		End Property

		<Browsable(True), Category("Appearance"), _
		Description("The bargraph back color.")> _
		Public Property BackColor() As String
			Get
				Return _backcolor
			End Get
			Set(ByVal Value As String)
				_backcolor = Value
			End Set
		End Property

		<Browsable(True), Category("Appearance"), _
		DefaultValue(100), _
		Description("The maximum possible bar height.")> _
		Public Property MaxBarHeight() As Integer
			Get
				Return _barheight
			End Get
			Set(ByVal Value As Integer)
				_barheight = Value
			End Set
		End Property

		<Browsable(True), Category("Appearance"), _
		DefaultValue(50), _
		Description("The width of each bar.")> _
		Public Property BarWidth() As Integer
			Get
				Return _barwidth
			End Get
			Set(ByVal Value As Integer)
				_barwidth = Value
			End Set
		End Property

		Public Sub Add(ByVal Value As Double, ByVal Text As String, ByVal Color As String, _
			ByVal Format As String)
			If _bar Is Nothing Then
				_bar = New Bar(Value, Text, Color, Format)
				_last = _bar
				_max = Value
			Else
				_last.NextBar = New Bar(Value, Text, Color, Format)
				_last = _last.NextBar
				If Value > _max Then
					_max = Value
				End If
			End If
		End Sub

		Protected Overrides Sub CreateChildControls()
			MyBase.CreateChildControls()
			Dim FACTOR As Double = IIf(_max = 0, 0, (_barheight / _max))
			Dim row As HtmlTableRow
			Dim cell As HtmlTableCell
			_table = New HtmlTable
			Controls.Add(_table)
			_table.CellPadding = 0
			_table.CellSpacing = 1
			If Not _backcolor Is Nothing Then _table.Style.Add("background-color", _backcolor)
			If Not _border Is Nothing Then _table.Style.Add("border", _border)
			row = New HtmlTableRow
			_table.Rows.Add(row)
			cell = New HtmlTableCell
			row.Cells.Add(cell)
			If Not _text Is Nothing Then
				cell.Style.Add("text-align", "center")
				cell.Style.Add("color", _headerforecolor)
				cell.Style.Add("background-color", _headerbackcolor)
				cell.InnerHtml = _text
			End If
			row = New HtmlTableRow
			_table.Rows.Add(row)
			cell = New HtmlTableCell
			row.Cells.Add(cell)
			_table = New HtmlTable
			cell.Controls.Add(_table)
			_table.CellPadding = 0
			_table.CellSpacing = 1
			row = New HtmlTableRow
			_table.Rows.Add(row)
			Dim nextb As Bar = _bar
			While Not nextb Is Nothing
				cell = New HtmlTableCell
				cell.Style.Add("vertical-align", "bottom")
				Dim tbl As New HtmlTable
				Dim tr As HtmlTableRow
				Dim td As HtmlTableCell
				tbl.CellPadding = 0
				tbl.CellSpacing = 0

				tr = New HtmlTableRow
				tbl.Rows.Add(tr)
				td = New HtmlTableCell
				td.Style.Add("text-align", "center")
				td.Style.Add("color", _forecolor)
				td.InnerHtml = String.Format(nextb.Format, nextb.Value)
				tr.Cells.Add(td)

				tr = New HtmlTableRow
				tbl.Rows.Add(tr)
				td = New HtmlTableCell
				tr.Cells.Add(td)
				Dim tbl2 As New HtmlTable
				tbl2.CellPadding = 0
				tbl2.CellSpacing = 0
				tbl2.Style.Add("height", Math.Round(nextb.Value * FACTOR) & "px")
				tbl2.Style.Add("width", _barwidth & "px")
				tbl2.Style.Add("border", "solid 1px " & nextb.Color)
				tbl2.Style.Add("background-color", nextb.Color)
				tr = New HtmlTableRow
				tr.Cells.Add(New HtmlTableCell)
				tbl2.Rows.Add(tr)
				td.Controls.Add(tbl2)

				tr = New HtmlTableRow
				tbl.Rows.Add(tr)
				td = New HtmlTableCell
				tr.Cells.Add(td)
				td.Style.Add("text-align", "center")
				td.Style.Add("color", _forecolor)
				td.InnerHtml = nextb.Text

				cell.Controls.Add(tbl)
				row.Cells.Add(cell)
				nextb = nextb.NextBar
			End While
		End Sub
	End Class

	< _
	AspNetHostingPermission(SecurityAction.Demand, _
		Level:=AspNetHostingPermissionLevel.Minimal), _
	AspNetHostingPermission(SecurityAction.InheritanceDemand, _
		Level:=AspNetHostingPermissionLevel.Minimal), _
	DefaultProperty("Text"), _
	ToolboxData("<{0}:BargraphHorizontal ID=""BargraphID"" ForeColor=""Black"" BarHeight=""25"" " & _
		"MaxBarWidth=""100"" runat=""server""></{0}:BargraphHorizontal>") _
	> _
	Public Class BargraphHorizontal
		Inherits Control

		Private Class Bar
			Public Value As Double
			Public Text As String
			Public Color As String
			Public Format As String
			Public NextBar As Bar
			Public Sub New(ByVal Value As Double, ByVal Text As String, ByVal Color As String, _
				ByVal Format As String)
				Me.Value = Value
				Me.Text = Text
				Me.Color = Color
				Me.Format = Format
			End Sub
		End Class

		Private _bar As Bar
		Private _last As Bar
		Private _max As Double
		Private _table As HtmlTable
		Private _forecolor As String
		Private _backcolor As String
		Private _headerforecolor As String
		Private _headerbackcolor As String
		Private _border As String
		Private _barheight As Integer = 25
		Private _barwidth As Integer = 100
		Private _text As String

		<Bindable(True), Category("Appearance"), _
		Description("The bargraph header."), _
		Localizable(True) _
		> _
		Public Property Text() As String
			Get
				Return _text
			End Get
			Set(ByVal Value As String)
				_text = Value
			End Set
		End Property

		<Browsable(True), Category("Appearance"), _
		Description("The bargraph border.")> _
		Public Property Border() As String
			Get
				Return _border
			End Get
			Set(ByVal Value As String)
				_border = Value
			End Set
		End Property

		<Browsable(True), Category("Appearance"), _
		Description("The bargraph header back color.")> _
		Public Property HeaderBackColor() As String
			Get
				Return _headerbackcolor
			End Get
			Set(ByVal Value As String)
				_headerbackcolor = Value
			End Set
		End Property

		<Browsable(True), Category("Appearance"), _
		Description("The bargraph header fore color.")> _
		Public Property HeaderForeColor() As String
			Get
				Return _headerforecolor
			End Get
			Set(ByVal Value As String)
				_headerforecolor = Value
			End Set
		End Property

		<Browsable(True), Category("Appearance"), _
		Description("The bargraph text color.")> _
		Public Property ForeColor() As String
			Get
				Return _forecolor
			End Get
			Set(ByVal Value As String)
				_forecolor = Value
			End Set
		End Property

		<Browsable(True), Category("Appearance"), _
		Description("The bargraph back color.")> _
		Public Property BackColor() As String
			Get
				Return _backcolor
			End Get
			Set(ByVal Value As String)
				_backcolor = Value
			End Set
		End Property

		<Browsable(True), Category("Appearance"), _
		DefaultValue(25), _
		Description("The height of each bar.")> _
		Public Property BarHeight() As Integer
			Get
				Return _barheight
			End Get
			Set(ByVal Value As Integer)
				_barheight = Value
			End Set
		End Property

		<Browsable(True), Category("Appearance"), _
		DefaultValue(100), _
		Description("The maximum possible bar width.")> _
		Public Property MaxBarWidth() As Integer
			Get
				Return _barwidth
			End Get
			Set(ByVal Value As Integer)
				_barwidth = Value
			End Set
		End Property

		Public Sub Add(ByVal Value As Double, ByVal Text As String, ByVal Color As String, _
			ByVal Format As String)
			If _bar Is Nothing Then
				_bar = New Bar(Value, Text, Color, Format)
				_last = _bar
				_max = Value
			Else
				_last.NextBar = New Bar(Value, Text, Color, Format)
				_last = _last.NextBar
				If Value > _max Then
					_max = Value
				End If
			End If
		End Sub

		Protected Overrides Sub CreateChildControls()
			MyBase.CreateChildControls()
			Dim FACTOR As Double = IIf(_max = 0, 0, (_barwidth / _max))
			Dim row As HtmlTableRow
			Dim cell As HtmlTableCell
			_table = New HtmlTable
			Controls.Add(_table)
			_table.CellPadding = 0
			_table.CellSpacing = 1
			If Not _backcolor Is Nothing Then _table.Style.Add("background-color", _backcolor)
			If Not _border Is Nothing Then _table.Style.Add("border", _border)
			row = New HtmlTableRow
			_table.Rows.Add(row)
			cell = New HtmlTableCell
			row.Cells.Add(cell)
			If Not _text Is Nothing Then
				cell.Style.Add("text-align", "center")
				cell.Style.Add("color", _headerforecolor)
				cell.Style.Add("background-color", _headerbackcolor)
				cell.InnerHtml = _text
			End If
			row = New HtmlTableRow
			_table.Rows.Add(row)
			cell = New HtmlTableCell
			row.Cells.Add(cell)
			_table = New HtmlTable
			cell.Controls.Add(_table)
			_table.CellPadding = 0
			_table.CellSpacing = 1
			Dim nextb As Bar = _bar
			While Not nextb Is Nothing
				Dim tbl As HtmlTable
				Dim tr As HtmlTableRow
				Dim td As HtmlTableCell

				row = New HtmlTableRow
				_table.Rows.Add(row)
				cell = New HtmlTableCell
				row.Cells.Add(cell)
				cell.Style.Add("text-align", "right")
				cell.Style.Add("vertical-align", "middle")
				cell.Style.Add("white-space", "nowrap")
				cell.Style.Add("color", _forecolor)
				cell.InnerHtml = nextb.Text

				cell = New HtmlTableCell
				row.Cells.Add(cell)
				cell.Style.Add("text-align", "left")
				cell.Style.Add("vertical-align", "middle")
				tbl = New HtmlTable
				cell.Controls.Add(tbl)
				tbl.CellPadding = 0
				tbl.CellSpacing = 0
				row = New HtmlTableRow
				tbl.Rows.Add(row)
				td = New HtmlTableCell
				row.Cells.Add(td)
				td.Style.Add("text-align", "left")
				td.Style.Add("vertical-align", "middle")
				Dim tbl2 As New HtmlTable
				td.Controls.Add(tbl2)
				tbl2.CellPadding = 0
				tbl2.CellSpacing = 0
				tbl2.Style.Add("width", Math.Round(nextb.Value * FACTOR) & "px")
				tbl2.Style.Add("height", _barheight & "px")
				tbl2.Style.Add("border", "solid 1px " & nextb.Color)
				tbl2.Style.Add("background-color", nextb.Color)
				tr = New HtmlTableRow
				tbl2.Rows.Add(tr)
				tr.Cells.Add(New HtmlTableCell)

				td = New HtmlTableCell
				row.Cells.Add(td)
				td.Style.Add("text-align", "left")
				td.Style.Add("vertical-align", "middle")
				td.Style.Add("white-space", "nowrap")
				td.Style.Add("color", _forecolor)
				td.InnerHtml = "&nbsp;" & String.Format(nextb.Format, nextb.Value)

				nextb = nextb.NextBar
			End While
		End Sub
	End Class

End Namespace

From the server, open a command window and change directory to

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

then execute

vbc.exe /t:library /out:C:\Bargraph.dll /r:System.dll,System.Web.dll C:\Bargraph.vb

Make sure you have proper rights in C:\. Copy the compiled assembly, C:\Bargraph.dll, to the website's \bin folder. Create a bin folder if needed. Create a new .aspx file in the website's directory and paste the following code into it:

<%@ Page Language="VB" %>
<%@ Register TagPrefix="abc" Namespace="prowaretech.Bargraph.Controls" Assembly="Bargraph" %>

<script runat="server">
	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
		BargraphVertical1.Add(2500, "Aug", "Green", "{0}")
		BargraphVertical1.Add(5000, "Sept", "Green", "{0}")
		BargraphVertical1.Add(10000, "Nov", "Green", "{0}")
		BargraphVertical1.Add(100, "Dec", "Green", "{0}")

		BargraphVertical2.Add(5435.99, "Newport", "#990000", "{0:c}")
		BargraphVertical2.Add(9005.45, "NYC", "#990000", "{0:c}")
		BargraphVertical2.Add(6060.55, "Springfield", "#990000", "{0:c}")
		BargraphVertical2.Add(707.12, "Austin", "#990000", "{0:c}")
		BargraphVertical2.Add(250.2, "San Diego", "#990000", "{0:c}")

		BargraphVertical3.Add(0.101, "2008", "#6666FF", "{0}")
		BargraphVertical3.Add(0.998, "2008", "Navy", "{0}")
		BargraphVertical3.Add(0.137, "2009", "#6666FF", "{0}")
		BargraphVertical3.Add(0.877, "2009", "Navy", "{0}")
		BargraphVertical3.Add(0.259, "2010", "#6666FF", "{0}")
		BargraphVertical3.Add(0.709, "2010", "Navy", "{0}")

		BargraphHorizontal1.Add(0.101, "2008", "#6666FF", "{0}")
		BargraphHorizontal1.Add(0.998, "2008", "Navy", "{0}")
		BargraphHorizontal1.Add(0.137, "2009", "#6666FF", "{0}")
		BargraphHorizontal1.Add(0.877, "2009", "Navy", "{0}")
		BargraphHorizontal1.Add(0.259, "2010", "#6666FF", "{0}")
		BargraphHorizontal1.Add(0.709, "2010", "Navy", "{0}")

		BargraphHorizontal2.Add(1360160000, "China", "#FF77FF", "{0:0,0}")
		BargraphHorizontal2.Add(1234390000, "India", "#EE66EE", "{0:0,0}")
		BargraphHorizontal2.Add(316743000, "United States", "#DD55DD", "{0:0,0}")
		BargraphHorizontal2.Add(237641326, "Indonesia", "#CC44CC", "{0:0,0}")
		BargraphHorizontal2.Add(201032714, "Brazil", "#BB33BB", "{0:0,0}")
		BargraphHorizontal2.Add(184351000, "Pakistan", "#AA22AA", "{0:0,0}")
		BargraphHorizontal2.Add(173615000, "Nigeria", "#991199", "{0:0,0}")
		BargraphHorizontal2.Add(152518015, "Bangladesh", "#880088", "{0:0,0}")
		BargraphHorizontal2.Border = "Solid 1px Black"
	End Sub
</script>

<html>
<head>
	<title>Bargraph Custom Server Control</title>
</head>
<body>
	<abc:BargraphVertical ID="BargraphVertical1" BarWidth="50" MaxBarHeight="100"
	ForeColor="Blue" HeaderForeColor="White" HeaderBackColor="Navy" Text="Bargraph"
	runat="server" BackColor="Wheat" Border="Solid 1px Maroon"></abc:BargraphVertical>
	<hr />
	<abc:BargraphVertical ID="BargraphVertical2" BarWidth="100" MaxBarHeight="300"
	ForeColor="Black" runat="server" />
	<hr />
	<abc:BargraphVertical ID="BargraphVertical3" BarWidth="50" MaxBarHeight="200"
	ForeColor="Maroon" runat="server" />
	<hr />
	<abc:BargraphHorizontal ID="BargraphHorizontal1" BarHeight="25" MaxBarWidth="200"
	ForeColor="Maroon" runat="server" />
	<hr />
	<abc:BargraphHorizontal ID="BargraphHorizontal2" BarHeight="10" MaxBarWidth="400"
	ForeColor="Gray" Text="Population By Country" HeaderForeColor="#333333" HeaderBackColor="Gray"
	runat="server" />
</body>
</html>
<%@ Page Language="C#" %>
<%@ Register TagPrefix="abc" Namespace="prowaretech.Bargraph.Controls" Assembly="Bargraph" %>

<script runat="server">
	void Page_Load(object sender, System.EventArgs e)
	{
		BargraphVertical1.Add(2500, "Aug", "Green", "{0}");
		BargraphVertical1.Add(5000, "Sept", "Green", "{0}");
		BargraphVertical1.Add(10000, "Nov", "Green", "{0}");
		BargraphVertical1.Add(100, "Dec", "Green", "{0}");

		BargraphVertical2.Add(5435.99, "Newport", "#990000", "{0:c}");
		BargraphVertical2.Add(9005.45, "NYC", "#990000", "{0:c}");
		BargraphVertical2.Add(6060.55, "Springfield", "#990000", "{0:c}");
		BargraphVertical2.Add(707.12, "Austin", "#990000", "{0:c}");
		BargraphVertical2.Add(250.2, "San Diego", "#990000", "{0:c}");

		BargraphVertical3.Add(0.101, "2008", "#6666FF", "{0}");
		BargraphVertical3.Add(0.998, "2008", "Navy", "{0}");
		BargraphVertical3.Add(0.137, "2009", "#6666FF", "{0}");
		BargraphVertical3.Add(0.877, "2009", "Navy", "{0}");
		BargraphVertical3.Add(0.259, "2010", "#6666FF", "{0}");
		BargraphVertical3.Add(0.709, "2010", "Navy", "{0}");

		BargraphHorizontal1.Add(0.101, "2008", "#6666FF", "{0}");
		BargraphHorizontal1.Add(0.998, "2008", "Navy", "{0}");
		BargraphHorizontal1.Add(0.137, "2009", "#6666FF", "{0}");
		BargraphHorizontal1.Add(0.877, "2009", "Navy", "{0}");
		BargraphHorizontal1.Add(0.259, "2010", "#6666FF", "{0}");
		BargraphHorizontal1.Add(0.709, "2010", "Navy", "{0}");

		BargraphHorizontal2.Add(1360160000, "China", "#FF77FF", "{0:0,0}");
		BargraphHorizontal2.Add(1234390000, "India", "#EE66EE", "{0:0,0}");
		BargraphHorizontal2.Add(316743000, "United States", "#DD55DD", "{0:0,0}");
		BargraphHorizontal2.Add(237641326, "Indonesia", "#CC44CC", "{0:0,0}");
		BargraphHorizontal2.Add(201032714, "Brazil", "#BB33BB", "{0:0,0}");
		BargraphHorizontal2.Add(184351000, "Pakistan", "#AA22AA", "{0:0,0}");
		BargraphHorizontal2.Add(173615000, "Nigeria", "#991199", "{0:0,0}");
		BargraphHorizontal2.Add(152518015, "Bangladesh", "#880088", "{0:0,0}");
		BargraphHorizontal2.Border = "Solid 1px Black";
	}
</script>

<html>
<head>
	<title>Bargraph Custom Server Control</title>
</head>
<body>
	<abc:BargraphVertical ID="BargraphVertical1" BarWidth="50" MaxBarHeight="100"
	ForeColor="Blue" HeaderForeColor="White" HeaderBackColor="Navy" Text="Bargraph"
	runat="server" BackColor="Wheat" Border="Solid 1px Maroon"></abc:BargraphVertical>
	<hr />
	<abc:BargraphVertical ID="BargraphVertical2" BarWidth="100" MaxBarHeight="300"
	ForeColor="Black" runat="server" />
	<hr />
	<abc:BargraphVertical ID="BargraphVertical3" BarWidth="50" MaxBarHeight="200"
	ForeColor="Maroon" runat="server" />
	<hr />
	<abc:BargraphHorizontal ID="BargraphHorizontal1" BarHeight="25" MaxBarWidth="200"
	ForeColor="Maroon" runat="server" />
	<hr />
	<abc:BargraphHorizontal ID="BargraphHorizontal2" BarHeight="10" MaxBarWidth="400"
	ForeColor="Gray" Text="Population By Country" HeaderForeColor="#333333" HeaderBackColor="Gray"
	runat="server" />
</body>
</html>

PROWAREtech

Hello there! How can I help you today?
Ask any question

PROWAREtech

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
ACCEPT REJECT