I have 3 columns datatable; I want to show values of INST_ID and Legends on my chart.
My problem is, INST_ID can be vary. It may have 5 different INST_IDs.
How can I show all series belongs to INST_ID and legends?
Here is the current Result
Here is the code that I generate for the Ultra Data Chart
private void Form1_Load(object sender, EventArgs e)
{
dtLineTest.Columns.Add("SAMPLE_TIME",typeof(DateTime));
dtLineTest.Columns.Add("INST_ID", typeof(Int32));
dtLineTest.Columns.Add("VALUE", typeof(Int32));
dtLineTest.Rows.Add(DateTime.Now,1,14);
dtLineTest.Rows.Add(DateTime.Now.AddMinutes(-1), 1, 18);
dtLineTest.Rows.Add(DateTime.Now.AddMinutes(-2), 1, 11);
dtLineTest.Rows.Add(DateTime.Now.AddMinutes(-3), 1, 7);
dtLineTest.Rows.Add(DateTime.Now.AddMinutes(-4), 1, 21);
dtLineTest.Rows.Add(DateTime.Now, 2, 16);
dtLineTest.Rows.Add(DateTime.Now.AddMinutes(-1), 2, 3);
dtLineTest.Rows.Add(DateTime.Now.AddMinutes(-2), 2, 5);
dtLineTest.Rows.Add(DateTime.Now.AddMinutes(-3), 2, 12);
dtLineTest.Rows.Add(DateTime.Now.AddMinutes(-4), 2, 13);
var xAxis = new CategoryXAxis();
var yAxis = new NumericYAxis();
xAxis.DataSource = dtLineTest;
xAxis.Label = "SAMPLE_TIME";
var legend = new UltraLegend();
var lineSeries = new LineSeries();
lineSeries.DataSource = dtLineTest;
lineSeries.ValueMemberPath = "VALUE";
lineSeries.Title = "INST_ID";
lineSeries.XAxis = xAxis;
lineSeries.YAxis = yAxis;
lineSeries.Legend = legend;
ultraDataChart1.Axes.Add(xAxis);
ultraDataChart1.Axes.Add(yAxis);
ultraDataChart1.Series.Add(lineSeries);
}
CodePudding user response:
To show several lines in the chart you need to add several lineSeries
to the chart. In your case you can group the data in the table by INST_ID
column and create a lineSeries
for each group. Something like this:
var groupedData = dtLineTest.AsEnumerable().GroupBy(g => g["INST_ID"]);
foreach (var group in groupedData)
{
var dt = new DataTable();
dt.Columns.Add("SAMPLE_TIME", typeof(DateTime));
dt.Columns.Add("INST_ID", typeof(Int32));
dt.Columns.Add("VALUE", typeof(Int32));
foreach (var row in group)
{
dt.Rows.Add(row.ItemArray);
}
var legend = new UltraLegend();
var lineSeries = new LineSeries();
lineSeries.DataSource = dt;
lineSeries.ValueMemberPath = "VALUE";
lineSeries.Title = "INST_ID";
lineSeries.XAxis = xAxis;
lineSeries.YAxis = yAxis;
lineSeries.Legend = legend;
ultraDataChart1.Series.Add(lineSeries);
}
Note: it will be much easier if you have separate table for each series beforehand.