Code Examples
  Home arrow Code Examples arrow Page 4 - 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 
Moblin 
JMSL Numerical Library 
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 / 29
    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


    Creating a Chart using Data from a Database with MS Chart Control - Assigning retrieved data to an array to display on the chart


    (Page 4 of 4 )

    MS Chart Control can also use data in an array and display it. The following code can be used to display data after assigning the recordset to the array. The same query as in the previous case is used for retrieving data from the same database.

    Private rsProducts As New ADODB.Recordset
    Private cn As New ADODB.Connection
     
    Private Sub Form_Load()
     
    MSChart1.ToDefaults
    ' declare variable to hold query string
    Dim strQuery As String 
     
       'This Connection string is specific to the SQL 2000 on this machine
       cn.ConnectionString = 
       _"Provider=SQLOLEDB.1;Password=XXXXX; Persist Security Info=True;" & _
      "User ID=sa; Initial Catalog=TestWiz; Data Source=XPHTEK"
     
       
       ' Open the connection.
       cn.Open
     
       ' this Query retrieves only four fields.
       strQuery = "SELECT ProductName, UnitPrice, UnitsInStock, 
    UnitsOnOrder FROM Products
    "
       ' Open the recordset.
       rsProducts.CursorLocation = adUseClient
       rsProducts.Open strQuery, cn, adOpenKeyset
    'this array will contain data for the 4 columns and variable number of rows
    ReDim chrtarr(1 To rsProducts.RecordCount, 1 To 4)
    MSChart1.ShowLegend = True
    MSChart1.chartType = VtChChartType2dBar
    'relate array to the recordset returned
    For X = 1 To rsProducts.RecordCount
    chrtarr(X, 1) = rsProducts("ProductName")
    chrtarr(X, 2) = rsProducts("UnitPrice")
    chrtarr(X, 3) = rsProducts("UnitsInStock")
    chrtarr(X, 4) = rsProducts("UnitsOnOrder")
    rsProducts.MoveNext
    Next X
    'feed chart from this array
    With MSChart1
    .ChartData = chrtarr
    'only the three columns, 2,3 4 will provide data
    .ColumnCount = 3
    .ColumnLabelCount = 1
    .Column = 1
    End With
    End Sub

    With this code the chart display is as shown in the next picture. The important lines are explained with the comments before the statement.

    This agrees with the number of rows and columns with values that can be verified.

    Summary

    This tutorial has shown some pitfalls in using this control to display data retrieved from a database. The simple table was specially created to test the functionality. All said and done, this is not an easy control to configure. The chart size will not accommodate to the form size. Column and row labels have to be assigned by code. ADO does not bind to this control as it is claimed. However you can bind the data to an array, the first field will be assigned to the x-axis of the bar graph. I recommend the readers of this article read my basic MS Chart article on this site.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT