Convert a datatable to a data structure to be read by MS stacked chart control?
-
29-09-2019 - |
Question
I have a chart control (stacked chart to be more precise) and a datatable that contains 3 columns. I want to bind them into a stacked bar chart. below is what the datatable looks like:
Resource | Queue | Hoursworked
Billy Jones | Projects - Internal | 234
Billy Jones | Tier 1 Support | 234
Alan Clark | Projects - Internal | 123
Alan Clark | Tier 1 Support | 234
I would have the Resource column as the x-axis, and the value (hours worked) of each Queue as the y-axis (stacked)
Can anyone help? I've tried just binding normally and I'm getting an error saying that it's the wrong data type.
Thanks,
Billy
Solution
Try this out for size:
DataTable table2 = new DataTable();
table2.Columns.Add("Resource", typeof(string));
table2.Columns.Add("Queue", typeof(string));
table2.Columns.Add("Hoursworked", typeof(int));
table2.Rows.Add("Billy Jones", "Projects - Internal", 234);
table2.Rows.Add("Billy Jones", "Tier 1 Support" , 234);
table2.Rows.Add("Alan Clark", "Projects - Internal" ,123);
table2.Rows.Add("Alan Clark", "Tier 1 Support", 234);
foreach (DataRow row in table2.Rows)
{
string seriesName1 = row["Queue"].ToString();
Series series = new Series(); ;
try {
series = Chart2.Series[seriesName1];
}
catch {
if (series.Name == "") {
Chart2.Series.Add(seriesName1);
Chart2.Series[seriesName1].ChartType = SeriesChartType.StackedColumn;
}
}
Chart2.Series[row["Queue"].ToString()].Points.AddXY(row["Resource"].ToString(), (int)row["Hoursworked"]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow