Hi codelife,
Refer the below code that i have used.
HTML
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
<cc1:LineChart ID="LineChart1" runat="server" ChartType="Basic">
</cc1:LineChart>
</div>
</form>
</body>
</html>
Code
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim query As String = String.Format("select shipcity, count(orderid) from orders where shipcountry IN ('{0}') group by shipcity", "USA")
Dim dt As DataTable = GetData(query)
Dim x As String() = New String(dt.Rows.Count - 1) {}
Dim y As Decimal() = New Decimal(dt.Rows.Count - 1) {}
For i As Integer = 0 To dt.Rows.Count - 1
x(i) = dt.Rows(i)(0).ToString()
y(i) = Convert.ToInt32(dt.Rows(i)(1))
Next
LineChart1.Series.Add(New AjaxControlToolkit.LineChartSeries() With { _
.Data = y _
})
LineChart1.CategoriesAxis = String.Join(",", x)
LineChart1.ChartTitle = String.Format("{0} Order Distribution", "USA")
If x.Length > 3 Then
LineChart1.ChartWidth = (x.Length * 75).ToString()
End If
LineChart1.Visible = True
End If
End Sub
Private Shared Function GetData(query As String) As DataTable
Dim dt As New DataTable()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(query)
Using sda As New SqlDataAdapter()
cmd.CommandType = CommandType.Text
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
End Using
End Using
Return dt
End Using
End Function
web.config
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
</system.web>
<connectionStrings>
<add name="constr" connectionString="Data Source=.;Initial Catalog=northwind;User id = sa;password=sa"/>
</connectionStrings>
</configuration>
Output
Result as per the query in sql
shipcity |
(No column name) |
Albuquerque |
18 |
Anchorage |
10 |
Boise |
31 |
Butte |
3 |
Elgin |
5 |
Eugene |
11 |
Kirkland |
3 |
Lander |
9 |
Portland |
12 |
San Francisco |
4 |
Seattle |
14 |
Walla Walla |
2 |
Screenshot
