SunQuest
 
       Code Examples
  Home arrow Code Examples arrow Page 3 - Creating a Chart using Data from a Databas...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Creating a Chart using Data from a Database with MS Chart Control
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 28
    2006-09-05

    Table of Contents:
  • Creating a Chart using Data from a Database with MS Chart Control
  • Visual Basic Project to retrieve data
  • Connecting the chart to data
  • Assigning retrieved data to an array to display on the chart

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!

    Creating a Chart using Data from a Database with MS Chart Control - Connecting the chart to data


    (Page 3 of 4 )

    Code to retrieve data using DataSource binding

    Drag and drop a Microsoft Chart control onto the form, Form1 as shown.

    From the properties window some of the following properties were set as shown:

    Series Type: 1 - VtChSeriesType2Bar
    RowCount: 4
    Row: 1
    RowLabel :R1
    ShowLegend: False
    ColumnCount: 3
    Column:1
    ColumnLabel:C1

    It could happen that the design time settings can override run time settings and you should test the control's behavior at run time and tune it appropriately.

    You could also access the Property pages for this ActiveX control to set some of the properties.

    In the form's Form_OnLoad () event use the following snippet to retrieve and connect the data using the DataSource property of the MS Chart Control. The snippet in purple is Microsoft's suggestion to bind data to the MS Chart Control (this is Microsoft's example adopted for this tutorial).

    Private rsProducts As ADODB.Recordset
    Private cn As ADODB.Connection
     
    Private Sub Form_Load()
    Set cn = New ADODB.Connection
    MSChart1.ToDefaults
     
     
    ' Establish the connection using the connection string.
       cn.ConnectionString = 
       _"Provider=SQLOLEDB.1;Password=XXXXXXX;" & _
       "Persist Security Info=True; User ID=sa;" & _
       "Initial Catalog=TestWiz; Data Source=XPHTEK"
     
       
    ' Open the connection.
       cn.Open
       Dim strQuery As String ' SQL query string.
     
       ' Create a query that retrieves only four fields.
       'strQuery = "SELECT ProductName, UnitPrice,
       'UnitsInStock, UnitsOnOrder  FROM Products"
       strQuery = "SELECT ProductName, UnitPrice, UnitsInStock, 
    UnitsOnOrder FROM Products
    "
       Set rsProducts = New ADODB.Recordset
       ' Open the recordset.
       
       rsProducts.CursorLocation = adUseClient
       rsProducts.Open strQuery, cn, adOpenKeyset
     
       ' Set the DataSource to the recordset.
       With MSChart1
             .ShowLegend = True
           Set .DataSource = rsProducts
        End With
       
    End Sub

    When the program runs without any errors, you get a chart as shown in the next picture. Instead of four rows there are only three. Also the display of the legend is not complete. This display is irrespective of the changes you make either in the Properties window, or the Property Pages.

    The databinding to MS Chart is less than satisfactory as it does not give a correct picture of the data.

    More Code Examples Articles
    More By Jayaram Krishnaswamy


       · Progress has a price to pay. Software getting outdated is going to remain for ever...
     

    CODE EXAMPLES ARTICLES

    - Handling Animations and Bitmaps Using GDI+ f...
    - Download a Web Page using the WebClient
    - Creating a Chart using Data from a Database ...
    - The Basics of Charting with the MS Chart Con...
    - Searching Body Text with textRange: Enter th...
    - Searching Body Text with textRange: Building...
    - Searching Body Text with textRange, part 1: ...
    - First Steps in Programming
    - Programming in C
    - Quick Introduction to ASF,ASX, and Networkin...
    - SatView: Pointer Perfect, Part 2: Constructi...
    - SatView: Pointer Perfect, Part 1
    - Style Case Studies: Construction Unions
    - Creating an Engine for Games for Windows
    - Style Case Studies: Generic Callbacks

    SunQuest




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway