1

I am creating stackedbar chart for may following datatable did search over google for all possible solutions and modified my code but i dont know if my logic is going wrong this is my datatable My DataTable

and this is my Code:

  private void fillStackedChart()
    {
        dt = new DataTable();
      cmd = new MySqlCommand("SELECT STATUS , count( resultstatus ) AS Total_Count, clashresult.floor_srno, floor_master.floor_Name FROM clashresult INNER JOIN floor_master ON clashresult.floor_srno = floor_master.floor_srno WHERE floor_master.project_srno =2 GROUP BY STATUS , clashresult.floor_srno, floor_master.floor_Name ORDER BY floor_master.floor_Name", con);
        da = new MySqlDataAdapter(cmd);
        dt = new DataTable();
        da.Fill(dt);
        StackedChart.DataSource = dt;
            DataTable dt2 = new DataTable();
        MySqlDataAdapter adp = new MySqlDataAdapter("SELECT Distinct status from clashresult inner join floor_master on clashresult.floor_srno=floor_master.floor_srno where floor_master.project_srno=2 group by STATUS,clashresult.floor_srno,floor_master.floor_Name", con);
          adp.Fill(dt2);
        int amountofrows = Convert.ToInt32(dt2.Rows.Count);
        for (int i = 0; i < amountofrows; i++)
        {
            List<string> xvals = new List<string>();
            List<decimal> yvals = new List<decimal>();

            string serieName = dt2.Rows[i]["status"].ToString();
            StackedChart.Series.Add(serieName);

            StackedChart.Series[i].ChartType = SeriesChartType.StackedBar;

            //FORMATTING THE CHART
            StackedChart.Series[i].Label = dt.Rows[i]["Total_Count"].ToString();
            StackedChart.Legends.Add(new Legend(dt2.Rows[i]["status"].ToString()) { Docking = Docking.Right });
            StackedChart.Series[i].BorderWidth = 0;
            StackedChart.Series[i].BorderColor = Color.Black;
            StackedChart.Series[i]["PixelPointWidth"] = "30";
            StackedChart.Series[i].LabelForeColor = Color.White;
            StackedChart.BackColor = Color.LightSkyBlue;

            foreach (DataRow dr in dt.Rows)
            {
                try
                {
                    if (String.Equals(serieName, dr["status"].ToString(), StringComparison.Ordinal))
                    {
                        xvals.Add(dr["floor_Name"].ToString());
                        yvals.Add(Convert.ToDecimal(dr["Total_Count"].ToString()));
                    }
                }
                catch (Exception)
                {
                    throw new InvalidOperationException("Diagrammet kunde inte ritas upp");
                }
            }
            try
            {
                StackedChart.Series[serieName].XValueType = ChartValueType.String;
                StackedChart.Series[serieName].YValueType = ChartValueType.Auto;
                StackedChart.Series[serieName].Points.DataBindXY(xvals.ToArray(), yvals.ToArray());
            }
            catch (Exception)
            {
                throw new InvalidOperationException("Diagram Cannnot Be displayed!");
            }
        }
        StackedChart.DataBind();
        StackedChart.Visible = true;
    }

and I am getting the out put like this enter image description here

but the output i am looking for is like this: all bars should look like the following bars enter image description here

This is just single stacked bar but i want the same in above chart. Please help where i am going wrong. Thanks in advance

Programmer
  • 63
  • 2
  • 8
  • In **stacked** charts, either column or bar, if there are _"missing"_ pieces of data, you must explicitly add it with a 0(zero) value, otherwise the whole thing will be misaligned. The chart is just not _smart_ enough to figure it all out for you. For instance, for **LEVEL01** you have **active** and **resolved**, but you're missing **new**, so you have to explicitly add it to your table with a 0(zero) value. – jsanalytics Jan 12 '17 at 15:49
  • 1
    @jstreet Thanks for your suggestion I'll try to fix it – Programmer Jan 13 '17 at 02:02

0 Answers0