Multiple Series Column Chart

577 views Asked by At

This is my SQL query result

enter image description here

I am trying to build column chart in C# web form by grouping rooms, as per hotel they belong to, like below chart

enter image description here

Above query result is stored in DataTable dt. Below is rest of code

List<string> rooms = (from p in dt.AsEnumerable() select p.Field<string>("Room")).Distinct().ToList();

string[] x = new string[dt.Rows.Count];
double[] y = new double[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
    string room = dt.Rows[i][1].ToString();

    //Get the Country for each Room.
   x[i] = dt.AsEnumerable().
               Where(row => row.Field<string>("Room") == room.ToString()).
           Select(row => row.Field<string>("Country")).FirstOrDefault();


    //Get the Busy % for each Room.
    y[i] = dt.AsEnumerable().
                Where(row => row.Field<string>("Room") == room.ToString())
                  .Select(m => m.Field<double>("Busy")).FirstOrDefault();

Chart1.Series.Add(new System.Web.UI.DataVisualization.Charting.Series(room));
Chart1.Series[room].IsValueShownAsLabel = true;
Chart1.Series[room].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;
Chart1.Series[room].Points.DataBindXY(x, y);
Array.Clear(x, 0, x.Length);
Array.Clear(y, 0, y.Length);
}
Chart1.Legends[0].Enabled = true;

Upon compilation, i can see below chart. How can i group room series belong to same hotel. Any help on this is high appreciated.

enter image description here

Update: Thanks TAW for helping with original post. I now see issue with Series alignment. Data table now has 4 columns Hotel, Room, Utilization, ID. Rooms belong to same hotel will have same ID and that's how i'm grouping rooms on x-axis basis on ID. Thereafter i'm replacing ID values back to Hotel Name as X-Axis label. But Series with different value is being shifted to +1 or -1 (Logical representation for alignment) each time new series is added to chart. enter image description here

0

There are 0 answers