Default.aspx.cs
//..
DataTable dt = new DataTable();
dt.Columns.Add("xValue");
dt.Columns.Add("yValue");
dt.Columns.Add("yValue1");
dt.Rows.Add(new Object[] { "John", 28, 31 });
dt.Rows.Add(new Object[] { "Jake", 25, 28 });
dt.Rows.Add(new Object[] { "Peter", 26, 30 });
dt.Rows.Add(new Object[] { "James", 27, 36 });
dt.Rows.Add(new Object[] { "Mary", 32, 34 });
Series series = new Series();
series.XName = "xValue";
series.YName = "yValue";
series.Name = "Series1";
this.Chart1.Series.Add(series);
Series series1 = new Series();
series1.XName = "xValue";
series1.YName = "yValue1";
series1.Name = "Series2";
this.Chart1.Series.Add(series1);
this.Chart1.DataSource = dt;
this.Chart1.DataBind();
//..
Default.aspx
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<script src="Scripts/jquery-3.3.1.min.js"></script>
<script src="Scripts/ej/web/ej.web.all.min.js"></script>
<ej:Chart ClientIDMode="Static" ID="Chart1" runat="server" Height="600" IsResponsive="true">
<PrimaryXAxis Visible="true" MajorGridLines-Visible="true" Title-Text="Manager" />
<PrimaryYAxis AxisLine-Visible="false" Title-Text="Sales" />
<Title Text="Sales Comparison"></Title>
<Legend Visible="false"></Legend>
</ej:Chart>
</asp:Content>
|
Default.aspx.cs
//…
Series series1 = new Series();
series1.XName = "Month";
series1.YName = "Total1";
series1.Name = "Series2";
series1.Type = SeriesType.Line;
this.Chart1.Series.Add(series1);
//… |
Default.aspx.cs
// To split based on categories
List<DataTable> result = dt.AsEnumerable()
.GroupBy(row => row.Field<string>("Category"))
.Select(g => g.CopyToDataTable())
.ToList();
// To assign splitted data table to series
foreach (var item in result)
{
Series series = new Series();
series.XName = "Month";
series.YName = "Total";
series.Type = SeriesType.Line;
// To convert datatable to list
List<ChartData> Data = new List<ChartData>();
for (int j = 0; j < item.Rows.Count; j++)
{
series.Name = item.Rows[j].ItemArray[1].ToString();
Data.Add(new ChartData(item.Rows[j].ItemArray[0].ToString(), item.Rows[j].ItemArray[1].ToString(), Convert.ToInt32(item.Rows[j].ItemArray[2])));
}
series.DataSource = Data;
this.Chart1.Series.Add(series);
}
this.Chart1.DataBind(); |