Thursday, February 14, 2013

Bar Chart in ASP.NET with C#


 private void ShowBarChart1()
        {
            DataSet ds = new DataSet();
            ds = (DataSet)(Session["ds"]);

            DataSet dset = new DataSet();

            string SelOptValue = hdnSelValue.Value;
            hdnSelectedbarText.Value = hdnSelValue.Value;

            using (DataBaseAccess db = new DataBaseAccess(true))
            {
                db.AddParameter("@Qid", id);
                db.AddParameter("@SelOption", SelOptValue);
                dset = db.ExecuteDataSet("proc_ReturnResponse");
                Session["dset"] = dset;
            }


            try
            {
                if (dset != null && dset.Tables[0].Rows.Count > 0)
                {
                    DataTable DT = new DataTable();
                    DT = dset.Tables[1];
                    DT.DefaultView.Sort = "Record";
                    DT = DT.DefaultView.ToTable();
                    string[] x1 = new string[rdOptions.Items.Count];
                    int[] y1 = new int[rdOptions.Items.Count];
                    int cntDs = 0;
                    int others = 0;

                    //get sum
                    int sum = 0;

                    for (int i = 0; i < DT.Rows.Count; i++)
                    {
                        sum += Convert.ToInt16(DT.Rows[i]["Record"]);
                        // onlineAnswer += Convert.ToInt16(dset.Tables[0].Rows[i]["Record"]);
                    }
                    for (int i = 0; i < rdOptions.Items.Count; i++)
                    {
                        if (!string.IsNullOrEmpty(ds.Tables[0].Rows[Convert.ToInt32(hdnCnt.Value)]["offline_answers" + (i + 1)].ToString()))
                            sum += Convert.ToInt16(ds.Tables[0].Rows[Convert.ToInt32(hdnCnt.Value)]["offline_answers" + (i + 1)]);
                    }
                    //  ResponseCount = sum;
                    DataSet dset1 = new DataSet();
                    for (int i = 0; i < rdOptions.Items.Count; i++)
                    {
                        int onlineAnswer = 0;
                        using (DataBaseAccess db1 = new DataBaseAccess(true))
                        {
                            db1.AddParameter("@Qid", id);
                            db1.AddParameter("@SelOption", rdOptions.Items[i].Text);
                            dset1 = db1.ExecuteDataSet("proc_ReturnResponse");
                            if (dset1 != null)
                            {
                                DataTable DT1 = new DataTable();
                                DT1 = dset1.Tables[1];
                                DT1.DefaultView.Sort = "Record";
                                DT1 = DT.DefaultView.ToTable();
                                if (dset1.Tables.Count > 0 && dset1.Tables[0].Rows.Count > 0)
                                {

                                    for (int cnt = 0; cnt < dset1.Tables[0].Rows.Count; cnt++)
                                    {
                                        onlineAnswer += Convert.ToInt16(dset1.Tables[0].Rows[cnt]["Record"]);
                                    }
                                }

                            }
                        }

                        Int32 offline_answered = 0;
                        if (!string.IsNullOrEmpty(ds.Tables[0].Rows[Convert.ToInt32(hdnCnt.Value)]["offline_answers" + (i + 1)].ToString()))
                            offline_answered = Convert.ToInt16(ds.Tables[0].Rows[Convert.ToInt32(hdnCnt.Value)]["offline_answers" + (i + 1)]);
                        Int32 calcPer = Convert.ToInt32(((offline_answered + onlineAnswer) * 100) / sum);

                        x1[i] = rdOptions.Items[i].Value.ToString();

                        y1[i] = calcPer;

                    }

                    //remoove empty elements
                    List<string> y = x1.ToList<string>();
                    // y.RemoveAll(p => string.IsNullOrEmpty(p));
                    x1 = y.ToArray();


                    List<int> z = y1.ToList<int>();
                    //  z.RemoveAll(p => p == 0);
                    y1 = z.ToArray();



                    Chart2.Series[0].Points.DataBindXY(x1, y1);
                    Chart2.Series[0].ChartType = SeriesChartType.Bar;

                    Random random = new Random();

                    foreach (var item in Chart2.Series[0].Points)
                    {
                        Color c = Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
                    }

                    ///////////////////

                    //  this.Chart1.Series[0]["PieLabelStyle"] = "Outside";
                    this.Chart2.Series[0].Label = "#PERCENT{P0}";
                    // Add a legend to the chart and dock it to the bottom-center
                    this.Chart2.Legends.Add("Legend1");
                    this.Chart2.Legends[0].Enabled = true;
                    this.Chart2.Legends[0].Docking = Docking.Bottom;
                    this.Chart2.Legends[0].Alignment = System.Drawing.StringAlignment.Near;

                    Chart2.ChartAreas["ChartArea2"].AxisX.MajorGrid.Enabled = false;
                    Chart2.ChartAreas["ChartArea2"].AxisY.MajorGrid.Enabled = false;
                    Chart2.ChartAreas[0].AxisY.Enabled = AxisEnabled.False;
                    Chart2.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;
                    Chart2.Width = 600;
                    Chart2.Series[0].IsValueShownAsLabel = true;
                    Chart2.Series[0].IsVisibleInLegend = false;

                    ///Code for showing legengs in in one line.
                    Chart2.Legends[0].LegendStyle = LegendStyle.Column;
                    Chart2.Legends[0].TextWrapThreshold = 450;
                    ///
                    foreach (var itemName in Chart2.Series[0].Points)
                    {
                        string Lablename = ((System.Web.UI.DataVisualization.Charting.DataPointCustomProperties)(itemName)).AxisLabel.ToLower().Trim();
                        if (hdnSelectedbarText.Value.ToLower().Trim() == Lablename)
                        {
                            Chart2.Series[0]["BarLabelStyle"] = "Left";
                            ((System.Web.UI.DataVisualization.Charting.DataPointCustomProperties)(itemName)).Label = "#PERCENT{P0}  " + "(You are here)";
                            ((System.Web.UI.DataVisualization.Charting.DataPointCustomProperties)(itemName)).Font = new System.Drawing.Font("Arial, Helvetica, sans-serif", 9, System.Drawing.FontStyle.Bold);
                        }
                        Lablename = string.Empty;
                    }

                    SetBarChartColor();
                    Chart2.SaveImage(HttpContext.Current.Request.PhysicalApplicationPath + "Chart2.jpg");
                }
            }
            catch (Exception ex)
            {
                //Response.Write("No Response Available");
            }
        }
     

No comments:

Post a Comment