0

The problem that I currently have is that I'm trying to populate a stacked bar chart with data from SQL server. Following is the database and code.

enter image description here

 public void LoadHospitalCat()
    {
        try
        {

            string connString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;

            using (SqlConnection sqlConn = new SqlConnection(connString))
            {
                //Fetch the Statistical data from database.
                string sqlQuery = @"  SELECT [HospitalCodesOrdered2].Hospital AS Hospital, Count([PatientInformation].PatientCategory) as PatientCategory,[PatientInformation].PatientCategory as PatientCategory2
                                  from [HospitalCodesOrdered2]
                                  INNER JOIN [PatientInformation] ON [HospitalCodesOrdered2].[Code]=[PatientInformation].[HospitalCode] group by PatientCategory, Hospital order by PatientCategory;";
                SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn);
                //DataTable dt = GetData(query);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);
                //Get the DISTINCT Countries.
                List<string> PatientCategories2 = (from p in table.AsEnumerable()
                                                   select p.Field<string>("PatientCategory2")).Distinct().ToList();

                //Remove the Default Series.
                if (chart10.Series.Count() == 1)
                {
                    chart10.Series.Remove(chart10.Series[0]);
                }

                //Loop through the Countries.
                foreach (string PatientCategory in PatientCategories2)
                {

                    //Get the Year for each Country.
                    string[] x = (from p in table.AsEnumerable()
                                  where p.Field<string>("PatientCategory2") == PatientCategory
                                  orderby p.Field<string>("Hospital") ascending
                                  select p.Field<string>("Hospital")).ToArray();

                    ////Get the Total of Orders for each Country.
                    int[] y = (from p in table.AsEnumerable()
                               where p.Field<string>("PatientCategory2") == PatientCategory
                               orderby p.Field<string>("Hospital") ascending
                               select p.Field<int>("PatientCategory")).ToArray();

                    //Add Series to the Chart.
                    chart10.Series.Add(new Series(PatientCategory));
                    chart10.Series[PatientCategory].IsValueShownAsLabel = true;
                    chart10.Series[PatientCategory].ChartType = SeriesChartType.StackedColumn;
                    chart10.Series[PatientCategory].Points.DataBindXY(x, y);
                }

                chart10.Legends[0].Enabled = true;
                chart10.ChartAreas.FirstOrDefault().AxisX.Interval = 1;
            }
        }



        catch (SqlException ex)
        {
            if (ex.Number == 11001)
            {
                MessageBox.Show("You Are Offline. Please Check your Internet Connection");
            }
            else throw;
        }
    }

The following is the graph first in per hospital basis and then all hospitals. enter image description here enter image description here

the first image has hospital Horana 1184 in yellow but in single it is 31. All the graphs are like that. Please help if you can.

Community
  • 1
  • 1
Dusk Hare
  • 55
  • 2
  • 8
  • You have shown 2 charts but provided code for only one. Please provide more code and please elaborate your question. what is it that you are expecting and what is it that is happening. How did you get the second graph. – Shehroze Malik May 16 '20 at 06:49
  • When creating this type of chart there are rules to follow. [Here](https://stackoverflow.com/questions/35744549/stacked-column-chart-in-c-sharp/35745456#35745456) and in the linked post is a discussion. If you can't or don't want to supply any missing points make sure to use the built-in tool to do so. To use it do set the Names of the series to nice short values !! Also make sure not to mess up the point order or else they can't be stacked since your x-values are missing (i.e. not numeic).. – TaW May 16 '20 at 07:08
  • BTW: the question is about MSChart and Winforms, not about the VS designer! Also: The ChartType is not a Bar but a Column chart! – TaW May 16 '20 at 07:59

0 Answers0