locked
Chart - Legend Text RRS feed

  • Question

  • User325035487 posted

    I am so thankful to Mikes help with my previous thread on chart. But being a newbie I am stuck on a silly issue again. Please find below the code which i modified to pull data from my database as per his solution. This is working fine except for the legend text (it is assigning the last value in the foreach loop - row.defintion in the code below) . All the legends are showing the same text. I must be making some silly error.

    @using System.Drawing
    @using WebChart = System.Web.UI.DataVisualization.Charting
    
    @{
    
        var db = Database.Open("db");
    
        var questionid = UrlData[0].IsEmpty() ? 0 : UrlData[0].AsInt();
    
        var sql_ans = "select Definition,cnt from sresponsequery where questionid=@0";
        var ans_data = db.Query(sql_ans, questionid);
    
        var sql_q = "SELECT TOP 1 SQuestion FROM SResponseQuery WHERE questionid=@0";
        var qdata = (string)db.QueryValue(sql_q, questionid);
    
    
        using (Bitmap image = new Bitmap(500, 50))
        {
            using (Graphics g = Graphics.FromImage(image))
            {
                var chart1 = new WebChart.Chart(); 
                chart1.Width = 1000;
                chart1.Height = 300;
                
                chart1.ChartAreas.Add("xAxis").BackColor = Color.FromArgb(64, Color.White);
                chart1.Series.Add("xAxis");
                chart1.Series["xAxis"].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Pie;
                chart1.Series["xAxis"]["PieLabelStyle"] = "Outside";
                chart1.Series["xAxis"]["PieLineColor"] = "Black";
    
                foreach (var row in ans_data)
                {
                    chart1.Series["xAxis"].Points.AddY(row.cnt);
                    chart1.Series["xAxis"].LegendText = row.Definition;               
                }
                chart1.Series["xAxis"].IsValueShownAsLabel = true;
                chart1.Legends.Add("Legend1");
                chart1.Legends["Legend1"].Enabled = true;  
                chart1.BackColor = Color.Transparent;
                MemoryStream imageStream = new MemoryStream();
                chart1.SaveImage(imageStream, WebChart.ChartImageFormat.Png);
                chart1.TextAntiAliasingQuality = WebChart.TextAntiAliasingQuality.SystemDefault;
                Response.ContentType = "image/png";
                imageStream.WriteTo(Response.OutputStream);
            }
        }
    }
    

    Wednesday, February 18, 2015 10:55 AM

Answers

  • User325035487 posted
        using (Bitmap image = new Bitmap(500, 50))
        {
            using (Graphics g = Graphics.FromImage(image))
            {
                var chart1 = new WebChart.Chart();
                chart1.Width = 1000;
                chart1.Height = 300;
    
                chart1.ChartAreas.Add("xAxis").BackColor = Color.FromArgb(64, Color.White);
                chart1.Series.Add("xAxis");
                chart1.Series["xAxis"].ChartType = WebChart.SeriesChartType.Pie;
                chart1.Series["xAxis"]["PieLabelStyle"] = "Outside";
                chart1.Series["xAxis"]["PieLineColor"] = "Black";
                //chart1.Legends.Add("Legend1");
                //chart1.Legends["Legend1"].Enabled = true;
                chart1.Legends.Add(new WebChart.Legend() { Name = "Legend" });
                chart1.Legends[0].Docking = WebChart.Docking.Right;
                if (ans_data != null)
                {
                    var count = ans_data.Count();
                    //storing total rows count to loop on each Record  
                    string[] XPointMember = new string[count];
                    int[] YPointMember = new int[count];
                    int index = 0;
                    foreach (var r in ans_data)  //for (int count = 0; count < ChartData.Rows.Count; count++)
                    {
                        //storing Values for X axis  
                        XPointMember[index] = r.Definition.ToString();
                        //storing values for Y Axis  
                        YPointMember[index] = Convert.ToInt32(r.cnt);
                        index++;
                    } 
                    chart1.Series[0].Points.DataBindXY(XPointMember, YPointMember); 
                }
    
                foreach (WebChart.Series charts in chart1.Series)
                {
                    foreach (WebChart.DataPoint point in charts.Points)
                    {
                        //switch (point.AxisLabel) //To Customize Point Label
                        //{
                        //    case "Q1": point.Color = Color.RoyalBlue; break;
                        //    case "Q2": point.Color = Color.SaddleBrown; break;
                        //    case "Q3": point.Color = Color.SpringGreen; break;
                        //}
                        point.Label = string.Format("{0:0} - {1}", point.YValues[0], point.AxisLabel);
    
                    }
                }              
                chart1.Series["xAxis"].IsValueShownAsLabel = true;
                chart1.BackColor = Color.Transparent;
                MemoryStream imageStream = new MemoryStream();
                chart1.SaveImage(imageStream, WebChart.ChartImageFormat.Png);
                chart1.TextAntiAliasingQuality = WebChart.TextAntiAliasingQuality.SystemDefault;
                Response.ContentType = "image/png";
                imageStream.WriteTo(Response.OutputStream);
            }
        }

    Working Code. Based on Simple query data like this

        var sql_ans = "select Definition,cnt from tablename where value=@0";
        var ans_data = db.Query(sql_ans, valueid);

    Thanks for the patience.....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 14, 2015 10:02 AM

