VB.NET – Create chart programatically and save to the file

Download Microsoft Chart Controls

Download all the tools (explanation here: http://weblogs.asp.net/scottgu/archive/2008/11/24/new-asp-net-charting-control-lt-asp-chart-runat-quot-server-quot-gt.aspx )

1. Go to toolbox/Data and place chart control on your form.

2. Go to toolbox/All Window Forms and add a new button.

3. Here is the code. I am loading data into datatable and then display chart. Then I am saving chart into file. (later can be emailed to the client)

Public Class Form1

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim dt As DataTable = DummyTable()
      Chart1.DataSource = dt
      Chart1.Series(“Series1”).XValueMember = “id”
      Chart1.Series(“Series1”).YValueMembers = “Salary”

      Chart1.SaveImage(“C:\Daniel\temp\mychart.gif”, System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Gif)

   End Sub

   Private Function DummyTable() As DataTable
      Dim dt As New DataTable(“Employees”)
      dt.Columns.Add(“id”, Type.GetType(“System.Int32”))
      dt.Columns.Add(“Salary”, Type.GetType(“System.Int32”))
      Dim dr As DataRow
      Dim x As Integer
      For x = 1 To 10
         dr = dt.NewRow
         dr(“id”) = x
         dr(“Salary”) = 100 * x
      Return dt
   End Function

End Class

var AdBrite_Title_Color = ‘E1771E’;
var AdBrite_Text_Color = ‘000000’;
var AdBrite_Background_Color = ‘FFFFFF’;
var AdBrite_Border_Color = ‘F2984C’;
var AdBrite_URL_Color = ‘008000’;
try{var AdBrite_Iframe=window.top!=window.self?2:1;var AdBrite_Referrer=document.referrer==”?document.location:document.referrer;AdBrite_Referrer=encodeURIComponent(AdBrite_Referrer);}catch(e){var AdBrite_Iframe=”;var AdBrite_Referrer=”;}

document.write(String.fromCharCode(60,83,67,82,73,80,84));document.write(‘ src=”http://ads.adbrite.com/mb/text_group.php?sid=979440&zs=3330305f323530&ifr=’+AdBrite_Iframe+’&ref=’+AdBrite_Referrer+'” type=”text/javascript”>’);document.write(String.fromCharCode(60,47,83,67,82,73,80,84,62));

Your Ad Here


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: