65.9K
CodeProject is changing. Read more.
Home

ASP.net Chart in 10 Simple Steps.

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Oct 11, 2013

CPOL

2 min read

viewsIcon

9942

ASP.net chart control is one of the newly introduced controls to the Visual Studio IDE from Visual Studio 2010 onwards. It is a very powerful, yet

ASP.net chart control is one of the newly introduced controls to the Visual Studio IDE from Visual Studio 2010 onwards. It is a very powerful, yet easy to use server side control. Let's take an example on how to use it.

Prerequisites:

  • You would need AdventureWorks database. If you don't have one, you can download an appropriate version from here.
  • Visual Studio 2010.

How can I create a simple chart with ASP.net?

Well, it's easy!

  1. Just drag and drop a chart control to your webform.
  2. Click on tasks shortcut ">" on top-right corner of the chart control. On the item "Choose Data Source", select "<New data source...>".
  3. Select "Database" item from data source types and click ok.
  4. Create a new connection to SQL Server or choose one from Web.config. Remember, you should connect to "AdventureWorks" sample database.
  5. Select the radio button "Specify a custom SQL statement or store procedure" and click next.
  6. Select the radio button "SQL statement", paste the following query and click next.

    SELECT     TOP (10) ISNULL(Person.Contact.FirstName, '') + ' ' + ISNULL(Person.Contact.MiddleName, '') + ' ' + ISNULL(Person.Contact.LastName, '') AS [Sales Person],
                          Sales.SalesPerson.SalesYTD AS [Sales]
    FROM         HumanResources.Employee INNER JOIN
                          Person.Contact ON HumanResources.Employee.ContactID = Person.Contact.ContactID INNER JOIN
                          Sales.SalesPerson ON HumanResources.Employee.EmployeeID = Sales.SalesPerson.SalesPersonID
  7. Click the button "Test Query" and then click finish.
  8. Re-size the chart control by dragging--make it more wider and a little taller.
  9. Click on tasks shortcut ">" on top-right corner of the chart control. On the item "X Value Member", select "Sales Person". Similarly, on "Y Value Member", select "Sales".
  10. Save the webform and test it!

You're Done!

Now, you can play with a variety of chart types such as "Line", "Spline",  "FastLine", "Bar", "Column". Once you get your own working chart control, you can evaluate more advanced features.

OSZAR »