{"id":211,"date":"2013-05-20T01:57:12","date_gmt":"2013-05-20T06:57:12","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=211"},"modified":"2013-05-20T01:57:12","modified_gmt":"2013-05-20T06:57:12","slug":"charting-with-vb-net-2010","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2013\/05\/20\/charting-with-vb-net-2010\/","title":{"rendered":"Charting with VB.Net 2010"},"content":{"rendered":"<p><strong>I have stopped updating this blog. This link can also be found in my Website.<\/strong><\/p>\n<p><strong><a href=\"http:\/\/www.siddharthrout.com\/2011\/10\/18\/charting-with-vb-net-2010\/\">http:\/\/www.siddharthrout.com\/2011\/10\/18\/charting-with-vb-net-2010\/<\/a><\/strong><\/p>\n<p align=\"justify\">\n<p align=\"justify\"><strong>T<\/strong>here are many tutorials on the web regarding VB.Net Charting but I couldn\u2019t find one which actually goes into details.\u00a0 So I thought of creating one. The example that I will be using retrieves the data from an Access Database and populates the chart in VB.Net.<\/p>\n<blockquote>\n<p align=\"justify\"><strong>VB.Net Version<\/strong>: 2010 Ultimate<\/p>\n<\/blockquote>\n<blockquote>\n<p align=\"justify\"><strong>MS Access<\/strong>\u00a0: 2010<\/p>\n<\/blockquote>\n<p align=\"justify\"><strong>1)<\/strong>\u00a0To start off, let\u2019s create a small database in Access. Your Access Database looks like this once it is ready.<\/p>\n<p align=\"justify\"><a href=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image9.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" alt=\"image\" src=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image_thumb9.png?w=354&amp;h=195\" width=\"354\" height=\"195\" border=\"0\" \/><\/a><a href=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image10.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" alt=\"image\" src=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image_thumb10.png?w=353&amp;h=206\" width=\"353\" height=\"206\" border=\"0\" \/><\/a><\/p>\n<p align=\"justify\">Name the Table as \u201c<strong>Table1<\/strong>\u201d and the Database as \u201c<strong>Sample.accdb<\/strong>\u201d. Save it to a location of your choice. Remember the location as we will have to use this database later from vb.net.<\/p>\n<p align=\"justify\"><strong>2)<\/strong>\u00a0Start Visual Studio and create a new Windows Application Project on VB.NET. Let\u2019s Call it \u201c<strong>Charting<\/strong>\u201d. See snapshot below<\/p>\n<p align=\"justify\"><a href=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image11.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" alt=\"image\" src=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image_thumb12.png?w=480&amp;h=311\" width=\"480\" height=\"311\" border=\"0\" \/><\/a><\/p>\n<p align=\"justify\"><strong>3)<\/strong>\u00a0Click \u2018OK\u2019 and then place the controls as shown in the image below.<\/p>\n<p align=\"justify\"><a href=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image12.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" alt=\"image\" src=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image_thumb13.png?w=483&amp;h=287\" width=\"483\" height=\"287\" border=\"0\" \/><\/a><\/p>\n<p align=\"justify\">In earlier versions of vb.net, you had to actually activate the chart control by right clicking on the \u201c<strong>ContextMenuStrip<\/strong>\u201d and then selecting MS Chart Control from the COM Tab. But now you can find that control right under the \u201c<strong>Data<\/strong>\u201d Tab.<\/p>\n<p align=\"justify\"><a href=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image13.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" alt=\"image\" src=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image_thumb14.png?w=400&amp;h=387\" width=\"400\" height=\"387\" border=\"0\" \/><\/a><\/p>\n<p align=\"justify\"><strong>4)<\/strong>\u00a0<strong>Creating Basic Chart<\/strong>: Once your controls are ready, open the code editor and simply paste this code. This code will create a very basic chart.<\/p>\n<div align=\"justify\">\n<pre>Imports System.Data.OleDb\nImports System.Windows.Forms.DataVisualization.Charting\n\nPublic Class Form1\n\n    '~~&gt; Code required to browse for the Access Database File\n    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click\n        With OpenFileDialog1\n            .DefaultExt = \"accdb\"\n            .DereferenceLinks = True\n            .Filter = _\n             \"Access files (*.accdb)|*.accdb\"\n            .Multiselect = False\n            .Title = \"Select an Access Database file to open\"\n            .ValidateNames = True\n\n            If .ShowDialog = Windows.Forms.DialogResult.OK Then\n                Try\n                    TextBox1.Text = .FileName\n                Catch fileException As Exception\n                    Throw fileException\n                End Try\n            End If\n        End With\n    End Sub\n\n    '~~&gt; Code to generate the chart\n    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click\n        Dim strConn As String = \"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\" &amp; TextBox1.Text &amp; _\n        \";Persist Security Info=False;\"\n        Dim tblFields As String = \"SELECT * from Table1\"\n\n        Dim conn As New OleDbConnection(strConn)\n        Dim oCmd As New OleDbCommand(tblFields, conn)\n        Dim oData As New OleDbDataAdapter(tblFields, conn)\n        Dim ds As New DataSet\n\n        conn.Open()\n        oData.Fill(ds, \"Table1\")\n        conn.Close()\n\n        Chart1.DataSource = ds.Tables(\"Table1\")\n        Dim Series1 As Series = Chart1.Series(\"Series1\")\n        Series1.Name = \"Sales\"\n        Chart1.Series(Series1.Name).XValueMember = \"nFruits\"\n        Chart1.Series(Series1.Name).YValueMembers = \"nSales\"\n\n        Chart1.Size = New System.Drawing.Size(780, 350)\n    End Sub\n\nEnd Class<\/pre>\n<\/div>\n<p>When you run the code your form will popup and looks as shown in the image below. Select your file by clicking on the \u201cLoad File\u201d button. Once you have selected the file, click on \u201c<strong>Create Chart<\/strong>\u201d<\/p>\n<p align=\"justify\"><a href=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image14.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" alt=\"image\" src=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image_thumb15.png?w=635&amp;h=325\" width=\"635\" height=\"325\" border=\"0\" \/><\/a><\/p>\n<p align=\"justify\">When you click on \u201c<strong>Create Chart<\/strong>\u201d you see a chart generated as shown below.<\/p>\n<p align=\"justify\"><a href=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image15.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" alt=\"image\" src=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image_thumb16.png?w=632&amp;h=324\" width=\"632\" height=\"324\" border=\"0\" \/><\/a><\/p>\n<p align=\"justify\">Now this was making a very basic chart. What if we want it to give a more professional look? What if we wanted to format the title or make the chart look more appealing?<\/p>\n<p align=\"justify\"><strong>5)<\/strong>\u00a0<strong>Advanced Charting<\/strong>: Now replace the old code with this new code. I have commented the code so you know what the code is doing.<\/p>\n<pre>Imports System.Data.OleDb\nImports System.Windows.Forms.DataVisualization.Charting\n\nPublic Class Form1\n\n    '~~&gt; Code required to browse for the Access Database File\n    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click\n        With OpenFileDialog1\n            .DefaultExt = \"accdb\"\n            .DereferenceLinks = True\n            .Filter = _\n             \"Access files (*.accdb)|*.accdb\"\n            .Multiselect = False\n            .Title = \"Select an Access Database file to open\"\n            .ValidateNames = True\n\n            If .ShowDialog = Windows.Forms.DialogResult.OK Then\n                Try\n                    TextBox1.Text = .FileName\n                Catch fileException As Exception\n                    Throw fileException\n                End Try\n            End If\n        End With\n    End Sub\n\n    '~~&gt; Code to generate the chart\n    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click\n        '~~&gt; Connection String to connect to the access Database\n        '~~&gt; If you are planning to use SQL Database then please visit www.connectionstrings.com for an\n        '~~&gt; appropriate connection string\n        Dim strConn As String = \"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\" &amp; TextBox1.Text &amp; _\n        \";Persist Security Info=False;\"\n\n        '~~&gt; Query string\n        Dim tblFields As String = \"SELECT * from Table1\"\n\n        '~~&gt; Connecting to Data base and storing the data in a dataset\n        Dim conn As New OleDbConnection(strConn)\n        Dim oCmd As New OleDbCommand(tblFields, conn)\n        Dim oData As New OleDbDataAdapter(tblFields, conn)\n        Dim ds As New DataSet\n\n        conn.Open()\n        oData.Fill(ds, \"Table1\")\n        conn.Close()\n\n        '''''''''''''''''''''''''''''\n        '~~&gt; SET DATA SOURCE &lt;~~'\n        '''''''''''''''''''''''''''''\n        Chart1.DataSource = ds.Tables(\"Table1\")\n\n        ''''''''''''''''''''''''''''''''\n        '~~&gt; WORKING WITH CHARTAREA &lt;~~'\n        ''''''''''''''''''''''''''''''''\n        Dim CArea As ChartArea = Chart1.ChartAreas(0)\n        CArea.BackColor = Color.Azure           '~~&gt; Changing the Back Color of the Chart Area \n        CArea.ShadowColor = Color.Red           '~~&gt; Changing the Shadow Color of the Chart Area \n        CArea.Area3DStyle.Enable3D = True       '~~&gt; Changing the Chart Style to 3D \n\n        '~~&gt; Formatting X Axis\n        CArea.AxisX.MajorGrid.Enabled = False   '~~&gt; Removed the X axis major grids\n        CArea.AxisX.LabelStyle.Font = New System.Drawing.Font(\"Times New Roman\", _\n        11.0F, System.Drawing.FontStyle.Italic) '~~&gt; Setting Font, Font Size and Italicizing\n\n        '~~&gt; Formatting Y Axis\n        CArea.AxisY.MajorGrid.Enabled = False   '~~&gt; Removed the Y axis major grids\n        CArea.AxisY.LabelStyle.Format = \"0.00%\" '~~&gt; Formatting Y axis to display values in %age\n        CArea.AxisY.Interval = 0.2              '~~&gt; Formatting Y axis Interval to 20%\n\n        ''''''''''''''''''''''''''''\n        '~~&gt; WORKING WITH TITLE &lt;~~'\n        ''''''''''''''''''''''''''''\n        '~~&gt; Adding a Title\n        Dim T As Title = Chart1.Titles.Add(\"Sales Report Fruits - Year 2010\")\n        '~~&gt; Formatting the Title\n        With T\n            .ForeColor = Color.Black            '~~&gt; Changing the Fore Color of the Title \n            .BackColor = Color.Coral            '~~&gt; Changing the Back Color of the Title \n\n            '~~&gt; Setting Font, Font Size and Bold\/Italicizing\n            .Font = New System.Drawing.Font(\"Times New Roman\", 11.0F, System.Drawing.FontStyle.Bold)\n            .Font = New System.Drawing.Font(\"Times New Roman\", 11.0F, System.Drawing.FontStyle.Underline)\n            .BorderColor = Color.Black          '~~&gt; Changing the Border Color of the Title \n            .BorderDashStyle = ChartDashStyle.DashDotDot '~~&gt; Changing the Border Dash Style of the Title \n        End With\n\n        '''''''''''''''''''''''''''''\n        '~~&gt; WORKING WITH SERIES &lt;~~'\n        '''''''''''''''''''''''''''''\n        Dim Series1 As Series = Chart1.Series(\"Series1\")\n        '~~&gt; Setting the series Name\n        Series1.Name = \"Sales\"\n        '~~&gt; Assigning values to X and Y Axis\n        Chart1.Series(Series1.Name).XValueMember = \"nFruits\"\n        Chart1.Series(Series1.Name).YValueMembers = \"nSales\"\n        '~~&gt; Setting Font, Font Size and Bold\n        Chart1.Series(Series1.Name).Font = New System.Drawing.Font(\"Times New Roman\", 11.0F, System.Drawing.FontStyle.Bold)\n        '~~&gt; Setting Value Type\n        Chart1.Series(Series1.Name).YValueType = ChartValueType.Double\n        '~~&gt; Setting the Chart Type for Display \n        Chart1.Series(Series1.Name).ChartType = SeriesChartType.Radar\n        '~~&gt; Display Data Labels\n        Chart1.Series(Series1.Name).IsValueShownAsLabel = True\n        '~~&gt; Setting label's Fore Color\n        Chart1.Series(Series1.Name).LabelForeColor = Color.DarkGreen\n        '~~&gt; Setting label's Format to %age\n        Chart1.Series(Series1.Name).LabelFormat = \"0.00%\"\n\n        '''''''''''''''''''''''''''''\n        '~~&gt; WORKING WITH LEGEND &lt;~~'\n        '''''''''''''''''''''''''''''\n        Dim LG As Legend = Chart1.Legends(0)\n        '~~&gt; Changing the Back Color of the Legend \n        LG.BackColor = Color.Wheat\n        '~~&gt; Changing the Fore Color of the Legend\n        LG.ForeColor = Color.DarkSlateBlue\n        '~~&gt; Setting Font, Font Size and Bold\n        LG.Font = New System.Drawing.Font(\"Times New Roman\", 11.0F, System.Drawing.FontStyle.Bold)\n        '~~&gt; Assigning a title for the legend\n        LG.Title = \"Legend\"\n\n        '~~&gt; Setting the location for the chart\n        Chart1.Size = New System.Drawing.Size(780, 350)\n    End Sub\nEnd Class<\/pre>\n<p align=\"justify\">When you run the above code you will see the old chart now transforms into a much more meaningful chart.<\/p>\n<p align=\"justify\"><a href=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image16.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" alt=\"image\" src=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image_thumb17.png?w=632&amp;h=324\" width=\"632\" height=\"324\" border=\"0\" \/><\/a><\/p>\n<p align=\"justify\">Similarly we can create other charts like Bar Charts, Pie Charts, Line Charts, Area Charts etc. All you need to do is change the line in the above code<\/p>\n<div align=\"justify\">\n<pre>Chart1.Series(Series1.Name).ChartType = SeriesChartType.Column<\/pre>\n<\/div>\n<p align=\"justify\"><strong>Few Examples<\/strong><\/p>\n<p align=\"justify\"><strong>Line Chart<\/strong><\/p>\n<div align=\"justify\">\n<pre>Chart1.Series(Series1.Name).ChartType = SeriesChartType.Line<\/pre>\n<\/div>\n<p align=\"justify\"><a href=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image17.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" alt=\"image\" src=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image_thumb18.png?w=501&amp;h=257\" width=\"501\" height=\"257\" border=\"0\" \/><\/a><\/p>\n<p align=\"justify\"><strong>Pie Chart<\/strong><\/p>\n<div align=\"justify\">\n<pre>Chart1.Series(Series1.Name).ChartType = SeriesChartType.Pie<\/pre>\n<\/div>\n<p align=\"justify\"><a href=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image18.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" alt=\"image\" src=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image_thumb19.png?w=505&amp;h=259\" width=\"505\" height=\"259\" border=\"0\" \/><\/a><\/p>\n<p align=\"justify\"><strong>Funnel Chart<\/strong><\/p>\n<div align=\"justify\">\n<pre>Chart1.Series(Series1.Name).ChartType = SeriesChartType.Funnel<\/pre>\n<\/div>\n<p align=\"justify\"><a href=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image19.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" alt=\"image\" src=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image_thumb20.png?w=500&amp;h=256\" width=\"500\" height=\"256\" border=\"0\" \/><\/a><\/p>\n<p align=\"justify\"><strong>Radar Chart<\/strong><\/p>\n<div align=\"justify\">\n<pre>Chart1.Series(Series1.Name).ChartType = SeriesChartType.Radar<\/pre>\n<\/div>\n<p align=\"justify\"><a href=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image20.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" alt=\"image\" src=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/image_thumb21.png?w=505&amp;h=259\" width=\"505\" height=\"259\" border=\"0\" \/><\/a><\/p>\n<p align=\"justify\">Hope this helps\u00a0<img decoding=\"async\" alt=\"Smile\" src=\"http:\/\/siddharthrout.files.wordpress.com\/2011\/10\/wlemoticon-smile2.png?w=630\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have stopped updating this blog. This link can also be found in my Website. http:\/\/www.siddharthrout.com\/2011\/10\/18\/charting-with-vb-net-2010\/ There are many tutorials on the web regarding VB.Net&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-211","post","type-post","status-publish","format-standard","hentry","category-vb-net"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=211"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/211\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}