Hi hsku6482,
Check this example. Now please take its reference and correct your code.
HTML
<asp:Button Text="Generate Chart" runat="server" OnClick="OnGenerate" />
<hr />
<asp:Chart ID="RateChart" runat="server" Height="300px" Width="1000px">
<Titles>
<asp:Title ShadowOffset="3" Name="Items" />
</Titles>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" BorderWidth="0" />
</ChartAreas>
</asp:Chart>
Code
C#
protected void OnGenerate(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Department");
dt.Columns.Add("Period_Shown");
dt.Columns.Add("Num_Int");
dt.Rows.Add("Dept_AED(EMW)", "2018_10", 0);
dt.Rows.Add("Dept_AED(EMW)", "2018_11", 2.89);
dt.Rows.Add("Dept_AED(EMW)", "2018_12", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_01", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_02", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_02", 2.39);
dt.Rows.Add("Dept_AED(EMW)", "2019_03", 2.06);
dt.Rows.Add("Dept_AED(EMW)", "2019_04", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_05", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_06", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_07", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_08", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_09", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_10", 0);
dt.Rows.Add("Dept_AED(EMW)", "2019_11", 2.24);
dt.Rows.Add("Dept_AED(EMW)", "2019_12", 0);
dt.Rows.Add("Dept_MG", "2017_01", 0.61);
dt.Rows.Add("Dept_MG", "2017_02", 0.35);
dt.Rows.Add("Dept_MG", "2017_03", 0.45);
dt.Rows.Add("Dept_MG", "2017_04", 0.66);
dt.Rows.Add("Dept_MG", "2017_05", 0.87);
dt.Rows.Add("Dept_MG", "2017_06", 0.71);
dt.Rows.Add("Dept_MG", "2017_07", 0.49);
dt.Rows.Add("Dept_MG", "2017_08", 0.51);
dt.Rows.Add("Dept_MG", "2017_09", 0.41);
dt.Rows.Add("Dept_MG", "2017_10", 0.4);
dt.Rows.Add("Dept_MG", "2017_11", 0.34);
dt.Rows.Add("Dept_MG", "2017_12", 0.5);
dt.Rows.Add("Dept_MG", "2018_01", 0.64);
dt.Rows.Add("Dept_MG", "2018_02", 0.46);
dt.Rows.Add("Dept_MG", "2018_03", 0.6);
dt.Rows.Add("Dept_MG", "2018_04", 0.82);
dt.Rows.Add("Dept_MG", "2018_05", 0.69);
dt.Rows.Add("Dept_MG", "2018_06", 0.71);
dt.Rows.Add("Dept_MG", "2018_07", 0.73);
dt.Rows.Add("Dept_MG", "2018_08", 0.95);
dt.Rows.Add("Dept_MG", "2018_09", 0.45);
dt.Rows.Add("Dept_MG", "2018_10", 0.53);
dt.Rows.Add("Dept_MG", "2018_11", 0.8);
dt.Rows.Add("Dept_MG", "2018_12", 0.41);
dt.Rows.Add("Dept_MG", "2019_01", 0.56);
dt.Rows.Add("Dept_MG", "2019_02", 0.44);
dt.Rows.Add("Dept_MG", "2019_03", 0.23);
dt.Rows.Add("Dept_MG", "2019_04", 1.03);
dt.Rows.Add("Dept_MG", "2019_05", 0.65);
dt.Rows.Add("Dept_MG", "2019_06", 0.36);
dt.Rows.Add("Dept_MG", "2019_07", 0.48);
dt.Rows.Add("Dept_MG", "2019_08", 0.78);
dt.Rows.Add("Dept_MG", "2019_09", 0.62);
dt.Rows.Add("Dept_MG", "2019_10", 0.72);
dt.Rows.Add("Dept_MG", "2019_11", 0.64);
gvInt.DataSource = dt;
gvInt.DataBind();
DataTable dtInt = new DataTable("dtInts");
foreach (TableCell cell in gvInt.HeaderRow.Cells)
{
dtInt.Columns.Add(cell.Text.Trim());
}
foreach (GridViewRow row in gvInt.Rows)
{
dtInt.Rows.Add();
for (int k = 0; k < row.Cells.Count; k++)
{
dtInt.Rows[row.RowIndex][k] = row.Cells[k].Text.Trim();
}
}
List<string> departments = (from p in dtInt.AsEnumerable()
select p.Field<string>("Department")).Distinct().ToList();
if (RateChart.Series.Count() == 1)
{
RateChart.Series.Remove(RateChart.Series[0]);
}
for (int i = 0; i < departments.Count; i++)
{
string[] x = (from p in dtInt.AsEnumerable()
orderby p.Field<string>("Period_Shown")
select p.Field<string>("Period_Shown")).Distinct().ToArray();
List<decimal> y = new List<decimal>();
for (int j = 0; j < x.Length; j++)
{
decimal numInt = (from p in dtInt.AsEnumerable()
where p.Field<string>("Department") == departments[i]
&& p.Field<string>("Period_Shown") == x[j]
select Convert.ToDecimal(p.Field<string>("Num_Int"))).FirstOrDefault();
y.Add(numInt);
}
RateChart.Series.Add(new Series(departments[i]));
RateChart.Series[departments[i]].IsValueShownAsLabel = false;
RateChart.Series[departments[i]].BorderWidth = 3;
RateChart.Series[departments[i]].ChartType = SeriesChartType.Line;
RateChart.Series[departments[i]].Points.DataBindXY(x, y);
RateChart.Series[departments[i]].MarkerStyle = MarkerStyle.Star10;
}
RateChart.Legends.Add("Default");
RateChart.Legends[0].Docking = Docking.Right;
RateChart.Legends[0].IsTextAutoFit = true;
RateChart.Legends[0].Alignment = System.Drawing.StringAlignment.Center;
RateChart.Legends[0].Enabled = true;
RateChart.ChartAreas[0].AxisX.Interval = 1;
RateChart.ChartAreas[0].AxisX.Title = "Period";
RateChart.ChartAreas[0].AxisX.TitleForeColor = System.Drawing.Color.Red;
RateChart.ChartAreas[0].AxisY.Title = "Number of incidents";
RateChart.ChartAreas[0].AxisY.TitleForeColor = System.Drawing.Color.Red;
RateChart.ChartAreas[0].AxisY.TitleAlignment = System.Drawing.StringAlignment.Far;
RateChart.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated90;
}
VB.Net
Protected Sub OnGenerate(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Department")
dt.Columns.Add("Period_Shown")
dt.Columns.Add("Num_Int")
dt.Rows.Add("Dept_AED(EMW)", "2018_10", 0)
dt.Rows.Add("Dept_AED(EMW)", "2018_11", 2.89)
dt.Rows.Add("Dept_AED(EMW)", "2018_12", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_01", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_02", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_02", 2.39)
dt.Rows.Add("Dept_AED(EMW)", "2019_03", 2.06)
dt.Rows.Add("Dept_AED(EMW)", "2019_04", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_05", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_06", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_07", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_08", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_09", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_10", 0)
dt.Rows.Add("Dept_AED(EMW)", "2019_11", 2.24)
dt.Rows.Add("Dept_AED(EMW)", "2019_12", 0)
dt.Rows.Add("Dept_MG", "2017_01", 0.61)
dt.Rows.Add("Dept_MG", "2017_02", 0.35)
dt.Rows.Add("Dept_MG", "2017_03", 0.45)
dt.Rows.Add("Dept_MG", "2017_04", 0.66)
dt.Rows.Add("Dept_MG", "2017_05", 0.87)
dt.Rows.Add("Dept_MG", "2017_06", 0.71)
dt.Rows.Add("Dept_MG", "2017_07", 0.49)
dt.Rows.Add("Dept_MG", "2017_08", 0.51)
dt.Rows.Add("Dept_MG", "2017_09", 0.41)
dt.Rows.Add("Dept_MG", "2017_10", 0.4)
dt.Rows.Add("Dept_MG", "2017_11", 0.34)
dt.Rows.Add("Dept_MG", "2017_12", 0.5)
dt.Rows.Add("Dept_MG", "2018_01", 0.64)
dt.Rows.Add("Dept_MG", "2018_02", 0.46)
dt.Rows.Add("Dept_MG", "2018_03", 0.6)
dt.Rows.Add("Dept_MG", "2018_04", 0.82)
dt.Rows.Add("Dept_MG", "2018_05", 0.69)
dt.Rows.Add("Dept_MG", "2018_06", 0.71)
dt.Rows.Add("Dept_MG", "2018_07", 0.73)
dt.Rows.Add("Dept_MG", "2018_08", 0.95)
dt.Rows.Add("Dept_MG", "2018_09", 0.45)
dt.Rows.Add("Dept_MG", "2018_10", 0.53)
dt.Rows.Add("Dept_MG", "2018_11", 0.8)
dt.Rows.Add("Dept_MG", "2018_12", 0.41)
dt.Rows.Add("Dept_MG", "2019_01", 0.56)
dt.Rows.Add("Dept_MG", "2019_02", 0.44)
dt.Rows.Add("Dept_MG", "2019_03", 0.23)
dt.Rows.Add("Dept_MG", "2019_04", 1.03)
dt.Rows.Add("Dept_MG", "2019_05", 0.65)
dt.Rows.Add("Dept_MG", "2019_06", 0.36)
dt.Rows.Add("Dept_MG", "2019_07", 0.48)
dt.Rows.Add("Dept_MG", "2019_08", 0.78)
dt.Rows.Add("Dept_MG", "2019_09", 0.62)
dt.Rows.Add("Dept_MG", "2019_10", 0.72)
dt.Rows.Add("Dept_MG", "2019_11", 0.64)
gvInt.DataSource = dt
gvInt.DataBind()
Dim dtInt As DataTable = New DataTable("dtInts")
For Each cell As TableCell In gvInt.HeaderRow.Cells
dtInt.Columns.Add(cell.Text.Trim())
Next
For Each row As GridViewRow In gvInt.Rows
dtInt.Rows.Add()
Dim k As Integer = 0
For k = 0 To row.Cells.Count - 1
dtInt.Rows(row.RowIndex)(k) = row.Cells(k).Text.Trim()
Next
Next
Dim departments As List(Of String) = New List(Of String)()
departments = (From p In dtInt.AsEnumerable()
Select p.Field(Of String)("Department")).Distinct().ToList()
If RateChart.Series.Count() = 1 Then
RateChart.Series.Remove(RateChart.Series(0))
End If
Dim i As Integer = 0
For i = 0 To departments.Count - 1 Step 1
Dim x As String() = (From p In dtInt.AsEnumerable()
Order By p.Field(Of String)("Period_Shown")
Select p.Field(Of String)("Period_Shown")).Distinct().ToArray()
Dim y As List(Of Decimal) = New List(Of Decimal)()
Dim j As Integer = 0
For j = 0 To x.Length - 1 Step 1
Dim numInt As Decimal = (From p In dtInt.AsEnumerable()
Where p.Field(Of String)("Department") = departments(i) _
AndAlso p.Field(Of String)("Period_Shown") = x(j)
Order By p.Field(Of String)("Period_Shown")
Select Convert.ToDecimal(p.Field(Of String)("Num_Int"))).FirstOrDefault()
y.Add(numInt)
Next
RateChart.Series.Add(New Series(departments(i)))
RateChart.Series(departments(i)).IsValueShownAsLabel = False
RateChart.Series(departments(i)).BorderWidth = 3
RateChart.Series(departments(i)).ChartType = SeriesChartType.Line
RateChart.Series(departments(i)).Points.DataBindXY(x, y)
RateChart.Series(departments(i)).MarkerStyle = MarkerStyle.Star10
Next
RateChart.Legends.Add("Default")
RateChart.Legends(0).Docking = Docking.Bottom
RateChart.Legends(0).IsTextAutoFit = True
RateChart.Legends(0).Alignment = Drawing.StringAlignment.Center
RateChart.Legends(0).Enabled = True
RateChart.ChartAreas(0).AxisX.Interval = 1
RateChart.ChartAreas(0).AxisX.Title = "Period"
RateChart.ChartAreas(0).AxisX.TitleForeColor = System.Drawing.Color.Red
RateChart.ChartAreas(0).AxisY.Title = "Number of incidents"
RateChart.ChartAreas(0).AxisY.TitleForeColor = Drawing.Color.Red
RateChart.ChartAreas(0).AxisY.TitleAlignment = Drawing.StringAlignment.Far
RateChart.ChartAreas(0).AxisY.TextOrientation = TextOrientation.Rotated90
End Sub
Screenshot