All replies

  • User187185635 posted

    It seems that this code snippet works fine, could you post the error message you got?

    Friday, February 20, 2015 9:53 PM
  • User325035487 posted

    I am sorry for not being clear. There is no error message. The chart is generating fine. The only problem is with the legend. Please see the attached pic. The legend text repeats the last field

    legend text

    This line of code chart1.Series["xAxis"].LegendText = row.Definition; inside the foreach loop generates the Legend Text. But only the last value in the ans_data.Defintion appears for all colors as you can see.

    Saturday, February 21, 2015 4:49 PM
  • User325035487 posted
        using (Bitmap image = new Bitmap(500, 50))
        {
            using (Graphics g = Graphics.FromImage(image))
            {
                var chart1 = new WebChart.Chart();
                chart1.Width = 1000;
                chart1.Height = 300;
    
                chart1.ChartAreas.Add("xAxis").BackColor = Color.FromArgb(64, Color.White);
                chart1.Series.Add("xAxis");
                chart1.Series["xAxis"].ChartType = WebChart.SeriesChartType.Pie;
                chart1.Series["xAxis"]["PieLabelStyle"] = "Outside";
                chart1.Series["xAxis"]["PieLineColor"] = "Black";
                //chart1.Legends.Add("Legend1");
                //chart1.Legends["Legend1"].Enabled = true;
                chart1.Legends.Add(new WebChart.Legend() { Name = "Legend" });
                chart1.Legends[0].Docking = WebChart.Docking.Right;
                if (ans_data != null)
                {
                    var count = ans_data.Count();
                    //storing total rows count to loop on each Record  
                    string[] XPointMember = new string[count];
                    int[] YPointMember = new int[count];
                    int index = 0;
                    foreach (var r in ans_data)  //for (int count = 0; count < ChartData.Rows.Count; count++)
                    {
                        //storing Values for X axis  
                        XPointMember[index] = r.Definition.ToString();
                        //storing values for Y Axis  
                        YPointMember[index] = Convert.ToInt32(r.cnt);
                        index++;
                    } 
                    chart1.Series[0].Points.DataBindXY(XPointMember, YPointMember); 
                }
    
                foreach (WebChart.Series charts in chart1.Series)
                {
                    foreach (WebChart.DataPoint point in charts.Points)
                    {
                        //switch (point.AxisLabel) //To Customize Point Label
                        //{
                        //    case "Q1": point.Color = Color.RoyalBlue; break;
                        //    case "Q2": point.Color = Color.SaddleBrown; break;
                        //    case "Q3": point.Color = Color.SpringGreen; break;
                        //}
                        point.Label = string.Format("{0:0} - {1}", point.YValues[0], point.AxisLabel);
    
                    }
                }              
                chart1.Series["xAxis"].IsValueShownAsLabel = true;
                chart1.BackColor = Color.Transparent;
                MemoryStream imageStream = new MemoryStream();
                chart1.SaveImage(imageStream, WebChart.ChartImageFormat.Png);
                chart1.TextAntiAliasingQuality = WebChart.TextAntiAliasingQuality.SystemDefault;
                Response.ContentType = "image/png";
                imageStream.WriteTo(Response.OutputStream);
            }
        }

    Working Code. Based on Simple query data like this

        var sql_ans = "select Definition,cnt from tablename where value=@0";
        var ans_data = db.Query(sql_ans, valueid);

    Thanks for the patience.....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 14, 2015 10:02 AM