Using the Graph2D Class

Using the Graph2D Class

The Graph2D class is easy to use. Simply instantiate it, add the data, set any properties that need to be set, and call its Render() method. This section shows how an application named GraphAppVB uses the Graph2D class.

Displaying the Rendered Image

The graph image has to be displayed in the HTML page in one way or another. I chose to output an <IMG> tag with the Response.Write() method. The application keeps the file name for the current image in a session variable named Graph. If the session variable is not nothing (null in C#), then the file name is output so that the HTML rendering engine will display it. This code can be seen in Listing 10.11 in a method named InsertGraphLink().

Listing 10.11 The InsertGraphLink() Method Outputs a Link for the Rendered Image.
 Public Sub InsertGraphLink()     If Session("Graph") <> Nothing Then         Response.Write("<IMG SRC=GraphImages/Graph" + _          Convert.ToString(Session("Graph")) + ".gif>")     End If End Sub 

Inserting Data into the TextBox

Because this demo application isn't meant to tax its users, it has some preloaded data. I didn't want users to have to type in lots of data just to see the graph appear. Of course, users can type in their own data, but doing so is not necessary.

The LoadData_Click() method fires when the user clicks the Load Data button, and it can be seen in Listing 10.12. This method simply sets the GraphData TextBox object to contain some data.

Listing 10.12 When the User Clicks the Load Data Button, This Method Is Fired, and the Selected Set of Default Data Is Loaded into the Text Box.
 Private Sub LoadData_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles LoadData.Click     Select Case DataSelection.SelectedIndex         Case 0             GraphData.Text = _                 "Beth;98.9" + vbCrLf + _                 "George;84.1" + vbCrLf + _                 "Jane;97.9" + vbCrLf + _                 "Judy;96.4" + vbCrLf + _                 "Matilda;78.9" + vbCrLf + _                 "Rick;58.9" + vbCrLf + _                 "Sam;97.2" + vbCrLf + _                 "Suzy;98.5" + vbCrLf + _                 "Tammy;88.9"         Case 1             GraphData.Text = _                 "America;5000000" + vbCrLf + _                 "BedRock;7000000" + vbCrLf + _                 "China;60000000" + vbCrLf + _                 "Easter Island;60000000" + vbCrLf + _                 "Japan;60000000" + vbCrLf + _                 "JellyStone;60000000" + vbCrLf + _                 "Lower Slobovia;64200000" + vbCrLf + _                 "Madagascar;60000000" + vbCrLf + _                 "Stonehenge;60000000"         Case 2             GraphData.Text = _                 "Acme;47" + vbCrLf + _                 "ExpenseCo;63" + vbCrLf + _                 "Generic;15" + vbCrLf + _                 "IBM;25" + vbCrLf + _                 "Intel;24" + vbCrLf + _                 "JSVentures;12" + vbCrLf + _                 "Microsoft;30" + vbCrLf + _                 "PBS;43" + vbCrLf + _                 "ProxyShield;4" + vbCrLf + _                 "SmithCo;53"     End Select End Sub 

Rendering the Graph

The GraphAppVB application calls on a method named RenderGraph() that can be seen in Listing 10.13. This method could be simple if no Graph2D properties were set. But it starts (after instantiating a Graph2D object) by taking information from the user interface objects and setting the Graph2D properties.

The data is parsed from the GraphData TextBox. Each data pair is separated by a carriage return/line-feed pair, and the data name and value are separated by a semi-colon (;). Each data name and value are added to the Graph2D object.

Finally, the Render() method is called, which draws the graph and saves it to disk.

Listing 10.13 The RenderGraph() Method Takes the Properties That Have Been Set in the User Interface, Puts Them into an Instantiated Graph2D Object, and Calls the Graph2D's Render() Method.
 Private Sub RenderGraph_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles RenderGraph.Click     Dim graph As New Graph2D()     ' Set graph properties...     graph.Title = GraphTitle.Text     Dim TitleColor As Color() = {Color.Red, Color.Green, _       Color.Blue, Color.Green}     graph.TitleColor = TitleColor(TitleColors.SelectedIndex)     Dim BackgroundColor As Color() = {Color.White, Color.Yellow, _       Color.Cyan, Color.LightGray, Color.LightGreen}     graph.BackgroundColor = _       BackgroundColor(BackgroundColors.SelectedIndex)     graph.Width = Convert.ToInt32(GraphWidth.Text)     graph.Height = Convert.ToInt32(GraphHeight.Text)     Dim separator As Char() = {ChrW(126)}     Dim strData As String() = _       GraphData.Text.Replace(vbCrLf, "~").Split(separator, 200)     Dim i As Integer     For i = 0 To strData.Length - 1         Dim SemiColonSeparator As Char() = {ChrW(59)}         Dim strPair As String() = strData(i).Split(SemiColonSeparator, 2)         If strPair(0) <> Nothing And _             strPair(0).Length > 0 And _             strPair(1) <> Nothing And _             strPair(1).Length > 0 Then             graph.AddPair(strPair(0), Convert.ToDouble(strPair(1)))         End If     Next     Session("Graph") = graph.Render(Request.MapPath(".")) End Sub 

ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ISBN: 321159659
Year: 2003
Pages: 175 © 2008-2017.
If you may any questions please contact us: