WebChart Demo

This demo illustrates how to display data from a DataSet in a WebChart control.



Source Code
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ register tagprefix="web" namespace="WebChart" assembly="WebChart"%>
<script runat="server" language="VB">

  Sub Page_Load(sender as Object, e as EventArgs)
    WebChart.ChartControl.PhysicalPath = Server.MapPath("/demos/")
    WebChart.ChartControl.VirtualPath = "/demos/"

    If Not Page.IsPostBack then
      'STEP 1: Get the data from the database
      Dim myConnection as New SqlConnection(Connection String)
    
      Const strSQL as String = "SELECT TOP 6 Name, ViewCount " & _
                               "FROM tblFAQCategory ORDER BY NEWID()"
      Dim myCommand as New SqlCommand(strSQL, myConnection)

      Dim reader as SqlDataReader = myCommand.ExecuteReader()      
      
      'STEP 2: Create the chart object
      Dim chart as New PieChart

      'STEP 3: Bind the DataTable to the WebChart
      chart.DataSource = reader
      chart.DataXValueField = "Name"
      chart.DataYValueField = "ViewCount"
      chart.DataBind()
      
      chart.DataLabels.Visible = True
     
      'STEP 4: Attach the chart object to the chart container
      ChartControl1.Charts.Add(chart)
      ChartControl1.RedrawChart()
      
      reader.Close()
      myConnection.Close()
    End If      
  End Sub

</script>


<web:chartcontrol runat="server" id="ChartControl1"
         height="400" width="350" gridlines="none" legend-position="Bottom" />


[Return to the article...]