Thursday, February 14, 2013

Pie Chart in ASP.NET with C#


   private void showPieChart()
        {
            try
            {
                DataSet ds = new DataSet();
                ds = (DataSet)(Session["ds"]);
                DataSet dset = new DataSet();
                dset = (DataSet)(Session["dset"]);
                if (dset != null && dset.Tables[0].Rows.Count > 0)
                {
                    string[] x1 = new string[rdOptions.Items.Count];
                    int[] y1 = new int[rdOptions.Items.Count];
                    int cntDs = 0;

                    int sum = 0;
                    for (int i = 0; i < dset.Tables[1].Rows.Count; i++)
                    {
                        sum += Convert.ToInt16(dset.Tables[1].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)]);
                    }
                    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)
                            {
                                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;   //have only offline record //Convert.ToInt16(dset.Tables[0].Rows[i]["Record"])
                        cntDs++;
                    }



                    //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();

                    Chart1.Series[0].Points.DataBindXY(x1, y1);

                    //Chart1.Series[0].Label = "#VALX (#PERCENT{P2})";


                    Chart1.Series[0].ChartType = SeriesChartType.Pie;
                    //Chart1.Series[0].ChartArea = SeriesChartType.StackedArea.ToString();
                    //Chart1.Series[0].ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), RadioButtonList1.SelectedItem.Text, true);

                    ///////////////////
                    this.Chart1.Series[0]["PieLabelStyle"] = "Outside";
                    this.Chart1.Series[0]["3DLabelLineSize"] = "30";
                    //this.Chart1.Series[0]["LabelsRadialLineSize"] = "0.15";
                    //this.Chart1.Series[0]["LabelsHorizontalLineSize"] = "0.15";
                  
                        this.Chart1.Series[0].Label = "#PERCENT{P0}";
                        // Add a legend to the chart and dock it to the bottom-center
                        this.Chart1.Legends.Add("Legend1");
                        this.Chart1.Legends[0].Enabled = true;
                        this.Chart1.Legends[0].Docking = Docking.Bottom;
                        this.Chart1.Legends[0].Alignment = System.Drawing.StringAlignment.Near;
                  
                    // Show labels in the legend in the format "Name (### %)"
                    this.Chart1.Series[0].LegendText = "#VALX";
                    Chart1.Legends[0].LegendStyle = LegendStyle.Column;
                    //Chart2.Legends[0].MaximumAutoSize = 50;
                    Chart1.Legends[0].TextWrapThreshold = 450;
                    // By sorting the data points, they show up in proper ascending order in the legend
                    // this.Chart1.DataManipulator.Sort(PointSortOrder.Descending, Chart1.Series[0]);
                    ////////////////////
                    foreach (var itemName in Chart1.Series[0].Points)
                    {

                        string Lablename = ((System.Web.UI.DataVisualization.Charting.DataPointCustomProperties)(itemName)).AxisLabel.ToLower().Trim();
                        if (hdnSelectedbarText.Value.ToLower().Trim() == Lablename)
                        {
                            Chart1.Series[0]["BarLabelStyle"] = "Center";

                            ((System.Web.UI.DataVisualization.Charting.DataPointCustomProperties)(itemName)).Label = "#PERCENT{P0}  " + System.Environment.NewLine + "(You are here)";
                            ((System.Web.UI.DataVisualization.Charting.DataPointCustomProperties)(itemName)).IsValueShownAsLabel = true;
                            ((System.Web.UI.DataVisualization.Charting.DataPointCustomProperties)(itemName)).Font = new System.Drawing.Font("Arial, Helvetica, sans-serif", 9, System.Drawing.FontStyle.Bold);
                        }
                        Lablename = string.Empty;
                        if (((DataPoint)(itemName)).YValues[0] == 0)
                        {
                            ((DataPoint)(itemName)).Label = string.Empty;
                            ((DataPoint)(itemName)).AxisLabel = string.Empty;
                            ((DataPoint)(itemName)).IsVisibleInLegend = false;
                         
                        }
                    }
                    Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;

                    SetColor();
                    Chart1.SaveImage(HttpContext.Current.Request.PhysicalApplicationPath + "Chart1.jpg");
                    //  lblNo.Text = sum.ToString();
                }
            }
            catch (Exception ex)
            {
                //Response.Write("No Response Available");
            }
        }

No comments:

Post a Comment