Hi vishalkal,
Please refer below sample
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms.DataVisualization.Charting;
using System.Drawing;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Windows.Forms.DataVisualization.Charting
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
//Fetch the Statistical data from database.
string query = "SELECT ShipCity, COUNT(OrderId) [Total]";
query += " FROM Orders WHERE ShipCountry = 'Brazil'";
query += " GROUP BY ShipCity";
DataTable dt = GetData(query);
//Get the names of Cities.
string[] x = (from p in dt.AsEnumerable()
orderby p.Field<string>("ShipCity") ascending
select p.Field<string>("ShipCity")).ToArray();
//Get the Total of Orders for each City.
int[] y = (from p in dt.AsEnumerable()
orderby p.Field<string>("ShipCity") ascending
select p.Field<int>("Total")).ToArray();
Chart1.Series[0].LegendText = "Brazil Order Statistics";
Chart1.Series[0].ChartType = SeriesChartType.Line;
Chart1.Series[0].BorderWidth = 3;
Chart1.Series[0].IsValueShownAsLabel = true;
Chart1.Series[0].Points.DataBindXY(x, y);
}
private static DataTable GetData(string query)
{
string constr = @"Data Source=.\SQL2005;Initial Catalog=Northwind;User ID=user;Password=password";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
ToolTip tt = null;
Point tl = Point.Empty;
private void Chart1_MouseMove(object sender, MouseEventArgs e)
{
if (tt == null)
{
tt = new ToolTip();
}
ChartArea ca = Chart1.ChartAreas[0];
if (InnerPlotPositionClientRectangle(Chart1, ca).Contains(e.Location))
{
Axis ax = ca.AxisX;
Axis ay = ca.AxisY;
double x = ax.PixelPositionToValue(e.X);
double y = ay.PixelPositionToValue(e.Y);
string s = DateTime.FromOADate(x).ToShortDateString();
if (e.Location != tl)
{
tt.SetToolTip(Chart1, string.Format("X = {0} " + Environment.NewLine + "Y = {1:0.00}", s, y));
}
tl = e.Location;
}
else tt.Hide(Chart1);
}
RectangleF ChartAreaClientRectangle(Chart chart, ChartArea ca)
{
RectangleF rf = ca.Position.ToRectangleF();
float pw = chart.ClientSize.Width / 100f;
float ph = chart.ClientSize.Height / 100f;
return new RectangleF(pw * rf.X, ph * rf.Y, pw * rf.Width, ph * rf.Height);
}
RectangleF InnerPlotPositionClientRectangle(Chart chart, ChartArea ca)
{
RectangleF rf1 = ca.InnerPlotPosition.ToRectangleF();
RectangleF rf2 = ChartAreaClientRectangle(chart, ca);
float pw = rf2.Width / 100f;
float ph = rf2.Height / 100f;
return new RectangleF(rf2.X + pw * rf1.X, rf2.Y + ph * rf1.Y, pw * rf1.Width, ph * rf1.Height);
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Fetch the Statistical data from database.
Dim query As String = "SELECT ShipCity, COUNT(OrderId) [Total]"
query &= " FROM Orders WHERE ShipCountry = 'Brazil'"
query &= " GROUP BY ShipCity"
Dim dt As DataTable = GetData(query)
'Get the names of Cities.
Dim x As String() = (From p In dt.AsEnumerable() _
Order By p.Field(Of String)("ShipCity") _
Select p.Field(Of String)("ShipCity")).ToArray()
'Get the Total of Orders for each City.
Dim y As Integer() = (From p In dt.AsEnumerable() _
Order By p.Field(Of String)("ShipCity") _
Select p.Field(Of Integer)("Total")).ToArray()
Chart1.Series(0).LegendText = "Brazil Order Statistics"
Chart1.Series(0).ChartType = SeriesChartType.Line
Chart1.Series(0).BorderWidth = 3
Chart1.Series(0).IsValueShownAsLabel = True
Chart1.Series(0).Points.DataBindXY(x, y)
End Sub
Private Shared Function GetData(ByVal query As String) As DataTable
Dim constr As String = "Data Source=.\SQL2005;Initial Catalog=Northwind;User ID=user;Password=password"
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(query, con)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Function
Private tt As ToolTip = Nothing
Private tl As Point = Point.Empty
Private Sub Chart1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
If tt Is Nothing Then
tt = New ToolTip()
End If
Dim ca As ChartArea = Chart1.ChartAreas(0)
If InnerPlotPositionClientRectangle(Chart1, ca).Contains(e.Location) Then
Dim ax As Axis = ca.AxisX
Dim ay As Axis = ca.AxisY
Dim x As Double = ax.PixelPositionToValue(e.X)
Dim y As Double = ay.PixelPositionToValue(e.Y)
Dim s As String = DateTime.FromOADate(x).ToShortDateString()
If e.Location <> tl Then
tt.SetToolTip(Chart1, String.Format("X = {0} " & Environment.NewLine & "Y = {1:0.00}", s, y))
End If
tl = e.Location
Else
tt.Hide(Chart1)
End If
End Sub
Private Function ChartAreaClientRectangle(ByVal chart As Chart, ByVal ca As ChartArea) As RectangleF
Dim rf As RectangleF = ca.Position.ToRectangleF()
Dim pw As Single = chart.ClientSize.Width / 100.0F
Dim ph As Single = chart.ClientSize.Height / 100.0F
Return New RectangleF(pw * rf.X, ph * rf.Y, pw * rf.Width, ph * rf.Height)
End Function
Private Function InnerPlotPositionClientRectangle(ByVal chart As Chart, ByVal ca As ChartArea) As RectangleF
Dim rf1 As RectangleF = ca.InnerPlotPosition.ToRectangleF()
Dim rf2 As RectangleF = ChartAreaClientRectangle(chart, ca)
Dim pw As Single = rf2.Width / 100.0F
Dim ph As Single = rf2.Height / 100.0F
Return New RectangleF(rf2.X + pw * rf1.X, rf2.Y + ph * rf1.Y, pw * rf1.Width, ph * rf1.Height)
End Function
Screenshot