Hi hsku6482,
Refer below code.
HTML
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Department" HeaderText="Department" />
<asp:BoundField DataField="Period_Shown" HeaderText="Period_Shown" />
<asp:BoundField DataField="Rate_Int" HeaderText="Rate_Int" />
</Columns>
</asp:GridView>
<asp:Chart ID="Chart1" runat="server" Height="300px" Width="400px">
<Titles>
<asp:Title ShadowOffset="3" Name="Items" />
</Titles>
<Legends>
<asp:Legend Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Name="Default"
LegendStyle="Row" />
</Legends>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" BorderWidth="0" />
</ChartAreas>
</asp:Chart>
Namespaces
C#
using System.Data;
using System.Web.UI.DataVisualization.Charting;
VB.Net
Imports System.Data
Imports System.Web.UI.DataVisualization.Charting
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = GetData();
GridView1.DataSource = dt;
GridView1.DataBind();
DataTable dtRate = new DataTable("dtRate");
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
dtRate.Columns.Add(cell.Text.Trim());
}
foreach (GridViewRow row in GridView1.Rows)
{
dtRate.Rows.Add();
for (int i = 0; i < row.Cells.Count; i++)
{
dtRate.Rows[row.RowIndex][i] = row.Cells[i].Text.Trim();
}
}
List<string> countries = (from p in dtRate.AsEnumerable()
select p.Field<string>("Department")).Distinct().ToList();
if (Chart1.Series.Count() == 1)
Chart1.Series.Remove(Chart1.Series[0]);
foreach (string country in countries)
{
int[] x = (from p in dtRate.AsEnumerable()
where p.Field<string>("Department") == country
orderby p.Field<string>("Period_Shown")
select Convert.ToInt32(p.Field<string>("Period_Shown"))).ToArray();
decimal[] y = (from p in dtRate.AsEnumerable()
where p.Field<string>("Department") == country
orderby p.Field<string>("Period_Shown")
select Convert.ToDecimal(p.Field<string>("Rate_Int"))).ToArray();
Chart1.Series.Add(new Series(country));
Chart1.Series[country].IsValueShownAsLabel = true;
Chart1.Series[country].BorderWidth = 3;
Chart1.Series[country].ChartType = SeriesChartType.Line;
Chart1.Series[country].Points.DataBindXY(x, y);
}
Chart1.Legends[0].Enabled = true;
}
}
private static DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add("Department", typeof(string));
dt.Columns.Add("Period_Shown", typeof(int));
dt.Columns.Add("Rate_Int", typeof(decimal));
dt.Rows.Add("EMW", 2018, 0.94);
dt.Rows.Add("EMW", 2019, 0.55);
dt.Rows.Add("ACH", 2015, 0.29);
dt.Rows.Add("ACH", 2016, 0.52);
dt.Rows.Add("ACH", 2017, 0.56);
dt.Rows.Add("ACH", 2018, 0.63);
dt.Rows.Add("ACH", 2019, 0.57);
return dt;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = GetData()
GridView1.DataSource = dt
GridView1.DataBind()
Dim dtRate As DataTable = New DataTable("dtRate")
For Each cell As TableCell In GridView1.HeaderRow.Cells
dtRate.Columns.Add(cell.Text.Trim())
Next
For Each row As GridViewRow In GridView1.Rows
dtRate.Rows.Add()
For i As Integer = 0 To row.Cells.Count - 1
dtRate.Rows(row.RowIndex)(i) = row.Cells(i).Text.Trim()
Next
Next
Dim countries As List(Of String) = (From p In dtRate.AsEnumerable() Select p.Field(Of String)("Department")).Distinct().ToList()
If Chart1.Series.Count() = 1 Then Chart1.Series.Remove(Chart1.Series(0))
For Each country As String In countries
Dim x As Integer() = (From p In dtRate.AsEnumerable()
Where p.Field(Of String)("Department") = country
Order By p.Field(Of String)("Period_Shown")
Select Convert.ToInt32(p.Field(Of String)("Period_Shown"))).ToArray()
Dim y As Decimal() = (From p In dtRate.AsEnumerable()
Where p.Field(Of String)("Department") = country
Order By p.Field(Of String)("Period_Shown")
Select Convert.ToDecimal(p.Field(Of String)("Rate_Int"))).ToArray()
Chart1.Series.Add(New Series(country))
Chart1.Series(country).IsValueShownAsLabel = True
Chart1.Series(country).BorderWidth = 3
Chart1.Series(country).ChartType = SeriesChartType.Line
Chart1.Series(country).Points.DataBindXY(x, y)
Next
Chart1.Legends(0).Enabled = True
End If
End Sub
Private Shared Function GetData() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Department", GetType(String))
dt.Columns.Add("Period_Shown", GetType(Integer))
dt.Columns.Add("Rate_Int", GetType(Decimal))
dt.Rows.Add("EMW", 2018, 0.94)
dt.Rows.Add("EMW", 2019, 0.55)
dt.Rows.Add("ACH", 2015, 0.29)
dt.Rows.Add("ACH", 2016, 0.52)
dt.Rows.Add("ACH", 2017, 0.56)
dt.Rows.Add("ACH", 2018, 0.63)
dt.Rows.Add("ACH", 2019, 0.57)
Return dt
End Function
Screenshot